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 teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
193 
194  // See base docs.
195  bool setTwistCommand(const mrpt::math::TTwist2D& t) override
196  {
197  setpointMtx_.lock();
198  setpoint_ = t;
199  setpointMtx_.unlock();
200  return true;
201  }
202 
204  mrpt::math::TTwist2D setpoint() const
205  {
206  setpointMtx_.lock();
207  auto t = setpoint_;
208  setpointMtx_.unlock();
209  return t;
210  }
211 
212  private:
213  double distWheels_ = 0;
214  mrpt::math::TTwist2D setpoint_{0, 0, 0};
215  mutable std::mutex setpointMtx_;
216 
217  double joyMaxLinSpeed = 1.0;
218  double joyMaxAngSpeed = 0.5;
219  };
220 
221  const ControllerBase::Ptr& getController() const { return controller_; }
222  ControllerBase::Ptr& getController() { return controller_; }
223  virtual ControllerBaseInterface* getControllerInterface() override { return controller_.get(); }
224  // end controllers
226 
227  virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override;
228 
229  protected:
230  // See base class docs
231  virtual void dynamics_load_params_from_xml(const rapidxml::xml_node<char>* xml_node) override;
232  // See base class docs
233  virtual std::vector<double> invoke_motor_controllers(const TSimulContext& context) override;
234  virtual void invoke_motor_controllers_post_step(const TSimulContext& context) override;
235 
237  const std::vector<ConfigPerWheel> configPerWheel_;
238 
239  private:
240  ControllerBase::Ptr controller_;
241 };
242 
247 {
248  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential_3_wheels)
249 
250  public:
253  parent, {
254  {"l_wheel", {0.0, 0.5}},
255  {"r_wheel", {0.0, -0.5}},
256  {"caster_wheel", {0.5, 0.0}},
257  })
258  {
259  }
260 };
261 
266 {
267  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsDifferential_4_wheels)
268 
269  public:
272  parent, {
273  {"lr_wheel", {0.0, 0.5}},
274  {"rr_wheel", {0.0, -0.5}},
275  {"lf_wheel", {0.5, 0.5}},
276  {"rf_wheel", {0.5, -0.5}},
277  })
278  {
279  }
280 };
281 
282 } // 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:204
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:247
Definition: VehicleDifferential.h:266
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:237
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