speed_limiter.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, PAL Robotics, S.L.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the PAL Robotics nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /*
36  * Author: Enrique Fernández
37  */
38 
39 #include <algorithm>
40 
42 
43 template<typename T>
44 T clamp(T x, T min, T max)
45 {
46  return std::min(std::max(min, x), max);
47 }
48 
49 namespace diff_drive_controller
50 {
51 
53  bool has_velocity_limits,
54  bool has_acceleration_limits,
55  bool has_jerk_limits,
56  double min_velocity,
57  double max_velocity,
58  double min_acceleration,
59  double max_acceleration,
60  double min_jerk,
61  double max_jerk
62  )
63  : has_velocity_limits(has_velocity_limits)
64  , has_acceleration_limits(has_acceleration_limits)
65  , has_jerk_limits(has_jerk_limits)
66  , min_velocity(min_velocity)
67  , max_velocity(max_velocity)
68  , min_acceleration(min_acceleration)
69  , max_acceleration(max_acceleration)
70  , min_jerk(min_jerk)
71  , max_jerk(max_jerk)
72  {
73  }
74 
75  double SpeedLimiter::limit(double& v, double v0, double v1, double dt)
76  {
77  const double tmp = v;
78 
79  limit_jerk(v, v0, v1, dt);
80  limit_acceleration(v, v0, dt);
81  limit_velocity(v);
82 
83  return tmp != 0.0 ? v / tmp : 1.0;
84  }
85 
86  double SpeedLimiter::limit_velocity(double& v)
87  {
88  const double tmp = v;
89 
91  {
93  }
94 
95  return tmp != 0.0 ? v / tmp : 1.0;
96  }
97 
98  double SpeedLimiter::limit_acceleration(double& v, double v0, double dt)
99  {
100  const double tmp = v;
101 
103  {
104  const double dv_min = min_acceleration * dt;
105  const double dv_max = max_acceleration * dt;
106 
107  const double dv = clamp(v - v0, dv_min, dv_max);
108 
109  v = v0 + dv;
110  }
111 
112  return tmp != 0.0 ? v / tmp : 1.0;
113  }
114 
115  double SpeedLimiter::limit_jerk(double& v, double v0, double v1, double dt)
116  {
117  const double tmp = v;
118 
119  if (has_jerk_limits)
120  {
121  const double dv = v - v0;
122  const double dv0 = v0 - v1;
123 
124  const double dt2 = 2. * dt * dt;
125 
126  const double da_min = min_jerk * dt2;
127  const double da_max = max_jerk * dt2;
128 
129  const double da = clamp(dv - dv0, da_min, da_max);
130 
131  v = v0 + dv0 + da;
132  }
133 
134  return tmp != 0.0 ? v / tmp : 1.0;
135  }
136 
137 } // namespace diff_drive_controller
SpeedLimiter(bool has_velocity_limits=false, bool has_acceleration_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_jerk=0.0, double max_jerk=0.0)
Constructor.
double limit_acceleration(double &v, double v0, double dt)
Limit the acceleration.
T clamp(T x, T min, T max)
double limit_velocity(double &v)
Limit the velocity.
double limit_jerk(double &v, double v0, double v1, double dt)
Limit the jerk.
double limit(double &v, double v0, double v1, double dt)
Limit the velocity and acceleration.


diff_drive_controller
Author(s): Bence Magyar
autogenerated on Sat Apr 18 2020 03:58:05