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-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 #include "hwdrivers-precomp.h" // Precompiled headers
11 
13 #include <mrpt/obs/CAction.h>
14 #include <mrpt/obs/CObservation.h>
15 
16 using namespace mrpt::obs;
17 using namespace mrpt::system;
18 using namespace mrpt::hwdrivers;
19 using namespace mrpt::serialization;
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::serialization::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::config::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...
size_t m_grab_decimation_counter
Used when "m_grab_decimation" is enabled.
Scalar * iterator
Definition: eigen_plugins.h:26
double m_process_rate
See CGenericSensor.
#define MRPT_START
Definition: exceptions.h:262
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
std::pair< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObsPair
std::string m_sensorLabel
See CGenericSensor.
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.
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
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.
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:84
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.
This namespace contains representation of robot actions and observations.
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
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...
size_t m_max_queue_len
See CGenericSensor.
GLsizei const GLchar ** string
Definition: glext.h:4101
Declares a class for storing a robot action.
Definition: CAction.h:27
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::multimap< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObservations
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
std::mutex m_csObjList
The critical section for m_objList.
#define MRPT_END
Definition: exceptions.h:266
A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.



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