MVSim
Lightweight simulator for 2.5D vehicles and robots
SensorBase.h
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2026 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 #pragma once
11 
12 #include <mrpt/obs/obs_frwds.h>
13 #include <mrpt/opengl/opengl_frwds.h>
14 #include <mrpt/poses/CPose3D.h>
15 #include <mvsim/ClassFactory.h>
16 #include <mvsim/Simulable.h>
17 #include <mvsim/VisualObject.h>
18 
19 #include <memory>
20 
21 namespace mvsim
22 {
23 class Simulable;
24 
34 class SensorBase : public VisualObject, public Simulable
35 {
36  public:
37  using Ptr = std::shared_ptr<SensorBase>;
38 
41  virtual ~SensorBase();
42 
45  static SensorBase::Ptr factory(Simulable& parent, const rapidxml::xml_node<char>* xml_node);
46 
49  virtual void loadConfigFrom(const rapidxml::xml_node<char>* root);
50 
51  void registerOnServer(mvsim::Client& c) override;
52 
53  virtual void simulateOn3DScene( //
54  [[maybe_unused]] mrpt::opengl::COpenGLScene& gl_scene)
55  {
56  }
57 
58  // Get all sensors visuals API:
59  static std::shared_ptr<mrpt::opengl::CSetOfObjects> GetAllSensorsOriginViz();
60 
61  static std::shared_ptr<mrpt::opengl::CSetOfObjects> GetAllSensorsFOVViz();
62  static void RegisterSensorFOVViz(const std::shared_ptr<mrpt::opengl::CSetOfObjects>& o);
63  static void RegisterSensorOriginViz(const std::shared_ptr<mrpt::opengl::CSetOfObjects>& o);
64 
65  double sensor_period() const { return sensor_period_; }
66 
68  Simulable& vehicle() { return vehicle_; }
69  const Simulable& vehicle() const { return vehicle_; }
70 
71  protected:
80  bool should_simulate_sensor(const TSimulContext& context);
81 
83 
84  World* world() { return vehicle_.getSimulableWorldObject(); }
85  const World* world() const { return vehicle_.getSimulableWorldObject(); }
86 
88  double sensor_period_ = 0.1;
89 
92 
94  std::string publishTopic_;
95 
97  std::map<std::string, std::string> varValues_;
98 
99  bool parseSensorPublish(
100  const rapidxml::xml_node<char>* node, const std::map<std::string, std::string>& varValues);
101 
102  void reportNewObservation(
103  const std::shared_ptr<mrpt::obs::CObservation>& obs, const TSimulContext& context);
104 
105  void reportNewObservation_lidar_2d(
106  const std::shared_ptr<mrpt::obs::CObservation2DRangeScan>& obs,
107  const TSimulContext& context);
108 
110  void make_sure_we_have_a_name(const std::string& prefix);
111 };
112 
113 using TListSensors = std::vector<SensorBase::Ptr>;
114 
115 // Class factory:
117 
118 extern TClassFactory_sensors classFactory_sensors;
119 
120 #define DECLARES_REGISTER_SENSOR(CLASS_NAME) \
121  DECLARES_REGISTER_CLASS2(CLASS_NAME, SensorBase, Simulable&, const rapidxml::xml_node<char>*)
122 
123 #define REGISTER_SENSOR(TEXTUAL_NAME, CLASS_NAME) \
124  REGISTER_CLASS2(TClassFactory_sensors, classFactory_sensors, TEXTUAL_NAME, CLASS_NAME)
125 } // namespace mvsim
Definition: ClassFactory.h:26
Definition: Client.h:49
Virtual base class for all sensors.
Definition: SensorBase.h:35
virtual void loadConfigFrom(const rapidxml::xml_node< char > *root)
void make_sure_we_have_a_name(const std::string &prefix)
Assign a sensible default name/sensor label if none is provided:
double sensor_period_
Definition: SensorBase.h:88
bool should_simulate_sensor(const TSimulContext &context)
std::map< std::string, std::string > varValues_
Filled in by SensorBase::loadConfigFrom()
Definition: SensorBase.h:97
std::string publishTopic_
Definition: SensorBase.h:94
static SensorBase::Ptr factory(Simulable &parent, const rapidxml::xml_node< char > *xml_node)
double sensor_last_timestamp_
Definition: SensorBase.h:91
Simulable & vehicle()
Definition: SensorBase.h:68
SensorBase(Simulable &vehicle)
Simulable & vehicle_
The vehicle this sensor is attached to.
Definition: SensorBase.h:82
Definition: Simulable.h:40
Definition: VisualObject.h:36
Definition: World.h:132
Definition: basic_types.h:58