Fawkes API  Fawkes Development Version
sensacq_thread.cpp
00001 
00002 /***************************************************************************
00003  *  sensaqt_thread.cpp - Katana sensor acqusition thread
00004  *
00005  *  Created: Fri Jun 12 15:08:56 2009
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "sensacq_thread.h"
00024 #include "controller.h"
00025 
00026 #include <cstdlib>
00027 
00028 using namespace fawkes;
00029 
00030 /** @class KatanaSensorAcquisitionThread "sensacq_thread.h"
00031  * Katana sensor acquisition thread.
00032  * This thread runs continuously and acquires data from the sensor. Since the
00033  * operation is blocking and may take several miliseconds it is done concurrently
00034  * to the main loop at specified intervals.
00035  * @author Tim Niemueller
00036  */
00037 
00038 /** Constructor.
00039  * @param katana katana controller base class
00040  * @param logger logger
00041  */
00042 KatanaSensorAcquisitionThread::KatanaSensorAcquisitionThread(fawkes::RefPtr<fawkes::KatanaController> katana,
00043                                                              fawkes::Logger *logger)
00044   : Thread("KatanaSensorAcqusitionThread", Thread::OPMODE_WAITFORWAKEUP)
00045 {
00046   __katana  = katana;
00047   __logger  = logger;
00048   __enabled = false;
00049 }
00050 
00051 
00052 /** Set whether data acquisition is enabled or not.
00053  * In general the thread should only be woken up if sensor data can be acquired.
00054  * But for safety data acqusition can also be turned off to be safe against
00055  * spurious wakeups. Additionally, this method will acquire the loop mutex,
00056  * thereby assuring that a possibly running loop has finished.
00057  * @param enabled true to enable sensor data acquisition, false to disable.
00058  */
00059 void
00060 KatanaSensorAcquisitionThread::set_enabled(bool enabled)
00061 {
00062   loop_mutex->lock();
00063   __enabled = enabled;
00064   loop_mutex->unlock();
00065 }
00066 
00067 
00068 void
00069 KatanaSensorAcquisitionThread::loop()
00070 {
00071   if (__enabled) {
00072     try {
00073       __katana->read_sensor_data();
00074     } catch (Exception &e) {
00075       __logger->log_warn(name(), e.what());
00076     }
00077   }
00078 }