Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
standard_traj_generator.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/standard_traj_generator.hpp"
36 #include <string>
37 #include <vector>
38 #include <algorithm>
39 #include <memory>
40 #include "dwb_plugins/xy_theta_iterator.hpp"
41 #include "pluginlib/class_list_macros.hpp"
42 #include "dwb_core/exceptions.hpp"
43 #include "tf2/utils.hpp"
44 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
45 
46 namespace dwb_plugins
47 {
48 
50  const nav2::LifecycleNode::SharedPtr & nh,
51  const std::string & plugin_name)
52 {
53  plugin_name_ = plugin_name;
54  kinematics_handler_ = std::make_shared<KinematicsHandler>();
55  kinematics_handler_->initialize(nh, plugin_name_);
57 
58  /*
59  * If discretize_by_time, then sim_granularity represents the amount of time that should be between
60  * two successive points on the trajectory.
61  *
62  * If discretize_by_time is false, then sim_granularity is the maximum amount of distance between
63  * two successive points on the trajectory, and angular_sim_granularity is the maximum amount of
64  * angular distance between two successive points.
65  */
66  sim_time_ = nh->declare_or_get_parameter(
67  plugin_name + ".sim_time", 1.7);
68  discretize_by_time_ = nh->declare_or_get_parameter(
69  plugin_name + ".discretize_by_time", false);
70  time_granularity_ = nh->declare_or_get_parameter(
71  plugin_name + ".time_granularity", 0.5);
72  linear_granularity_ = nh->declare_or_get_parameter(
73  plugin_name + ".linear_granularity", 0.5);
74  angular_granularity_ = nh->declare_or_get_parameter(
75  plugin_name + ".angular_granularity", 0.025);
76  limit_vel_cmd_in_traj_ = nh->declare_or_get_parameter(
77  plugin_name + ".limit_vel_cmd_in_traj", false);
78 }
79 
81 {
82  kinematics_handler_->activate();
83 }
84 
86 {
87  kinematics_handler_->deactivate();
88 }
89 
91  const nav2::LifecycleNode::SharedPtr & nh)
92 {
93  velocity_iterator_ = std::make_shared<XYThetaIterator>();
94  velocity_iterator_->initialize(nh, kinematics_handler_, plugin_name_);
95 }
96 
98  const nav_2d_msgs::msg::Twist2D & current_velocity)
99 {
100  velocity_iterator_->startNewIteration(current_velocity, sim_time_);
101 }
102 
104 {
105  return velocity_iterator_->hasMoreTwists();
106 }
107 
108 nav_2d_msgs::msg::Twist2D StandardTrajectoryGenerator::nextTwist()
109 {
110  return velocity_iterator_->nextTwist();
111 }
112 
114  const nav_2d_msgs::msg::Twist2D & cmd_vel)
115 {
116  std::vector<double> steps;
117  if (discretize_by_time_) {
118  steps.resize(ceil(sim_time_ / time_granularity_));
119  } else { // discretize by distance
120  double vmag = hypot(cmd_vel.x, cmd_vel.y);
121 
122  // the distance the robot would travel in sim_time if it did not change velocity
123  double projected_linear_distance = vmag * sim_time_;
124 
125  // the angle the robot would rotate in sim_time
126  double projected_angular_distance = fabs(cmd_vel.theta) * sim_time_;
127 
128  // Pick the maximum of the two
129  int num_steps = ceil(
130  std::max(
131  projected_linear_distance / linear_granularity_,
132  projected_angular_distance / angular_granularity_));
133  steps.resize(num_steps);
134  }
135  if (steps.size() == 0) {
136  steps.resize(1);
137  }
138  std::fill(steps.begin(), steps.end(), sim_time_ / steps.size());
139  return steps;
140 }
141 
143  const geometry_msgs::msg::Pose & start_pose,
144  const nav_2d_msgs::msg::Twist2D & start_vel,
145  const nav_2d_msgs::msg::Twist2D & cmd_vel)
146 {
147  dwb_msgs::msg::Trajectory2D traj;
148  traj.velocity = cmd_vel;
149  // simulate the trajectory
150  geometry_msgs::msg::Pose pose = start_pose;
151  nav_2d_msgs::msg::Twist2D vel = start_vel;
152  double running_time = 0.0;
153  std::vector<double> steps = getTimeSteps(cmd_vel);
154  bool first_vel = false;
155  for (double dt : steps) {
156  traj.poses.push_back(pose);
157  traj.time_offsets.push_back(rclcpp::Duration::from_seconds(running_time));
158 
159  // calculate velocities
160  vel = computeNewVelocity(cmd_vel, vel, dt);
161  if (!first_vel && limit_vel_cmd_in_traj_) {
162  traj.velocity = vel;
163  first_vel = true;
164  }
165 
166  // update the position of the robot using the velocities passed in
167  pose = computeNewPosition(pose, vel, dt);
168  running_time += dt;
169  } // end for simulation steps
170 
171  traj.poses.push_back(pose);
172  traj.time_offsets.push_back(rclcpp::Duration::from_seconds(running_time));
173 
174  return traj;
175 }
176 
181  const nav_2d_msgs::msg::Twist2D & cmd_vel,
182  const nav_2d_msgs::msg::Twist2D & start_vel, const double dt)
183 {
184  KinematicParameters kinematics = kinematics_handler_->getKinematics();
185  nav_2d_msgs::msg::Twist2D new_vel;
186  new_vel.x = projectVelocity(
187  start_vel.x, kinematics.getAccX(),
188  kinematics.getDecelX(), dt, cmd_vel.x);
189  new_vel.y = projectVelocity(
190  start_vel.y, kinematics.getAccY(),
191  kinematics.getDecelY(), dt, cmd_vel.y);
192  new_vel.theta = projectVelocity(
193  start_vel.theta,
194  kinematics.getAccTheta(), kinematics.getDecelTheta(),
195  dt, cmd_vel.theta);
196  return new_vel;
197 }
198 
200  const geometry_msgs::msg::Pose start_pose,
201  const nav_2d_msgs::msg::Twist2D & vel, const double dt)
202 {
203  geometry_msgs::msg::Pose new_pose;
204 
205  double theta = tf2::getYaw(start_pose.orientation);
206  new_pose.position.x = start_pose.position.x +
207  (vel.x * cos(theta) + vel.y * cos(M_PI_2 + theta)) * dt;
208  new_pose.position.y = start_pose.position.y +
209  (vel.x * sin(theta) + vel.y * sin(M_PI_2 + theta)) * dt;
210 
211  double new_theta = theta + vel.theta * dt;
212  tf2::Quaternion q;
213  q.setRPY(0.0, 0.0, new_theta);
214  new_pose.orientation = tf2::toMsg(q);
215 
216  return new_pose;
217 }
218 
219 } // namespace dwb_plugins
220 
221 PLUGINLIB_EXPORT_CLASS(
Interface for iterating through possible velocities and creating trajectories.
Standard DWA-like trajectory generator.
void startNewIteration(const nav_2d_msgs::msg::Twist2D &current_velocity) override
Start a new iteration based on the current velocity.
double angular_granularity_
If not discretizing by time, the amount of angular space between points.
virtual nav_2d_msgs::msg::Twist2D computeNewVelocity(const nav_2d_msgs::msg::Twist2D &cmd_vel, const nav_2d_msgs::msg::Twist2D &start_vel, const double dt)
Calculate the velocity after a set period of time, given the desired velocity and acceleration limits...
std::string plugin_name_
the name of the overlying plugin ID
bool hasMoreTwists() override
Test to see whether there are more twists to test.
virtual std::vector< double > getTimeSteps(const nav_2d_msgs::msg::Twist2D &cmd_vel)
Compute an array of time deltas between the points in the generated trajectory.
bool limit_vel_cmd_in_traj_
Option to limit velocity in the trajectory generator by using current velocity.
void activate() override
Registers callbacks for dynamic parameter handling.
double linear_granularity_
If not discretizing by time, the amount of linear space between points.
virtual geometry_msgs::msg::Pose computeNewPosition(const geometry_msgs::msg::Pose start_pose, const nav_2d_msgs::msg::Twist2D &vel, const double dt)
Use the robot's kinematic model to predict new positions for the robot.
double time_granularity_
If discretizing by time, the amount of time between each point in the traj.
void initialize(const nav2::LifecycleNode::SharedPtr &nh, const std::string &plugin_name) override
Initialize parameters as needed.
virtual void initializeIterator(const nav2::LifecycleNode::SharedPtr &nh)
Initialize the VelocityIterator pointer. Put in its own function for easy overriding.
dwb_msgs::msg::Trajectory2D generateTrajectory(const geometry_msgs::msg::Pose &start_pose, const nav_2d_msgs::msg::Twist2D &start_vel, const nav_2d_msgs::msg::Twist2D &cmd_vel) override
Given a cmd_vel in the robot's frame and initial conditions, generate a Trajectory2D.
void deactivate() override
Resets callbacks for dynamic parameter handling.
nav_2d_msgs::msg::Twist2D nextTwist() override
Return the next twist and advance the iteration.
A struct containing one representation of the robot's kinematics.