493 const geometry_msgs::msg::PoseStamped & pose,
const geometry_msgs::msg::Twist & velocity,
494 nav2_core::GoalChecker * goal_checker)
498 "[BackwardLocalPlanner] ------------------- LOCAL PLANNER LOOP -----------------");
505 nh_->get_logger(),
"[BackwardLocalPlanner] Current pose frame id: "
507 <<
", path pose frame id: " << pose.header.frame_id);
511 RCLCPP_ERROR_STREAM(
nh_->get_logger(),
"[BackwardLocalPlanner] Inconsistent frames");
519 geometry_msgs::msg::Pose posetol;
520 geometry_msgs::msg::Twist twistol;
521 if (goal_checker->getTolerances(posetol, twistol))
527 nh_->get_logger(),
"[BackwardLocalPlanner] xy_goal_tolerance_: "
534 nh_->get_logger(),
"[BackwardLocalPlanner] could not get tolerances from goal checker");
540 "[BackwardLocalPlanner] ------------------- LOCAL PLANNER LOOP -----------------");
542 geometry_msgs::msg::TwistStamped cmd_vel;
543 RCLCPP_INFO(
nh_->get_logger(),
"[BackwardLocalPlanner] LOCAL PLANNER LOOP");
544 geometry_msgs::msg::PoseStamped paux;
545 geometry_msgs::msg::PoseStamped tfpose;
551 "[BackwardLocalPlanner] missing robot pose, canceling compute Velocity Command");
555 RCLCPP_INFO_STREAM(
nh_->get_logger(),
"[BackwardLocalPlanner] carrot goal created");
558 double rho_error = 0.0, betta_error = 0.0, alpha_error = 0.0;
562 tf2::convert(tfpose.pose.orientation, q);
567 const geometry_msgs::msg::PoseStamped & carrotgoalpose =
570 nh_->get_logger(),
"[BackwardLocalPlanner] carrot goal pose current index: "
573 const geometry_msgs::msg::Point & carrotGoalPosition = carrotgoalpose.pose.position;
575 tf2::Quaternion goalQ;
576 tf2::fromMsg(carrotgoalpose.pose.orientation, goalQ);
577 RCLCPP_INFO_STREAM(
nh_->get_logger(),
"[BackwardLocalPlanner] -- Control Policy --");
579 double betta = tf2::getYaw(goalQ);
580 RCLCPP_INFO_STREAM(
nh_->get_logger(),
"[BackwardLocalPlanner] goal orientation: " << betta);
583 double dx = carrotGoalPosition.x - tfpose.pose.position.x;
584 double dy = carrotGoalPosition.y - tfpose.pose.position.y;
587 rho_error = sqrt(dx * dx + dy * dy);
590 double theta = tf2::getYaw(q);
591 double alpha = atan2(dy, dx);
594 alpha_error = angles::shortest_angular_distance(alpha, theta);
595 betta_error = angles::shortest_angular_distance(betta, theta);
598 bool linearGoalReached =
false;
599 bool currentPoseInGoal =
606 if (currentPoseInGoal && carrotInFinalGoalIndex)
611 "[BackwardLocalPlanner] GOAL REACHED. Send stop command and skipping trajectory collision: "
613 cmd_vel.twist.linear.x = 0;
614 cmd_vel.twist.angular.z = 0;
618 carrotInLinearGoalRange &&
636 tfpose, vetta, gamma, alpha_error, betta_error, rho_error);
641 vetta =
k_rho_ * rho_error;
650 "[BackwardLocalPlanner] we entered in a pure spinning state even in not pure-spining "
652 "carrotDistanceGoalReached: %d",
653 carrotInLinearGoalRange);
662 cmd_vel.twist.linear.x = vetta;
663 cmd_vel.twist.angular.z = gamma;
686 nh_->get_logger(),
"[BackwardLocalPlanner] local planner,"
688 <<
" current pose in goal: " << currentPoseInGoal <<
'\n'
689 <<
" carrot in final goal index: " << carrotInFinalGoalIndex <<
'\n'
690 <<
" carrot in linear goal range: " << carrotInLinearGoalRange <<
'\n'
694 <<
" theta: " << theta <<
'\n'
695 <<
" betta: " << theta <<
'\n'
696 <<
" err_x: " << dx <<
'\n'
697 <<
" err_y:" << dy <<
'\n'
698 <<
" rho_error:" << rho_error <<
'\n'
699 <<
" alpha_error:" << alpha_error <<
'\n'
700 <<
" betta_error:" << betta_error <<
'\n'
701 <<
" vetta:" << vetta <<
'\n'
702 <<
" gamma:" << gamma <<
'\n'
703 <<
" cmd_vel.lin.x:" << cmd_vel.twist.linear.x <<
'\n'
704 <<
" cmd_vel.ang.z:" << cmd_vel.twist.angular.z);
710 if (carrotHalfPlaneConstraintFailure)
714 "[BackwardLocalPlanner] CarrotHalfPlaneConstraintFailure detected. Sending "
715 "emergency stop and success to the planner.");
716 cmd_vel.twist.linear.x = 0;
721 geometry_msgs::msg::PoseStamped global_pose;
725 auto yaw = tf2::getYaw(global_pose.pose.orientation);
727 auto & pos = global_pose.pose.position;
729 Eigen::Vector3f currentpose(pos.x, pos.y, yaw);
730 Eigen::Vector3f currentvel(
731 cmd_vel.twist.linear.x, cmd_vel.twist.linear.y, cmd_vel.twist.angular.z);
732 std::vector<Eigen::Vector3f> trajectory;
734 currentpose, currentvel, 0.8 , M_PI / 8 , 3.0 , 0.05 ,
738 bool acceptedLocalTrajectoryFreeOfObstacles =
true;
740 unsigned int mx = 0, my = 0;
750 geometry_msgs::msg::Twist mockzerospeed;
752 for (
auto & p : trajectory)
754 float goaldx = p[0] - finalgoalpose.pose.position.x;
755 float goaldy = p[1] - finalgoalpose.pose.position.y;
757 float dst = sqrt(goaldx * goaldx + goaldy * goaldy);
762 "[BackwardLocalPlanner] trajectory simulation for collision checking: goal "
763 "reached with no collision");
767 costmap2d->worldToMap(p[0], p[1], mx, my);
769 if (costmap2d->getCost(mx, my) >= nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
771 acceptedLocalTrajectoryFreeOfObstacles =
false;
774 "[BackwardLocalPlanner] ABORTED LOCAL PLAN BECAUSE OBSTACLE DETEDTED at point "
775 << i <<
"/" << trajectory.size() <<
'\n'
776 << p[0] <<
", " << p[1]);
785 nh_->get_logger(),
"[BackwardLocalPlanner] Abort local - Backwards global plan size: %ld",
787 cmd_vel.twist.angular.z = 0;
788 cmd_vel.twist.linear.x = 0;
792 if (acceptedLocalTrajectoryFreeOfObstacles)
797 "[BackwardLocalPlanner] accepted local trajectory free of obstacle. Local planner "
804 cmd_vel.twist.linear.x = 0;
805 cmd_vel.twist.angular.z = 0;
812 nh_->get_logger(),
"[BackwardLocalPlanner][Not accepted local plan] starting countdown");
821 nh_->get_logger(),
"[BackwardLocalPlanner][Abort local] timeout! duration %lf/%f",
823 cmd_vel.twist.linear.x = 0;
824 cmd_vel.twist.angular.z = 0;
916 RCLCPP_INFO(
nh_->get_logger(),
"[BackwardLocalPlanner] resample precise");
921 "[BackwardLocalPlanner] resample precise skipping, size: " <<
backwardsPlanPath_.size());
930 if (maxallowedLinearError <= 0.0 || maxallowedAngularError <= 0.0)
933 nh_->get_logger(),
"[BackwardLocalPlanner] resample precise skipping: carrot_distance ("
944 RCLCPP_INFO_STREAM(
nh_->get_logger(),
"[BackwardLocalPlanner] resample precise, check: " << i);
948 tf2::Quaternion qCurrent, qNext;
949 tf2::convert(currpose.pose.orientation, qCurrent);
950 tf2::convert(nextpose.pose.orientation, qNext);
952 double dx = nextpose.pose.position.x - currpose.pose.position.x;
953 double dy = nextpose.pose.position.y - currpose.pose.position.y;
954 double dist = sqrt(dx * dx + dy * dy);
956 bool resample =
false;
957 if (dist > maxallowedLinearError)
960 nh_->get_logger(),
"[BackwardLocalPlanner] resampling point, linear distance:"
961 << dist <<
"(" << maxallowedLinearError <<
")" << i);
966 double currentAngle = tf2::getYaw(qCurrent);
967 double nextAngle = tf2::getYaw(qNext);
969 double angularError = fabs(angles::shortest_angular_distance(currentAngle, nextAngle));
970 if (angularError > maxallowedAngularError)
974 nh_->get_logger(),
"[BackwardLocalPlanner] resampling point, angular distance:"
975 << angularError <<
"(" << maxallowedAngularError <<
")" << i);
981 geometry_msgs::msg::PoseStamped pintermediate;
982 auto duration = rclcpp::Time(nextpose.header.stamp) - rclcpp::Time(currpose.header.stamp);
984 pintermediate.header.frame_id = currpose.header.frame_id;
985 pintermediate.header.stamp = rclcpp::Time(currpose.header.stamp) + duration * 0.5;
987 pintermediate.pose.position.x = 0.5 * (currpose.pose.position.x + nextpose.pose.position.x);
988 pintermediate.pose.position.y = 0.5 * (currpose.pose.position.y + nextpose.pose.position.y);
989 pintermediate.pose.position.z = 0.5 * (currpose.pose.position.z + nextpose.pose.position.z);
990 tf2::Quaternion intermediateQuat = tf2::slerp(qCurrent, qNext, 0.5);
991 pintermediate.pose.orientation = tf2::toMsg(intermediateQuat);
1003 nh_->get_logger(),
"[BackwardLocalPlanner] End resampling. resampled:" << counter
1004 <<
" new inserted poses "