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



Page generated by Doxygen 1.9.1 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at mar 26 may 2026 13:06:43 CEST