VehicleAckermann_ControllerFrontSteerPID.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2024 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 
18  : ControllerBase(veh),
19  setpoint_lin_speed(0),
20  setpoint_steer_ang(0),
21  KP(100),
22  KI(0),
23  KD(0),
24  max_torque(100.0),
25  twist_control_(veh)
26 {
27  // Get distance between wheels:
28  r2f_L_ = veh_.wheels_info_[WHEEL_FL].x - veh_.wheels_info_[WHEEL_RL].x;
29  ASSERT_(r2f_L_ > 0.0);
30 }
31 
32 // See base class docs
35 {
36  // Equivalent v/w velocities:
37  const double v = setpoint_lin_speed;
38  double w;
39  if (setpoint_steer_ang == 0.0)
40  {
41  w = 0.0;
42  }
43  else
44  {
45  // ang = atan(r2f_L/R) -> R= r2f_L / tan(ang)
46  // R = v/w -> w=v/R
47  const double R = r2f_L_ / tan(setpoint_steer_ang);
48  w = v / R;
49  }
50 
51  // Let the twist controller do the calculations:
52  twist_control_.setpoint_lin_speed = v;
53  twist_control_.setpoint_ang_speed = w;
54 
55  twist_control_.KP = KP;
56  twist_control_.KI = KI;
57  twist_control_.KD = KD;
58  twist_control_.max_torque = max_torque;
59 
60  twist_control_.control_step(ci, co);
61  co.steer_ang = setpoint_steer_ang; // Mainly for the case of v=0
62 }
63 
65 {
66  TParameterDefinitions params;
67  params["KP"] = TParamEntry("%lf", &KP);
68  params["KI"] = TParamEntry("%lf", &KI);
69  params["KD"] = TParamEntry("%lf", &KD);
70  params["max_torque"] = TParamEntry("%lf", &max_torque);
71 
72  // Initial speed.
73  params["V"] = TParamEntry("%lf", &this->setpoint_lin_speed);
74  params["STEER_ANG"] = TParamEntry("%lf_deg", &this->setpoint_steer_ang);
75 
76  parse_xmlnode_children_as_param(node, params);
77 }
78 
80  const TeleopInput& in, TeleopOutput& out)
81 {
83 
84  switch (in.keycode)
85  {
86  case 'W':
87  case 'w':
88  setpoint_lin_speed += 0.1;
89  break;
90 
91  case 'S':
92  case 's':
93  setpoint_lin_speed -= 0.1;
94  break;
95 
96  case 'A':
97  case 'a':
98  setpoint_steer_ang += 1.0 * M_PI / 180.0;
99  mrpt::keep_min(setpoint_steer_ang, veh_.getMaxSteeringAngle());
100  break;
101 
102  case 'D':
103  case 'd':
104  setpoint_steer_ang -= 1.0 * M_PI / 180.0;
105  mrpt::keep_max(setpoint_steer_ang, -veh_.getMaxSteeringAngle());
106  break;
107  case ' ':
108  setpoint_lin_speed = .0;
109  break;
110  };
111 
112  out.append_gui_lines += "[Controller=" + std::string(class_name()) + "]";
113 
114  if (in.js && in.js->axes.size() >= 2)
115  {
116  const auto& js = in.js.value();
117  const float js_x = js.axes[0];
118  const float js_y = js.axes[1];
119 
120  setpoint_lin_speed = -js_y * joyMaxLinSpeed;
121  setpoint_steer_ang = -js_x * joyMaxSteerAng;
122 
123  if (js.buttons.size() >= 7)
124  {
125  if (js.buttons[5]) joyMaxLinSpeed *= 1.01;
126  if (js.buttons[7]) joyMaxLinSpeed /= 1.01;
127 
128  if (js.buttons[4]) joyMaxSteerAng *= 1.01;
129  if (js.buttons[6]) joyMaxSteerAng /= 1.01;
130 
131  if (js.buttons[3]) // brake
132  {
133  setpoint_lin_speed = 0;
134  }
135  }
136 
137  out.append_gui_lines += mrpt::format(
138  "Teleop joystick:\n"
139  "maxLinSpeed=%.03f m/s\n"
140  "maxSteerAng=%.03f deg\n",
141  joyMaxLinSpeed, mrpt::RAD2DEG(joyMaxSteerAng));
142  }
143  else
144  {
145  out.append_gui_lines +=
146  "Teleop keys:\n"
147  "w/s=forward/backward.\n"
148  "a/d=left/right.\n"
149  "spacebar=stop.\n";
150  }
151 
152  out.append_gui_lines += mrpt::format(
153  "setpoint: v=%.03f steer=%.03f deg\n", setpoint_lin_speed,
154  setpoint_steer_ang * 180.0 / M_PI);
155 }
move-object-example.R
int R
Definition: move-object-example.py:41
mvsim
Definition: Client.h:21
mvsim::ControllerBaseTempl::teleop_interface
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: ControllerBase.h:67
mvsim::DynamicsAckermann::ControllerFrontSteerPID::r2f_L_
double r2f_L_
Definition: VehicleAckermann.h:132
mvsim::TParamEntry
Definition: TParameterDefinitions.h:38
mvsim::parse_xmlnode_children_as_param
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:215
mvsim::ControllerBaseInterface::TeleopOutput::append_gui_lines
std::string append_gui_lines
Definition: ControllerBase.h:35
mvsim::DynamicsAckermann::ControllerFrontSteerPID::load_config
virtual void load_config(const rapidxml::xml_node< char > &node) override
Definition: VehicleAckermann_ControllerFrontSteerPID.cpp:64
xml_utils.h
mvsim::DynamicsAckermann::ControllerFrontSteerPID::control_step
virtual void control_step(const DynamicsAckermann::TControllerInput &ci, DynamicsAckermann::TControllerOutput &co) override
Definition: VehicleAckermann_ControllerFrontSteerPID.cpp:33
mvsim::ControllerBaseTempl
Definition: ControllerBase.h:59
mvsim::DynamicsAckermann::TControllerInput
Definition: VehicleAckermann.h:43
mvsim::TParameterDefinitions
std::map< std::string, TParamEntry > TParameterDefinitions
Definition: TParameterDefinitions.h:64
mvsim::ControllerBaseInterface::TeleopOutput
Definition: ControllerBase.h:33
mvsim::DynamicsAckermann::ControllerFrontSteerPID::ControllerFrontSteerPID
ControllerFrontSteerPID(DynamicsAckermann &veh)
Definition: VehicleAckermann_ControllerFrontSteerPID.cpp:17
mvsim::DynamicsAckermann::TControllerOutput::steer_ang
double steer_ang
Equivalent Ackermann steering angle.
Definition: VehicleAckermann.h:50
mvsim::DynamicsAckermann::ControllerFrontSteerPID::teleop_interface
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: VehicleAckermann_ControllerFrontSteerPID.cpp:79
rapidxml::xml_node< char >
mvsim::DynamicsAckermann::WHEEL_RL
@ WHEEL_RL
Definition: VehicleAckermann.h:29
mvsim::DynamicsAckermann::WHEEL_FL
@ WHEEL_FL
Definition: VehicleAckermann.h:31
std
VehicleAckermann.h
mvsim::DynamicsAckermann
Definition: VehicleAckermann.h:22
mvsim::ControllerBaseInterface::TeleopInput
Definition: ControllerBase.h:25
mvsim::ControllerBaseTempl::veh_
VEH_DYNAMICS & veh_
Definition: ControllerBase.h:124
mvsim::ControllerBaseInterface::TeleopInput::js
std::optional< TJoyStickEvent > js
Definition: ControllerBase.h:28
mvsim::ControllerBaseInterface::TeleopInput::keycode
int keycode
Definition: ControllerBase.h:27
mvsim::DynamicsAckermann::TControllerOutput
Definition: VehicleAckermann.h:47


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08