standard_traj_generator.cpp
Go to the documentation of this file.
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 
39 #include <nav_core2/exceptions.h>
40 #include <string>
41 #include <vector>
42 #include <algorithm>
43 
45 
46 namespace dwb_plugins
47 {
48 
50 {
51  kinematics_ = std::make_shared<KinematicParameters>();
52  kinematics_->initialize(nh);
54 
55  nh.param("sim_time", sim_time_, 1.7);
56  checkUseDwaParam(nh);
57 
58  nh.param("include_last_point", include_last_point_, true);
59 
60  /*
61  * If discretize_by_time, then sim_granularity represents the amount of time that should be between
62  * two successive points on the trajectory.
63  *
64  * If discretize_by_time is false, then sim_granularity is the maximum amount of distance between
65  * two successive points on the trajectory, and angular_sim_granularity is the maximum amount of
66  * angular distance between two successive points.
67  */
68  nh.param("discretize_by_time", discretize_by_time_, false);
70  {
71  time_granularity_ = loadParameterWithDeprecation(nh, "time_granularity", "sim_granularity", 0.025);
72  }
73  else
74  {
75  linear_granularity_ = loadParameterWithDeprecation(nh, "linear_granularity", "sim_granularity", 0.025);
76  angular_granularity_ = loadParameterWithDeprecation(nh, "angular_granularity", "angular_sim_granularity", 0.1);
77  }
78 }
79 
81 {
82  velocity_iterator_ = std::make_shared<XYThetaIterator>();
83  velocity_iterator_->initialize(nh, kinematics_);
84 }
85 
87 {
88  bool use_dwa;
89  nh.param("use_dwa", use_dwa, false);
90  if (use_dwa)
91  {
92  throw nav_core2::PlannerException("Deprecated parameter use_dwa set to true. "
93  "Please use LimitedAccelGenerator for that functionality.");
94  }
95 }
96 
97 void StandardTrajectoryGenerator::startNewIteration(const nav_2d_msgs::Twist2D& current_velocity)
98 {
99  velocity_iterator_->startNewIteration(current_velocity, sim_time_);
100 }
101 
103 {
104  return velocity_iterator_->hasMoreTwists();
105 }
106 
108 {
109  return velocity_iterator_->nextTwist();
110 }
111 
112 std::vector<double> StandardTrajectoryGenerator::getTimeSteps(const nav_2d_msgs::Twist2D& cmd_vel)
113 {
114  std::vector<double> steps;
116  {
117  steps.resize(ceil(sim_time_ / time_granularity_));
118  }
119  else // discretize by distance
120  {
121  double vmag = hypot(cmd_vel.x, cmd_vel.y);
122 
123  // the distance the robot would travel in sim_time if it did not change velocity
124  double projected_linear_distance = vmag * sim_time_;
125 
126  // the angle the robot would rotate in sim_time
127  double projected_angular_distance = fabs(cmd_vel.theta) * sim_time_;
128 
129  // Pick the maximum of the two
130  int num_steps = ceil(std::max(projected_linear_distance / linear_granularity_,
131  projected_angular_distance / angular_granularity_));
132  steps.resize(num_steps);
133  }
134  if (steps.size() == 0)
135  {
136  steps.resize(1);
137  }
138  std::fill(steps.begin(), steps.end(), sim_time_ / steps.size());
139  return steps;
140 }
141 
142 dwb_msgs::Trajectory2D StandardTrajectoryGenerator::generateTrajectory(const geometry_msgs::Pose2D& start_pose,
143  const nav_2d_msgs::Twist2D& start_vel,
144  const nav_2d_msgs::Twist2D& cmd_vel)
145 {
146  dwb_msgs::Trajectory2D traj;
147  traj.velocity = cmd_vel;
148 
149  // simulate the trajectory
150  geometry_msgs::Pose2D pose = start_pose;
151  nav_2d_msgs::Twist2D vel = start_vel;
152  double running_time = 0.0;
153  std::vector<double> steps = getTimeSteps(cmd_vel);
154  for (double dt : steps)
155  {
156  traj.poses.push_back(pose);
157  traj.time_offsets.push_back(ros::Duration(running_time));
158  // calculate velocities
159  vel = computeNewVelocity(cmd_vel, vel, dt);
160 
161  // update the position of the robot using the velocities passed in
162  pose = computeNewPosition(pose, vel, dt);
163  running_time += dt;
164  } // end for simulation steps
165 
167  {
168  traj.poses.push_back(pose);
169  traj.time_offsets.push_back(ros::Duration(running_time));
170  }
171 
172  return traj;
173 }
174 
178 nav_2d_msgs::Twist2D StandardTrajectoryGenerator::computeNewVelocity(const nav_2d_msgs::Twist2D& cmd_vel,
179  const nav_2d_msgs::Twist2D& start_vel, const double dt)
180 {
181  nav_2d_msgs::Twist2D new_vel;
182  new_vel.x = projectVelocity(start_vel.x, kinematics_->getAccX(), kinematics_->getDecelX(), dt, cmd_vel.x);
183  new_vel.y = projectVelocity(start_vel.y, kinematics_->getAccY(), kinematics_->getDecelY(), dt, cmd_vel.y);
184  new_vel.theta = projectVelocity(start_vel.theta, kinematics_->getAccTheta(), kinematics_->getDecelTheta(),
185  dt, cmd_vel.theta);
186  return new_vel;
187 }
188 
189 geometry_msgs::Pose2D StandardTrajectoryGenerator::computeNewPosition(const geometry_msgs::Pose2D start_pose,
190  const nav_2d_msgs::Twist2D& vel, const double dt)
191 {
192  geometry_msgs::Pose2D new_pose;
193  new_pose.x = start_pose.x + (vel.x * cos(start_pose.theta) + vel.y * cos(M_PI_2 + start_pose.theta)) * dt;
194  new_pose.y = start_pose.y + (vel.x * sin(start_pose.theta) + vel.y * sin(M_PI_2 + start_pose.theta)) * dt;
195  new_pose.theta = start_pose.theta + vel.theta * dt;
196  return new_pose;
197 }
198 
199 } // namespace dwb_plugins
200 
dwb_msgs::Trajectory2D generateTrajectory(const geometry_msgs::Pose2D &start_pose, const nav_2d_msgs::Twist2D &start_vel, const nav_2d_msgs::Twist2D &cmd_vel) override
virtual nav_2d_msgs::Twist2D computeNewVelocity(const nav_2d_msgs::Twist2D &cmd_vel, const nav_2d_msgs::Twist2D &start_vel, const double dt)
Calculate the velocity after a set period of time, given the desired velocity and acceleration limits...
virtual std::vector< double > getTimeSteps(const nav_2d_msgs::Twist2D &cmd_vel)
Compute an array of time deltas between the points in the generated trajectory.
virtual void initializeIterator(ros::NodeHandle &nh)
Initialize the VelocityIterator pointer. Put in its own function for easy overriding.
void startNewIteration(const nav_2d_msgs::Twist2D &current_velocity) override
Standard DWA-like trajectory generator.
bool param(const std::string &param_name, T &param_val, const T &default_val) const
virtual geometry_msgs::Pose2D computeNewPosition(const geometry_msgs::Pose2D start_pose, const nav_2d_msgs::Twist2D &vel, const double dt)
Use the robot&#39;s kinematic model to predict new positions for the robot.
virtual void checkUseDwaParam(const ros::NodeHandle &nh)
Check if the deprecated use_dwa parameter is set to the functionality that matches this class...
std::shared_ptr< VelocityIterator > velocity_iterator_
double angular_granularity_
If not discretizing by time, the amount of angular space between points.
double linear_granularity_
If not discretizing by time, the amount of linear space between points.
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
double projectVelocity(double v0, double accel, double decel, double dt, double target)
Given initial conditions and a time, figure out the end velocity.
param_t loadParameterWithDeprecation(const ros::NodeHandle &nh, const std::string current_name, const std::string old_name, const param_t &default_value)
void initialize(ros::NodeHandle &nh) override
double time_granularity_
If discretizing by time, the amount of time between each point in the traj.


dwb_plugins
Author(s):
autogenerated on Wed Jun 26 2019 20:06:16