VehicleAckermann_Drivetrain_ControllerFrontSteerPID.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_steer_ang(0),
22  KP(100),
23  KI(0),
24  KD(0),
25  max_torque(100.0),
26  twist_control_(veh)
27 {
28  // Get distance between wheels:
29  r2f_L_ = veh_.wheels_info_[WHEEL_FL].x - veh_.wheels_info_[WHEEL_RL].x;
30  ASSERT_(r2f_L_ > 0.0);
31 }
32 
33 // See base class docs
37 {
38  // Equivalent v/w velocities:
39  const double v = setpoint_lin_speed;
40  double w;
41  if (setpoint_steer_ang == 0.0)
42  {
43  w = 0.0;
44  }
45  else
46  {
47  // ang = atan(r2f_L/R) -> R= r2f_L / tan(ang)
48  // R = v/w -> w=v/R
49  const double R = r2f_L_ / tan(setpoint_steer_ang);
50  w = v / R;
51  }
52 
53  // Let the twist controller do the calculations:
56 
61 
63  co.steer_ang = setpoint_steer_ang; // Mainly for the case of v=0
64 }
65 
67  const rapidxml::xml_node<char>& node)
68 {
69  TParameterDefinitions params;
70  params["KP"] = TParamEntry("%lf", &KP);
71  params["KI"] = TParamEntry("%lf", &KI);
72  params["KD"] = TParamEntry("%lf", &KD);
73  params["max_torque"] = TParamEntry("%lf", &max_torque);
74 
75  // Initial speed.
76  params["V"] = TParamEntry("%lf", &this->setpoint_lin_speed);
77  params["STEER_ANG"] = TParamEntry("%lf_deg", &this->setpoint_steer_ang);
78 
79  parse_xmlnode_children_as_param(node, params);
80 }
81 
83  const TeleopInput& in, TeleopOutput& out)
84 {
86 
87  switch (in.keycode)
88  {
89  case 'W':
90  case 'w':
91  setpoint_lin_speed += 0.1;
92  break;
93 
94  case 'S':
95  case 's':
96  setpoint_lin_speed -= 0.1;
97  break;
98 
99  case 'A':
100  case 'a':
101  setpoint_steer_ang += 1.0 * M_PI / 180.0;
102  mrpt::keep_min(setpoint_steer_ang, veh_.getMaxSteeringAngle());
103  break;
104 
105  case 'D':
106  case 'd':
107  setpoint_steer_ang -= 1.0 * M_PI / 180.0;
108  mrpt::keep_max(setpoint_steer_ang, -veh_.getMaxSteeringAngle());
109  break;
110  case ' ':
111  setpoint_lin_speed = .0;
112  break;
113  };
114  out.append_gui_lines += "[Controller=" + string(class_name()) +
115  "] Teleop keys: w/s=incr/decr lin speed. "
116  "a/d=left/right steering. spacebar=stop.\n";
117  out.append_gui_lines += mrpt::format(
118  "setpoint: v=%.03f steer=%.03f deg\n", setpoint_lin_speed,
119  setpoint_steer_ang * 180.0 / M_PI);
120 }
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
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
virtual void control_step(const DynamicsAckermannDrivetrain::TControllerInput &ci, DynamicsAckermannDrivetrain::TControllerOutput &co) override
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
double steer_ang
Equivalent Ackermann steering angle.
virtual void control_step(const DynamicsAckermannDrivetrain::TControllerInput &ci, DynamicsAckermannDrivetrain::TControllerOutput &co) override
INLINE Rall1d< T, V, S > tan(const Rall1d< T, V, S > &arg)
double max_torque
Maximum abs. value torque (for clamp) [Nm].


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