Vehicleackermann_Drivetrain_ControllerTwistFrontSteerPID.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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 
11 
12 #include "xml_utils.h"
13 
14 using namespace mvsim;
15 using namespace std;
16 
19  : ControllerBase(veh),
20  setpoint_lin_speed(0),
21  setpoint_ang_speed(0),
22  KP(100),
23  KI(0),
24  KD(0),
25  max_torque(400.0)
26 {
27  // Get distance between wheels:
29  veh_.wheels_info_[WHEEL_FL].y - veh_.wheels_info_[WHEEL_FR].y;
30  r2f_L_ = veh_.wheels_info_[WHEEL_FL].x - veh_.wheels_info_[WHEEL_RL].x;
31 
32  ASSERT_(dist_fWheels_ > 0.0);
33  ASSERT_(r2f_L_ > 0.0);
34 }
35 
39 {
40  // 1st: desired steering angle:
41  // --------------------------------
42  if (setpoint_ang_speed == 0)
43  {
44  co.steer_ang = 0.0;
45  }
46  else
47  {
48  const double R = setpoint_lin_speed / setpoint_ang_speed;
49  co.steer_ang = atan(r2f_L_ / R);
50  }
51 
52  PID_.KP = KP;
53  PID_.KI = KI;
54  PID_.KD = KD;
56 
57  const double vel_act = veh_.getVelocityLocalOdoEstimate().vx;
58  const double vel_des = setpoint_lin_speed;
59 
60  // "-" because \tau<0 makes robot moves forwards.
61  co.drive_torque = -PID_.compute(vel_des - vel_act, ci.context.dt);
62 }
63 
65  const rapidxml::xml_node<char>& node)
66 {
67  TParameterDefinitions params;
68  params["KP"] = TParamEntry("%lf", &KP);
69  params["KI"] = TParamEntry("%lf", &KI);
70  params["KD"] = TParamEntry("%lf", &KD);
71  params["max_torque"] = TParamEntry("%lf", &max_torque);
72 
73  // Initial speed.
74  params["V"] = TParamEntry("%lf", &this->setpoint_lin_speed);
75  params["W"] = TParamEntry("%lf_deg", &this->setpoint_ang_speed);
76 
77  parse_xmlnode_children_as_param(node, params);
78 }
79 
82 {
84 
85  switch (in.keycode)
86  {
87  case 'W':
88  case 'w':
89  setpoint_lin_speed += 0.1;
90  break;
91 
92  case 'S':
93  case 's':
94  setpoint_lin_speed -= 0.1;
95  break;
96 
97  case 'A':
98  case 'a':
99  setpoint_ang_speed += 1.0 * M_PI / 180.0;
100  break;
101 
102  case 'D':
103  case 'd':
104  setpoint_ang_speed -= 1.0 * M_PI / 180.0;
105  break;
106 
107  case ' ':
108  setpoint_lin_speed = .0;
109  setpoint_ang_speed = .0;
110  break;
111  };
112  out.append_gui_lines += "[Controller=" + string(class_name()) +
113  "] Teleop keys:\n"
114  "w/s=incr/decr lin speed.\n"
115  "a/d=left/right steering.\n"
116  "spacebar=stop.\n";
117  out.append_gui_lines += mrpt::format(
118  "setpoint: v=%.03f w=%.03f deg/s\n", setpoint_lin_speed,
119  setpoint_ang_speed * 180.0 / M_PI);
120 }
std::map< std::string, TParamEntry > TParameterDefinitions
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const TParameterDefinitions &params, const std::map< std::string, std::string > &variableNamesValues={}, const char *functionNameContext="", mrpt::system::COutputLogger *logger=nullptr)
Definition: xml_utils.cpp:224
double max_torque
Maximum abs. value torque (for clamp) [Nm].
#define M_PI
double dt
timestep
Definition: basic_types.h:60
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
INLINE Rall1d< T, V, S > atan(const Rall1d< T, V, S > &x)
double steer_ang
Equivalent Ackermann steering angle.
double max_out
For clamping (0=no clamp)
double compute(double err, double dt)
virtual void control_step(const DynamicsAckermannDrivetrain::TControllerInput &ci, DynamicsAckermannDrivetrain::TControllerOutput &co) override


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21