MVSim
Lightweight simulator for 2.5D vehicles and robots
VehicleAckermann_Drivetrain.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 {
29 {
30  DECLARES_REGISTER_VEHICLE_DYNAMICS(DynamicsAckermannDrivetrain)
31  public:
32  // Wheels: [0]:rear-left, [1]:rear-right, [2]: front-left, [3]: front-right
33  enum
34  {
35  WHEEL_RL = 0,
36  WHEEL_RR = 1,
37  WHEEL_FL = 2,
38  WHEEL_FR = 3
39  };
40 
41  enum DifferentialType
42  {
43  DIFF_OPEN_FRONT = 0,
44  DIFF_OPEN_REAR = 1,
45  DIFF_OPEN_4WD = 2,
46 
47  DIFF_TORSEN_FRONT = 3,
48  DIFF_TORSEN_REAR = 4,
49  DIFF_TORSEN_4WD = 5,
50 
51  DIFF_MAX
52  };
53 
55 
57  double getMaxSteeringAngle() const { return max_steer_ang_; }
58  void setMaxSteeringAngle(double val) { max_steer_ang_ = val; }
63  {
64  TSimulContext context;
65  };
67  {
68  double drive_torque;
69  double steer_ang;
70  TControllerOutput() : drive_torque(0), steer_ang(0) {}
71  };
72 
76 
78  {
79  public:
81  static const char* class_name() { return "raw"; }
84  double setpoint_wheel_torque, setpoint_steer_ang;
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 
95  {
96  public:
98  static const char* class_name() { return "twist_front_steer_pid"; }
101  double setpoint_lin_speed,
103  virtual void control_step(
106  virtual void load_config(const rapidxml::xml_node<char>& node) override;
107  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
108 
109  double KP, KI, KD;
110  double N = 0;
111  double max_torque;
112 
113  bool enable_antiwindup = false;
114  bool enable_feedforward = false;
115  double feedforward_gain = 1.0;
116  bool enable_reference_filter = false;
117  double reference_filter_tau = 0.1;
118  int reference_filter_order = 1;
119 
120  // See base docs.
121  virtual bool setTwistCommand(const mrpt::math::TTwist2D& t) override
122  {
123  setpoint_lin_speed = t.vx;
124  setpoint_ang_speed = t.omega;
125  return true;
126  }
127 
128  private:
129  double dist_fWheels_, r2f_L_;
130  PID_Controller PID_;
131 
132  double joyMaxLinSpeed = 1.0;
133  double joyMaxAngSpeed = 0.7;
134  };
135 
137  {
138  public:
140  static const char* class_name() { return "front_steer_pid"; }
143  double setpoint_lin_speed, setpoint_steer_ang;
146  virtual void control_step(
149  virtual void load_config(const rapidxml::xml_node<char>& node) override;
150  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override;
151 
152  double KP, KI, KD;
153  double max_torque;
154  private:
155  ControllerTwistFrontSteerPID twist_control_;
156  double r2f_L_;
157  };
158 
159  const ControllerBase::Ptr& getController() const { return controller_; }
160  ControllerBase::Ptr& getController() { return controller_; }
161  virtual ControllerBaseInterface* getControllerInterface() override { return controller_.get(); }
162  // end controllers
164 
165  virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override;
166 
173  const double desired_equiv_steer_ang, double& out_fl_ang, double& out_fr_ang) const;
174 
178  const double w1, const double w2, const double diffBias, const double defaultSplitRatio,
179  double& t1, double& t2);
180 
181  protected:
182  // See base class docs
183  virtual void dynamics_load_params_from_xml(const rapidxml::xml_node<char>* xml_node) override;
184  // See base class doc
185  virtual std::vector<double> invoke_motor_controllers(const TSimulContext& context) override;
186 
187  private:
188  ControllerBase::Ptr controller_;
189 
190  double max_steer_ang_;
192 
193  DifferentialType diff_type_;
194 
195  double FrontRearSplit_;
196  double FrontLRSplit_;
197  double RearLRSplit_;
198 
199  double FrontRearBias_;
200  double FrontLRBias_;
201  double RearLRBias_;
202 };
203 } // namespace mvsim
Definition: ControllerBase.h:60
Definition: VehicleAckermann_Drivetrain.h:137
double setpoint_steer_ang
Definition: VehicleAckermann_Drivetrain.h:143
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
double max_torque
Maximum abs. value torque (for clamp) [Nm].
Definition: VehicleAckermann_Drivetrain.h:153
double KD
PID controller parameters.
Definition: VehicleAckermann_Drivetrain.h:152
static const char * class_name()
Definition: VehicleAckermann_Drivetrain.h:140
Definition: VehicleAckermann_Drivetrain.h:78
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
static const char * class_name()
Definition: VehicleAckermann_Drivetrain.h:81
Definition: VehicleAckermann_Drivetrain.h:95
double setpoint_ang_speed
desired velocities (m/s) and (rad/s)
Definition: VehicleAckermann_Drivetrain.h:102
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
static const char * class_name()
Definition: VehicleAckermann_Drivetrain.h:98
double KD
PID controller parameters.
Definition: VehicleAckermann_Drivetrain.h:109
double N
Derivative filter coefficient.
Definition: VehicleAckermann_Drivetrain.h:110
double max_torque
Maximum abs. value torque (for clamp) [Nm].
Definition: VehicleAckermann_Drivetrain.h:111
Definition: VehicleAckermann_Drivetrain.h:29
void computeDiffTorqueSplit(const double w1, const double w2, const double diffBias, const double defaultSplitRatio, double &t1, double &t2)
double getMaxSteeringAngle() const
Definition: VehicleAckermann_Drivetrain.h:57
virtual void dynamics_load_params_from_xml(const rapidxml::xml_node< char > *xml_node) override
void computeFrontWheelAngles(const double desired_equiv_steer_ang, double &out_fl_ang, double &out_fr_ang) const
virtual mrpt::math::TTwist2D getVelocityLocalOdoEstimate() const override
Definition: VehicleBase.h:44
Definition: World.h:132
Definition: ControllerBase.h:26
Definition: ControllerBase.h:34
Definition: VehicleAckermann_Drivetrain.h:63
Definition: VehicleAckermann_Drivetrain.h:67
double steer_ang
Equivalent Ackermann steering angle.
Definition: VehicleAckermann_Drivetrain.h:69
Definition: PID_Controller.h:21
Definition: basic_types.h:58