segment_controller.cpp
Go to the documentation of this file.
2 #include <ros/ros.h>
3 #include <memory>
4 #include <cmath>
5 
6 
7 namespace velocity_controller
8 {
10  {
11  actual_cmd_ = run;
12  }
13 
14  void SegmentController::setPath( std::shared_ptr< std::vector< PathPoint > > _path )
15  {
16  actualPreconditions.clear();
17  path_ = _path;
18  pathCounter_ = 0;
19  plan_active = true;
20  }
21 
23  {
24  return pathCounter_ - 1;
25  }
26 
27 
29  {
30  goal_radius_ = r;
31  }
32 
34  {
35  if(actualPreconditions.size() <= pc.robot)
36  actualPreconditions.resize(pc.robot + 1, 0);
37 
39  }
40 
41 
43  {
44  for ( const auto & pc : p.precondition )
45  {
46  int count = 0;
47 
48  if ( actualPreconditions.size() > pc.robot )
49  {
50  count = actualPreconditions[pc.robot];
51  }
52  if(count <= pc.stepCondition) //??? <= TODO
53  {
54  return false;
55  }
56  }
57  return true;
58  }
59 
60 
62  {
64  {
65  float dy = ( float )( _odom.y - (*path_)[pathCounter_].y );
66  float dx = ( float )( _odom.x - (*path_)[pathCounter_].x );
67 
68  if ( sqrt( dx * dx + dy * dy ) < goal_radius_ )
69  {
70  if ( pathCounter_ < path_->size() )
71  {
72  //ROS_INFO( "++" );
73  pathCounter_++;
74 
75  if ( actual_cmd_ == step )
77 
78 
79  if ( pathCounter_ >= path_->size() )
80  {
81  ROS_INFO( "Multi Robot Controller: goal reached" );
82  plan_active = false;
83  }
84  }
85  }
86  }
87  }
88 
89 
90  void SegmentController::setPID( float _Kp, float _Ki, float _Kd )
91  {
92  Kp_ = _Kp;
93  Kd_ = _Kd;
94  Ki_ = _Ki;
95  }
96 
98  {
99  actual_cmd_ = s;
100  }
101 
102 
103  void SegmentController::update( PathPoint _odom, float _delta_t )
104  {
105  checkGoal( _odom );
106 
108  {
109  float theta = atan2( _odom.y - (*path_)[pathCounter_].y, _odom.x - (*path_)[pathCounter_].x );
110  float d_theta = normalizeAngle( _odom.theta - theta + M_PI );
111 
112  float d_theta_comp = M_PI / 2 + d_theta;
113  float distance_to_goal = sqrt( ( _odom.x - (*path_)[pathCounter_].x ) * ( _odom.x - (*path_)[pathCounter_].x ) + ( _odom.y - (*path_)[pathCounter_].y ) * ( _odom.y - (*path_)[pathCounter_].y ) );
114  float turn_radius_to_goal = absolute( distance_to_goal / 2 / cos( d_theta_comp ) );
115 
116 
117  float e = -d_theta;
118 
119  e_dot_ += e;
120 
121  w_ = Kp_ * e + Ki_ * e_dot_ * _delta_t + Kd_ * ( e - e_last_ ) / _delta_t;
122  e_last_ = e;
123 
124  if ( w_ > max_w_ )
125  w_ = max_w_;
126  else if ( w_ < -max_w_ )
127  w_ = -max_w_;
128 
129  v_ = max_v_;
130 
131  if ( absolute( d_theta ) > M_PI / 4 ) //If angle is bigger than 90deg the robot should turn on point
132  {
133  v_ = 0;
134  }
135  else if ( turn_radius_to_goal < 10 * distance_to_goal ) //If we have a small radius to goal we should turn with the right radius
136  {
137  float v_h = turn_radius_to_goal * absolute( w_ );
138 
139  if ( v_ > v_h )
140  v_ = v_h;
141  }
142 
143  //ROS_INFO("(%f %f)(%f %f)", odom.x, odom.y, path_iterator->x, path_iterator->y);
144  //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_);
145  }
146  else
147  {
148  v_ = 0;
149  w_ = 0;
150  }
151  }
152 
153  float SegmentController::absolute( float _val )
154  {
155  if ( _val < 0 )
156  _val = -_val;
157 
158  return _val;
159 
160  }
161 
162  float SegmentController::normalizeAngle( float _angle )
163  {
164 
165  while ( _angle > M_PI )
166  {
167  _angle -= 2 * M_PI;
168  }
169 
170  while ( _angle < -M_PI )
171  {
172  _angle += 2 * M_PI;
173  }
174 
175  return _angle;
176  }
177 
178 
179  void SegmentController::setSpeedParams( float _max_v, float _max_w )
180  {
181  max_v_ = _max_v;
182  max_w_ = _max_w;
183  }
184 
185  void SegmentController::getSpeed( float* _v, float* _w )
186  {
187  *_v = v_;
188  *_w = w_;
189  }
190 
191 }
void setState(state s)
Set the State (run stop step wait_step)
void setGoalRadius(float _r)
Set the Goal Radius.
void setSpeedParams(float _max_v, float _max_w)
Set the Speed Params of the controller.
SegmentController()
Construct a new Segment Controller object.
std::shared_ptr< std::vector< PathPoint > > path_
void update(PathPoint _odom, float _delta_t)
updates the controller with the current robot position
int getCount()
Returns the checkpoint number the robot is in.
#define ROS_INFO(...)
void setPID(float _Kp, float _Ki, float _Kd)
sets the control parameters
enum velocity_controller::state_t state
void updatePrecondition(PathPrecondition pc)
updates the sync step of other robots
void setPath(std::shared_ptr< std::vector< PathPoint >> _path)
Set the path the contorler has to follow.
void getSpeed(float *_v, float *_w)
Returns the current speed values.


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