Main MRPT website > C++ reference for MRPT 1.9.9
CGenericSensor.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef CGenericSensor_H
11 #define CGenericSensor_H
12 
14 #include <mrpt/obs/CObservation.h>
15 #include <map>
16 #include <mutex>
17 
18 namespace mrpt
19 {
20 /** Contains classes for various device interfaces.
21  * \ingroup mrpt_hwdrivers_grp
22  */
23 namespace hwdrivers
24 {
25 class CGenericSensor;
26 
27 /** A structure for runtime ID class type information in the context of
28  * hwdrivers::CGenericSensor.
29  */
31 {
32  /** Class name */
33  const char* className;
34  /** Pointer to class constructor */
35  CGenericSensor* (*ptrCreateObject)();
36 };
37 
38 /** A generic interface for a wide-variety of sensors designed to be used in the
39  *application RawLogGrabber.
40  * Derived classes should be designed with the following execution flow in
41  *mind:
42  * - Object constructor
43  * - CGenericSensor::loadConfig: The following parameters are common to all
44  *sensors in rawlog-grabber (they are automatically loaded by rawlog-grabber) -
45  *see each class documentation for additional parameters:
46  * - "process_rate": (Mandatory) The rate in Hertz (Hz) at which the
47  *sensor
48  *thread should invoke "doProcess".
49  * - "max_queue_len": (Optional) The maximum number of objects in the
50  *observations queue (default is 200). If overflow occurs, an error message
51  *will be issued at run-time.
52  * - "grab_decimation": (Optional) Grab only 1 out of N observations
53  *captured
54  *by the sensor (default is 1, i.e. do not decimate).
55  * - CGenericSensor::initialize
56  * - CGenericSensor::doProcess
57  * - CGenericSensor::getObservations
58  *
59  * Notice that there are helper methods for managing the internal list of
60  *objects (see CGenericSensor::appendObservation).
61  *
62  * <b>Class Factory:</b> This is also a factory of derived classes, through
63  *the static method CGenericSensor::createSensor
64  *
65  *
66  * For more details on RawLogGrabber refer to the wiki page:
67  * http://www.mrpt.org/Application:RawLogGrabber
68  * \ingroup mrpt_hwdrivers_grp
69  */
71 {
72  public:
73  using Ptr = std::shared_ptr<CGenericSensor>;
74  virtual const mrpt::hwdrivers::TSensorClassId* GetRuntimeClass() const = 0;
75 
76  using TListObservations = std::multimap<
78  using TListObsPair = std::pair<
80 
81  /** The current state of the sensor
82  * \sa CGenericSensor::getState
83  */
85  {
89  };
90 
91  /** The current state of the sensor */
92  inline TSensorState getState() const { return m_state; }
93  inline double getProcessRate() const { return m_process_rate; }
94  inline std::string getSensorLabel() const { return m_sensorLabel; }
95  inline void setSensorLabel(const std::string& sensorLabel)
96  {
97  m_sensorLabel = sensorLabel;
98  }
99 
100  /** Enable or disable extra debug info dumped to std::cout during sensor
101  * operation.
102  * Default: disabled unless the environment variable
103  * "MRPT_HWDRIVERS_VERBOSE" is set to "1" during object creation.
104  */
105  inline void enableVerbose(bool enabled = true) { m_verbose = enabled; }
106  inline bool isVerboseEnabled() const { return m_verbose; }
107  /** Register a class into the internal list of "CGenericSensor" descendents.
108  * Used internally in the macros DEFINE_GENERIC_SENSOR, etc...
109  *
110  * Can be used as "CGenericSensor::registerClass(
111  * SENSOR_CLASS_ID(CMySensor) );" if
112  * building custom sensors outside mrpt libraries in user code.
113  */
114  static void registerClass(const TSensorClassId* pNewClass);
115 
116  private:
117  /** The critical section for m_objList */
118  std::mutex m_csObjList;
119  /** The queue of objects to be returned by getObservations */
121 
122  /** Used in registerClass */
124  std::map<std::string, const TSensorClassId*>;
125  /** Access to singleton */
127 
128  protected:
129  /** @name Common settings to any sensor, loaded in "loadConfig"
130  @{ */
131 
132  /** See CGenericSensor */
134  /** See CGenericSensor */
136  /** If set to N>=2, only 1 out of N observations will be saved to m_objList.
137  */
139  /** See CGenericSensor */
141 
142  /** @} */
143 
144  /** Used when "m_grab_decimation" is enabled */
146 
148  bool m_verbose;
149 
150  // === Data for off-rawlog file external image directory ====
151  // Only used by a few sensor classes.
152  /** The path where to save off-rawlog images: empty means save images
153  * embedded in the rawlog. */
155  /** The extension ("jpg","gif","png",...) that determines the format of
156  * images saved externally \sa setPathForExternalImages */
158  /** For JPEG images, the quality (default=95%). */
160  // ======================================
161 
162  /** This method must be called by derived classes to enqueue a new
163  observation in the list to be returned by getObservations.
164  * Passed objects must be created in dynamic memory and a smart pointer
165  passed. Example of creation:
166  \code
167  mrpt::obs::CObservationGPS::Ptr o = CObservationGPS::Ptr( new
168  CObservationGPS() );
169  o-> .... // Set data
170  appendObservation(o);
171  \endcode
172  * If several observations are passed at once in the vector, they'll be
173  considered as a block regarding the grabbing decimation factor.
174  */
175  void appendObservations(
176  const std::vector<mrpt::serialization::CSerializable::Ptr>& obj);
177 
178  //! Like appendObservations() but for just one observation.
180  {
182  std::vector<mrpt::serialization::CSerializable::Ptr>(1, obj));
183  }
184 
185  /** Auxiliary structure used for CSerializable runtime class ID support.
186  */
188  {
190  {
192  }
193  };
194 
195  /** Loads specific configuration for the device from a given source of
196  * configuration parameters, for example, an ".ini" file, loading from the
197  * section "[iniSection]" (see config::CConfigFileBase and derived classes)
198  * \exception This method must throw an exception with a descriptive
199  * message if some critical parameter is missing or has an invalid value.
200  */
201  virtual void loadConfig_sensorSpecific(
202  const mrpt::config::CConfigFileBase& configSource,
203  const std::string& section) = 0;
204 
205  public:
206  /** Creates a sensor by a name of the class.
207  * Typically the user may want to create a smart pointer around the
208  * returned pointer, whis is made with:
209  * \code
210  * CGenericSensor::Ptr sensor = CGenericSensor::Ptr(
211  * CGenericSensor::createSensor("XXX") );
212  * \endcode
213  * \return A pointer to a new class, or nullptr if class name is unknown.
214  */
215  static CGenericSensor* createSensor(const std::string& className);
216 
217  /** Just like createSensor, but returning a smart pointer to the newly
218  * created sensor object. */
220  const std::string& className)
221  {
222  return CGenericSensor::Ptr(createSensor(className));
223  }
224 
225  /** Constructor */
226  CGenericSensor();
227 
228  CGenericSensor(const CGenericSensor&) = delete;
229  CGenericSensor& operator=(const CGenericSensor&) = delete;
230 
231  /** Destructor */
232  virtual ~CGenericSensor();
233 
234  /** Loads the generic settings common to any sensor (See CGenericSensor),
235  * then call to "loadConfig_sensorSpecific"
236  * \exception This method throws an exception with a descriptive message
237  * if some critical parameter is missing or has an invalid value.
238  */
239  void loadConfig(
240  const mrpt::config::CConfigFileBase& configSource,
241  const std::string& section);
242 
243  /** This method can or cannot be implemented in the derived class, depending
244  * on the need for it.
245  * \exception This method must throw an exception with a descriptive
246  * message if some critical error is found.
247  */
248  virtual void initialize() {} // Default method does nothing.
249  /** This method will be invoked at a minimum rate of "process_rate" (Hz)
250  * \exception This method must throw an exception with a descriptive
251  * message if some critical error is found.
252  */
253  virtual void doProcess() = 0;
254 
255  /** Returns a list of enqueued objects, emptying it (thread-safe). The
256  * objects must be freed by the invoker.
257  */
258  void getObservations(TListObservations& lstObjects);
259 
260  /** Set the path where to save off-rawlog image files (will be ignored in
261  * those sensors where this is not applicable).
262  * An empty string (the default value at construction) means to save
263  * images embedded in the rawlog, instead of on separate files.
264  * \exception std::exception If the directory doesn't exists and cannot be
265  * created.
266  */
267  virtual void setPathForExternalImages(const std::string& directory)
268  {
269  MRPT_UNUSED_PARAM(directory);
270  // In this base class, the default is to ignore image paths.
271  }
272 
273  /** Set the extension ("jpg","gif","png",...) that determines the format of
274  * images saved externally
275  * The default is "jpg".
276  * \sa setPathForExternalImages, setExternalImageJPEGQuality
277  */
279  {
281  }
282 
283  /** The quality of JPEG compression, when external images is enabled and the
284  * format is "jpg". \sa setExternalImageFormat */
285  void setExternalImageJPEGQuality(const unsigned int quality)
286  {
288  }
289  unsigned int getExternalImageJPEGQuality() const
290  {
292  }
293 
294  public:
296 
297 }; // end of class
298 
299 static_assert(
302  "Copy Check");
303 
304 #define SENSOR_CLASS_ID(class_name) \
305  static_cast<const mrpt::hwdrivers::TSensorClassId*>( \
306  &mrpt::hwdrivers::class_name::class##class_name)
307 
308 #define SENSOR_IS_CLASS(ptrObj, class_name) \
309  (ptrObj->GetRuntimeClass() == SENSOR_CLASS_ID(class_name))
310 
311 /** This declaration must be inserted in all CGenericSensor classes definition,
312  * within the class declaration.
313  */
314 #define DEFINE_GENERIC_SENSOR(class_name) \
315  protected: \
316  static mrpt::hwdrivers::CGenericSensor::CLASSINIT_GENERIC_SENSOR \
317  _init_##class_name; \
318  \
319  public: \
320  static mrpt::hwdrivers::TSensorClassId class##class_name; \
321  virtual const mrpt::hwdrivers::TSensorClassId* GetRuntimeClass() const; \
322  static mrpt::hwdrivers::CGenericSensor* CreateObject(); \
323  static void doRegister() \
324  { \
325  CGenericSensor::registerClass(SENSOR_CLASS_ID(class_name)); \
326  }
327 
328 /** This must be inserted in all CGenericSensor classes implementation files:
329  */
330 #define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace) \
331  mrpt::hwdrivers::CGenericSensor* NameSpace::class_name::CreateObject() \
332  { \
333  return static_cast<hwdrivers::CGenericSensor*>( \
334  new NameSpace::class_name); \
335  } \
336  mrpt::hwdrivers::TSensorClassId NameSpace::class_name::class##class_name = \
337  {#class_name, NameSpace::class_name::CreateObject}; \
338  const mrpt::hwdrivers::TSensorClassId* \
339  NameSpace::class_name::GetRuntimeClass() const \
340  { \
341  return SENSOR_CLASS_ID(class_name); \
342  }
343 
344 } // namespace hwdrivers
345 } // namespace mrpt
346 
347 #endif
mrpt::hwdrivers::CGenericSensor::setExternalImageJPEGQuality
void setExternalImageJPEGQuality(const unsigned int quality)
The quality of JPEG compression, when external images is enabled and the format is "jpg".
Definition: CGenericSensor.h:285
mrpt::hwdrivers::CGenericSensor::TListObservations
std::multimap< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObservations
Definition: CGenericSensor.h:77
mrpt::hwdrivers::CGenericSensor::Ptr
std::shared_ptr< CGenericSensor > Ptr
Definition: CGenericSensor.h:73
mrpt::hwdrivers::TSensorClassId::className
const char * className
Class name.
Definition: CGenericSensor.h:33
mrpt::hwdrivers::CGenericSensor::~CGenericSensor
virtual ~CGenericSensor()
Destructor.
Definition: CGenericSensor.cpp:44
mrpt::serialization::CSerializable::Ptr
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:37
mrpt::hwdrivers::CGenericSensor::getState
TSensorState getState() const
The current state of the sensor
Definition: CGenericSensor.h:92
mrpt::hwdrivers::CGenericSensor::CLASSINIT_GENERIC_SENSOR
Auxiliary structure used for CSerializable runtime class ID support.
Definition: CGenericSensor.h:187
mrpt::hwdrivers::CGenericSensor::m_path_for_external_images
std::string m_path_for_external_images
The path where to save off-rawlog images: empty means save images embedded in the rawlog.
Definition: CGenericSensor.h:154
mrpt::hwdrivers::CGenericSensor::GetRuntimeClass
virtual const mrpt::hwdrivers::TSensorClassId * GetRuntimeClass() const =0
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::hwdrivers::CGenericSensor::ssWorking
@ ssWorking
Definition: CGenericSensor.h:87
mrpt::hwdrivers::CGenericSensor::enableVerbose
void enableVerbose(bool enabled=true)
Enable or disable extra debug info dumped to std::cout during sensor operation.
Definition: CGenericSensor.h:105
mrpt::hwdrivers::CGenericSensor::ssError
@ ssError
Definition: CGenericSensor.h:88
mrpt::hwdrivers::CGenericSensor::m_max_queue_len
size_t m_max_queue_len
See CGenericSensor.
Definition: CGenericSensor.h:135
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::hwdrivers::CGenericSensor::m_process_rate
double m_process_rate
See CGenericSensor.
Definition: CGenericSensor.h:133
mrpt::hwdrivers::CGenericSensor::TSensorState
TSensorState
The current state of the sensor.
Definition: CGenericSensor.h:84
mrpt::hwdrivers::CGenericSensor::m_state
TSensorState m_state
Definition: CGenericSensor.h:147
mrpt::hwdrivers::CGenericSensor::createSensor
static CGenericSensor * createSensor(const std::string &className)
Creates a sensor by a name of the class.
Definition: CGenericSensor.cpp:103
mrpt::hwdrivers::CGenericSensor::m_grab_decimation_counter
size_t m_grab_decimation_counter
Used when "m_grab_decimation" is enabled.
Definition: CGenericSensor.h:145
mrpt::hwdrivers::CGenericSensor::createSensorPtr
static CGenericSensor::Ptr createSensorPtr(const std::string &className)
Just like createSensor, but returning a smart pointer to the newly created sensor object.
Definition: CGenericSensor.h:219
mrpt::hwdrivers::CGenericSensor::operator=
CGenericSensor & operator=(const CGenericSensor &)=delete
mrpt::hwdrivers::CGenericSensor::doProcess
virtual void doProcess()=0
This method will be invoked at a minimum rate of "process_rate" (Hz)
mrpt::system::TTimeStamp
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:31
mrpt::hwdrivers::CGenericSensor::registered_sensor_classes_t
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
Definition: CGenericSensor.h:124
mrpt::hwdrivers::CGenericSensor::getExternalImageJPEGQuality
unsigned int getExternalImageJPEGQuality() const
Definition: CGenericSensor.h:289
CConfigFileBase.h
mrpt::hwdrivers::CGenericSensor::setPathForExternalImages
virtual void setPathForExternalImages(const std::string &directory)
Set the path where to save off-rawlog image files (will be ignored in those sensors where this is not...
Definition: CGenericSensor.h:267
mrpt::hwdrivers::CGenericSensor::m_external_images_format
std::string m_external_images_format
The extension ("jpg","gif","png",...) that determines the format of images saved externally.
Definition: CGenericSensor.h:157
mrpt::hwdrivers::CGenericSensor::m_verbose
bool m_verbose
Definition: CGenericSensor.h:148
mrpt::hwdrivers::CGenericSensor::setExternalImageFormat
void setExternalImageFormat(const std::string &ext)
Set the extension ("jpg","gif","png",...) that determines the format of images saved externally The d...
Definition: CGenericSensor.h:278
mrpt::hwdrivers::CGenericSensor::getProcessRate
double getProcessRate() const
Definition: CGenericSensor.h:93
mrpt::hwdrivers::CGenericSensor::CLASSINIT_GENERIC_SENSOR::CLASSINIT_GENERIC_SENSOR
CLASSINIT_GENERIC_SENSOR(const TSensorClassId *pNewClass)
Definition: CGenericSensor.h:189
mrpt::config::CConfigFileBase
This class allows loading and storing values and vectors of different types from a configuration text...
Definition: config/CConfigFileBase.h:44
mrpt::hwdrivers::CGenericSensor::registerClass
static void registerClass(const TSensorClassId *pNewClass)
Register a class into the internal list of "CGenericSensor" descendents.
Definition: CGenericSensor.cpp:121
mrpt::hwdrivers::CGenericSensor::m_csObjList
std::mutex m_csObjList
The critical section for m_objList.
Definition: CGenericSensor.h:118
mrpt::hwdrivers::CGenericSensor::appendObservations
void appendObservations(const std::vector< mrpt::serialization::CSerializable::Ptr > &obj)
This method must be called by derived classes to enqueue a new observation in the list to be returned...
Definition: CGenericSensor.cpp:53
mrpt::hwdrivers::CGenericSensor::getObservations
void getObservations(TListObservations &lstObjects)
Returns a list of enqueued objects, emptying it (thread-safe).
Definition: CGenericSensor.cpp:91
mrpt::hwdrivers::CGenericSensor::appendObservation
void appendObservation(const mrpt::serialization::CSerializable::Ptr &obj)
Like appendObservations() but for just one observation.
Definition: CGenericSensor.h:179
mrpt::hwdrivers::CGenericSensor::setSensorLabel
void setSensorLabel(const std::string &sensorLabel)
Definition: CGenericSensor.h:95
mrpt::hwdrivers::CGenericSensor::m_sensorLabel
std::string m_sensorLabel
See CGenericSensor.
Definition: CGenericSensor.h:140
mrpt::hwdrivers::CGenericSensor
A generic interface for a wide-variety of sensors designed to be used in the application RawLogGrabbe...
Definition: CGenericSensor.h:70
mrpt::hwdrivers::TSensorClassId
A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.
Definition: CGenericSensor.h:30
mrpt::hwdrivers::CGenericSensor::m_objList
TListObservations m_objList
The queue of objects to be returned by getObservations.
Definition: CGenericSensor.h:120
mrpt::hwdrivers::CGenericSensor::m_external_images_jpeg_quality
unsigned int m_external_images_jpeg_quality
For JPEG images, the quality (default=95%).
Definition: CGenericSensor.h:159
mrpt::hwdrivers::CGenericSensor::loadConfig_sensorSpecific
virtual void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &section)=0
Loads specific configuration for the device from a given source of configuration parameters,...
mrpt::hwdrivers::CGenericSensor::isVerboseEnabled
bool isVerboseEnabled() const
Definition: CGenericSensor.h:106
mrpt::hwdrivers::CGenericSensor::CGenericSensor
CGenericSensor()
Constructor.
Definition: CGenericSensor.cpp:25
CObservation.h
value
GLsizei const GLfloat * value
Definition: glext.h:4117
mrpt::hwdrivers::CGenericSensor::TListObsPair
std::pair< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObsPair
Definition: CGenericSensor.h:79
MRPT_MAKE_ALIGNED_OPERATOR_NEW
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Put this macro inside any class with members that require {16,32,64}-byte memory alignment (e....
Definition: aligned_allocator.h:90
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::hwdrivers::CGenericSensor::ssInitializing
@ ssInitializing
Definition: CGenericSensor.h:86
mrpt::hwdrivers::CGenericSensor::loadConfig
void loadConfig(const mrpt::config::CConfigFileBase &configSource, const std::string &section)
Loads the generic settings common to any sensor (See CGenericSensor), then call to "loadConfig_sensor...
Definition: CGenericSensor.cpp:132
mrpt::hwdrivers::CGenericSensor::getSensorLabel
std::string getSensorLabel() const
Definition: CGenericSensor.h:94
mrpt::hwdrivers::CGenericSensor::initialize
virtual void initialize()
This method can or cannot be implemented in the derived class, depending on the need for it.
Definition: CGenericSensor.h:248
mrpt::hwdrivers::CGenericSensor::get_registered_sensor_classes
static registered_sensor_classes_t & get_registered_sensor_classes()
Access to singleton.
Definition: CGenericSensor.cpp:112
mrpt::hwdrivers::CGenericSensor::m_grab_decimation
size_t m_grab_decimation
If set to N>=2, only 1 out of N observations will be saved to m_objList.
Definition: CGenericSensor.h:138



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST