52 double estimateDeltaT(
const PoseSE2& start,
const PoseSE2& end,
53 double max_vel_x,
double max_vel_theta)
55 double dt_constant_motion = 0.1;
57 double trans_dist = (end.position() - start.position()).norm();
58 dt_constant_motion = trans_dist / max_vel_x;
60 if (max_vel_theta > 0) {
61 double rot_dist = std::abs(g2o::normalize_theta(end.theta() - start.theta()));
62 dt_constant_motion = std::max(dt_constant_motion, rot_dist / max_vel_theta);
64 return dt_constant_motion;
75 ROS_DEBUG(
"Destructor Timed_Elastic_Band...");
103 ROS_ASSERT_MSG(dt > 0.,
"Adding a timediff requires a positive dt");
118 ROS_ERROR(
"Method addPoseAndTimeDiff: Add one single Pose first. Timediff describes the time difference between last conf and given conf");
131 ROS_ERROR(
"Method addPoseAndTimeDiff: Add one single Pose first. Timediff describes the time difference between last conf and given conf");
139 addPose(position, theta,
false);
142 ROS_ERROR(
"Method addPoseAndTimeDiff: Add one single Pose first. Timediff describes the time difference between last conf and given conf");
157 for (
int i = index; i<index+number; ++i)
172 for (
int i = index; i<index+number; ++i)
204 for (PoseSequence::iterator pose_it =
pose_vec_.begin(); pose_it !=
pose_vec_.end(); ++pose_it)
231 bool modified =
true;
233 for (
int rep = 0; rep < 100 && modified; ++rep)
284 if (fast_mode)
break;
295 time += (*dt_it)->dt();
306 for(
int i = 0; i < index; ++i)
332 double timestep = 0.1;
337 double dir_to_goal = std::atan2(point_to_goal[1],point_to_goal[0]);
338 double dx = diststep*std::cos(dir_to_goal);
339 double dy = diststep*std::sin(dir_to_goal);
340 double orient_init = dir_to_goal;
343 orient_init = g2o::normalize_theta(orient_init+M_PI);
346 double dist_to_goal = point_to_goal.norm();
347 double no_steps_d = dist_to_goal/std::abs(diststep);
348 unsigned int no_steps = (
unsigned int) std::floor(no_steps_d);
350 if (max_vel_x > 0) timestep = diststep / max_vel_x;
352 for (
unsigned int i=1; i<=no_steps; i++)
354 if (i==no_steps && no_steps_d==(
float) no_steps)
364 ROS_DEBUG(
"initTEBtoGoal(): number of generated samples is less than specified by min_samples. Forcing the insertion of more samples...");
381 ROS_WARN(
"Cannot init TEB between given configuration and goal, because TEB vectors are not empty or TEB is already initialized (call this function before adding states yourself)!");
389 bool TimedElasticBand::initTrajectoryToGoal(
const std::vector<geometry_msgs::PoseStamped>& plan,
double max_vel_x,
double max_vel_theta,
bool estimate_orient,
int min_samples,
bool guess_backwards_motion)
394 PoseSE2 start(plan.front().pose);
395 PoseSE2 goal(plan.back().pose);
400 bool backwards =
false;
401 if (guess_backwards_motion && (goal.position()-start.position()).
dot(start.orientationUnitVec()) < 0)
405 for (
int i=1; i<(int)plan.size()-1; ++i)
411 double dx = plan[i+1].pose.position.x - plan[i].pose.position.x;
412 double dy = plan[i+1].pose.position.y - plan[i].pose.position.y;
413 yaw = std::atan2(dy,dx);
415 yaw = g2o::normalize_theta(yaw+M_PI);
421 PoseSE2 intermediate_pose(plan[i].pose.position.x, plan[i].pose.position.y, yaw);
422 double dt = estimateDeltaT(
BackPose(), intermediate_pose, max_vel_x, max_vel_theta);
429 ROS_DEBUG(
"initTEBtoGoal(): number of generated samples is less than specified by min_samples. Forcing the insertion of more samples...");
434 double dt = estimateDeltaT(
BackPose(), intermediate_pose, max_vel_x, max_vel_theta);
440 double dt = estimateDeltaT(
BackPose(), goal, max_vel_x, max_vel_theta);
446 ROS_WARN(
"Cannot init TEB between given configuration and goal, because TEB vectors are not empty or TEB is already initialized (call this function before adding states yourself)!");
458 if (begin_idx < 0 || begin_idx >= n)
461 double min_dist_sq = std::numeric_limits<double>::max();
464 for (
int i = begin_idx; i < n; i++)
466 double dist_sq = (ref_point -
Pose(i).
position()).squaredNorm();
467 if (dist_sq < min_dist_sq)
469 min_dist_sq = dist_sq;
475 *distance = std::sqrt(min_dist_sq);
483 double min_dist = std::numeric_limits<double>::max();
498 *distance = min_dist;
504 if (vertices.empty())
506 else if (vertices.size() == 1)
508 else if (vertices.size() == 2)
511 double min_dist = std::numeric_limits<double>::max();
517 double dist_to_polygon = std::numeric_limits<double>::max();
518 for (
int j = 0; j < (int) vertices.size()-1; ++j)
523 if (dist_to_polygon < min_dist)
525 min_dist = dist_to_polygon;
531 *distance = min_dist;
564 double dist_cache = (new_start->position()-
Pose(0).
position()).norm();
566 int lookahead = std::min<int>(
sizePoses()-min_samples, 10);
569 for (
int i = 1; i<=lookahead; ++i)
571 dist = (new_start->position()-
Pose(i).
position()).norm();
590 Pose(0) = *new_start;
605 double radius_sq = radius*radius;
606 double max_dist_behind_robot_sq = max_dist_behind_robot*max_dist_behind_robot;
609 for (
int i=1; i<
sizePoses(); i=i+skip_poses+1)
612 double dist_sq = dist_vec.squaredNorm();
614 if (dist_sq > radius_sq)
621 if (max_dist_behind_robot >= 0 && dist_vec.dot(robot_orient) < 0 && dist_sq > max_dist_behind_robot_sq)
void setTimeDiffVertexFixed(int index, bool status)
Set a timediff vertex at pos index of the timediff sequence to be fixed or unfixed during optimizatio...
void deleteTimeDiffs(int index, int number)
Delete multiple (number) time differences starting at pos. index in the timediff sequence.
Eigen::Vector2d orientationUnitVec() const
Return the unit vector of the current orientation.
void insertPose(int index, const PoseSE2 &pose)
Insert a new pose vertex at pos. index to the pose sequence.
virtual const Eigen::Vector2d & getCentroid() const =0
Get centroid coordinates of the obstacle.
Eigen::Vector2d & position()
Access the 2D position part.
PoseSequence pose_vec_
Internal container storing the sequence of optimzable pose vertices.
static double getYaw(const Quaternion &bt_q)
static PoseSE2 average(const PoseSE2 &pose1, const PoseSE2 &pose2)
Get the mean / average of two poses and return the result (static) For the position part: 0...
bool initTrajectoryToGoal(const PoseSE2 &start, const PoseSE2 &goal, double diststep=0, double max_vel_x=0.5, int min_samples=3, bool guess_backwards_motion=false)
Initialize a trajectory between a given start and goal pose.
void addPoseAndTimeDiff(const PoseSE2 &pose, double dt)
Append a (pose, timediff) vertex pair to the end of the current trajectory (pose and timediff sequenc...
double & x()
Access the x-coordinate the pose.
std::vector< Eigen::Vector2d, Eigen::aligned_allocator< Eigen::Vector2d > > Point2dContainer
Abbrev. for a container storing 2d points.
TimeDiffSequence & timediffs()
Access the complete timediff sequence.
PoseSE2 & BackPose()
Access the last PoseSE2 in the pose sequence.
void addPose(const PoseSE2 &pose, bool fixed=false)
Append a new pose vertex to the back of the pose sequence.
Implements a polygon obstacle with an arbitrary number of vertices.
doubleAcc dot(const VectorAcc &lhs, const VectorAcc &rhs)
Implements a 2D line obstacle.
double getSumOfTimeDiffsUpToIdx(int index) const
Calculate the estimated transition time up to the pose denoted by index.
void deletePose(int index)
Delete pose at pos. index in the pose sequence.
double getAccumulatedDistance() const
Calculate the length (accumulated euclidean distance) of the trajectory.
void deletePoses(int index, int number)
Delete multiple (number) poses starting at pos. index in the pose sequence.
void updateAndPruneTEB(boost::optional< const PoseSE2 &> new_start, boost::optional< const PoseSE2 &> new_goal, int min_samples=3)
Hot-Start from an existing trajectory with updated start and goal poses.
#define ROS_ASSERT_MSG(cond,...)
double & TimeDiff(int index)
Access the time difference at pos index of the time sequence.
Implements a 2D point obstacle.
bool isTrajectoryInsideRegion(double radius, double max_dist_behind_robot=-1, int skip_poses=0)
Check if all trajectory points are contained in a specific region.
TimeDiffSequence timediff_vec_
Internal container storing the sequence of optimzable timediff vertices.
double distance_point_to_segment_2d(const Eigen::Ref< const Eigen::Vector2d > &point, const Eigen::Ref< const Eigen::Vector2d > &line_start, const Eigen::Ref< const Eigen::Vector2d > &line_end)
Helper function to calculate the distance between a line segment and a point.
void clearTimedElasticBand()
clear all poses and timediffs from the trajectory. The pose and timediff sequences will be empty and ...
bool isInit() const
Check whether the trajectory is initialized (nonzero pose and timediff sequences) ...
int sizeTimeDiffs() const
Get the length of the internal timediff sequence.
PoseSE2 & Pose(int index)
Access the pose at pos index of the pose sequence.
double & y()
Access the y-coordinate the pose.
This class implements a pose in the domain SE2: The pose consist of the position x and y and an orie...
int findClosestTrajectoryPose(const Eigen::Ref< const Eigen::Vector2d > &ref_point, double *distance=NULL, int begin_idx=0) const
Find the closest point on the trajectory w.r.t. to a provided reference point.
const Eigen::Vector2d & position() const
Return the current position of the obstacle (read-only)
virtual ~TimedElasticBand()
Destruct the class.
void addTimeDiff(double dt, bool fixed=false)
Append a new time difference vertex to the back of the time diff sequence.
This class stores and wraps a SE2 pose (position and orientation) into a vertex that can be optimized...
void deleteTimeDiff(int index)
Delete pose at pos. index in the timediff sequence.
void setPoseVertexFixed(int index, bool status)
Set a pose vertex at pos index of the pose sequence to be fixed or unfixed during optimization...
TimedElasticBand()
Construct the class.
int sizePoses() const
Get the length of the internal pose sequence.
void insertTimeDiff(int index, double dt)
Insert a new timediff vertex at pos. index to the timediff sequence.
void autoResize(double dt_ref, double dt_hysteresis, int min_samples=3, int max_samples=1000, bool fast_mode=false)
Resize the trajectory by removing or inserting a (pose,dt) pair depending on a reference temporal res...
Abstract class that defines the interface for modelling obstacles.
double getSumOfAllTimeDiffs() const
Calculate the total transition time (sum over all time intervals of the timediff sequence) ...
This class stores and wraps a time difference into a vertex that can be optimized via g2o...