38 #include <moveit_msgs/JointLimits.h> 48 double max_time_change_per_it)
49 : max_iterations_(max_iterations), max_time_change_per_it_(max_time_change_per_it)
57 void printPoint(
const trajectory_msgs::JointTrajectoryPoint& point, std::size_t i)
59 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
" time [%zu]= %f", i,
60 point.time_from_start.toSec());
61 if (point.positions.size() >= 7)
63 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
" pos_ [%zu]= %f %f %f %f %f %f %f", i,
64 point.positions[0], point.positions[1], point.positions[2], point.positions[3], point.positions[4],
65 point.positions[5], point.positions[6]);
67 if (point.velocities.size() >= 7)
69 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
" vel_ [%zu]= %f %f %f %f %f %f %f", i,
70 point.velocities[0], point.velocities[1], point.velocities[2], point.velocities[3],
71 point.velocities[4], point.velocities[5], point.velocities[6]);
73 if (point.accelerations.size() >= 7)
75 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
" acc_ [%zu]= %f %f %f %f %f %f %f", i,
76 point.accelerations[0], point.accelerations[1], point.accelerations[2], point.accelerations[3],
77 point.accelerations[4], point.accelerations[5], point.accelerations[6]);
81 void printStats(
const trajectory_msgs::JointTrajectory&
trajectory,
const std::vector<moveit_msgs::JointLimits>& limits)
83 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
"jointNames= %s %s %s %s %s %s %s",
84 limits[0].joint_name.c_str(), limits[1].joint_name.c_str(), limits[2].joint_name.c_str(),
85 limits[3].joint_name.c_str(), limits[4].joint_name.c_str(), limits[5].joint_name.c_str(),
86 limits[6].joint_name.c_str());
87 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
"maxVelocities= %f %f %f %f %f %f %f",
88 limits[0].max_velocity, limits[1].max_velocity, limits[2].max_velocity, limits[3].max_velocity,
89 limits[4].max_velocity, limits[5].max_velocity, limits[6].max_velocity);
90 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
"maxAccelerations= %f %f %f %f %f %f %f",
91 limits[0].max_acceleration, limits[1].max_acceleration, limits[2].max_acceleration,
92 limits[3].max_acceleration, limits[4].max_acceleration, limits[5].max_acceleration,
93 limits[6].max_acceleration);
95 for (std::size_t i = 0; i < trajectory.points.size(); ++i)
96 printPoint(trajectory.points[i], i);
102 std::vector<double>& time_diff,
103 const double max_velocity_scaling_factor)
const 111 double velocity_scaling_factor = 1.0;
113 if (max_velocity_scaling_factor > 0.0 && max_velocity_scaling_factor <= 1.0)
114 velocity_scaling_factor = max_velocity_scaling_factor;
115 else if (max_velocity_scaling_factor == 0.0)
116 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
117 "A max_velocity_scaling_factor of 0.0 was specified, defaulting to %f instead.",
118 velocity_scaling_factor);
120 ROS_WARN_NAMED(
"trajectory_processing.iterative_time_parameterization",
121 "Invalid max_velocity_scaling_factor %f specified, defaulting to %f instead.",
122 max_velocity_scaling_factor, velocity_scaling_factor);
124 for (
int i = 0; i < num_points - 1; ++i)
126 const robot_state::RobotStatePtr& curr_waypoint = rob_trajectory.
getWayPointPtr(i);
127 const robot_state::RobotStatePtr& next_waypoint = rob_trajectory.
getWayPointPtr(i + 1);
129 for (std::size_t j = 0; j < vars.size(); ++j)
136 const double dq1 = curr_waypoint->getVariablePosition(idx[j]);
137 const double dq2 = next_waypoint->getVariablePosition(idx[j]);
138 const double t_min = std::abs(dq2 - dq1) / v_max;
139 if (t_min > time_diff[i])
140 time_diff[i] = t_min;
149 const double a_max)
const 151 const double mult_factor = 1.01;
152 double v1 = (dq1) / dt1;
153 double v2 = (dq2) / dt2;
154 double a = 2.0 * (v2 - v1) / (dt1 + dt2);
156 while (std::abs(a) > a_max)
160 a = 2.0 * (v2 - v1) / (dt1 + dt2);
168 const double a_max)
const 170 const double mult_factor = 1.01;
171 double v1 = (dq1) / dt1;
172 double v2 = (dq2) / dt2;
173 double a = 2.0 * (v2 - v1) / (dt1 + dt2);
175 while (std::abs(a) > a_max)
179 a = 2.0 * (v2 - v1) / (dt1 + dt2);
193 if (time_diff.empty())
196 double time_sum = 0.0;
198 robot_state::RobotStatePtr prev_waypoint;
199 robot_state::RobotStatePtr curr_waypoint;
200 robot_state::RobotStatePtr next_waypoint;
211 for (
int i = 1; i < num_points; ++i)
220 for (
int i = 0; i < num_points; ++i)
227 if (i < num_points - 1)
230 for (std::size_t j = 0; j < vars.size(); ++j)
241 q1 = next_waypoint->getVariablePosition(idx[j]);
242 q2 = curr_waypoint->getVariablePosition(idx[j]);
245 dt1 = dt2 = time_diff[i];
247 else if (i < num_points - 1)
250 q1 = prev_waypoint->getVariablePosition(idx[j]);
251 q2 = curr_waypoint->getVariablePosition(idx[j]);
252 q3 = next_waypoint->getVariablePosition(idx[j]);
254 dt1 = time_diff[i - 1];
260 q1 = prev_waypoint->getVariablePosition(idx[j]);
261 q2 = curr_waypoint->getVariablePosition(idx[j]);
264 dt1 = dt2 = time_diff[i - 1];
269 bool start_velocity =
false;
270 if (dt1 == 0.0 || dt2 == 0.0)
280 if (curr_waypoint->hasVelocities())
282 start_velocity =
true;
283 v1 = curr_waypoint->getVariableVelocity(idx[j]);
286 v1 = start_velocity ? v1 : (q2 - q1) / dt1;
288 v2 = start_velocity ? v1 : (q3 - q2) / dt2;
289 a = 2.0 * (v2 - v1) / (dt1 + dt2);
292 curr_waypoint->setVariableVelocity(idx[j], (v2 + v1) / 2.0);
293 curr_waypoint->setVariableAcceleration(idx[j], a);
302 const double max_acceleration_scaling_factor)
const 304 robot_state::RobotStatePtr prev_waypoint;
305 robot_state::RobotStatePtr curr_waypoint;
306 robot_state::RobotStatePtr next_waypoint;
317 bool backwards =
false;
327 double acceleration_scaling_factor = 1.0;
329 if (max_acceleration_scaling_factor > 0.0 && max_acceleration_scaling_factor <= 1.0)
330 acceleration_scaling_factor = max_acceleration_scaling_factor;
331 else if (max_acceleration_scaling_factor == 0.0)
332 ROS_DEBUG_NAMED(
"trajectory_processing.iterative_time_parameterization",
333 "A max_acceleration_scaling_factor of 0.0 was specified, defaulting to %f instead.",
334 acceleration_scaling_factor);
336 ROS_WARN_NAMED(
"trajectory_processing.iterative_time_parameterization",
337 "Invalid max_acceleration_scaling_factor %f specified, defaulting to %f instead.",
338 max_acceleration_scaling_factor, acceleration_scaling_factor);
347 for (
unsigned int j = 0; j < num_joints; ++j)
350 for (
int count = 0; count < 2; ++count)
352 for (
int i = 0; i < num_points - 1; ++i)
354 int index = backwards ? (num_points - 1) - i : i;
361 if (index < num_points - 1)
374 q1 = next_waypoint->getVariablePosition(idx[j]);
375 q2 = curr_waypoint->getVariablePosition(idx[j]);
376 q3 = next_waypoint->getVariablePosition(idx[j]);
378 dt1 = dt2 = time_diff[index];
381 else if (index < num_points - 1)
384 q1 = prev_waypoint->getVariablePosition(idx[j]);
385 q2 = curr_waypoint->getVariablePosition(idx[j]);
386 q3 = next_waypoint->getVariablePosition(idx[j]);
388 dt1 = time_diff[index - 1];
389 dt2 = time_diff[index];
394 q1 = prev_waypoint->getVariablePosition(idx[j]);
395 q2 = curr_waypoint->getVariablePosition(idx[j]);
396 q3 = prev_waypoint->getVariablePosition(idx[j]);
398 dt1 = dt2 = time_diff[index - 1];
402 if (dt1 == 0.0 || dt2 == 0.0)
410 bool start_velocity =
false;
413 if (curr_waypoint->hasVelocities())
415 start_velocity =
true;
416 v1 = curr_waypoint->getVariableVelocity(idx[j]);
419 v1 = start_velocity ? v1 : (q2 - q1) / dt1;
420 v2 = (q3 - q2) / dt2;
421 a = 2.0 * (v2 - v1) / (dt1 + dt2);
429 time_diff[index] = dt2;
434 time_diff[index - 1] = dt1;
438 if (dt1 == 0.0 || dt2 == 0.0)
446 v1 = (q2 - q1) / dt1;
447 v2 = (q3 - q2) / dt2;
448 a = 2 * (v2 - v1) / (dt1 + dt2);
452 backwards = !backwards;
457 }
while (num_updates > 0 && iteration < static_cast<int>(
max_iterations_));
461 const double max_velocity_scaling_factor,
462 const double max_acceleration_scaling_factor)
const 464 if (trajectory.
empty())
470 ROS_ERROR_NAMED(
"trajectory_processing.iterative_time_parameterization",
"It looks like the planner did not set " 471 "the group the plan was computed for");
479 std::vector<double> time_diff(num_points - 1, 0.0);
484 updateTrajectory(trajectory, time_diff);
double findT2(const double d1, const double d2, const double t1, double t2, const double a_max) const
const RobotModel & getParentModel() const
Get the kinematic model this group is part of.
void applyVelocityConstraints(robot_trajectory::RobotTrajectory &rob_trajectory, std::vector< double > &time_diff, const double max_velocity_scaling_factor) const
maximum allowed time change per iteration in seconds
robot_state::RobotStatePtr & getWayPointPtr(std::size_t index)
const std::vector< std::string > & getVariableNames() const
Get the names of the variables that make up the joints included in this group. The number of returned...
#define ROS_WARN_NAMED(name,...)
const VariableBounds & getVariableBounds(const std::string &variable) const
Get the bounds for a specific variable. Throw an exception of variable is not found.
static const double ROUNDING_THRESHOLD
double max_time_change_per_it_
maximum number of iterations to find solution
Definition of a kinematic model. This class is not thread safe, however multiple instances can be cre...
unsigned int getVariableCount() const
Get the number of variables that describe this joint group. This includes variables necessary for mim...
#define ROS_DEBUG_NAMED(name,...)
const robot_model::JointModelGroup * getGroup() const
double findT1(const double d1, const double d2, double t1, const double t2, const double a_max) const
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")
Maintain a sequence of waypoints and the time durations between these waypoints.
static const double DEFAULT_VEL_MAX
void setWayPointDurationFromPrevious(std::size_t index, double value)
unsigned int max_iterations_
std::size_t getWayPointCount() const
const std::vector< int > & getVariableIndexList() const
Get the index locations in the complete robot state for all the variables in this group...
void applyAccelerationConstraints(robot_trajectory::RobotTrajectory &rob_trajectory, std::vector< double > &time_diff, const double max_acceleration_scaling_factor) const
bool computeTimeStamps(robot_trajectory::RobotTrajectory &trajectory, const double max_velocity_scaling_factor=1.0, const double max_acceleration_scaling_factor=1.0) const
#define ROS_ERROR_NAMED(name,...)
IterativeParabolicTimeParameterization(unsigned int max_iterations=100, double max_time_change_per_it=.01)
~IterativeParabolicTimeParameterization()
static const double DEFAULT_ACCEL_MAX
bool acceleration_bounded_
moveit::core::RobotModelConstPtr rmodel