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:
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
TListObservations m_objList
The queue of objects to be returned by getObservations.
A generic interface for a wide-variety of sensors designed to be used in the application RawLogGrabbe...
size_t m_grab_decimation_counter
Used when "m_grab_decimation" is enabled.
double m_process_rate
See CGenericSensor.
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
void appendObservation(const mrpt::serialization::CSerializable::Ptr &obj)
Like appendObservations() but for just one observation.
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...
std::pair< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObsPair
std::string m_sensorLabel
See CGenericSensor.
virtual void doProcess()=0
This method will be invoked at a minimum rate of "process_rate" (Hz)
void enableVerbose(bool enabled=true)
Enable or disable extra debug info dumped to std::cout during sensor operation.
void getObservations(TListObservations &lstObjects)
Returns a list of enqueued objects, emptying it (thread-safe).
std::shared_ptr< CGenericSensor > Ptr
static CGenericSensor * createSensor(const std::string &className)
Creates a sensor by a name of the class.
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Put this macro inside any class with members that require {16,32,64}-byte memory alignment (e...
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
unsigned int getExternalImageJPEGQuality() const
static CGenericSensor::Ptr createSensorPtr(const std::string &className)
Just like createSensor, but returning a smart pointer to the newly created sensor object...
CGenericSensor & operator=(const CGenericSensor &)=delete
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...
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
This class allows loading and storing values and vectors of different types from a configuration text...
virtual ~CGenericSensor()
Destructor.
const char * className
Class name.
static void registerClass(const TSensorClassId *pNewClass)
Register a class into the internal list of "CGenericSensor" descendents.
static registered_sensor_classes_t & get_registered_sensor_classes()
Access to singleton.
CLASSINIT_GENERIC_SENSOR(const TSensorClassId *pNewClass)
TSensorState getState() const
The current state of the sensor.
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...
void setExternalImageFormat(const std::string &ext)
Set the extension ("jpg","gif","png",...) that determines the format of images saved externally The d...
size_t m_max_queue_len
See CGenericSensor.
GLsizei const GLchar ** string
Definition: glext.h:4101
void setExternalImageJPEGQuality(const unsigned int quality)
The quality of JPEG compression, when external images is enabled and the format is "jpg"...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t m_grab_decimation
If set to N>=2, only 1 out of N observations will be saved to m_objList.
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, for example, an ".ini" file, loading from the section "[iniSection]" (see config::CConfigFileBase and derived classes)
std::string getSensorLabel() const
std::multimap< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObservations
unsigned int m_external_images_jpeg_quality
For JPEG images, the quality (default=95%).
std::mutex m_csObjList
The critical section for m_objList.
void setSensorLabel(const std::string &sensorLabel)
TSensorState
The current state of the sensor.
virtual void initialize()
This method can or cannot be implemented in the derived class, depending on the need for it...
virtual const mrpt::hwdrivers::TSensorClassId * GetRuntimeClass() const =0
std::string m_external_images_format
The extension ("jpg","gif","png",...) that determines the format of images saved externally.
GLsizei const GLfloat * value
Definition: glext.h:4117
Auxiliary structure used for CSerializable runtime class ID support.
std::string m_path_for_external_images
The path where to save off-rawlog images: empty means save images embedded in the rawlog...
A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020