149 const Eigen::Vector3f & pos,
const Eigen::Vector3f & vel,
float maxdist,
float maxanglediff,
150 float maxtime,
float dt, std::vector<Eigen::Vector3f> & outtraj)
155 Eigen::Vector3f currentpos = pos;
173 auto dx = newpos[0] - currentpos[0];
174 auto dy = newpos[1] - currentpos[1];
175 float dist, angledist;
178 dist = sqrt(dx * dx + dy * dy);
187 angledist = fabs(angles::shortest_angular_distance(currentpos[2], newpos[2]));
188 if (angledist > maxanglediff)
195 outtraj.push_back(newpos);
230 visualization_msgs::msg::Marker marker;
232 marker.header.frame_id =
costmapRos_->getGlobalFrameID();
233 marker.header.stamp =
nh_->now();
234 marker.ns =
"my_namespace2";
236 marker.type = visualization_msgs::msg::Marker::ARROW;
237 marker.action = visualization_msgs::msg::Marker::ADD;
238 marker.pose.orientation.w = 1;
239 marker.lifetime = rclcpp::Duration(1.0s);
241 marker.scale.x = 0.1;
242 marker.scale.y = 0.3;
243 marker.scale.z = 0.1;
244 marker.color.a = 1.0;
247 marker.color.b = 1.0;
249 geometry_msgs::msg::Point start, end;
253 end.x = x + 0.5 * cos(phi);
254 end.y = y + 0.5 * sin(phi);
256 marker.points.push_back(start);
257 marker.points.push_back(end);
259 visualization_msgs::msg::MarkerArray ma;
260 ma.markers.push_back(marker);
283 rclcpp::Node::SharedPtr nh_, geometry_msgs::msg::Twist & cmd_vel,
double max_linear_x_speed_,
284 double max_angular_z_speed_)
286 if (max_angular_z_speed_ == 0 || max_linear_x_speed_ == 0)
return;
288 if (cmd_vel.angular.z == 0)
290 cmd_vel.linear.x = max_linear_x_speed_;
294 double kurvature = cmd_vel.linear.x / cmd_vel.angular.z;
296 double linearAuthority = fabs(cmd_vel.linear.x / max_linear_x_speed_);
297 double angularAuthority = fabs(cmd_vel.angular.z / max_angular_z_speed_);
298 if (linearAuthority < angularAuthority)
301 cmd_vel.linear.x = max_linear_x_speed_;
302 cmd_vel.angular.z = kurvature / max_linear_x_speed_;
304 nh_->get_logger(),
"k=" << kurvature <<
"lets go to maximum linear capacity: " << cmd_vel);
309 cmd_vel.angular.x = max_angular_z_speed_;
310 cmd_vel.linear.x = kurvature * max_angular_z_speed_;
311 RCLCPP_WARN_STREAM(nh_->get_logger(),
"lets go to maximum angular capacity: " << cmd_vel);
323 const geometry_msgs::msg::PoseStamped & currentPose,
324 const geometry_msgs::msg::Twist & , nav2_core::GoalChecker * goal_checker)
328 if (this->
plan_.size() > 0)
331 nh_->get_logger(),
"[ForwardLocalPlanner] Current pose frame id: "
332 <<
plan_.front().header.frame_id
333 <<
", path pose frame id: " << currentPose.header.frame_id);
335 if (
plan_.front().header.frame_id != currentPose.header.frame_id)
337 RCLCPP_ERROR_STREAM(
nh_->get_logger(),
"[ForwardLocalPlanner] Inconsistent frames");
345 geometry_msgs::msg::Pose posetol;
346 geometry_msgs::msg::Twist twistol;
347 if (goal_checker->getTolerances(posetol, twistol))
355 <<
", yaw_goal_tolerance_: "
361 nh_->get_logger(),
"[ForwardLocalPlanner] could not get tolerances from goal checker");
365 geometry_msgs::msg::TwistStamped cmd_vel;
368 nh_->get_logger(),
"[ForwardLocalPlanner] ----- COMPUTE VELOCITY COMMAND LOCAL PLANNER ---");
377 const geometry_msgs::msg::Point & p = pose.pose.position;
379 tf2::fromMsg(pose.pose.orientation, q);
382 double dx = p.x - currentPose.pose.position.x;
383 double dy = p.y - currentPose.pose.position.y;
384 double dist = sqrt(dx * dx + dy * dy);
386 double pangle = tf2::getYaw(q);
387 double angle = tf2::getYaw(currentPose.pose.orientation);
388 double angular_error = angles::shortest_angular_distance(pangle, angle);
396 "current index: %d, carrot goal percentaje: %lf, dist: %lf, maxdist: %lf, angle_error: "
408 nh_->get_logger(),
"[ForwardLocalPlanner] selected carrot pose index "
414 cmd_vel.twist.linear.x = 0;
415 cmd_vel.twist.angular.z = 0;
425 const geometry_msgs::msg::PoseStamped & finalgoalpose =
plan_.back();
427 const geometry_msgs::msg::Point & goalposition = carrot_goalpose.pose.position;
429 tf2::Quaternion carrotGoalQ;
430 tf2::fromMsg(carrot_goalpose.pose.orientation, carrotGoalQ);
434 double betta = tf2::getYaw(carrot_goalpose.pose.orientation) +
betta_offset_;
435 double dx = goalposition.x - currentPose.pose.position.x;
436 double dy = goalposition.y - currentPose.pose.position.y;
439 double rho_error = sqrt(dx * dx + dy * dy);
441 tf2::Quaternion currentOrientation;
442 tf2::convert(currentPose.pose.orientation, currentOrientation);
445 double theta = tf2::getYaw(currentOrientation);
446 double alpha = atan2(dy, dx);
449 double alpha_error = angles::shortest_angular_distance(alpha, theta);
450 double betta_error = angles::shortest_angular_distance(betta, theta);
460 vetta =
k_rho_ * rho_error;
470 RCLCPP_DEBUG(
nh_->get_logger(),
"GOAL REACHED");
496 cmd_vel.twist.linear.x = vetta;
497 cmd_vel.twist.angular.z = gamma;
506 nh_->get_logger(),
"Forward local planner,"
508 <<
" theta: " << theta << std::endl
509 <<
" betta: " << betta << std::endl
510 <<
" err_x: " << dx << std::endl
511 <<
" err_y:" << dy << std::endl
512 <<
" rho_error:" << rho_error << std::endl
513 <<
" alpha_error:" << alpha_error << std::endl
514 <<
" betta_error:" << betta_error << std::endl
515 <<
" vetta:" << vetta << std::endl
516 <<
" gamma:" << gamma << std::endl
526 assert(currentPose.header.frame_id ==
"odom" || currentPose.header.frame_id ==
"map");
527 auto global_pose = currentPose;
531 auto yaw = tf2::getYaw(global_pose.pose.orientation);
533 auto & pos = global_pose.pose.position;
535 Eigen::Vector3f currentpose(pos.x, pos.y, yaw);
536 Eigen::Vector3f currentvel(
537 cmd_vel.twist.linear.x, cmd_vel.twist.linear.y, cmd_vel.twist.angular.z);
539 std::vector<Eigen::Vector3f> trajectory;
541 currentpose, currentvel, 0.8 , M_PI / 8 , 3.0 , 0.05 ,
545 bool aceptedplan =
true;
551 for (
auto & p : trajectory)
553 float dx = p[0] - finalgoalpose.pose.position.x;
554 float dy = p[1] - finalgoalpose.pose.position.y;
556 float dst = sqrt(dx * dx + dy * dy);
563 costmap2d->worldToMap(p[0], p[1], mx, my);
573 if (costmap2d->getCost(mx, my) >= nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
582 bool success =
false;
587 RCLCPP_DEBUG(
nh_->get_logger(),
"simulated trajectory is accepted.");
591 RCLCPP_DEBUG(
nh_->get_logger(),
"simulated trajectory is not accepted. Stop command.");
594 cmd_vel.twist.linear.x = 0;
595 cmd_vel.twist.angular.z = 0;
599 RCLCPP_DEBUG(
nh_->get_logger(),
"Start waiting obstacle disappear");
607 nh_->get_logger(),
"waiting obstacle disappear, elapsed: %lf seconds",
608 waitingduration.seconds());
613 nh_->get_logger(),
"TIMEOUT waiting obstacle disappear, elapsed: %lf seconds",
614 waitingduration.seconds());
624 "[ForwardLocalPlanner] object detected waiting stopped until it disappears.");
627 cmd_vel.header.stamp =
nh_->now();