one_d_velocity_iterator.h
Go to the documentation of this file.
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_H
36 #define DWB_PLUGINS_ONE_D_VELOCITY_ITERATOR_H
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 magnitude;
59  if (fabs(v0) < EPSILON)
60  {
61  // Starting from standstill, always accelerate
62  if (target >= 0.0)
63  {
64  magnitude = fabs(accel);
65  }
66  else
67  {
68  magnitude = -fabs(accel);
69  }
70  }
71  else if (v0 > 0.0)
72  {
73  if (v0 < target)
74  {
75  // Acceleration (speed magnitude increases)
76  magnitude = fabs(accel);
77  }
78  else
79  {
80  // Deceleration (speed magnitude decreases)
81  magnitude = -fabs(decel);
82  }
83  }
84  else
85  {
86  if (v0 < target)
87  {
88  // Deceleration (speed magnitude decreases)
89  magnitude = fabs(decel);
90  }
91  else
92  {
93  // Acceleration (speed magnitude increases)
94  magnitude = -fabs(accel);
95  }
96  }
97 
98  double v1 = v0 + magnitude * dt;
99  if (magnitude > 0.0)
100  {
101  return std::min(target, v1);
102  }
103  else
104  {
105  return std::max(target, v1);
106  }
107 }
108 
124 {
125 public:
136  OneDVelocityIterator(double current, double min, double max, double acc_limit, double decel_limit, double acc_time,
137  int num_samples)
138  {
139  if (current < min)
140  {
141  current = min;
142  }
143  else if (current > max)
144  {
145  current = max;
146  }
147  max_vel_ = projectVelocity(current, acc_limit, decel_limit, acc_time, max);
148  min_vel_ = projectVelocity(current, acc_limit, decel_limit, acc_time, min);
149  reset();
150 
151  if (fabs(min_vel_ - max_vel_) < EPSILON)
152  {
153  increment_ = 1.0;
154  return;
155  }
156  num_samples = std::max(2, num_samples);
157 
158  // e.g. for 4 samples, split distance in 3 even parts
159  increment_ = (max_vel_ - min_vel_) / std::max(1, (num_samples - 1));
160  }
161 
165  double getVelocity() const
166  {
167  if (return_zero_now_) return 0.0;
168  return current_;
169  }
170 
175  {
176  if (return_zero_ && current_ < 0.0 && current_ + increment_ > 0.0 && current_ + increment_ <= max_vel_ + EPSILON)
177  {
178  return_zero_now_ = true;
179  return_zero_ = false;
180  }
181  else
182  {
183  current_ += increment_;
184  return_zero_now_ = false;
185  }
186  return *this;
187  }
188 
192  void reset()
193  {
194  current_ = min_vel_;
195  return_zero_ = true;
196  return_zero_now_ = false;
197  }
198 
202  bool isFinished() const
203  {
204  return current_ > max_vel_ + EPSILON;
205  }
206 
207 private:
210  double current_;
211  double increment_;
212 };
213 } // namespace dwb_plugins
214 
215 #endif // DWB_PLUGINS_ONE_D_VELOCITY_ITERATOR_H
dwb_plugins::OneDVelocityIterator::reset
void reset()
Reset back to the first velocity.
Definition: one_d_velocity_iterator.h:192
dwb_plugins::OneDVelocityIterator::getVelocity
double getVelocity() const
Get the next velocity available.
Definition: one_d_velocity_iterator.h:165
min
int min(int a, int b)
dwb_plugins::OneDVelocityIterator
An iterator for generating a number of samples in a range.
Definition: one_d_velocity_iterator.h:123
dwb_plugins::OneDVelocityIterator::min_vel_
double min_vel_
Definition: one_d_velocity_iterator.h:209
dwb_plugins::OneDVelocityIterator::OneDVelocityIterator
OneDVelocityIterator(double current, double min, double max, double acc_limit, double decel_limit, double acc_time, int num_samples)
Constructor for the velocity iterator.
Definition: one_d_velocity_iterator.h:136
dwb_plugins::OneDVelocityIterator::return_zero_
bool return_zero_
Definition: one_d_velocity_iterator.h:208
dwb_plugins::OneDVelocityIterator::current_
double current_
Definition: one_d_velocity_iterator.h:210
dwb_plugins::EPSILON
const double EPSILON
Definition: one_d_velocity_iterator.h:44
dwb_plugins
Definition: kinematic_parameters.h:43
dwb_plugins::OneDVelocityIterator::isFinished
bool isFinished() const
Definition: one_d_velocity_iterator.h:202
dwb_plugins::OneDVelocityIterator::increment_
double increment_
Definition: one_d_velocity_iterator.h:211
dwb_plugins::OneDVelocityIterator::return_zero_now_
bool return_zero_now_
Definition: one_d_velocity_iterator.h:208
dwb_plugins::projectVelocity
double projectVelocity(double v0, double accel, double decel, double dt, double target)
Given initial conditions and a time, figure out the end velocity.
Definition: one_d_velocity_iterator.h:56
dwb_plugins::OneDVelocityIterator::max_vel_
double max_vel_
Definition: one_d_velocity_iterator.h:209
dwb_plugins::OneDVelocityIterator::operator++
OneDVelocityIterator & operator++()
Increment the iterator.
Definition: one_d_velocity_iterator.h:174


dwb_plugins
Author(s):
autogenerated on Sun May 18 2025 02:47:27