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;
13 }
14 
15 void Controller::setPath(std::shared_ptr<std::vector<PathPoint> > _path)
16 {
17  path_ = *_path;
19  plan_active = true;
20 }
21 
23 {
24  goal_radius_ = r;
25 }
26 
28 {
30  {
31  const PathPoint &target = path_[idx_path_target_point_];
32  float dy = (float)(_odom.y - target.y);
33  float dx = (float)(_odom.x - target.x);
34  if (sqrt(dx * dx + dy * dy) < goal_radius_)
35  {
36  if (idx_path_target_point_ < path_.size())
37  {
38  // ROS_INFO("++");
40  if (actual_cmd_ == step)
42 
43  if (idx_path_target_point_ == path_.size())
44  {
45  ROS_INFO("Multi Robot Controller: goal reached");
46  robot_status = tuw_multi_robot_msgs::RobotInfo::STATUS_DONE;
47  plan_active = false;
48  }
49  }
50  }
51  }
52 }
53 
54 void Controller::setPID(float _Kp, float _Ki, float _Kd)
55 {
56  Kp_ = _Kp;
57  Kd_ = _Kd;
58  Ki_ = _Ki;
59 }
60 
63 }
64 
66  return plan_active;
67 }
68 
70 {
71  actual_cmd_ = s;
72 }
73 
74 void Controller::update(PathPoint _odom, float _delta_t)
75 {
76  current_pose_ = _odom;
77  checkGoal(_odom);
78 
80  {
81  const PathPoint &target = path_[idx_path_target_point_];
82  float theta = atan2(_odom.y - target.y, _odom.x - target.x);
83  float d_theta = normalizeAngle(_odom.theta - theta + M_PI);
84 
85  float d_theta_comp = M_PI / 2 + d_theta;
86  float distance_to_goal = sqrt((_odom.x - target.x) * (_odom.x - target.x) +
87  (_odom.y - target.y) * (_odom.y - target.y));
88  float turn_radius_to_goal = absolute(distance_to_goal / 2 / cos(d_theta_comp));
89 
90  float e = -d_theta;
91 
92  e_dot_ += e;
93 
94  w_ = Kp_ * e + Ki_ * e_dot_ * _delta_t + Kd_ * (e - e_last_) / _delta_t;
95  e_last_ = e;
96 
97  if (w_ > max_w_)
98  w_ = max_w_;
99  else if (w_ < -max_w_)
100  w_ = -max_w_;
101 
102  v_ = max_v_;
103  if (absolute(d_theta) > M_PI / 4) // If angle is bigger than 90deg the robot should turn on point
104  {
105  v_ = 0;
106  }
107  else if (turn_radius_to_goal <
108  10 * distance_to_goal) // If we have a small radius to goal we should turn with the right radius
109  {
110  float v_h = turn_radius_to_goal * absolute(w_);
111  if (v_ > v_h)
112  v_ = v_h;
113  }
114 
115  // ROS_INFO("(%f %f)(%f %f)", odom.x, odom.y, path_iterator->x, path_iterator->y);
116  // 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_);
117  }
118  else
119  {
120  v_ = 0;
121  w_ = 0;
122  }
123 }
124 
125 float Controller::absolute(float _val)
126 {
127  if (_val < 0)
128  _val = -_val;
129 
130  return _val;
131 }
132 
133 float Controller::normalizeAngle(float _angle)
134 {
135  while (_angle > M_PI)
136  {
137  _angle -= 2 * M_PI;
138  }
139  while (_angle < -M_PI)
140  {
141  _angle += 2 * M_PI;
142  }
143  return _angle;
144 }
145 
146 void Controller::setSpeedParams(float _max_v, float _max_w)
147 {
148  max_v_ = _max_v;
149  max_w_ = _max_w;
150 }
151 
152 void Controller::getSpeed(float* _v, float* _w)
153 {
154  *_v = v_;
155  *_w = w_;
156 }
157 
159  {
160  return robot_status;
161  }
162 
164  {
165  this->orderId = orderId;
166  }
168  {
169  this->goodId = goodId;
170  }
171 
173  {
174  return goodId;
175  }
177  {
178  this->orderStatus = orderStatus;
179  }
181  {
182  return this->orderStatus;
183  }
184 }
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:15
bool checkGoal(PathPoint _odom)
Definition: controller.cpp:27
size_t getProgress()
return progress,
Definition: controller.cpp:61
void setState(state s)
Set the State (run stop step wait_step)
Definition: controller.cpp:69
void setPID(float _Kp, float _Ki, float _Kd)
sets the control parameters
Definition: controller.cpp:54
void getSpeed(float *_v, float *_w)
Returns the current speed values.
Definition: controller.cpp:152
std::vector< PathPoint > path_
Definition: controller.h:109
#define ROS_INFO(...)
void setSpeedParams(float _max_v, float _max_w)
Set the Speed Params of the controller.
Definition: controller.cpp:146
float normalizeAngle(float _angle)
Definition: controller.cpp:133
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:22
void update(PathPoint _odom, float _delta_t)
updates the controller with the current robot position
Definition: controller.cpp:74
bool isActive()
return progress, passed waypoints on the given path
Definition: controller.cpp:65


tuw_multi_robot_ctrl
Author(s): Benjamin Binder
autogenerated on Mon Feb 28 2022 23:57:38