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-2017, 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  typedef std::multimap<mrpt::system::TTimeStamp,
79  typedef std::pair<mrpt::system::TTimeStamp, mrpt::utils::CSerializable::Ptr>
81 
82  /** The current state of the sensor
83  * \sa CGenericSensor::getState
84  */
86  {
90  };
91 
92  /** The current state of the sensor */
93  inline TSensorState getState() const { return m_state; }
94  inline double getProcessRate() const { return m_process_rate; }
95  inline std::string getSensorLabel() const { return m_sensorLabel; }
96  inline void setSensorLabel(const std::string& sensorLabel)
97  {
98  m_sensorLabel = sensorLabel;
99  }
100 
101  /** Enable or disable extra debug info dumped to std::cout during sensor
102  * operation.
103  * Default: disabled unless the environment variable
104  * "MRPT_HWDRIVERS_VERBOSE" is set to "1" during object creation.
105  */
106  inline void enableVerbose(bool enabled = true) { m_verbose = enabled; }
107  inline bool isVerboseEnabled() const { return m_verbose; }
108  /** Register a class into the internal list of "CGenericSensor" descendents.
109  * Used internally in the macros DEFINE_GENERIC_SENSOR, etc...
110  *
111  * Can be used as "CGenericSensor::registerClass(
112  * SENSOR_CLASS_ID(CMySensor) );" if
113  * building custom sensors outside mrpt libraries in user code.
114  */
115  static void registerClass(const TSensorClassId* pNewClass);
116 
117  private:
118  /** The critical section for m_objList */
119  std::mutex m_csObjList;
120  /** The queue of objects to be returned by getObservations */
122 
123  /** Used in registerClass */
124  typedef std::map<std::string, const TSensorClassId*>
126  /** Access to singleton */
128 
129  protected:
130  /** @name Common settings to any sensor, loaded in "loadConfig"
131  @{ */
132 
133  /** See CGenericSensor */
135  /** See CGenericSensor */
137  /** If set to N>=2, only 1 out of N observations will be saved to m_objList.
138  */
140  /** See CGenericSensor */
142 
143  /** @} */
144 
145  /** Used when "m_grab_decimation" is enabled */
147 
149  bool m_verbose;
150 
151  // === Data for off-rawlog file external image directory ====
152  // Only used by a few sensor classes.
153  /** The path where to save off-rawlog images: empty means save images
154  * embedded in the rawlog. */
156  /** The extension ("jpg","gif","png",...) that determines the format of
157  * images saved externally \sa setPathForExternalImages */
159  /** For JPEG images, the quality (default=95%). */
161  // ======================================
162 
163  /** This method must be called by derived classes to enqueue a new
164  observation in the list to be returned by getObservations.
165  * Passed objects must be created in dynamic memory and a smart pointer
166  passed. Example of creation:
167  \code
168  mrpt::obs::CObservationGPS::Ptr o = CObservationGPS::Ptr( new
169  CObservationGPS() );
170  o-> .... // Set data
171  appendObservation(o);
172  \endcode
173  * If several observations are passed at once in the vector, they'll be
174  considered as a block regarding the grabbing decimation factor.
175  */
176  void appendObservations(
177  const std::vector<mrpt::utils::CSerializable::Ptr>& obj);
178 
179  //! Like appendObservations() but for just one observation.
181  {
183  std::vector<mrpt::utils::CSerializable::Ptr>(1, obj));
184  }
185 
186  /** Auxiliary structure used for CSerializable runtime class ID support.
187  */
189  {
191  {
193  }
194  };
195 
196  /** Loads specific configuration for the device from a given source of
197  * configuration parameters, for example, an ".ini" file, loading from the
198  * section "[iniSection]" (see utils::CConfigFileBase and derived classes)
199  * \exception This method must throw an exception with a descriptive
200  * message if some critical parameter is missing or has an invalid value.
201  */
202  virtual void loadConfig_sensorSpecific(
203  const mrpt::utils::CConfigFileBase& configSource,
204  const std::string& section) = 0;
205 
206  public:
207  /** Creates a sensor by a name of the class.
208  * Typically the user may want to create a smart pointer around the
209  * returned pointer, whis is made with:
210  * \code
211  * CGenericSensor::Ptr sensor = CGenericSensor::Ptr(
212  * CGenericSensor::createSensor("XXX") );
213  * \endcode
214  * \return A pointer to a new class, or nullptr if class name is unknown.
215  */
216  static CGenericSensor* createSensor(const std::string& className);
217 
218  /** Just like createSensor, but returning a smart pointer to the newly
219  * created sensor object. */
221  const std::string& className)
222  {
223  return CGenericSensor::Ptr(createSensor(className));
224  }
225 
226  /** Constructor */
227  CGenericSensor();
228 
229  CGenericSensor(const CGenericSensor&) = delete;
230  CGenericSensor& operator=(const CGenericSensor&) = delete;
231 
232  /** Destructor */
233  virtual ~CGenericSensor();
234 
235  /** Loads the generic settings common to any sensor (See CGenericSensor),
236  * then call to "loadConfig_sensorSpecific"
237  * \exception This method throws an exception with a descriptive message
238  * if some critical parameter is missing or has an invalid value.
239  */
240  void loadConfig(
241  const mrpt::utils::CConfigFileBase& configSource,
242  const std::string& section);
243 
244  /** This method can or cannot be implemented in the derived class, depending
245  * on the need for it.
246  * \exception This method must throw an exception with a descriptive
247  * message if some critical error is found.
248  */
249  virtual void initialize() {} // Default method does nothing.
250  /** This method will be invoked at a minimum rate of "process_rate" (Hz)
251  * \exception This method must throw an exception with a descriptive
252  * message if some critical error is found.
253  */
254  virtual void doProcess() = 0;
255 
256  /** Returns a list of enqueued objects, emptying it (thread-safe). The
257  * objects must be freed by the invoker.
258  */
259  void getObservations(TListObservations& lstObjects);
260 
261  /** Set the path where to save off-rawlog image files (will be ignored in
262  * those sensors where this is not applicable).
263  * An empty string (the default value at construction) means to save
264  * images embedded in the rawlog, instead of on separate files.
265  * \exception std::exception If the directory doesn't exists and cannot be
266  * created.
267  */
268  virtual void setPathForExternalImages(const std::string& directory)
269  {
270  MRPT_UNUSED_PARAM(directory);
271  // In this base class, the default is to ignore image paths.
272  }
273 
274  /** Set the extension ("jpg","gif","png",...) that determines the format of
275  * images saved externally
276  * The default is "jpg".
277  * \sa setPathForExternalImages, setExternalImageJPEGQuality
278  */
280  {
282  }
283 
284  /** The quality of JPEG compression, when external images is enabled and the
285  * format is "jpg". \sa setExternalImageFormat */
286  void setExternalImageJPEGQuality(const unsigned int quality)
287  {
289  }
290  unsigned int getExternalImageJPEGQuality() const
291  {
293  }
294 
295  public:
297 
298 }; // end of class
299 
300 static_assert(
303  "Copy Check");
304 
305 #define SENSOR_CLASS_ID(class_name) \
306  static_cast<const mrpt::hwdrivers::TSensorClassId*>( \
307  &mrpt::hwdrivers::class_name::class##class_name)
308 
309 #define SENSOR_IS_CLASS(ptrObj, class_name) \
310  (ptrObj->GetRuntimeClass() == SENSOR_CLASS_ID(class_name))
311 
312 /** This declaration must be inserted in all CGenericSensor classes definition,
313  * within the class declaration.
314  */
315 #define DEFINE_GENERIC_SENSOR(class_name) \
316  protected: \
317  static mrpt::hwdrivers::CGenericSensor::CLASSINIT_GENERIC_SENSOR \
318  _init_##class_name; \
319  \
320  public: \
321  static mrpt::hwdrivers::TSensorClassId class##class_name; \
322  virtual const mrpt::hwdrivers::TSensorClassId* GetRuntimeClass() const; \
323  static mrpt::hwdrivers::CGenericSensor* CreateObject(); \
324  static void doRegister() \
325  { \
326  CGenericSensor::registerClass(SENSOR_CLASS_ID(class_name)); \
327  }
328 
329 /** This must be inserted in all CGenericSensor classes implementation files:
330  */
331 #define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace) \
332  mrpt::hwdrivers::CGenericSensor* NameSpace::class_name::CreateObject() \
333  { \
334  return static_cast<hwdrivers::CGenericSensor*>( \
335  new NameSpace::class_name); \
336  } \
337  mrpt::hwdrivers::TSensorClassId NameSpace::class_name::class##class_name = \
338  {#class_name, NameSpace::class_name::CreateObject}; \
339  const mrpt::hwdrivers::TSensorClassId* \
340  NameSpace::class_name::GetRuntimeClass() const \
341  { \
342  return SENSOR_CLASS_ID(class_name); \
343  }
344 
345 } // end of namespace
346 } // end of namespace
347 
348 #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...
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:32
size_t m_grab_decimation_counter
Used when "m_grab_decimation" is enabled.
double m_process_rate
See CGenericSensor.
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...
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Definition: memory.h:134
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.
std::multimap< mrpt::system::TTimeStamp, mrpt::utils::CSerializable::Ptr > TListObservations
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 loadConfig(const mrpt::utils::CConfigFileBase &configSource, const std::string &section)
Loads the generic settings common to any sensor (See CGenericSensor), then call to "loadConfig_sensor...
This class allows loading and storing values and vectors of different types from a configuration text...
virtual ~CGenericSensor()
Destructor.
const char * className
Class name.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
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 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"...
void appendObservation(const mrpt::utils::CSerializable::Ptr &obj)
Like appendObservations() but for just one observation.
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:47
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual void loadConfig_sensorSpecific(const mrpt::utils::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 utils::CConfigFileBase and derived classes)
size_t m_grab_decimation
If set to N>=2, only 1 out of N observations will be saved to m_objList.
std::string getSensorLabel() const
unsigned int m_external_images_jpeg_quality
For JPEG images, the quality (default=95%).
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
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.
std::pair< mrpt::system::TTimeStamp, mrpt::utils::CSerializable::Ptr > TListObsPair
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.
void appendObservations(const std::vector< mrpt::utils::CSerializable::Ptr > &obj)
This method must be called by derived classes to enqueue a new observation in the list to be returned...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019