Main MRPT website > C++ reference for MRPT 1.5.7
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/utils/CUncopiable.h>
15 #include <mrpt/obs/CObservation.h>
17 #include <mrpt/system/threads.h>
18 #include <map>
19 
21 #include <map>
22 
23 
24 namespace mrpt
25 {
26  /** Contains classes for various device interfaces.
27  * \ingroup mrpt_hwdrivers_grp
28  */
29  namespace hwdrivers
30  {
32 
33  /** A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.
34  */
36  {
37  const char* className; //!< Class name
38  CGenericSensor* (*ptrCreateObject)(); //!< Pointer to class constructor
39  };
40 
42  {
43  std::shared_ptr<CGenericSensor> m_ptr;
44  inline CGenericSensorPtr() {}
45  explicit inline CGenericSensorPtr(CGenericSensor* data) : m_ptr(data) { }
46  virtual CGenericSensor * pointer() { return m_ptr.get(); }
47  virtual CGenericSensor * get() { return m_ptr.get(); }
48  virtual const CGenericSensor * pointer() const { return m_ptr.get(); }
49  virtual const CGenericSensor * get() const { return m_ptr.get(); }
50  virtual CGenericSensor* operator ->(void) { return m_ptr.get(); }
51  virtual const CGenericSensor* operator ->(void) const { return m_ptr.get(); }
52  virtual CGenericSensor& operator *(void) { ASSERT_(m_ptr); return *m_ptr.get(); }
53  virtual const CGenericSensor& operator *(void) const { ASSERT_(m_ptr); return *m_ptr.get(); }
54  void clear() { m_ptr.reset(); }
55  bool operator !() const { return !m_ptr.operator bool(); }
56  operator bool() const { return m_ptr.operator bool(); }
57  bool present() const { return m_ptr.get()!=NULL; }
58  void set(CGenericSensor* p) { m_ptr.reset(p); }
59  void reset(CGenericSensor* p) { m_ptr.reset(p); }
60  void clear_unique() { m_ptr.reset(); }
61  };
62 
63 
64  /** A generic interface for a wide-variety of sensors designed to be used in the application RawLogGrabber.
65  * Derived classes should be designed with the following execution flow in mind:
66  * - Object constructor
67  * - CGenericSensor::loadConfig: The following parameters are common to all sensors in rawlog-grabber (they are automatically loaded by rawlog-grabber) - see each class documentation for additional parameters:
68  * - "process_rate": (Mandatory) The rate in Hertz (Hz) at which the sensor thread should invoke "doProcess".
69  * - "max_queue_len": (Optional) The maximum number of objects in the observations queue (default is 200). If overflow occurs, an error message will be issued at run-time.
70  * - "grab_decimation": (Optional) Grab only 1 out of N observations captured by the sensor (default is 1, i.e. do not decimate).
71  * - CGenericSensor::initialize
72  * - CGenericSensor::doProcess
73  * - CGenericSensor::getObservations
74  *
75  * Notice that there are helper methods for managing the internal list of objects (see CGenericSensor::appendObservation).
76  *
77  * <b>Class Factory:</b> This is also a factory of derived classes, through the static method CGenericSensor::createSensor
78  *
79  *
80  * For more details on RawLogGrabber refer to the wiki page:
81  * http://www.mrpt.org/Application:RawLogGrabber
82  * \ingroup mrpt_hwdrivers_grp
83  */
85  {
86  public:
87  virtual const mrpt::hwdrivers::TSensorClassId* GetRuntimeClass() const = 0;
88 
89  typedef std::multimap< mrpt::system::TTimeStamp, mrpt::utils::CSerializablePtr > TListObservations;
90  typedef std::pair< mrpt::system::TTimeStamp, mrpt::utils::CSerializablePtr > TListObsPair;
91 
92  /** The current state of the sensor
93  * \sa CGenericSensor::getState
94  */
96  {
97  ssInitializing = 0,
99  ssError
100  };
101 
102  /** The current state of the sensor */
103  inline TSensorState getState() const { return m_state; }
104 
105  inline double getProcessRate() const { return m_process_rate; }
106 
107  inline std::string getSensorLabel() const { return m_sensorLabel; }
108  inline void setSensorLabel(const std::string& sensorLabel) { m_sensorLabel=sensorLabel; }
109 
110  /** Enable or disable extra debug info dumped to std::cout during sensor operation.
111  * Default: disabled unless the environment variable "MRPT_HWDRIVERS_VERBOSE" is set to "1" during object creation.
112  */
113  inline void enableVerbose(bool enabled=true) { m_verbose=enabled; }
114  inline bool isVerboseEnabled() const { return m_verbose; }
115 
116  /** Register a class into the internal list of "CGenericSensor" descendents.
117  * Used internally in the macros DEFINE_GENERIC_SENSOR, etc...
118  *
119  * Can be used as "CGenericSensor::registerClass( SENSOR_CLASS_ID(CMySensor) );" if
120  * building custom sensors outside mrpt libraries in user code.
121  */
122  static void registerClass(const TSensorClassId* pNewClass);
124  typedef std::shared_ptr<const CGenericSensor> ConstPtr;
125  private:
126  synch::CCriticalSection m_csObjList; //!< The critical section for m_objList
127  TListObservations m_objList; //!< The queue of objects to be returned by getObservations
128 
129  /** Used in registerClass */
130  typedef std::map< std::string , const TSensorClassId *> registered_sensor_classes_t;
131  static registered_sensor_classes_t & get_registered_sensor_classes(); //!< Access to singleton
132 
133  protected:
134  /** @name Common settings to any sensor, loaded in "loadConfig"
135  @{ */
136 
137  double m_process_rate; //!< See CGenericSensor
138  size_t m_max_queue_len; //!< See CGenericSensor
139  size_t m_grab_decimation; //!< If set to N>=2, only 1 out of N observations will be saved to m_objList.
140  std::string m_sensorLabel; //!< See CGenericSensor
141 
142  /** @} */
143 
144  size_t m_grab_decimation_counter; //!< Used when "m_grab_decimation" is enabled
145 
147  bool m_verbose;
148 
149  // === Data for off-rawlog file external image directory ====
150  // Only used by a few sensor classes.
151  std::string m_path_for_external_images; //!< The path where to save off-rawlog images: empty means save images embedded in the rawlog.
152  std::string m_external_images_format; //!< The extension ("jpg","gif","png",...) that determines the format of images saved externally \sa setPathForExternalImages
153  unsigned int m_external_images_jpeg_quality; //!< For JPEG images, the quality (default=95%).
154  // ======================================
155 
156  /** This method must be called by derived classes to enqueue a new observation in the list to be returned by getObservations.
157  * Passed objects must be created in dynamic memory and a smart pointer passed. Example of creation:
158  \code
159  mrpt::obs::CObservationGPSPtr o = CObservationGPSPtr( new CObservationGPS() );
160  o-> .... // Set data
161  appendObservation(o);
162  \endcode
163  * If several observations are passed at once in the vector, they'll be considered as a block regarding the grabbing decimation factor.
164  */
165  void appendObservations( const std::vector<mrpt::utils::CSerializablePtr> &obj);
166 
167  //! Like appendObservations() but for just one observation.
168  void appendObservation( const mrpt::utils::CSerializablePtr &obj)
169  {
170  appendObservations(std::vector<mrpt::utils::CSerializablePtr>(1, obj));
171  }
172 
173  /** Auxiliary structure used for CSerializable runtime class ID support.
174  */
176  {
178  {
180  }
181  };
182 
183  /** 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)
184  * \exception This method must throw an exception with a descriptive message if some critical parameter is missing or has an invalid value.
185  */
186  virtual void loadConfig_sensorSpecific(
187  const mrpt::utils::CConfigFileBase &configSource,
188  const std::string &section ) = 0;
189 
190  public:
191  /** Creates a sensor by a name of the class.
192  * Typically the user may want to create a smart pointer around the returned pointer, whis is made with:
193  * \code
194  * CGenericSensorPtr sensor = CGenericSensorPtr( CGenericSensor::createSensor("XXX") );
195  * \endcode
196  * \return A pointer to a new class, or NULL if class name is unknown.
197  */
198  static CGenericSensor* createSensor(const std::string &className);
199 
200  /** Just like createSensor, but returning a smart pointer to the newly created sensor object. */
201  static inline CGenericSensorPtr createSensorPtr(const std::string &className)
202  {
203  return CGenericSensorPtr(createSensor(className));
204  }
205 
206  /** Constructor */
207  CGenericSensor( );
208 
209  /** Destructor */
210  virtual ~CGenericSensor();
211 
212  /** Loads the generic settings common to any sensor (See CGenericSensor), then call to "loadConfig_sensorSpecific"
213  * \exception This method throws an exception with a descriptive message if some critical parameter is missing or has an invalid value.
214  */
215  void loadConfig(
216  const mrpt::utils::CConfigFileBase &configSource,
217  const std::string &section );
218 
219  /** This method can or cannot be implemented in the derived class, depending on the need for it.
220  * \exception This method must throw an exception with a descriptive message if some critical error is found.
221  */
222  virtual void initialize()
223  { } // Default method does nothing.
224 
225  /** This method will be invoked at a minimum rate of "process_rate" (Hz)
226  * \exception This method must throw an exception with a descriptive message if some critical error is found.
227  */
228  virtual void doProcess() = 0;
229 
230  /** Returns a list of enqueued objects, emptying it (thread-safe). The objects must be freed by the invoker.
231  */
232  void getObservations( TListObservations &lstObjects );
233 
234  /** Set the path where to save off-rawlog image files (will be ignored in those sensors where this is not applicable).
235  * An empty string (the default value at construction) means to save images embedded in the rawlog, instead of on separate files.
236  * \exception std::exception If the directory doesn't exists and cannot be created.
237  */
238  virtual void setPathForExternalImages( const std::string &directory ) {
239  MRPT_UNUSED_PARAM(directory);
240  // In this base class, the default is to ignore image paths.
241  }
242 
243  /** Set the extension ("jpg","gif","png",...) that determines the format of images saved externally
244  * The default is "jpg".
245  * \sa setPathForExternalImages, setExternalImageJPEGQuality
246  */
247  void setExternalImageFormat( const std::string &ext ) {
248  m_external_images_format = ext;
249  }
250 
251  /** The quality of JPEG compression, when external images is enabled and the format is "jpg". \sa setExternalImageFormat */
252  void setExternalImageJPEGQuality(const unsigned int quality) {
253  m_external_images_jpeg_quality = quality;
254  }
255  unsigned int getExternalImageJPEGQuality()const {
256  return m_external_images_jpeg_quality;
257  }
258 
259  public:
261 
262  }; // end of class
263 
264 
265  #define SENSOR_CLASS_ID(class_name) \
266  static_cast<const mrpt::hwdrivers::TSensorClassId*>(& mrpt::hwdrivers::class_name::class##class_name)
267 
268  #define SENSOR_IS_CLASS( ptrObj, class_name ) (ptrObj->GetRuntimeClass()==SENSOR_CLASS_ID(class_name))
269 
270 
271  /** This declaration must be inserted in all CGenericSensor classes definition, within the class declaration.
272  */
273  #define DEFINE_GENERIC_SENSOR(class_name) \
274  protected: \
275  static mrpt::hwdrivers::CGenericSensor::CLASSINIT_GENERIC_SENSOR _init_##class_name;\
276  public: \
277  static mrpt::hwdrivers::TSensorClassId class##class_name; \
278  virtual const mrpt::hwdrivers::TSensorClassId* GetRuntimeClass() const; \
279  static mrpt::hwdrivers::CGenericSensor* CreateObject(); \
280  static void doRegister() \
281  { CGenericSensor::registerClass( SENSOR_CLASS_ID( class_name ) ); }
282 
283  /** This must be inserted in all CGenericSensor classes implementation files:
284  */
285  #define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace) \
286  mrpt::hwdrivers::CGenericSensor* NameSpace::class_name::CreateObject() \
287  { return static_cast<hwdrivers::CGenericSensor*>( new NameSpace::class_name ); } \
288  mrpt::hwdrivers::TSensorClassId NameSpace::class_name::class##class_name = { \
289  #class_name, NameSpace::class_name::CreateObject }; \
290  const mrpt::hwdrivers::TSensorClassId* NameSpace::class_name::GetRuntimeClass() const \
291  { return SENSOR_CLASS_ID(class_name); }
292 
293 
294  } // end of namespace
295 } // end of namespace
296 
297 #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.
This class provides simple critical sections functionality.
double m_process_rate
See CGenericSensor.
synch::CCriticalSection m_csObjList
The critical section for m_objList.
std::shared_ptr< const CGenericSensor > ConstPtr
std::multimap< mrpt::system::TTimeStamp, mrpt::utils::CSerializablePtr > TListObservations
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:112
std::string m_sensorLabel
See CGenericSensor.
void enableVerbose(bool enabled=true)
Enable or disable extra debug info dumped to std::cout during sensor operation.
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
unsigned int getExternalImageJPEGQuality() const
This class allows loading and storing values and vectors of different types from a configuration text...
virtual const CGenericSensor * pointer() const
int quality
Definition: mrpt_jpeglib.h:916
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.
CLASSINIT_GENERIC_SENSOR(const TSensorClassId *pNewClass)
TSensorState getState() const
The current state of the sensor.
void reset(CGenericSensor *p)
CGenericSensorPtr(CGenericSensor *data)
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:30
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
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:3919
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.
std::pair< mrpt::system::TTimeStamp, mrpt::utils::CSerializablePtr > TListObsPair
std::string getSensorLabel() const
Eigen::MatrixBase< Derived >::PlainObject operator!(const Eigen::MatrixBase< Derived > &m)
Unary inversion operator.
Definition: ops_matrices.h:38
unsigned int m_external_images_jpeg_quality
For JPEG images, the quality (default=95%).
class HWDRIVERS_IMPEXP CGenericSensor
void setSensorLabel(const std::string &sensorLabel)
TSensorState
The current state of the sensor.
#define ASSERT_(f)
static CGenericSensorPtr createSensorPtr(const std::string &className)
Just like createSensor, but returning a smart pointer to the newly created sensor object...
virtual void initialize()
This method can or cannot be implemented in the derived class, depending on the need for it...
void appendObservation(const mrpt::utils::CSerializablePtr &obj)
Like appendObservations() but for just one observation.
std::string m_external_images_format
The extension ("jpg","gif","png",...) that determines the format of images saved externally.
void BASE_IMPEXP registerClass(const mrpt::utils::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CSerializable" descendents.
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...
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
GLfloat GLfloat p
Definition: glext.h:5587
A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.
virtual CGenericSensor * pointer()
std::shared_ptr< CGenericSensor > m_ptr
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:59



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019