MVSim
Lightweight simulator for 2.5D vehicles and robots
VehicleAckermann.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/img/TColor.h>
13 #include <mvsim/PID_Controller.h>
14 #include <mvsim/VehicleBase.h>
15 
16 namespace mvsim
17 {
23 {
24  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsAckermann)
25  public:
26  // Wheels: [0]:rear-left, [1]:rear-right, [2]: front-left, [3]: front-right
27  enum
28  {
29  WHEEL_RL = 0,
30  WHEEL_RR = 1,
31  WHEEL_FL = 2,
32  WHEEL_FR = 3
33  };
34 
35  DynamicsAckermann(World* parent);
36 
38  double getMaxSteeringAngle() const { return max_steer_ang_; }
39  void setMaxSteeringAngle(double val) { max_steer_ang_ = val; }
44  {
45  TSimulContext context;
46  };
48  {
49  double fl_torque, fr_torque, rl_torque, rr_torque;
50  double steer_ang;
51  TControllerOutput() : fl_torque(0), fr_torque(0), rl_torque(0), rr_torque(0), steer_ang(0)
52  {
53  }
54  };
55 
58 
60  {
61  public:
63  static const char* class_name() { return "raw"; }
66  double setpoint_wheel_torque_l, setpoint_wheel_torque_r, setpoint_steer_ang;
67  virtual void control_step(
70  virtual void load_config(const rapidxml::xml_node<char>& node) override;
71  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
72  };
73 
77  {
78  public:
80  static const char* class_name() { return "twist_front_steer_pid"; }
83  double setpoint_lin_speed,
85  virtual void control_step(
88  virtual void load_config(const rapidxml::xml_node<char>& node) override;
89  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
90 
91  double KP, KI, KD;
92  double N = 0;
93  double max_torque;
95  double zero_threshold = 0.001;
97  double stop_threshold = 0.05;
98 
99  bool enable_antiwindup = false;
100  bool enable_feedforward = false;
101  double feedforward_gain = 1.0;
102  bool enable_reference_filter = false;
103  double reference_filter_tau = 0.1;
104  int reference_filter_order = 1;
105 
106  // See base docs.
107  virtual bool setTwistCommand(const mrpt::math::TTwist2D& t) override
108  {
109  setpoint_lin_speed = t.vx;
110  setpoint_ang_speed = t.omega;
111  return true;
112  }
113 
114  private:
115  double dist_fWheels_, r2f_L_;
116  PID_Controller PID_[2]; //<! [0]:fl, [1]: fr
117 
118  double joyMaxLinSpeed = 1.0;
119  double joyMaxAngSpeed = 0.7;
120  };
121 
125  {
126  public:
128  static const char* class_name() { return "front_steer_pid"; }
131  double setpoint_lin_speed, setpoint_steer_ang;
134  virtual void control_step(
137  virtual void load_config(const rapidxml::xml_node<char>& node) override;
138  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
139 
140  double KP, KI, KD;
141  double max_torque;
142  private:
143  ControllerTwistFrontSteerPID twist_control_;
144  double r2f_L_;
145 
146  double joyMaxLinSpeed = 1.0;
147  double joyMaxSteerAng = 0.7;
148  };
149 
161  {
162  public:
164  static const char* class_name() { return "twist_ideal"; }
165 
167  mrpt::math::TTwist2D setpoint() const
168  {
169  auto lck = mrpt::lockHelper(setpointMtx_);
170  return setpoint_;
171  }
172  void setSetpoint(const mrpt::math::TTwist2D& sp)
173  {
174  auto lck = mrpt::lockHelper(setpointMtx_);
175  setpoint_ = sp;
176  }
177 
178  virtual void control_step(
181 
182  virtual void on_post_step(const TSimulContext& context) override;
183 
184  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
185 
186  // Accept Twist commands from ROS / mvsim-server
187  virtual bool setTwistCommand(const mrpt::math::TTwist2D& t) override
188  {
189  setSetpoint(t);
190  return true;
191  }
192 
193  private:
194  mutable std::mutex setpointMtx_;
195  mrpt::math::TTwist2D setpoint_{0, 0, 0};
196 
197  double r2f_L_ = 1.0;
198 
199  double joyMaxLinSpeed = 1.0;
200  double joyMaxAngSpeed = 0.7;
201  };
202 
203  const ControllerBase::Ptr& getController() const { return controller_; }
204  ControllerBase::Ptr& getController() { return controller_; }
205  virtual ControllerBaseInterface* getControllerInterface() override { return controller_.get(); }
206  // end controllers
208 
209  virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override;
210 
217  const double desired_equiv_steer_ang, double& out_fl_ang, double& out_fr_ang) const;
218 
219  protected:
220  // See base class docs
221  virtual void dynamics_load_params_from_xml(const rapidxml::xml_node<char>* xml_node) override;
222  // See base class doc
223  virtual std::vector<double> invoke_motor_controllers(const TSimulContext& context) override;
224  virtual void invoke_motor_controllers_post_step(const TSimulContext& context) override;
225 
226  private:
227  ControllerBase::Ptr controller_;
228 
230  double max_steer_ang_ = mrpt::DEG2RAD(30);
231 };
232 } // namespace mvsim
Definition: ControllerBase.h:60
Definition: VehicleAckermann.h:125
static const char * class_name()
Definition: VehicleAckermann.h:128
double KD
PID controller parameters.
Definition: VehicleAckermann.h:140
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
double setpoint_steer_ang
Definition: VehicleAckermann.h:131
double max_torque
Maximum abs. value torque (for clamp) [Nm].
Definition: VehicleAckermann.h:141
Definition: VehicleAckermann.h:60
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
static const char * class_name()
Definition: VehicleAckermann.h:63
static const char * class_name()
Definition: VehicleAckermann.h:80
double max_torque
Definition: VehicleAckermann.h:93
double zero_threshold
setpoint magnitude below which it is treated as zero [m/s]
Definition: VehicleAckermann.h:95
double setpoint_ang_speed
desired velocities (m/s) and (rad/s)
Definition: VehicleAckermann.h:84
double stop_threshold
velocity magnitude below which vehicle is considered stopped [m/s]
Definition: VehicleAckermann.h:97
double N
Derivative filter coefficient.
Definition: VehicleAckermann.h:92
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
double KD
PID controller parameters.
Definition: VehicleAckermann.h:91
Definition: VehicleAckermann.h:161
mrpt::math::TTwist2D setpoint() const
Definition: VehicleAckermann.h:167
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: VehicleAckermann.h:23
void computeFrontWheelAngles(const double desired_equiv_steer_ang, double &out_fl_ang, double &out_fr_ang) const
virtual void dynamics_load_params_from_xml(const rapidxml::xml_node< char > *xml_node) override
virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override
double getMaxSteeringAngle() const
Definition: VehicleAckermann.h:38
Definition: VehicleBase.h:44
Definition: World.h:132
Definition: ControllerBase.h:26
Definition: ControllerBase.h:34
Definition: VehicleAckermann.h:44
Definition: VehicleAckermann.h:48
double steer_ang
Equivalent Ackermann steering angle.
Definition: VehicleAckermann.h:50
Definition: PID_Controller.h:21
Definition: basic_types.h:58