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