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 #include "controller.h"
25 
26 #include <cstdlib>
27 
28 using namespace fawkes;
29 
30 /** @class KatanaSensorAcquisitionThread "sensacq_thread.h"
31  * Katana sensor acquisition thread.
32  * This thread runs continuously and acquires data from the sensor. Since the
33  * operation is blocking and may take several miliseconds it is done concurrently
34  * to the main loop at specified intervals.
35  * @author Tim Niemueller
36  */
37 
38 /** Constructor.
39  * @param katana katana controller base class
40  * @param logger logger
41  */
43  fawkes::Logger *logger)
44  : Thread("KatanaSensorAcqusitionThread", Thread::OPMODE_WAITFORWAKEUP)
45 {
46  __katana = katana;
47  __logger = logger;
48  __enabled = false;
49 }
50 
51 
52 /** Set whether data acquisition is enabled or not.
53  * In general the thread should only be woken up if sensor data can be acquired.
54  * But for safety data acqusition can also be turned off to be safe against
55  * spurious wakeups. Additionally, this method will acquire the loop mutex,
56  * thereby assuring that a possibly running loop has finished.
57  * @param enabled true to enable sensor data acquisition, false to disable.
58  */
59 void
61 {
62  loop_mutex->lock();
63  __enabled = enabled;
64  loop_mutex->unlock();
65 }
66 
67 
68 void
70 {
71  if (__enabled) {
72  try {
73  __katana->read_sensor_data();
74  } catch (Exception &e) {
75  __logger->log_warn(name(), e.what());
76  }
77  }
78 }
void set_enabled(bool enabled)
Set whether data acquisition is enabled or not.
Fawkes library namespace.
void unlock()
Unlock the mutex.
Definition: mutex.cpp:135
KatanaSensorAcquisitionThread(fawkes::RefPtr< fawkes::KatanaController > katana, fawkes::Logger *logger)
Constructor.
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
virtual void read_sensor_data()=0
Read all sensor data from device into controller libray.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Mutex * loop_mutex
Mutex that is used to protect a call to loop().
Definition: thread.h:139
Base class for exceptions in Fawkes.
Definition: exception.h:36
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void loop()
Code to execute in the thread.
void lock()
Lock this mutex.
Definition: mutex.cpp:89
Interface for logging.
Definition: logger.h:34