SensorBase.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/core/format.h>
12 #include <mvsim/VehicleBase.h>
13 #include <mvsim/World.h>
14 #include <map>
15 #include <rapidxml.hpp>
16 #include <rapidxml_print.hpp>
17 #include <rapidxml_utils.hpp>
18 #include <sstream> // std::stringstream
19 #include <string>
20 
21 #include "xml_utils.h"
22 
23 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
24 #include "GenericObservation.pb.h"
25 #endif
26 
27 using namespace mvsim;
28 
30 
31 // Explicit registration calls seem to be one (the unique?) way to assure
32 // registration takes place:
34 {
35  static bool done = false;
36  if (done)
37  return;
38  else
39  done = true;
40 
42 }
43 
45  : VisualObject(vehicle.getWorldObject()),
46  m_sensor_period(0.1),
47  m_vehicle(vehicle),
48  m_sensor_last_timestamp(0)
49 {
50 }
51 
52 SensorBase::~SensorBase() = default;
53 
55  VehicleBase& parent, const rapidxml::xml_node<char>* root)
56 {
58 
59  using namespace std;
60  using namespace rapidxml;
61 
62  if (!root) throw runtime_error("[SensorBase::factory] XML node is nullptr");
63  if (0 != strcmp(root->name(), "sensor"))
64  throw runtime_error(mrpt::format(
65  "[SensorBase::factory] XML root element is '%s' ('sensor' "
66  "expected)",
67  root->name()));
68 
69  // Get "class" attrib:
70  const xml_attribute<>* sensor_class = root->first_attribute("class");
71  if (!sensor_class || !sensor_class->value())
72  throw runtime_error(
73  "[VehicleBase::factory] Missing mandatory attribute 'class' in "
74  "node <sensor>");
75 
76  const string sName = string(sensor_class->value());
77 
78  // Class factory:
79  auto we = classFactory_sensors.create(sName, parent, root);
80 
81  if (!we)
82  throw runtime_error(mrpt::format(
83  "[SensorBase::factory] Unknown sensor type '%s'", root->name()));
84 
85  return we;
86 }
87 
89  const rapidxml::xml_node<char>* node,
90  const std::map<std::string, std::string>& varValues)
91 {
93 
94  if (node == nullptr) return false;
95 
97  params["publish_topic"] = TParamEntry("%s", &publishTopic_);
98 
99  // Parse XML params:
100  parse_xmlnode_children_as_param(*node, params, varValues);
101 
102  return true;
103  MRPT_END
104 }
105 
107  const std::shared_ptr<mrpt::obs::CObservation>& obs,
108  const TSimulContext& context)
109 {
110  // Notify the world:
111  m_world->onNewObservation(m_vehicle, obs.get());
112 
113  // Publish:
114 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
115  if (!publishTopic_.empty())
116  {
117  mvsim_msgs::GenericObservation msg;
118  msg.set_unixtimestamp(mrpt::Clock::toDouble(obs->timestamp));
119  msg.set_sourceobjectid(m_vehicle.getName());
120 
121  std::vector<uint8_t> serializedData;
122  mrpt::serialization::ObjectToOctetVector(obs.get(), serializedData);
123 
124  msg.set_mrptserializedobservation(
125  serializedData.data(), serializedData.size());
126 
127  context.world->commsClient().publishTopic(publishTopic_, msg);
128  }
129 #endif
130 }
131 
133 {
134  // Default base stuff:
136 
137 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
138  // Topic:
139  if (!publishTopic_.empty())
140  c.advertiseTopic<mvsim_msgs::GenericObservation>(publishTopic_);
141 #endif
142 }
This file contains rapidxml parser and DOM implementation.
static SensorBase::Ptr factory(VehicleBase &parent, const rapidxml::xml_node< char > *xml_node)
Definition: SensorBase.cpp:54
std::string publishTopic_
Definition: SensorBase.h:48
virtual void onNewObservation([[maybe_unused]] const VehicleBase &veh, [[maybe_unused]] const mrpt::obs::CObservation *obs)
Definition: World.h:235
std::map< std::string, TParamEntry > TParameterDefinitions
Ch * value() const
Definition: rapidxml.hpp:692
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const TParameterDefinitions &params, const std::map< std::string, std::string > &variableNamesValues={}, const char *functionNameContext="")
Definition: xml_utils.cpp:179
const GLfloat * c
void publishTopic(const std::string &topicName, const google::protobuf::Message &msg)
bool parseSensorPublish(const rapidxml::xml_node< char > *node, const std::map< std::string, std::string > &varValues)
Definition: SensorBase.cpp:88
void BASE_IMPEXP ObjectToOctetVector(const CSerializable *o, vector_byte &out_vector)
void registerOnServer(mvsim::Client &c) override
(in seconds) (Default = 0.1)
Definition: SensorBase.cpp:132
#define MRPT_END
const std::string & getName() const
Definition: Simulable.h:95
SensorBase(VehicleBase &vehicle)
Definition: SensorBase.cpp:44
VehicleBase & m_vehicle
The vehicle this sensor is attached to.
Definition: SensorBase.h:43
void reportNewObservation(const std::shared_ptr< mrpt::obs::CObservation > &obs, const TSimulContext &context)
Definition: SensorBase.cpp:106
Ch * name() const
Definition: rapidxml.hpp:673
std::shared_ptr< SensorBase > Ptr
Definition: SensorBase.h:24
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
void register_all_sensors()
Definition: SensorBase.cpp:33
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1025
Ptr create(const std::string &class_name, ARG1 a1) const
Definition: ClassFactory.h:39
virtual void registerOnServer(mvsim::Client &c)
Definition: Simulable.cpp:150
#define MRPT_START
virtual ~SensorBase()
which the sensor is attached.
GLsizei const GLcharARB ** string
TClassFactory_sensors classFactory_sensors
Definition: SensorBase.cpp:29
void advertiseTopic(const std::string &topicName)
Definition: Client.h:158
#define REGISTER_SENSOR(TEXTUAL_NAME, CLASS_NAME)
Definition: SensorBase.h:68
GLfloat * params
This file contains rapidxml printer implementation.
mvsim::Client & commsClient()
Definition: World.h:247


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51