speed_limiter.cpp
Go to the documentation of this file.
1 
6 #include <algorithm>
7 #include <math.h>
9 
10 template<typename T>
11 T clamp(T x, T min, T max)
12 {
13  return std::min(std::max(min, x), max);
14 }
15 
16 namespace ackermann_controller
17 {
18 
20  bool has_velocity_limits,
21  bool has_acceleration_limits,
22  bool has_deceleration_limits,
23  bool has_jerk_limits,
24  double min_velocity,
25  double max_velocity,
26  double min_acceleration,
27  double max_acceleration,
28  double min_deceleration,
29  double max_deceleration,
30  double min_jerk,
31  double max_jerk
32 )
33  : has_velocity_limits(has_velocity_limits)
34  , has_acceleration_limits(has_acceleration_limits)
35  , has_deceleration_limits(has_deceleration_limits)
36  , has_jerk_limits(has_jerk_limits)
37  , min_velocity(min_velocity)
38  , max_velocity(max_velocity)
39  , min_acceleration(min_acceleration)
40  , max_acceleration(max_acceleration)
41  , min_deceleration(min_deceleration)
42  , max_deceleration(max_deceleration)
43  , min_jerk(min_jerk)
44  , max_jerk(max_jerk)
45 {
46 }
47 
48 double SpeedLimiter::limit(double& v, double v0, double v1, double dt)
49 {
50  const double tmp = v;
51 
52  limit_jerk(v, v0, v1, dt);
53  limit_acceleration(v, v0, dt);
54  limit_velocity(v);
55 
56  return tmp != 0.0 ? v / tmp : 1.0;
57 }
58 
60 {
61  const double tmp = v;
62 
64  {
66  }
67 
68  return tmp != 0.0 ? v / tmp : 1.0;
69 }
70 
71 double SpeedLimiter::limit_acceleration(double& v, double v0, double dt)
72 {
73  const double tmp = v;
74 
75  if (fabs(v) > fabs(v0))
76  {
78  {
79  const double dv_min = min_acceleration * dt;
80  const double dv_max = max_acceleration * dt;
81 
82  const double dv = clamp(v - v0, dv_min, dv_max);
83 
84  v = v0 + dv;
85  }
86  }
87  else
88  {
90  {
91  const double dv_min = min_deceleration * dt;
92  const double dv_max = max_deceleration * dt;
93 
94  const double dv = clamp(v - v0, dv_min, dv_max);
95 
96  v = v0 + dv;
97  }
98  }
99 
100  return tmp != 0.0 ? v / tmp : 1.0;
101 }
102 
103 double SpeedLimiter::limit_jerk(double& v, double v0, double v1, double dt)
104 {
105  const double tmp = v;
106 
107  if (has_jerk_limits)
108  {
109  const double dv = v - v0;
110  const double dv0 = v0 - v1;
111 
112  const double dt2 = 2. * dt * dt;
113 
114  const double da_min = min_jerk * dt2;
115  const double da_max = max_jerk * dt2;
116 
117  const double da = clamp(dv - dv0, da_min, da_max);
118 
119  v = v0 + dv0 + da;
120  }
121 
122  return tmp != 0.0 ? v / tmp : 1.0;
123 }
124 }
double limit_jerk(double &v, double v0, double v1, double dt)
Limit the jerk.
double limit_velocity(double &v)
Limit the velocity.
SpeedLimiter(bool has_velocity_limits=false, bool has_acceleration_limits=false, bool has_deceleration_limits=false, bool has_jerk_limits=false, double min_velocity=0.0, double max_velocity=0.0, double min_acceleration=0.0, double max_acceleration=0.0, double min_deceleration=0.0, double max_deceleration=0.0, double min_jerk=0.0, double max_jerk=0.0)
Constructor.
double limit_acceleration(double &v, double v0, double dt)
Limit the acceleration.
double limit(double &v, double v0, double v1, double dt)
Limit the velocity and acceleration.
T clamp(T x, T min, T max)


ackermann_controller
Author(s): GĂ©rald Lelong
autogenerated on Mon Jun 10 2019 12:44:49