ros2_control - rolling
speed_limiter.hpp
1 // Copyright 2020 PAL Robotics S.L.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /*
16  * Author: Enrique Fernández
17  */
18 
19 #ifndef DIFF_DRIVE_CONTROLLER__SPEED_LIMITER_HPP_
20 #define DIFF_DRIVE_CONTROLLER__SPEED_LIMITER_HPP_
21 
22 #include <limits>
23 
24 #include "control_toolbox/rate_limiter.hpp"
25 
26 namespace diff_drive_controller
27 {
29 {
30 public:
43  [[deprecated]] SpeedLimiter(
44  bool has_velocity_limits = true, bool has_acceleration_limits = true,
45  bool has_jerk_limits = true, double min_velocity = std::numeric_limits<double>::quiet_NaN(),
46  double max_velocity = std::numeric_limits<double>::quiet_NaN(),
47  double max_deceleration = std::numeric_limits<double>::quiet_NaN(),
48  double max_acceleration = std::numeric_limits<double>::quiet_NaN(),
49  double min_jerk = std::numeric_limits<double>::quiet_NaN(),
50  double max_jerk = std::numeric_limits<double>::quiet_NaN())
51  {
52  if (!has_velocity_limits)
53  {
54  min_velocity = max_velocity = std::numeric_limits<double>::quiet_NaN();
55  }
56  if (!has_acceleration_limits)
57  {
58  max_deceleration = max_acceleration = std::numeric_limits<double>::quiet_NaN();
59  }
60  if (!has_jerk_limits)
61  {
62  min_jerk = max_jerk = std::numeric_limits<double>::quiet_NaN();
63  }
64  speed_limiter_ = control_toolbox::RateLimiter<double>(
65  min_velocity, max_velocity, max_deceleration, max_acceleration,
66  std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), min_jerk,
67  max_jerk);
68  }
69 
84  double min_velocity = std::numeric_limits<double>::quiet_NaN(),
85  double max_velocity = std::numeric_limits<double>::quiet_NaN(),
86  double max_acceleration_reverse = std::numeric_limits<double>::quiet_NaN(),
87  double max_acceleration = std::numeric_limits<double>::quiet_NaN(),
88  double max_deceleration = std::numeric_limits<double>::quiet_NaN(),
89  double max_deceleration_reverse = std::numeric_limits<double>::quiet_NaN(),
90  double min_jerk = std::numeric_limits<double>::quiet_NaN(),
91  double max_jerk = std::numeric_limits<double>::quiet_NaN())
92  {
93  speed_limiter_ = control_toolbox::RateLimiter<double>(
94  min_velocity, max_velocity, max_acceleration_reverse, max_acceleration, max_deceleration,
95  max_deceleration_reverse, min_jerk, max_jerk);
96  }
97 
106  double limit(double & v, double v0, double v1, double dt)
107  {
108  return speed_limiter_.limit(v, v0, v1, dt);
109  }
110 
116  double limit_velocity(double & v) { return speed_limiter_.limit_value(v); }
117 
125  double limit_acceleration(double & v, double v0, double dt)
126  {
127  return speed_limiter_.limit_first_derivative(v, v0, dt);
128  }
129 
139  double limit_jerk(double & v, double v0, double v1, double dt)
140  {
141  return speed_limiter_.limit_second_derivative(v, v0, v1, dt);
142  }
143 
144 private:
145  control_toolbox::RateLimiter<double> speed_limiter_; // Instance of the new RateLimiter
146 };
147 
148 } // namespace diff_drive_controller
149 
150 #endif // DIFF_DRIVE_CONTROLLER__SPEED_LIMITER_HPP_
T limit_value(T &v)
Limit the value.
Definition: rate_limiter.hpp:256
T limit_second_derivative(T &v, T v0, T v1, T dt)
Limit the second_derivative.
Definition: rate_limiter.hpp:300
T limit(T &v, T v0, T v1, T dt)
Limit the value and first_derivative.
Definition: rate_limiter.hpp:244
T limit_first_derivative(T &v, T v0, T dt)
Limit the first_derivative.
Definition: rate_limiter.hpp:269
Definition: speed_limiter.hpp:29
double limit_jerk(double &v, double v0, double v1, double dt)
Limit the jerk.
Definition: speed_limiter.hpp:139
double limit(double &v, double v0, double v1, double dt)
Limit the velocity and acceleration.
Definition: speed_limiter.hpp:106
SpeedLimiter(double min_velocity=std::numeric_limits< double >::quiet_NaN(), double max_velocity=std::numeric_limits< double >::quiet_NaN(), double max_acceleration_reverse=std::numeric_limits< double >::quiet_NaN(), double max_acceleration=std::numeric_limits< double >::quiet_NaN(), double max_deceleration=std::numeric_limits< double >::quiet_NaN(), double max_deceleration_reverse=std::numeric_limits< double >::quiet_NaN(), double min_jerk=std::numeric_limits< double >::quiet_NaN(), double max_jerk=std::numeric_limits< double >::quiet_NaN())
Constructor.
Definition: speed_limiter.hpp:83
SpeedLimiter(bool has_velocity_limits=true, bool has_acceleration_limits=true, bool has_jerk_limits=true, double min_velocity=std::numeric_limits< double >::quiet_NaN(), double max_velocity=std::numeric_limits< double >::quiet_NaN(), double max_deceleration=std::numeric_limits< double >::quiet_NaN(), double max_acceleration=std::numeric_limits< double >::quiet_NaN(), double min_jerk=std::numeric_limits< double >::quiet_NaN(), double max_jerk=std::numeric_limits< double >::quiet_NaN())
Constructor.
Definition: speed_limiter.hpp:43
double limit_acceleration(double &v, double v0, double dt)
Limit the acceleration.
Definition: speed_limiter.hpp:125
double limit_velocity(double &v)
Limit the velocity.
Definition: speed_limiter.hpp:116