Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
one_d_velocity_iterator.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
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 copyright holder 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 HOLDER 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 #ifndef DWB_PLUGINS__ONE_D_VELOCITY_ITERATOR_HPP_
36 #define DWB_PLUGINS__ONE_D_VELOCITY_ITERATOR_HPP_
37 
38 #include <algorithm>
39 #include <cmath>
40 
41 namespace dwb_plugins
42 {
43 
44 const double EPSILON = 1E-5;
45 
56 inline double projectVelocity(double v0, double accel, double decel, double dt, double target)
57 {
58  double v1;
59  if (v0 < target) {
60  v1 = v0 + accel * dt;
61  return std::min(target, v1);
62  } else {
63  v1 = v0 + decel * dt;
64  return std::max(target, v1);
65  }
66 }
67 
83 {
84 public:
96  double current, double min, double max, double acc_limit, double decel_limit, double acc_time,
97  int num_samples)
98  {
99  if (current < min) {
100  current = min;
101  } else if (current > max) {
102  current = max;
103  }
104  max_vel_ = projectVelocity(current, acc_limit, decel_limit, acc_time, max);
105  min_vel_ = projectVelocity(current, acc_limit, decel_limit, acc_time, min);
106  reset();
107 
108  if (fabs(min_vel_ - max_vel_) < EPSILON) {
109  increment_ = 1.0;
110  return;
111  }
112  num_samples = std::max(2, num_samples);
113 
114  // e.g. for 4 samples, split distance in 3 even parts
115  increment_ = (max_vel_ - min_vel_) / std::max(1, (num_samples - 1));
116  }
117 
121  double getVelocity() const
122  {
123  if (return_zero_now_) {return 0.0;}
124  return current_;
125  }
126 
131  {
132  if (return_zero_ && current_ < 0.0 && current_ + increment_ > 0.0 &&
133  current_ + increment_ <= max_vel_ + EPSILON)
134  {
135  return_zero_now_ = true;
136  return_zero_ = false;
137  } else {
138  current_ += increment_;
139  return_zero_now_ = false;
140  }
141  return *this;
142  }
143 
147  void reset()
148  {
149  current_ = min_vel_;
150  return_zero_ = true;
151  return_zero_now_ = false;
152  }
153 
157  bool isFinished() const
158  {
159  return current_ > max_vel_ + EPSILON;
160  }
161 
162 private:
163  bool return_zero_, return_zero_now_;
164  double min_vel_, max_vel_;
165  double current_;
166  double increment_;
167 };
168 } // namespace dwb_plugins
169 
170 #endif // DWB_PLUGINS__ONE_D_VELOCITY_ITERATOR_HPP_
An iterator for generating a number of samples in a range.
void reset()
Reset back to the first velocity.
OneDVelocityIterator & operator++()
Increment the iterator.
double getVelocity() const
Get the next velocity available.
OneDVelocityIterator(double current, double min, double max, double acc_limit, double decel_limit, double acc_time, int num_samples)
Constructor for the velocity iterator.