VehicleDifferential_ControllerTwistPID.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under GNU General Public License version 3 |
7  | See <http://www.gnu.org/licenses/> |
8  +-------------------------------------------------------------------------+ */
9 
11 #include "xml_utils.h"
12 
13 using namespace mvsim;
14 using namespace std;
15 
18  : ControllerBase(veh),
19  setpoint_lin_speed(0),
20  setpoint_ang_speed(0),
21  KP(100),
22  KI(0),
23  KD(0),
24  max_torque(100.0)
25 {
26  // Get distance between wheels:
27  // Warning: the controller *assumes* that both wheels are parallel (as it's
28  // a rule in differential robots!!)
29  m_distWheels = m_veh.m_wheels_info[0].y - m_veh.m_wheels_info[1].y;
30 }
31 
32 // See base class docs
36 {
37  // For each wheel:
38  // 1) Compute desired velocity set-point (in m/s)
39  // 2) Run the PI/PID for that wheel independently (in newtons)
40  const double vel_l =
42  const double vel_r =
44 
45  // Compute each wheel actual velocity (from an "odometry" estimation of
46  // velocity, not ground-truth!):
47  const vec3 vehVelOdo = m_veh.getVelocityLocalOdoEstimate();
48  const double act_vel_l =
49  vehVelOdo.vals[0] - 0.5 * vehVelOdo.vals[2] * m_distWheels;
50  const double act_vel_r =
51  vehVelOdo.vals[0] + 0.5 * vehVelOdo.vals[2] * m_distWheels;
52 
53  // Apply controller:
54  for (int i = 0; i < 2; i++)
55  {
56  m_PID[i].KP = KP;
57  m_PID[i].KI = KI;
58  m_PID[i].KD = KD;
60  }
61 
62  co.wheel_torque_l = -m_PID[0].compute(
63  vel_l - act_vel_l,
64  ci.context.dt); // "-" because \tau<0 makes robot moves forwards.
65  co.wheel_torque_r = -m_PID[1].compute(vel_r - act_vel_r, ci.context.dt);
66 }
67 
69  const rapidxml::xml_node<char>& node)
70 {
71  std::map<std::string, TParamEntry> params;
72  params["KP"] = TParamEntry("%lf", &KP);
73  params["KI"] = TParamEntry("%lf", &KI);
74  params["KD"] = TParamEntry("%lf", &KD);
75  params["max_torque"] = TParamEntry("%lf", &max_torque);
76 
77  // Initial speed.
78  params["V"] = TParamEntry("%lf", &this->setpoint_lin_speed);
79  params["W"] = TParamEntry("%lf_deg", &this->setpoint_ang_speed);
80 
81  parse_xmlnode_children_as_param(node, params);
82 }
83 
85  const TeleopInput& in, TeleopOutput& out)
86 {
88 
89  switch (in.keycode)
90  {
91  case 'W':
92  case 'w':
93  setpoint_lin_speed += 0.1;
94  break;
95 
96  case 'S':
97  case 's':
98  setpoint_lin_speed -= 0.1;
99  break;
100 
101  case 'A':
102  case 'a':
103  setpoint_ang_speed += 2.0 * M_PI / 180;
104  break;
105 
106  case 'D':
107  case 'd':
108  setpoint_ang_speed -= 2.0 * M_PI / 180;
109  break;
110 
111  case ' ':
112  setpoint_lin_speed = 0.0;
113  setpoint_ang_speed = 0.0;
114  break;
115  };
116  out.append_gui_lines +=
117  "[Controller=" + string(class_name()) +
118  "] Teleop keys: w/s=forward/backward. a/d=left/right. spacebar=stop.\n";
119  out.append_gui_lines += mrpt::format(
120  "setpoint: lin=%.03f ang=%.03f deg/s\n", setpoint_lin_speed,
121  180.0 / M_PI * setpoint_ang_speed);
122 }
double vals[3]
Definition: basic_types.h:64
double dt
timestep
Definition: basic_types.h:57
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const std::map< std::string, TParamEntry > &params, const char *function_name_context="")
Definition: xml_utils.cpp:196
virtual void control_step(const DynamicsDifferential::TControllerInput &ci, DynamicsDifferential::TControllerOutput &co)
double max_out
For clamping (0=no clamp)
double compute(double err, double dt)
double max_torque
Maximum abs. value torque (for clamp) [Nm].
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out)
double setpoint_ang_speed
desired velocities (m/s) and (rad/s)
virtual void load_config(const rapidxml::xml_node< char > &node)


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40