Nav2 Navigation Stack - humble  humble
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 #define EPSILON 1E-5
45 
46 namespace dwb_plugins
47 {
48 void XYThetaIterator::initialize(
49  const nav2_util::LifecycleNode::SharedPtr & nh,
50  KinematicsHandler::Ptr kinematics,
51  const std::string & plugin_name)
52 {
53  kinematics_handler_ = kinematics;
54 
55  nav2_util::declare_parameter_if_not_declared(
56  nh,
57  plugin_name + ".vx_samples", rclcpp::ParameterValue(20));
58  nav2_util::declare_parameter_if_not_declared(
59  nh,
60  plugin_name + ".vy_samples", rclcpp::ParameterValue(5));
61  nav2_util::declare_parameter_if_not_declared(
62  nh,
63  plugin_name + ".vtheta_samples", rclcpp::ParameterValue(20));
64 
65  nh->get_parameter(plugin_name + ".vx_samples", vx_samples_);
66  nh->get_parameter(plugin_name + ".vy_samples", vy_samples_);
67  nh->get_parameter(plugin_name + ".vtheta_samples", vtheta_samples_);
68 }
69 
70 void XYThetaIterator::startNewIteration(
71  const nav_2d_msgs::msg::Twist2D & current_velocity,
72  double dt)
73 {
74  KinematicParameters kinematics = kinematics_handler_->getKinematics();
75  x_it_ = std::make_shared<OneDVelocityIterator>(
76  current_velocity.x,
77  kinematics.getMinX(), kinematics.getMaxX(),
78  kinematics.getAccX(), kinematics.getDecelX(),
79  dt, vx_samples_);
80  y_it_ = std::make_shared<OneDVelocityIterator>(
81  current_velocity.y,
82  kinematics.getMinY(), kinematics.getMaxY(),
83  kinematics.getAccY(), kinematics.getDecelY(),
84  dt, vy_samples_);
85  th_it_ = std::make_shared<OneDVelocityIterator>(
86  current_velocity.theta,
87  kinematics.getMinTheta(), kinematics.getMaxTheta(),
88  kinematics.getAccTheta(), kinematics.getDecelTheta(),
89  dt, vtheta_samples_);
90  if (!isValidVelocity()) {
91  iterateToValidVelocity();
92  }
93 }
94 
95 bool XYThetaIterator::isValidSpeed(double x, double y, double theta)
96 {
97  KinematicParameters kinematics = kinematics_handler_->getKinematics();
98  double vmag_sq = x * x + y * y;
99  if (kinematics.getMaxSpeedXY() >= 0.0 && vmag_sq > kinematics.getMaxSpeedXY_SQ() + EPSILON) {
100  return false;
101  }
102  if (kinematics.getMinSpeedXY() >= 0.0 && vmag_sq + EPSILON < kinematics.getMinSpeedXY_SQ() &&
103  kinematics.getMinSpeedTheta() >= 0.0 && fabs(theta) + EPSILON < kinematics.getMinSpeedTheta())
104  {
105  return false;
106  }
107  if (vmag_sq == 0.0 && th_it_->getVelocity() == 0.0) {
108  return false;
109  }
110  return true;
111 }
112 
113 bool XYThetaIterator::isValidVelocity()
114 {
115  return isValidSpeed(
116  x_it_->getVelocity(), y_it_->getVelocity(),
117  th_it_->getVelocity());
118 }
119 
120 bool XYThetaIterator::hasMoreTwists()
121 {
122  return x_it_ && !x_it_->isFinished();
123 }
124 
125 nav_2d_msgs::msg::Twist2D XYThetaIterator::nextTwist()
126 {
127  nav_2d_msgs::msg::Twist2D velocity;
128  velocity.x = x_it_->getVelocity();
129  velocity.y = y_it_->getVelocity();
130  velocity.theta = th_it_->getVelocity();
131 
132  iterateToValidVelocity();
133 
134  return velocity;
135 }
136 
137 void XYThetaIterator::iterateToValidVelocity()
138 {
139  bool valid = false;
140  while (!valid && hasMoreTwists()) {
141  ++(*th_it_);
142  if (th_it_->isFinished()) {
143  th_it_->reset();
144  ++(*y_it_);
145  if (y_it_->isFinished()) {
146  y_it_->reset();
147  ++(*x_it_);
148  }
149  }
150  valid = isValidVelocity();
151  }
152 }
153 
154 } // namespace dwb_plugins
bool isValidSpeed(double x, double y, double theta)
Check to see whether the combined x/y/theta velocities are valid.
A struct containing one representation of the robot's kinematics.