Main MRPT website > C++ reference for MRPT 1.9.9
CGenericSensor.cpp
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 #include "hwdrivers-precomp.h" // Precompiled headers
11 
13 #include <mrpt/obs/CAction.h>
14 #include <mrpt/obs/CObservation.h>
15 
16 using namespace mrpt::utils;
17 using namespace mrpt::obs;
18 using namespace mrpt::system;
19 using namespace mrpt::hwdrivers;
20 using namespace std;
21 
22 /*-------------------------------------------------------------
23  Constructor
24 -------------------------------------------------------------*/
25 CGenericSensor::CGenericSensor()
26  : m_process_rate(0),
27  m_max_queue_len(200),
28  m_grab_decimation(0),
29  m_sensorLabel("UNNAMED_SENSOR"),
30  m_grab_decimation_counter(0),
31  m_state(ssInitializing),
32  m_verbose(false),
33  m_path_for_external_images(),
34  m_external_images_format("png"),
35  m_external_images_jpeg_quality(95)
36 {
37  const char* sVerbose = getenv("MRPT_HWDRIVERS_VERBOSE");
38  m_verbose = (sVerbose != nullptr) && atoi(sVerbose) != 0;
39 }
40 
41 /*-------------------------------------------------------------
42  Destructor
43 -------------------------------------------------------------*/
45 {
46  // Free objects in list, if any:
47  m_objList.clear();
48 }
49 
50 /*-------------------------------------------------------------
51  appendObservations
52 -------------------------------------------------------------*/
54  const std::vector<mrpt::utils::CSerializable::Ptr>& objs)
55 {
57  {
59 
60  std::lock_guard<std::mutex> lock(m_csObjList);
61 
62  for (size_t i = 0; i < objs.size(); i++)
63  {
64  const CSerializable::Ptr& obj = objs[i];
65  if (!obj) continue;
66 
67  // It must be a CObservation or a CAction!
68  TTimeStamp timestamp;
69 
70  if (obj->GetRuntimeClass()->derivedFrom(CLASS_ID(CAction)))
71  {
72  timestamp = dynamic_cast<CAction*>(obj.get())->timestamp;
73  }
74  else if (
75  obj->GetRuntimeClass()->derivedFrom(CLASS_ID(CObservation)))
76  {
77  timestamp = dynamic_cast<CObservation*>(obj.get())->timestamp;
78  }
79  else
80  THROW_EXCEPTION("Passed object must be CObservation.");
81 
82  // Add it:
83  m_objList.insert(TListObsPair(timestamp, obj));
84  }
85  }
86 }
87 
88 /*-------------------------------------------------------------
89  getObservations
90 -------------------------------------------------------------*/
92 {
93  std::lock_guard<std::mutex> lock(m_csObjList);
94  lstObjects = m_objList;
95  m_objList.clear(); // Memory of objects will be freed by invoker.
96 }
97 
98 /*-------------------------------------------------------------
99 
100  createSensor
101 
102 -------------------------------------------------------------*/
104 {
106  const registered_sensor_classes_t::iterator it = regs.find(className);
107  return it == regs.end() ? nullptr : it->second->ptrCreateObject();
108 }
109 
110 // Singleton
113 {
114  static registered_sensor_classes_t reg;
115  return reg;
116 }
117 
118 /*-------------------------------------------------------------
119  registerClass
120 -------------------------------------------------------------*/
122 {
124  regs[pNewClass->className] = pNewClass;
125 }
126 
127 /** Loads the generic settings common to any sensor (See CGenericSensor), then
128  * call to "loadConfig_sensorSpecific"
129  * \exception This method throws an exception with a descriptive message if
130  * some critical parameter is missing or has an invalid value.
131  */
133  const mrpt::utils::CConfigFileBase& cfg, const std::string& sect)
134 {
135  MRPT_START
136 
138  sect, "process_rate", 0); // Leave it to 0 so rawlog-grabber can detect
139  // if it's not set by the user.
140  m_max_queue_len = static_cast<size_t>(
141  cfg.read_int(sect, "max_queue_len", int(m_max_queue_len)));
142  m_grab_decimation = static_cast<size_t>(
143  cfg.read_int(sect, "grab_decimation", int(m_grab_decimation)));
144 
145  m_sensorLabel = cfg.read_string(sect, "sensorLabel", m_sensorLabel);
146 
148 
149  loadConfig_sensorSpecific(cfg, sect);
150 
151  MRPT_END
152 }
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.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
std::string m_sensorLabel
See CGenericSensor.
#define THROW_EXCEPTION(msg)
Scalar * iterator
Definition: eigen_plugins.h:26
Contains classes for various device interfaces.
void getObservations(TListObservations &lstObjects)
Returns a list of enqueued objects, emptying it (thread-safe).
static CGenericSensor * createSensor(const std::string &className)
Creates a sensor by a name of the class.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
std::multimap< mrpt::system::TTimeStamp, mrpt::utils::CSerializable::Ptr > TListObservations
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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...
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
virtual ~CGenericSensor()
Destructor.
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
const char * className
Class name.
#define MRPT_END
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.
This namespace contains representation of robot actions and observations.
size_t m_max_queue_len
See CGenericSensor.
GLsizei const GLchar ** string
Definition: glext.h:4101
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:47
Declares a class for storing a robot action.
Definition: CAction.h:28
#define MRPT_START
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.
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:41
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
std::mutex m_csObjList
The critical section for m_objList.
std::pair< mrpt::system::TTimeStamp, mrpt::utils::CSerializable::Ptr > TListObsPair
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