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