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/VehicleBase.h>
15 
16 namespace mvsim
17 {
31 {
32  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential)
33  public:
34  enum
35  {
36  // common to all:
37  WHEEL_L = 0,
38  WHEEL_R = 1,
39  // 3 wheels:
40  WHEEL_CASTER_FRONT = 2,
41  // 4 wheels:
42  WHEEL_LR = 0,
43  WHEEL_RR = 1,
44  WHEEL_LF = 2,
45  WHEEL_RF = 3
46  };
47 
49  {
50  ConfigPerWheel() = default;
51  ConfigPerWheel(const std::string& _name, const mrpt::math::TPoint2D& _pos)
52  : name(_name), pos(_pos)
53  {
54  }
55 
56  std::string name;
57  mrpt::math::TPoint2D pos;
58  };
59 
62  parent, {
63  {"l_wheel", {0.0, 0.5}},
64  {"r_wheel", {0.0, -0.5}},
65  })
66  {
67  }
68 
69  DynamicsDifferential(World* parent, const std::vector<ConfigPerWheel>& cfgPerWheel);
70 
75  {
76  TSimulContext context;
77  };
78 
80  {
81  TControllerOutput() = default;
82 
83  double wheel_torque_l = 0;
84  double wheel_torque_r = 0;
85  };
86 
89 
91  {
92  public:
94 
95  static const char* class_name() { return "raw"; }
96 
99  double setpoint_wheel_torque_l = 0, setpoint_wheel_torque_r = 0;
100 
101  double setpoint_teleop_steps = 5e-2;
102 
103  virtual void control_step(
106  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
107  };
108 
112  {
113  public:
115  static const char* class_name() { return "twist_pid"; }
116 
117  virtual void control_step(
120 
121  virtual void load_config(const rapidxml::xml_node<char>& node) override;
122  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
123 
125  double KP = 10, KI = 0, KD = 0;
126 
128  double N = 0;
129 
131  double max_torque = 100;
132 
133  bool enable_antiwindup = false;
134 
136  bool enable_feedforward = false;
137 
139  double feedforward_gain = 1.0;
140 
143 
145  double reference_filter_tau = 0.1;
146 
149 
150  // See base docs.
151  bool setTwistCommand(const mrpt::math::TTwist2D& t) override
152  {
153  setpointMtx_.lock();
154  setpoint_ = t;
155  setpointMtx_.unlock();
156  return true;
157  }
158 
160  mrpt::math::TTwist2D setpoint() const
161  {
162  setpointMtx_.lock();
163  auto t = setpoint_;
164  setpointMtx_.unlock();
165  return t;
166  }
167 
168  private:
169  double distWheels_ = 0;
170  std::array<PID_Controller, 2> PIDs_;
171  mrpt::math::TTwist2D setpoint_{0, 0, 0};
172  mutable std::mutex setpointMtx_;
173 
174  double joyMaxLinSpeed = 1.0;
175  double joyMaxAngSpeed = 0.5;
176  };
177 
182  {
183  public:
185  static const char* class_name() { return "twist_ideal"; }
186 
187  void control_step(
190  void on_post_step(const TSimulContext& context) override;
191 
192  virtual void load_config(const rapidxml::xml_node<char>& node) override;
193  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
194 
195  // See base docs.
196  bool setTwistCommand(const mrpt::math::TTwist2D& t) override
197  {
198  setpointMtx_.lock();
199  setpoint_ = t;
200  setpointMtx_.unlock();
201  return true;
202  }
203 
205  mrpt::math::TTwist2D setpoint() const
206  {
207  setpointMtx_.lock();
208  auto t = setpoint_;
209  setpointMtx_.unlock();
210  return t;
211  }
212 
213  private:
214  double distWheels_ = 0;
215  mrpt::math::TTwist2D setpoint_{0, 0, 0};
216  mutable std::mutex setpointMtx_;
217 
218  double joyMaxLinSpeed = 1.0;
219  double joyMaxAngSpeed = 0.5;
220  };
221 
222  const ControllerBase::Ptr& getController() const { return controller_; }
223  ControllerBase::Ptr& getController() { return controller_; }
224  virtual ControllerBaseInterface* getControllerInterface() override { return controller_.get(); }
225  // end controllers
227 
228  virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override;
229 
230  protected:
231  // See base class docs
232  virtual void dynamics_load_params_from_xml(const rapidxml::xml_node<char>* xml_node) override;
233  // See base class docs
234  virtual std::vector<double> invoke_motor_controllers(const TSimulContext& context) override;
235  virtual void invoke_motor_controllers_post_step(const TSimulContext& context) override;
236 
238  const std::vector<ConfigPerWheel> configPerWheel_;
239 
240  private:
241  ControllerBase::Ptr controller_;
242 };
243 
248 {
249  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential_3_wheels)
250 
251  public:
254  parent, {
255  {"l_wheel", {0.0, 0.5}},
256  {"r_wheel", {0.0, -0.5}},
257  {"caster_wheel", {0.5, 0.0}},
258  })
259  {
260  }
261 };
262 
267 {
268  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential_4_wheels)
269 
270  public:
273  parent, {
274  {"lr_wheel", {0.0, 0.5}},
275  {"rr_wheel", {0.0, -0.5}},
276  {"lf_wheel", {0.5, 0.5}},
277  {"rf_wheel", {0.5, -0.5}},
278  })
279  {
280  }
281 };
282 
283 } // namespace mvsim
Definition: ControllerBase.h:60
Definition: VehicleDifferential.h:91
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
static const char * class_name()
Definition: VehicleDifferential.h:95
Definition: VehicleDifferential.h:182
mrpt::math::TTwist2D setpoint() const
Definition: VehicleDifferential.h:205
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: VehicleDifferential.h:112
mrpt::math::TTwist2D setpoint() const
Definition: VehicleDifferential.h:160
bool enable_feedforward
Definition: VehicleDifferential.h:136
double KP
PID controller parameters.
Definition: VehicleDifferential.h:125
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
bool enable_reference_filter
Definition: VehicleDifferential.h:142
double N
Definition: VehicleDifferential.h:128
int reference_filter_order
Definition: VehicleDifferential.h:148
double reference_filter_tau
Definition: VehicleDifferential.h:145
double max_torque
Maximum abs. value torque (for clamp) [Nm].
Definition: VehicleDifferential.h:131
double feedforward_gain
Definition: VehicleDifferential.h:139
Definition: VehicleDifferential.h:248
Definition: VehicleDifferential.h:267
Definition: VehicleDifferential.h:31
ControllerBaseTempl< DynamicsDifferential > ControllerBase
Definition: VehicleDifferential.h:88
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:238
Definition: VehicleBase.h:44
Definition: World.h:132
Definition: ControllerBase.h:26
Definition: ControllerBase.h:34
Definition: VehicleDifferential.h:49
Definition: VehicleDifferential.h:75
Definition: VehicleDifferential.h:80
Definition: basic_types.h:58