Vehicleackermann_Drivetrain_ControllerTwistFrontSteerPID.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 
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:
28  dist_fWheels_ = veh_.wheels_info_[WHEEL_FL].y - veh_.wheels_info_[WHEEL_FR].y;
29  r2f_L_ = veh_.wheels_info_[WHEEL_FL].x - veh_.wheels_info_[WHEEL_RL].x;
30 
31  ASSERT_(dist_fWheels_ > 0.0);
32  ASSERT_(r2f_L_ > 0.0);
33 }
34 
38 {
39  // 1st: desired steering angle:
40  // --------------------------------
41  if (setpoint_ang_speed == 0)
42  {
43  co.steer_ang = 0.0;
44  }
45  else
46  {
47  const double R = setpoint_lin_speed / setpoint_ang_speed;
48  co.steer_ang = atan(r2f_L_ / R);
49  }
50 
51  PID_.KP = KP;
52  PID_.KI = KI;
53  PID_.KD = KD;
54  PID_.max_out = max_torque;
55 
56  const double vel_act = veh_.getVelocityLocalOdoEstimate().vx;
57  const double vel_des = setpoint_lin_speed;
58 
59  // "-" because \tau<0 makes robot moves forwards.
60  co.drive_torque = -PID_.compute(vel_des - vel_act, ci.context.dt);
61 }
62 
64  const rapidxml::xml_node<char>& node)
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["W"] = TParamEntry("%lf_deg", &this->setpoint_ang_speed);
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_ang_speed += 1.0 * M_PI / 180.0;
99  break;
100 
101  case 'D':
102  case 'd':
103  setpoint_ang_speed -= 1.0 * M_PI / 180.0;
104  break;
105 
106  case ' ':
107  setpoint_lin_speed = .0;
108  setpoint_ang_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_ang_speed = -js_x * joyMaxAngSpeed;
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]) joyMaxAngSpeed *= 1.01;
129  if (js.buttons[6]) joyMaxAngSpeed /= 1.01;
130 
131  if (js.buttons[3]) // brake
132  {
133  setpoint_lin_speed = 0;
134  setpoint_ang_speed = 0;
135  }
136  }
137 
138  out.append_gui_lines += mrpt::format(
139  "Teleop joystick:\n"
140  "maxLinSpeed=%.03f m/s\n"
141  "maxAngSpeed=%.03f deg/s\n",
142  joyMaxLinSpeed, mrpt::RAD2DEG(joyMaxAngSpeed));
143  }
144  else
145  {
146  out.append_gui_lines +=
147  "Teleop keys:\n"
148  "w/s=forward/backward.\n"
149  "a/d=left/right.\n"
150  "spacebar=stop.\n";
151  }
152 
153  out.append_gui_lines += mrpt::format(
154  "setpoint: lin=%.03f ang=%.03f deg/s\n", setpoint_lin_speed,
155  180.0 / M_PI * setpoint_ang_speed);
156 }
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::DynamicsAckermannDrivetrain::WHEEL_FR
@ WHEEL_FR
Definition: VehicleAckermann_Drivetrain.h:38
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::DynamicsAckermannDrivetrain
Definition: VehicleAckermann_Drivetrain.h:28
mvsim::DynamicsAckermannDrivetrain::TControllerOutput::drive_torque
double drive_torque
Definition: VehicleAckermann_Drivetrain.h:68
xml_utils.h
mvsim::DynamicsAckermannDrivetrain::ControllerTwistFrontSteerPID::teleop_interface
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: Vehicleackermann_Drivetrain_ControllerTwistFrontSteerPID.cpp:79
VehicleAckermann_Drivetrain.h
mvsim::TSimulContext::dt
double dt
timestep
Definition: basic_types.h:63
mvsim::DynamicsAckermannDrivetrain::ControllerTwistFrontSteerPID::control_step
virtual void control_step(const DynamicsAckermannDrivetrain::TControllerInput &ci, DynamicsAckermannDrivetrain::TControllerOutput &co) override
Definition: Vehicleackermann_Drivetrain_ControllerTwistFrontSteerPID.cpp:35
mvsim::ControllerBaseTempl
Definition: ControllerBase.h:59
mvsim::DynamicsAckermannDrivetrain::TControllerOutput
Definition: VehicleAckermann_Drivetrain.h:66
mvsim::DynamicsAckermannDrivetrain::ControllerTwistFrontSteerPID::dist_fWheels_
double dist_fWheels_
Definition: VehicleAckermann_Drivetrain.h:121
mvsim::TParameterDefinitions
std::map< std::string, TParamEntry > TParameterDefinitions
Definition: TParameterDefinitions.h:64
mvsim::ControllerBaseInterface::TeleopOutput
Definition: ControllerBase.h:33
mvsim::DynamicsAckermannDrivetrain::ControllerTwistFrontSteerPID::ControllerTwistFrontSteerPID
ControllerTwistFrontSteerPID(DynamicsAckermannDrivetrain &veh)
Definition: Vehicleackermann_Drivetrain_ControllerTwistFrontSteerPID.cpp:17
mvsim::DynamicsAckermannDrivetrain::TControllerInput::context
TSimulContext context
Definition: VehicleAckermann_Drivetrain.h:64
mvsim::DynamicsAckermannDrivetrain::TControllerInput
Definition: VehicleAckermann_Drivetrain.h:62
mvsim::DynamicsAckermannDrivetrain::WHEEL_RL
@ WHEEL_RL
Definition: VehicleAckermann_Drivetrain.h:35
rapidxml::xml_node< char >
mvsim::DynamicsAckermannDrivetrain::ControllerTwistFrontSteerPID::load_config
virtual void load_config(const rapidxml::xml_node< char > &node) override
Definition: Vehicleackermann_Drivetrain_ControllerTwistFrontSteerPID.cpp:63
std
mvsim::DynamicsAckermannDrivetrain::TControllerOutput::steer_ang
double steer_ang
Equivalent Ackermann steering angle.
Definition: VehicleAckermann_Drivetrain.h:69
mvsim::DynamicsAckermannDrivetrain::WHEEL_FL
@ WHEEL_FL
Definition: VehicleAckermann_Drivetrain.h:37
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::DynamicsAckermannDrivetrain::ControllerTwistFrontSteerPID::r2f_L_
double r2f_L_
Definition: VehicleAckermann_Drivetrain.h:121


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