MVSim
Lightweight simulator for 2.5D vehicles and robots
VehicleDifferential.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/math/TPoint2D.h>
13 #include <mvsim/PID_Controller.h>
14 #include <mvsim/PoseTrajectoryFollower.h>
15 #include <mvsim/VehicleBase.h>
16 
17 namespace mvsim
18 {
32 {
33  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential)
34  public:
35  enum
36  {
37  // common to all:
38  WHEEL_L = 0,
39  WHEEL_R = 1,
40  // 3 wheels:
41  WHEEL_CASTER_FRONT = 2,
42  // 4 wheels:
43  WHEEL_LR = 0,
44  WHEEL_RR = 1,
45  WHEEL_LF = 2,
46  WHEEL_RF = 3
47  };
48 
50  {
51  ConfigPerWheel() = default;
52  ConfigPerWheel(const std::string& _name, const mrpt::math::TPoint2D& _pos)
53  : name(_name), pos(_pos)
54  {
55  }
56 
57  std::string name;
58  mrpt::math::TPoint2D pos;
59  };
60 
63  parent, {
64  {"l_wheel", {0.0, 0.5}},
65  {"r_wheel", {0.0, -0.5}},
66  })
67  {
68  }
69 
70  DynamicsDifferential(World* parent, const std::vector<ConfigPerWheel>& cfgPerWheel);
71 
76  {
77  TSimulContext context;
78  };
79 
81  {
82  TControllerOutput() = default;
83 
84  double wheel_torque_l = 0;
85  double wheel_torque_r = 0;
86  };
87 
90 
92  {
93  public:
95 
96  static const char* class_name() { return "raw"; }
97 
100  double setpoint_wheel_torque_l = 0, setpoint_wheel_torque_r = 0;
101 
102  double setpoint_teleop_steps = 5e-2;
103 
104  virtual void control_step(
107  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
108  };
109 
113  {
114  public:
116  static const char* class_name() { return "twist_pid"; }
117 
118  virtual void control_step(
121 
122  virtual void load_config(const rapidxml::xml_node<char>& node) override;
123  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
124 
126  double KP = 10, KI = 0, KD = 0;
127 
129  double N = 0;
130 
132  double max_torque = 100;
133 
134  bool enable_antiwindup = false;
135 
137  bool enable_feedforward = false;
138 
140  double feedforward_gain = 1.0;
141 
144 
146  double reference_filter_tau = 0.1;
147 
150 
151  // See base docs.
152  bool setTwistCommand(const mrpt::math::TTwist2D& t) override
153  {
154  setpointMtx_.lock();
155  setpoint_ = t;
156  setpointMtx_.unlock();
157  return true;
158  }
159 
161  mrpt::math::TTwist2D setpoint() const
162  {
163  setpointMtx_.lock();
164  auto t = setpoint_;
165  setpointMtx_.unlock();
166  return t;
167  }
168 
169  private:
170  double distWheels_ = 0;
171  std::array<PID_Controller, 2> PIDs_;
172  mrpt::math::TTwist2D setpoint_{0, 0, 0};
173  mutable std::mutex setpointMtx_;
174 
175  double joyMaxLinSpeed = 1.0;
176  double joyMaxAngSpeed = 0.5;
177  };
178 
183  {
184  public:
186  static const char* class_name() { return "twist_ideal"; }
187 
188  void control_step(
191  void on_post_step(const TSimulContext& context) override;
192 
193  virtual void load_config(const rapidxml::xml_node<char>& node) override;
194  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
195 
196  // See base docs.
197  bool setTwistCommand(const mrpt::math::TTwist2D& t) override
198  {
199  setpointMtx_.lock();
200  setpoint_ = t;
201  setpointMtx_.unlock();
202  return true;
203  }
204 
206  mrpt::math::TTwist2D setpoint() const
207  {
208  setpointMtx_.lock();
209  auto t = setpoint_;
210  setpointMtx_.unlock();
211  return t;
212  }
213 
214  private:
215  double distWheels_ = 0;
216  mrpt::math::TTwist2D setpoint_{0, 0, 0};
217  mutable std::mutex setpointMtx_;
218 
219  double joyMaxLinSpeed = 1.0;
220  double joyMaxAngSpeed = 0.5;
221  };
222 
244  {
245  public:
247  static const char* class_name() { return "trajectory"; }
248 
249  void control_step(
252  void on_post_step(const TSimulContext& context) override;
253 
254  virtual void load_config(const rapidxml::xml_node<char>& node) override;
255 
256  bool getTrajectoryPlotPoints(
257  std::vector<mrpt::math::TPoint2D>& pts, double& height) const override;
258 
259  private:
260  PoseTrajectoryFollower follower_;
261  double vizHeight_ = 0.5;
262  };
263 
264  const ControllerBase::Ptr& getController() const { return controller_; }
265  ControllerBase::Ptr& getController() { return controller_; }
266  virtual ControllerBaseInterface* getControllerInterface() override { return controller_.get(); }
267  // end controllers
269 
270  virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override;
271 
272  protected:
273  // See base class docs
274  virtual void dynamics_load_params_from_xml(const rapidxml::xml_node<char>* xml_node) override;
275  // See base class docs
276  virtual std::vector<double> invoke_motor_controllers(const TSimulContext& context) override;
277  virtual void invoke_motor_controllers_post_step(const TSimulContext& context) override;
278 
280  const std::vector<ConfigPerWheel> configPerWheel_;
281 
282  private:
283  ControllerBase::Ptr controller_;
284 };
285 
290 {
291  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential_3_wheels)
292 
293  public:
296  parent, {
297  {"l_wheel", {0.0, 0.5}},
298  {"r_wheel", {0.0, -0.5}},
299  {"caster_wheel", {0.5, 0.0}},
300  })
301  {
302  }
303 };
304 
309 {
310  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential_4_wheels)
311 
312  public:
315  parent, {
316  {"lr_wheel", {0.0, 0.5}},
317  {"rr_wheel", {0.0, -0.5}},
318  {"lf_wheel", {0.5, 0.5}},
319  {"rf_wheel", {0.5, -0.5}},
320  })
321  {
322  }
323 };
324 
325 } // namespace mvsim
Definition: ControllerBase.h:77
Definition: VehicleDifferential.h:92
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
static const char * class_name()
Definition: VehicleDifferential.h:96
Definition: VehicleDifferential.h:244
Definition: VehicleDifferential.h:183
mrpt::math::TTwist2D setpoint() const
Definition: VehicleDifferential.h:206
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: VehicleDifferential.h:113
mrpt::math::TTwist2D setpoint() const
Definition: VehicleDifferential.h:161
bool enable_feedforward
Definition: VehicleDifferential.h:137
double KP
PID controller parameters.
Definition: VehicleDifferential.h:126
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
bool enable_reference_filter
Definition: VehicleDifferential.h:143
double N
Definition: VehicleDifferential.h:129
int reference_filter_order
Definition: VehicleDifferential.h:149
double reference_filter_tau
Definition: VehicleDifferential.h:146
double max_torque
Maximum abs. value torque (for clamp) [Nm].
Definition: VehicleDifferential.h:132
double feedforward_gain
Definition: VehicleDifferential.h:140
Definition: VehicleDifferential.h:290
Definition: VehicleDifferential.h:309
Definition: VehicleDifferential.h:32
ControllerBaseTempl< DynamicsDifferential > ControllerBase
Definition: VehicleDifferential.h:89
virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override
virtual void dynamics_load_params_from_xml(const rapidxml::xml_node< char > *xml_node) override
const std::vector< ConfigPerWheel > configPerWheel_
Defined at ctor time:
Definition: VehicleDifferential.h:280
Definition: PoseTrajectoryFollower.h:38
Definition: VehicleBase.h:44
Definition: World.h:133
Definition: ControllerBase.h:26
Definition: ControllerBase.h:34
Definition: VehicleDifferential.h:50
Definition: VehicleDifferential.h:76
Definition: VehicleDifferential.h:81
Definition: basic_types.h:58