MRPT  2.0.2
CGenericSensor.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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_sensorLabel("UNNAMED_SENSOR"),
27 
28  m_path_for_external_images(),
29  m_external_images_format("png")
30 
31 {
32  const char* sVerbose = getenv("MRPT_HWDRIVERS_VERBOSE");
33  m_verbose = (sVerbose != nullptr) && atoi(sVerbose) != 0;
34 }
35 
36 /*-------------------------------------------------------------
37  Destructor
38 -------------------------------------------------------------*/
40 {
41  // Free objects in list, if any:
42  m_objList.clear();
43 }
44 
45 /*-------------------------------------------------------------
46  appendObservations
47 -------------------------------------------------------------*/
49  const std::vector<mrpt::serialization::CSerializable::Ptr>& objs)
50 {
52  {
54 
55  std::lock_guard<std::mutex> lock(m_csObjList);
56 
57  for (const auto& obj : objs)
58  {
59  if (!obj) continue;
60 
61  // It must be a CObservation or a CAction!
62  TTimeStamp timestamp;
63 
64  if (obj->GetRuntimeClass()->derivedFrom(CLASS_ID(CAction)))
65  {
66  timestamp = dynamic_cast<CAction*>(obj.get())->timestamp;
67  }
68  else if (obj->GetRuntimeClass()->derivedFrom(
70  {
71  timestamp = dynamic_cast<CObservation*>(obj.get())->timestamp;
72  }
73  else
74  THROW_EXCEPTION("Passed object must be CObservation.");
75 
76  // Add it:
77  m_objList.insert(TListObsPair(timestamp, obj));
78  }
79  }
80 }
81 
82 /*-------------------------------------------------------------
83  getObservations
84 -------------------------------------------------------------*/
86 {
87  std::lock_guard<std::mutex> lock(m_csObjList);
88  lstObjects = m_objList;
89  m_objList.clear(); // Memory of objects will be freed by invoker.
90 }
91 
92 /*-------------------------------------------------------------
93 
94  createSensor
95 
96 -------------------------------------------------------------*/
97 CGenericSensor* CGenericSensor::createSensor(const std::string& className)
98 {
100  const auto it = regs.find(className);
101  return it == regs.end() ? nullptr : it->second->ptrCreateObject();
102 }
103 
104 // Singleton
107 {
108  static registered_sensor_classes_t reg;
109  return reg;
110 }
111 
112 /*-------------------------------------------------------------
113  registerClass
114 -------------------------------------------------------------*/
116 {
118  regs[pNewClass->className] = pNewClass;
119 }
120 
121 /** Loads the generic settings common to any sensor (See CGenericSensor), then
122  * call to "loadConfig_sensorSpecific"
123  * \exception This method throws an exception with a descriptive message if
124  * some critical parameter is missing or has an invalid value.
125  */
127  const mrpt::config::CConfigFileBase& cfg, const std::string& sect)
128 {
129  MRPT_START
130 
132  sect, "process_rate", 0); // Leave it to 0 so rawlog-grabber can detect
133  // if it's not set by the user.
134  m_max_queue_len = static_cast<size_t>(
135  cfg.read_int(sect, "max_queue_len", int(m_max_queue_len)));
136  m_grab_decimation = static_cast<size_t>(
137  cfg.read_int(sect, "grab_decimation", int(m_grab_decimation)));
138 
139  m_sensorLabel = cfg.read_string(sect, "sensorLabel", m_sensorLabel);
140 
142 
144 
145  MRPT_END
146 }
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.
double m_process_rate
See CGenericSensor.
#define MRPT_START
Definition: exceptions.h:241
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:67
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.
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:102
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.
constexpr auto sect
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.
Declares a class for storing a robot action.
Definition: CAction.h:24
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:245
A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020