controller.cpp
Go to the documentation of this file.
2 #include <ros/ros.h>
3 #include <memory>
4 #include <cmath>
5 
6 namespace velocity_controller
7 {
9 {
10  actual_cmd_ = run;
12 }
13 
14 void Controller::setPath(std::shared_ptr<std::vector<PathPoint> > _path)
15 {
16  path_ = _path;
17  path_iterator_ = path_->begin();
18  plan_active = true;
19 }
20 
22 {
23  goal_radius_ = r;
24 }
25 
27 {
29  {
30  float dy = (float)(_odom.y - path_iterator_->y);
31  float dx = (float)(_odom.x - path_iterator_->x);
32  if (sqrt(dx * dx + dy * dy) < goal_radius_)
33  {
34  if (path_iterator_ != path_->end())
35  {
36  // ROS_INFO("++");
38  if (actual_cmd_ == step)
40 
41  if (path_iterator_ == path_->end())
42  {
43  ROS_INFO("Multi Robot Controller: goal reached");
44  plan_active = false;
45  }
46  }
47  }
48  }
49 }
50 
51 void Controller::setPID(float _Kp, float _Ki, float _Kd)
52 {
53  Kp_ = _Kp;
54  Kd_ = _Kd;
55  Ki_ = _Ki;
56 }
57 
59 {
60  actual_cmd_ = s;
61 }
62 
63 void Controller::update(PathPoint _odom, float _delta_t)
64 {
65  current_pose_ = _odom;
66  checkGoal(_odom);
67 
69  {
70  float theta = atan2(_odom.y - path_iterator_->y, _odom.x - path_iterator_->x);
71  float d_theta = normalizeAngle(_odom.theta - theta + M_PI);
72 
73  float d_theta_comp = M_PI / 2 + d_theta;
74  float distance_to_goal = sqrt((_odom.x - path_iterator_->x) * (_odom.x - path_iterator_->x) +
75  (_odom.y - path_iterator_->y) * (_odom.y - path_iterator_->y));
76  float turn_radius_to_goal = absolute(distance_to_goal / 2 / cos(d_theta_comp));
77 
78  float e = -d_theta;
79 
80  e_dot_ += e;
81 
82  w_ = Kp_ * e + Ki_ * e_dot_ * _delta_t + Kd_ * (e - e_last_) / _delta_t;
83  e_last_ = e;
84 
85  if (w_ > max_w_)
86  w_ = max_w_;
87  else if (w_ < -max_w_)
88  w_ = -max_w_;
89 
90  v_ = max_v_;
91  if (absolute(d_theta) > M_PI / 4) // If angle is bigger than 90deg the robot should turn on point
92  {
93  v_ = 0;
94  }
95  else if (turn_radius_to_goal <
96  10 * distance_to_goal) // If we have a small radius to goal we should turn with the right radius
97  {
98  float v_h = turn_radius_to_goal * absolute(w_);
99  if (v_ > v_h)
100  v_ = v_h;
101  }
102 
103  // ROS_INFO("(%f %f)(%f %f)", odom.x, odom.y, path_iterator->x, path_iterator->y);
104  // ROS_INFO("d %f t %f r %f, v %f w %f", distance_to_goal, d_theta / M_PI * 180, turn_radius_to_goal, v_, w_);
105  }
106  else
107  {
108  v_ = 0;
109  w_ = 0;
110  }
111 }
112 
113 float Controller::absolute(float _val)
114 {
115  if (_val < 0)
116  _val = -_val;
117 
118  return _val;
119 }
120 
121 float Controller::normalizeAngle(float _angle)
122 {
123  while (_angle > M_PI)
124  {
125  _angle -= 2 * M_PI;
126  }
127  while (_angle < -M_PI)
128  {
129  _angle += 2 * M_PI;
130  }
131  return _angle;
132 }
133 
134 void Controller::setSpeedParams(float _max_v, float _max_w)
135 {
136  max_v_ = _max_v;
137  max_w_ = _max_w;
138 }
139 
140 void Controller::getSpeed(float* _v, float* _w)
141 {
142  *_v = v_;
143  *_w = w_;
144 }
145 }
struct velocity_controller::Point_t PathPoint
void setPath(std::shared_ptr< std::vector< PathPoint >> _path)
Set the path the contorler has to follow.
Definition: controller.cpp:14
bool checkGoal(PathPoint _odom)
Definition: controller.cpp:26
void setState(state s)
Set the State (run stop step wait_step)
Definition: controller.cpp:58
void setPID(float _Kp, float _Ki, float _Kd)
sets the control parameters
Definition: controller.cpp:51
void getSpeed(float *_v, float *_w)
Returns the current speed values.
Definition: controller.cpp:140
std::vector< PathPoint >::iterator path_iterator_
Definition: controller.h:91
#define ROS_INFO(...)
void setSpeedParams(float _max_v, float _max_w)
Set the Speed Params of the controller.
Definition: controller.cpp:134
float normalizeAngle(float _angle)
Definition: controller.cpp:121
Controller()
Construct a new Controller object.
Definition: controller.cpp:8
enum velocity_controller::state_t state
void setGoalRadius(float _r)
Set the Goal Radius.
Definition: controller.cpp:21
void update(PathPoint _odom, float _delta_t)
updates the controller with the current robot position
Definition: controller.cpp:63
std::shared_ptr< std::vector< PathPoint > > path_
Definition: controller.h:88


tuw_multi_robot_ctrl
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:29