Fawkes API  Fawkes Development Version
sensacq_thread.cpp
1 
2 /***************************************************************************
3  * sensaqt_thread.cpp - Katana sensor acqusition thread
4  *
5  * Created: Fri Jun 12 15:08:56 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "sensacq_thread.h"
24 
25 #include "controller.h"
26 
27 #include <cstdlib>
28 
29 using namespace fawkes;
30 
31 /** @class KatanaSensorAcquisitionThread "sensacq_thread.h"
32  * Katana sensor acquisition thread.
33  * This thread runs continuously and acquires data from the sensor. Since the
34  * operation is blocking and may take several miliseconds it is done concurrently
35  * to the main loop at specified intervals.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param katana katana controller base class
41  * @param logger logger
42  */
45  fawkes::Logger * logger)
46 : Thread("KatanaSensorAcqusitionThread", Thread::OPMODE_WAITFORWAKEUP)
47 {
48  katana_ = katana;
49  logger_ = logger;
50  enabled_ = false;
51 }
52 
53 /** Set whether data acquisition is enabled or not.
54  * In general the thread should only be woken up if sensor data can be acquired.
55  * But for safety data acqusition can also be turned off to be safe against
56  * spurious wakeups. Additionally, this method will acquire the loop mutex,
57  * thereby assuring that a possibly running loop has finished.
58  * @param enabled true to enable sensor data acquisition, false to disable.
59  */
60 void
62 {
63  loop_mutex->lock();
64  enabled_ = enabled;
65  loop_mutex->unlock();
66 }
67 
68 void
70 {
71  if (enabled_) {
72  try {
73  katana_->read_sensor_data();
74  } catch (Exception &e) {
75  logger_->log_warn(name(), "Exception while reading sensor data: %s", e.what());
76  }
77  }
78 }
void set_enabled(bool enabled)
Set whether data acquisition is enabled or not.
virtual void loop()
Code to execute in the thread.
KatanaSensorAcquisitionThread(fawkes::RefPtr< fawkes::KatanaController > katana, fawkes::Logger *logger)
Constructor.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual const char * what() const
Get primary string.
Definition: exception.cpp:639
virtual void read_sensor_data()=0
Read all sensor data from device into controller libray.
Interface for logging.
Definition: logger.h:42
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
void lock()
Lock this mutex.
Definition: mutex.cpp:87
void unlock()
Unlock the mutex.
Definition: mutex.cpp:131
Thread class encapsulation of pthreads.
Definition: thread.h:46
Mutex * loop_mutex
Mutex that is used to protect a call to loop().
Definition: thread.h:152
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.