acquisition_thread.cpp

00001
00002 /***************************************************************************
00003  *  acqusition_thread.cpp - Thread that retrieves the laser data
00004  *
00005  *  Created: Wed Oct 08 13:42:32 2008
00006  *  Copyright  2008  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 "acquisition_thread.h"
00024
00025 #include <core/threading/mutex.h>
00026
00027 using namespace fawkes;
00028 
00029 /** @class LaserAcquisitionThread "acquisition_thread.h"
00030  * Laser acqusition thread.
00031  * Interface for different laser types.
00032  * @author Tim Niemueller
00033  *
00034  * @fn void LaserAcquisitionThread::pre_init(fawkes::Configuration *config, fawkes::Logger *logger) = 0;
00035  * Pre initialization.
00036  * This method is called by the sensor thread for pre-initialization. After this
00037  * method has been executed the methods get_distances_data_size() and
00038  * get_echo_data_size() must return valid data.
00039  * @param config configuration
00040  * @param logger logger instance
00041  */
00042 
00043 /** @var fawkes::Mutex * LaserAcquisitionThread::_data_mutex
00044  * Lock while writing to distances or echoes array or marking new data
00045  */
00046 
00047 /** @var bool LaserAcquisitionThread::_new_data
00048  * Set to true in your loop if new data is available. Set to false automatically
00049  * in get_distance_data() and get_echoes_data().
00050  */
00051 
00052 /** @var float * LaserAcquisitionThread::_distances
00053  * Allocate a float array and copy your distance values measured in meters here.
00054  */
00055 
00056 /** @var float * LaserAcquisitionThread::_echoes
00057  * Allocate a float array and copy your echo values here.
00058  */
00059 
00060 /** @var unsigned int LaserAcquisitionThread::_distances_size
00061  * Assign this the size of the _distances array
00062  */
00063 
00064 /** @var unsigned int LaserAcquisitionThread::_echoes_size
00065  * Assign this the size of the _echoes array
00066  */
00067
00068 
00069 /** Constructor.
00070  * @param thread_name name of the thread, be descriptive
00071  */
00072 LaserAcquisitionThread::LaserAcquisitionThread(const char *thread_name)
00073   : Thread(thread_name, Thread::OPMODE_CONTINUOUS)
00074 {
00075   _data_mutex = new Mutex();
00076   _new_data   = false;
00077   _distances = NULL;
00078   _echoes = NULL;
00079   _distances_size = 0;
00080   _echoes_size = 0;
00081 }
00082
00083 LaserAcquisitionThread::~LaserAcquisitionThread()
00084 {
00085   delete _data_mutex;
00086 }
00087
00088 
00089 /** Lock data if fresh.
00090  * If new data has been received since get_distance_data() or get_echo_data()
00091  * was called last the data is locked, no new data can arrive until you call
00092  * unlock(), otherwise the lock is immediately released after checking.
00093  * @return true if the lock was acquired and there is new data, false otherwise
00094  */
00095 bool
00096 LaserAcquisitionThread::lock_if_new_data()
00097 {
00098   _data_mutex->lock();
00099   if (_new_data) {
00100     return true;
00101   } else {
00102     _data_mutex->unlock();
00103     return false;
00104   }
00105 }
00106
00107 
00108 /** Unlock data, */
00109 void
00110 LaserAcquisitionThread::unlock()
00111 {
00112   _data_mutex->unlock();
00113 }
00114
00115 
00116 /** Get distance data.
00117  * @return Float array with distance values
00118  */
00119 const float *
00120 LaserAcquisitionThread::get_distance_data()
00121 {
00122   _new_data = false;
00123   return _distances;
00124 }
00125
00126 
00127 /** Get echo data.
00128  * @return Float array with echo values
00129  */
00130 const float *
00131 LaserAcquisitionThread::get_echo_data()
00132 {
00133   _new_data = false;
00134   return _echoes;
00135 }
00136
00137 
00138 /** Get distance data size.
00139  * @return size of data float array
00140  */
00141 unsigned int
00142 LaserAcquisitionThread::get_distance_data_size()
00143 {
00144   return _distances_size;
00145 }
00146
00147 
00148 /** Get echo data size.
00149  * @return size of data float array
00150  */
00151 unsigned int
00152 LaserAcquisitionThread::get_echo_data_size()
00153 {
00154   return _echoes_size;
00155 }