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 <algorithm>
41 #include <memory>
42 #include <string>
43 #include <vector>
44 
46 
47 namespace dwb_plugins
48 {
49 
51 {
52  kinematics_ = std::make_shared<KinematicParameters>();
53  kinematics_->initialize(nh);
55 
56  nh.param("sim_time", sim_time_, 1.7);
57  checkUseDwaParam(nh);
58 
59  nh.param("include_last_point", include_last_point_, true);
60 
61  /*
62  * If discretize_by_time, then sim_granularity represents the amount of time that should be between
63  * two successive points on the trajectory.
64  *
65  * If discretize_by_time is false, then sim_granularity is the maximum amount of distance between
66  * two successive points on the trajectory, and angular_sim_granularity is the maximum amount of
67  * angular distance between two successive points.
68  */
69  nh.param("discretize_by_time", discretize_by_time_, false);
71  {
72  time_granularity_ = loadParameterWithDeprecation(nh, "time_granularity", "sim_granularity", 0.025);
73  }
74  else
75  {
76  linear_granularity_ = loadParameterWithDeprecation(nh, "linear_granularity", "sim_granularity", 0.025);
77  angular_granularity_ = loadParameterWithDeprecation(nh, "angular_granularity", "angular_sim_granularity", 0.1);
78  }
79 }
80 
82 {
83  velocity_iterator_ = std::make_shared<XYThetaIterator>();
84  velocity_iterator_->initialize(nh, kinematics_);
85 }
86 
88 {
89  bool use_dwa;
90  nh.param("use_dwa", use_dwa, false);
91  if (use_dwa)
92  {
93  throw nav_core2::PlannerException("Deprecated parameter use_dwa set to true. "
94  "Please use LimitedAccelGenerator for that functionality.");
95  }
96 }
97 
98 void StandardTrajectoryGenerator::startNewIteration(const nav_2d_msgs::Twist2D& current_velocity)
99 {
100  velocity_iterator_->startNewIteration(current_velocity, sim_time_);
101 }
102 
104 {
105  return velocity_iterator_->hasMoreTwists();
106 }
107 
109 {
110  return velocity_iterator_->nextTwist();
111 }
112 
113 std::vector<double> StandardTrajectoryGenerator::getTimeSteps(const nav_2d_msgs::Twist2D& cmd_vel)
114 {
115  std::vector<double> steps;
117  {
118  steps.resize(ceil(sim_time_ / time_granularity_));
119  }
120  else // discretize by distance
121  {
122  double vmag = hypot(cmd_vel.x, cmd_vel.y);
123 
124  // the distance the robot would travel in sim_time if it did not change velocity
125  double projected_linear_distance = vmag * sim_time_;
126 
127  // the angle the robot would rotate in sim_time
128  double projected_angular_distance = fabs(cmd_vel.theta) * sim_time_;
129 
130  // Pick the maximum of the two
131  int num_steps = ceil(std::max(projected_linear_distance / linear_granularity_,
132  projected_angular_distance / angular_granularity_));
133  steps.resize(num_steps);
134  }
135  if (steps.size() == 0)
136  {
137  steps.resize(1);
138  }
139  std::fill(steps.begin(), steps.end(), sim_time_ / steps.size());
140  return steps;
141 }
142 
143 dwb_msgs::Trajectory2D StandardTrajectoryGenerator::generateTrajectory(const geometry_msgs::Pose2D& start_pose,
144  const nav_2d_msgs::Twist2D& start_vel,
145  const nav_2d_msgs::Twist2D& cmd_vel)
146 {
147  dwb_msgs::Trajectory2D traj;
148  traj.velocity = cmd_vel;
149 
150  // simulate the trajectory
151  geometry_msgs::Pose2D pose = start_pose;
152  nav_2d_msgs::Twist2D vel = start_vel;
153  double running_time = 0.0;
154  std::vector<double> steps = getTimeSteps(cmd_vel);
155  for (double dt : steps)
156  {
157  traj.poses.push_back(pose);
158  traj.time_offsets.push_back(ros::Duration(running_time));
159  // calculate velocities
160  vel = computeNewVelocity(cmd_vel, vel, dt);
161 
162  // update the position of the robot using the velocities passed in
163  pose = computeNewPosition(pose, vel, dt);
164  running_time += dt;
165  } // end for simulation steps
166 
168  {
169  traj.poses.push_back(pose);
170  traj.time_offsets.push_back(ros::Duration(running_time));
171  }
172 
173  return traj;
174 }
175 
179 nav_2d_msgs::Twist2D StandardTrajectoryGenerator::computeNewVelocity(const nav_2d_msgs::Twist2D& cmd_vel,
180  const nav_2d_msgs::Twist2D& start_vel, const double dt)
181 {
182  nav_2d_msgs::Twist2D new_vel;
183  new_vel.x = projectVelocity(start_vel.x, kinematics_->getAccX(), kinematics_->getDecelX(), dt, cmd_vel.x);
184  new_vel.y = projectVelocity(start_vel.y, kinematics_->getAccY(), kinematics_->getDecelY(), dt, cmd_vel.y);
185  new_vel.theta = projectVelocity(start_vel.theta, kinematics_->getAccTheta(), kinematics_->getDecelTheta(),
186  dt, cmd_vel.theta);
187  return new_vel;
188 }
189 
190 geometry_msgs::Pose2D StandardTrajectoryGenerator::computeNewPosition(const geometry_msgs::Pose2D start_pose,
191  const nav_2d_msgs::Twist2D& vel, const double dt)
192 {
193  geometry_msgs::Pose2D new_pose;
194  new_pose.x = start_pose.x + (vel.x * cos(start_pose.theta) + vel.y * cos(M_PI_2 + start_pose.theta)) * dt;
195  new_pose.y = start_pose.y + (vel.x * sin(start_pose.theta) + vel.y * sin(M_PI_2 + start_pose.theta)) * dt;
196  new_pose.theta = start_pose.theta + vel.theta * dt;
197  return new_pose;
198 }
199 
200 } // namespace dwb_plugins
201 
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 Sun Jan 10 2021 04:08:37