Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
xy_theta_iterator.cpp
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 #include "dwb_plugins/xy_theta_iterator.hpp"
36 
37 #include <cmath>
38 #include <memory>
39 #include <string>
40 
41 namespace dwb_plugins
42 {
43 void XYThetaIterator::initialize(
44  const nav2::LifecycleNode::SharedPtr & nh,
45  KinematicsHandler::Ptr kinematics,
46  const std::string & plugin_name)
47 {
48  kinematics_handler_ = kinematics;
49 
50  vx_samples_ = nh->declare_or_get_parameter(
51  plugin_name + ".vx_samples", 20);
52  vy_samples_ = nh->declare_or_get_parameter(
53  plugin_name + ".vy_samples", 5);
54  vtheta_samples_ = nh->declare_or_get_parameter(
55  plugin_name + ".vtheta_samples", 20);
56 }
57 
58 void XYThetaIterator::startNewIteration(
59  const nav_2d_msgs::msg::Twist2D & current_velocity,
60  double dt)
61 {
62  KinematicParameters kinematics = kinematics_handler_->getKinematics();
63  x_it_ = std::make_shared<OneDVelocityIterator>(
64  current_velocity.x,
65  kinematics.getMinX(), kinematics.getMaxX(),
66  kinematics.getAccX(), kinematics.getDecelX(),
67  dt, vx_samples_);
68  y_it_ = std::make_shared<OneDVelocityIterator>(
69  current_velocity.y,
70  kinematics.getMinY(), kinematics.getMaxY(),
71  kinematics.getAccY(), kinematics.getDecelY(),
72  dt, vy_samples_);
73  th_it_ = std::make_shared<OneDVelocityIterator>(
74  current_velocity.theta,
75  kinematics.getMinTheta(), kinematics.getMaxTheta(),
76  kinematics.getAccTheta(), kinematics.getDecelTheta(),
77  dt, vtheta_samples_);
78  if (!isValidVelocity()) {
79  iterateToValidVelocity();
80  }
81 }
82 
83 bool XYThetaIterator::isValidVelocity()
84 {
85  return kinematics_handler_->getKinematics().isValidSpeed(
86  x_it_->getVelocity(), y_it_->getVelocity(), th_it_->getVelocity());
87 }
88 
89 bool XYThetaIterator::hasMoreTwists()
90 {
91  return x_it_ && !x_it_->isFinished();
92 }
93 
94 nav_2d_msgs::msg::Twist2D XYThetaIterator::nextTwist()
95 {
96  nav_2d_msgs::msg::Twist2D velocity;
97  velocity.x = x_it_->getVelocity();
98  velocity.y = y_it_->getVelocity();
99  velocity.theta = th_it_->getVelocity();
100 
101  iterateToValidVelocity();
102 
103  return velocity;
104 }
105 
106 void XYThetaIterator::iterateToValidVelocity()
107 {
108  bool valid = false;
109  while (!valid && hasMoreTwists()) {
110  ++(*th_it_);
111  if (th_it_->isFinished()) {
112  th_it_->reset();
113  ++(*y_it_);
114  if (y_it_->isFinished()) {
115  y_it_->reset();
116  ++(*x_it_);
117  }
118  }
119  valid = isValidVelocity();
120  }
121 }
122 
123 } // namespace dwb_plugins