VehicleBase.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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 <box2d/b2_body.h>
13 #include <box2d/b2_fixture.h>
14 #include <box2d/b2_polygon_shape.h>
15 #include <box2d/b2_world.h>
16 #include <mrpt/img/TColor.h>
17 #include <mrpt/opengl/CSetOfLines.h>
18 #include <mrpt/opengl/CSetOfObjects.h>
19 #include <mrpt/poses/CPose2D.h>
20 #include <mvsim/ClassFactory.h>
21 #include <mvsim/ControllerBase.h>
24 #include <mvsim/Simulable.h>
25 #include <mvsim/VisualObject.h>
26 #include <mvsim/Wheel.h>
27 #include <mvsim/basic_types.h>
28 
29 #include <map>
30 #include <mutex>
31 #include <string>
32 
33 #include "CsvLogger.h"
34 
35 namespace mvsim
36 {
41 class VehicleBase : public VisualObject, public Simulable
42 {
43  public:
44  using Ptr = std::shared_ptr<VehicleBase>;
45 
48  static Ptr factory(World* parent, const rapidxml::xml_node<char>* xml_node);
50  static Ptr factory(World* parent, const std::string& xml_text);
51 
54  static void register_vehicle_class(
55  const rapidxml::xml_node<char>* xml_node);
56 
57  // ------- Interface with "World" ------
58  virtual void simul_pre_timestep(const TSimulContext& context) override;
59  virtual void simul_post_timestep(const TSimulContext& context) override;
60  virtual void apply_force(
61  const mrpt::math::TVector2D& force,
62  const mrpt::math::TPoint2D& applyPoint =
63  mrpt::math::TPoint2D(0, 0)) override;
64 
67  virtual void create_multibody_system(b2World& world);
68 
71  virtual float getMaxVehicleRadius() const { return maxRadius_; }
73  virtual double getChassisMass() const { return chassis_mass_; }
74  b2Body* getBox2DChassisBody() { return b2dBody_; }
75  mrpt::math::TPoint2D getChassisCenterOfMass() const
76  {
77  return chassis_com_;
78  }
79 
80  size_t getNumWheels() const { return wheels_info_.size(); }
81  const Wheel& getWheelInfo(const size_t idx) const
82  {
83  return wheels_info_[idx];
84  }
85  Wheel& getWheelInfo(const size_t idx) { return wheels_info_[idx]; }
86 
89  std::vector<mrpt::math::TVector2D> getWheelsVelocityLocal(
90  const mrpt::math::TTwist2D& veh_vel_local) const;
91 
96  virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const = 0;
97 
98  const TListSensors& getSensors() const { return sensors_; }
100  std::shared_ptr<CSVLogger> getLoggerPtr(std::string logger_name)
101  {
102  return loggers_[logger_name];
103  }
104 
107  const mrpt::math::TPolygon2D& getChassisShape() const
108  {
109  return chassis_poly_;
110  }
111 
113  void setVehicleIndex(size_t idx) { vehicle_index_ = idx; }
115  size_t getVehicleIndex() const { return vehicle_index_; }
116  void setRecording(bool record)
117  {
118  for (auto& logger : loggers_) logger.second->setRecording(record);
119  }
120  void clearLogs()
121  {
122  for (auto& logger : loggers_) logger.second->clear();
123  }
125  {
126  for (auto& logger : loggers_) logger.second->newSession();
127  }
128 
130 
131  void registerOnServer(mvsim::Client& c) override;
132 
134  std::vector<b2Fixture*>& get_fixture_wheels() { return fixture_wheels_; }
135  const b2Fixture* get_fixture_chassis() const { return fixture_chassis_; }
136  const std::vector<b2Fixture*>& get_fixture_wheels() const
137  {
138  return fixture_wheels_;
139  }
140 
141  void freeOpenGLResources() override
142  {
143  for (auto& sensor : sensors_) sensor->freeOpenGLResources();
144  }
145  void chassisAndWheelsVisible(bool visible);
146 
147  protected:
148  std::map<std::string, std::shared_ptr<CSVLogger>> loggers_;
149  std::string log_path_;
150 
151  virtual void initLoggers();
152  virtual void writeLogStrings();
153  virtual void internalGuiUpdate(
154  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& viz,
155  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& physical,
156  bool childrenOnly) override;
157 
158  protected:
159  // Protected ctor for class factory
160  VehicleBase(World* parent, size_t nWheels);
161 
165  virtual void dynamics_load_params_from_xml(
166  const rapidxml::xml_node<char>* xml_node) = 0;
167 
168  virtual std::vector<double> invoke_motor_controllers(
169  const TSimulContext& context) = 0;
170 
172  [[maybe_unused]] const TSimulContext& context)
173  {
174  }
175 
176  VisualObject* meAsVisualObject() override { return this; }
177 
180  size_t vehicle_index_ = 0;
181 
184 
186 
187  // Chassis info:
188  double chassis_mass_ = 15.0;
189  mrpt::math::TPolygon2D chassis_poly_;
190 
193  double maxRadius_ = 0.1;
194 
195  double chassis_z_min_ = 0.05, chassis_z_max_ = 0.6;
196 
197  mrpt::img::TColor chassis_color_{0xff, 0x00, 0x00};
198 
201  mrpt::math::TPoint2D chassis_com_{0, 0};
202 
204 
209  std::deque<Wheel> wheels_info_;
210 
211  // Box2D elements:
213 
216  std::vector<b2Fixture*> fixture_wheels_;
217 
218  private:
219  // Called from internalGuiUpdate()
221  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& viz,
222  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& physical);
223  // Called from internalGuiUpdate()
224  void internal_internalGuiUpdate_forces(mrpt::opengl::COpenGLScene& scene);
225 
226  mrpt::opengl::CSetOfObjects::Ptr glChassis_;
227  std::vector<mrpt::opengl::CSetOfObjects::Ptr> glWheels_;
228  mrpt::opengl::CSetOfLines::Ptr glForces_;
229  mrpt::opengl::CSetOfLines::Ptr glMotorTorques_;
230 
231  std::vector<mrpt::math::TSegment3D> forceSegmentsForRendering_;
232  std::vector<mrpt::math::TSegment3D> torqueSegmentsForRendering_;
234 
235  public: // data logger header entries
236  static constexpr char DL_TIMESTAMP[] = "timestamp";
237  static constexpr char LOGGER_POSE[] = "logger_pose";
238  static constexpr char LOGGER_WHEEL[] = "logger_wheel";
239 
240  static constexpr char PL_Q_X[] = "Qx";
241  static constexpr char PL_Q_Y[] = "Qy";
242  static constexpr char PL_Q_Z[] = "Qz";
243  static constexpr char PL_Q_YAW[] = "Qyaw";
244  static constexpr char PL_Q_PITCH[] = "Qpitch";
245  static constexpr char PL_Q_ROLL[] = "Qroll";
246  static constexpr char PL_DQ_X[] = "dQx";
247  static constexpr char PL_DQ_Y[] = "dQy";
248  static constexpr char PL_DQ_Z[] = "dQz";
249 
250  static constexpr char WL_TORQUE[] = "torque";
251  static constexpr char WL_WEIGHT[] = "weight";
252  static constexpr char WL_VEL_X[] = "velocity_x";
253  static constexpr char WL_VEL_Y[] = "velocity_y";
254  static constexpr char WL_FRIC_X[] = "friction_x";
255  static constexpr char WL_FRIC_Y[] = "friction_y";
256 }; // end VehicleBase
257 
258 // Class factory:
260 extern TClassFactory_vehicleDynamics classFactory_vehicleDynamics;
261 
262 #define DECLARES_REGISTER_VEHICLE_DYNAMICS(CLASS_NAME) \
263  DECLARES_REGISTER_CLASS1(CLASS_NAME, VehicleBase, World*)
264 
265 #define REGISTER_VEHICLE_DYNAMICS(TEXTUAL_NAME, CLASS_NAME) \
266  REGISTER_CLASS1( \
267  TClassFactory_vehicleDynamics, classFactory_vehicleDynamics, \
268  TEXTUAL_NAME, CLASS_NAME)
269 } // namespace mvsim
size_t getVehicleIndex() const
Definition: VehicleBase.h:115
mrpt::opengl::CSetOfObjects::Ptr glChassis_
Definition: VehicleBase.h:226
void internal_internalGuiUpdate_forces(mrpt::opengl::COpenGLScene &scene)
b2Body * getBox2DChassisBody()
Definition: VehicleBase.h:74
std::shared_ptr< Simulable > Ptr
Definition: Simulable.h:37
void updateMaxRadiusFromPoly()
const Wheel & getWheelInfo(const size_t idx) const
Definition: VehicleBase.h:81
std::vector< SensorBase::Ptr > TListSensors
Definition: SensorBase.h:110
static constexpr char PL_DQ_Y[]
Definition: VehicleBase.h:247
static Ptr factory(World *parent, const rapidxml::xml_node< char > *xml_node)
ClassFactory< VehicleBase, World * > TClassFactory_vehicleDynamics
Definition: VehicleBase.h:259
virtual double getChassisMass() const
Definition: VehicleBase.h:73
b2Fixture * get_fixture_chassis()
Definition: VehicleBase.h:133
std::vector< mrpt::math::TSegment3D > forceSegmentsForRendering_
Definition: VehicleBase.h:231
static constexpr char PL_Q_ROLL[]
Definition: VehicleBase.h:245
virtual void invoke_motor_controllers_post_step([[maybe_unused]] const TSimulContext &context)
Definition: VehicleBase.h:171
std::string log_path_
Definition: VehicleBase.h:149
virtual void simul_post_timestep(const TSimulContext &context) override
std::map< std::string, std::shared_ptr< CSVLogger > > loggers_
Definition: VehicleBase.h:148
virtual void dynamics_load_params_from_xml(const rapidxml::xml_node< char > *xml_node)=0
virtual void initLoggers()
static constexpr char LOGGER_WHEEL[]
Definition: VehicleBase.h:238
static constexpr char WL_VEL_X[]
Definition: VehicleBase.h:252
mrpt::opengl::CSetOfLines::Ptr glForces_
Definition: VehicleBase.h:228
std::shared_ptr< CSVLogger > getLoggerPtr(std::string logger_name)
Definition: VehicleBase.h:100
static constexpr char PL_Q_X[]
Definition: VehicleBase.h:240
static constexpr char WL_TORQUE[]
Definition: VehicleBase.h:250
mrpt::opengl::CSetOfLines::Ptr glMotorTorques_
Definition: VehicleBase.h:229
std::deque< Wheel > wheels_info_
Definition: VehicleBase.h:209
static constexpr char PL_Q_Z[]
Definition: VehicleBase.h:242
static constexpr char PL_Q_PITCH[]
Definition: VehicleBase.h:244
mrpt::math::TPoint2D chassis_com_
Definition: VehicleBase.h:201
const mrpt::math::TPolygon2D & getChassisShape() const
Definition: VehicleBase.h:107
std::shared_ptr< FrictionBase > FrictionBasePtr
Definition: FrictionBase.h:71
TClassFactory_vehicleDynamics classFactory_vehicleDynamics
Definition: VehicleBase.cpp:40
static constexpr char PL_Q_YAW[]
Definition: VehicleBase.h:243
static constexpr char PL_DQ_Z[]
Definition: VehicleBase.h:248
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
std::vector< mrpt::math::TSegment3D > torqueSegmentsForRendering_
Definition: VehicleBase.h:232
VehicleBase(World *parent, size_t nWheels)
Definition: VehicleBase.cpp:84
Wheel & getWheelInfo(const size_t idx)
Definition: VehicleBase.h:85
std::mutex forceSegmentsForRenderingMtx_
Definition: VehicleBase.h:233
virtual void create_multibody_system(b2World &world)
void chassisAndWheelsVisible(bool visible)
const std::vector< b2Fixture * > & get_fixture_wheels() const
Definition: VehicleBase.h:136
std::vector< mrpt::opengl::CSetOfObjects::Ptr > glWheels_
Definition: VehicleBase.h:227
static constexpr char PL_Q_Y[]
Definition: VehicleBase.h:241
static constexpr char WL_FRIC_X[]
Definition: VehicleBase.h:254
virtual ControllerBaseInterface * getControllerInterface()=0
static constexpr char PL_DQ_X[]
Definition: VehicleBase.h:246
virtual float getMaxVehicleRadius() const
Definition: VehicleBase.h:71
mrpt::math::TPolygon2D chassis_poly_
Definition: VehicleBase.h:189
b2Fixture * fixture_chassis_
Created at.
Definition: VehicleBase.h:212
static void register_vehicle_class(const rapidxml::xml_node< char > *xml_node)
VisualObject * meAsVisualObject() override
Definition: VehicleBase.h:176
void registerOnServer(mvsim::Client &c) override
const TListSensors & getSensors() const
Definition: VehicleBase.h:98
void setRecording(bool record)
Definition: VehicleBase.h:116
virtual void internalGuiUpdate(const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &viz, const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &physical, bool childrenOnly) override
std::vector< b2Fixture * > & get_fixture_wheels()
Definition: VehicleBase.h:134
virtual std::vector< double > invoke_motor_controllers(const TSimulContext &context)=0
virtual void writeLogStrings()
TListSensors sensors_
Sensors aboard.
Definition: VehicleBase.h:185
void freeOpenGLResources() override
Definition: VehicleBase.h:141
mrpt::img::TColor chassis_color_
Definition: VehicleBase.h:197
TListSensors & getSensors()
Definition: VehicleBase.h:99
static constexpr char WL_VEL_Y[]
Definition: VehicleBase.h:253
static constexpr char LOGGER_POSE[]
Definition: VehicleBase.h:237
const b2Fixture * get_fixture_chassis() const
Definition: VehicleBase.h:135
void setVehicleIndex(size_t idx)
Definition: VehicleBase.h:113
mrpt::math::TPoint2D getChassisCenterOfMass() const
In local coordinates (this excludes the mass of wheels)
Definition: VehicleBase.h:75
FrictionBasePtr friction_
Definition: VehicleBase.h:183
void internal_internalGuiUpdate_sensors(const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &viz, const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &physical)
static constexpr char WL_WEIGHT[]
Definition: VehicleBase.h:251
std::vector< mrpt::math::TVector2D > getWheelsVelocityLocal(const mrpt::math::TTwist2D &veh_vel_local) const
static constexpr char WL_FRIC_Y[]
Definition: VehicleBase.h:255
virtual void apply_force(const mrpt::math::TVector2D &force, const mrpt::math::TPoint2D &applyPoint=mrpt::math::TPoint2D(0, 0)) override
size_t getNumWheels() const
Definition: VehicleBase.h:80
std::vector< b2Fixture * > fixture_wheels_
Definition: VehicleBase.h:216
virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const =0
static constexpr char DL_TIMESTAMP[]
Definition: VehicleBase.h:236
virtual void simul_pre_timestep(const TSimulContext &context) override


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21