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-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 }
mrpt::hwdrivers::CGenericSensor::TListObservations
std::multimap< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObservations
Definition: CGenericSensor.h:77
mrpt::hwdrivers::TSensorClassId::className
const char * className
Class name.
Definition: CGenericSensor.h:33
mrpt::hwdrivers::CGenericSensor::~CGenericSensor
virtual ~CGenericSensor()
Destructor.
Definition: CGenericSensor.cpp:44
mrpt::serialization::CSerializable::Ptr
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:37
mrpt::config::CConfigFileBase::read_double
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:101
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
mrpt::hwdrivers::CGenericSensor::m_max_queue_len
size_t m_max_queue_len
See CGenericSensor.
Definition: CGenericSensor.h:135
mrpt::hwdrivers::CGenericSensor::m_process_rate
double m_process_rate
See CGenericSensor.
Definition: CGenericSensor.h:133
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::hwdrivers::CGenericSensor::createSensor
static CGenericSensor * createSensor(const std::string &className)
Creates a sensor by a name of the class.
Definition: CGenericSensor.cpp:103
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::hwdrivers::CGenericSensor::m_grab_decimation_counter
size_t m_grab_decimation_counter
Used when "m_grab_decimation" is enabled.
Definition: CGenericSensor.h:145
mrpt::config::CConfigFileBase::read_string
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:169
mrpt::system::TTimeStamp
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:31
mrpt::hwdrivers::CGenericSensor::registered_sensor_classes_t
std::map< std::string, const TSensorClassId * > registered_sensor_classes_t
Used in registerClass.
Definition: CGenericSensor.h:124
mrpt::config::CConfigFileBase::read_int
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:125
mrpt::hwdrivers::CGenericSensor::m_verbose
bool m_verbose
Definition: CGenericSensor.h:148
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::config::CConfigFileBase
This class allows loading and storing values and vectors of different types from a configuration text...
Definition: config/CConfigFileBase.h:44
mrpt::hwdrivers::CGenericSensor::registerClass
static void registerClass(const TSensorClassId *pNewClass)
Register a class into the internal list of "CGenericSensor" descendents.
Definition: CGenericSensor.cpp:121
mrpt::hwdrivers::CGenericSensor::m_csObjList
std::mutex m_csObjList
The critical section for m_objList.
Definition: CGenericSensor.h:118
mrpt::hwdrivers::CGenericSensor::appendObservations
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...
Definition: CGenericSensor.cpp:53
CGenericSensor.h
mrpt::hwdrivers::CGenericSensor::getObservations
void getObservations(TListObservations &lstObjects)
Returns a list of enqueued objects, emptying it (thread-safe).
Definition: CGenericSensor.cpp:91
mrpt::hwdrivers::CGenericSensor::m_sensorLabel
std::string m_sensorLabel
See CGenericSensor.
Definition: CGenericSensor.h:140
mrpt::hwdrivers::CGenericSensor
A generic interface for a wide-variety of sensors designed to be used in the application RawLogGrabbe...
Definition: CGenericSensor.h:70
mrpt::serialization
Definition: aligned_serialization.h:14
mrpt::hwdrivers::TSensorClassId
A structure for runtime ID class type information in the context of hwdrivers::CGenericSensor.
Definition: CGenericSensor.h:30
mrpt::hwdrivers::CGenericSensor::m_objList
TListObservations m_objList
The queue of objects to be returned by getObservations.
Definition: CGenericSensor.h:120
mrpt::hwdrivers::CGenericSensor::loadConfig_sensorSpecific
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,...
CLASS_ID
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
CAction.h
CObservation.h
mrpt::hwdrivers::CGenericSensor::TListObsPair
std::pair< mrpt::system::TTimeStamp, mrpt::serialization::CSerializable::Ptr > TListObsPair
Definition: CGenericSensor.h:79
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::obs::CObservation
Declares a class that represents any robot's observation.
Definition: CObservation.h:43
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::hwdrivers::CGenericSensor::loadConfig
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...
Definition: CGenericSensor.cpp:132
mrpt::obs::CAction
Declares a class for storing a robot action.
Definition: CAction.h:27
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::hwdrivers::CGenericSensor::get_registered_sensor_classes
static registered_sensor_classes_t & get_registered_sensor_classes()
Access to singleton.
Definition: CGenericSensor.cpp:112
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
hwdrivers-precomp.h
mrpt::hwdrivers::CGenericSensor::m_grab_decimation
size_t m_grab_decimation
If set to N>=2, only 1 out of N observations will be saved to m_objList.
Definition: CGenericSensor.h:138



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST