27 #include "builtin_interfaces/msg/duration.hpp"
28 #include "lifecycle_msgs/msg/state.hpp"
29 #include "nav2_util/costmap.hpp"
30 #include "nav2_util/node_utils.hpp"
31 #include "nav2_util/geometry_utils.hpp"
32 #include "nav2_costmap_2d/cost_values.hpp"
33 #include "nav2_costmap_2d/footprint_collision_checker.hpp"
35 #include "nav2_planner/planner_server.hpp"
37 using namespace std::chrono_literals;
38 using rcl_interfaces::msg::ParameterType;
39 using std::placeholders::_1;
41 namespace nav2_planner
44 PlannerServer::PlannerServer(
const rclcpp::NodeOptions & options)
45 : nav2_util::LifecycleNode(
"planner_server",
"", options),
46 gp_loader_(
"nav2_core",
"nav2_core::GlobalPlanner"),
47 default_ids_{
"GridBased"},
48 default_types_{
"nav2_navfn_planner/NavfnPlanner"},
51 RCLCPP_INFO(get_logger(),
"Creating");
54 declare_parameter(
"planner_plugins", default_ids_);
55 declare_parameter(
"expected_planner_frequency", 1.0);
57 get_parameter(
"planner_plugins", planner_ids_);
58 if (planner_ids_ == default_ids_) {
59 for (
size_t i = 0; i < default_ids_.size(); ++i) {
60 declare_parameter(default_ids_[i] +
".plugin", default_types_[i]);
65 costmap_ros_ = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
66 "global_costmap", std::string{get_namespace()},
"global_costmap");
76 costmap_thread_.reset();
79 nav2_util::CallbackReturn
82 RCLCPP_INFO(get_logger(),
"Configuring");
84 costmap_ros_->configure();
85 costmap_ = costmap_ros_->getCostmap();
87 if (!costmap_ros_->getUseRadius()) {
89 std::make_unique<nav2_costmap_2d::FootprintCollisionChecker<nav2_costmap_2d::Costmap2D *>>(
94 costmap_thread_ = std::make_unique<nav2_util::NodeThread>(costmap_ros_);
97 get_logger(),
"Costmap size: %d,%d",
100 tf_ = costmap_ros_->getTfBuffer();
102 planner_types_.resize(planner_ids_.size());
106 for (
size_t i = 0; i != planner_ids_.size(); i++) {
108 planner_types_[i] = nav2_util::get_plugin_type_param(
109 node, planner_ids_[i]);
110 nav2_core::GlobalPlanner::Ptr planner =
111 gp_loader_.createUniqueInstance(planner_types_[i]);
113 get_logger(),
"Created global planner plugin %s of type %s",
114 planner_ids_[i].c_str(), planner_types_[i].c_str());
115 planner->configure(node, planner_ids_[i], tf_, costmap_ros_);
116 planners_.insert({planner_ids_[i], planner});
117 }
catch (
const pluginlib::PluginlibException & ex) {
119 get_logger(),
"Failed to create global planner. Exception: %s",
121 return nav2_util::CallbackReturn::FAILURE;
125 for (
size_t i = 0; i != planner_ids_.size(); i++) {
126 planner_ids_concat_ += planner_ids_[i] + std::string(
" ");
131 "Planner Server has %s planners available.", planner_ids_concat_.c_str());
133 double expected_planner_frequency;
134 get_parameter(
"expected_planner_frequency", expected_planner_frequency);
135 if (expected_planner_frequency > 0) {
136 max_planner_duration_ = 1 / expected_planner_frequency;
140 "The expected planner frequency parameter is %.4f Hz. The value should to be greater"
141 " than 0.0 to turn on duration overrrun warning messages", expected_planner_frequency);
142 max_planner_duration_ = 0.0;
146 plan_publisher_ = create_publisher<nav_msgs::msg::Path>(
"plan", 1);
149 action_server_pose_ = std::make_unique<ActionServerToPose>(
151 "compute_path_to_pose",
154 std::chrono::milliseconds(500),
157 action_server_poses_ = std::make_unique<ActionServerThroughPoses>(
159 "compute_path_through_poses",
162 std::chrono::milliseconds(500),
165 return nav2_util::CallbackReturn::SUCCESS;
168 nav2_util::CallbackReturn
171 RCLCPP_INFO(get_logger(),
"Activating");
173 plan_publisher_->on_activate();
174 action_server_pose_->activate();
175 action_server_poses_->activate();
176 costmap_ros_->activate();
178 PlannerMap::iterator it;
179 for (it = planners_.begin(); it != planners_.end(); ++it) {
180 it->second->activate();
185 is_path_valid_service_ = node->create_service<nav2_msgs::srv::IsPathValid>(
189 std::placeholders::_1, std::placeholders::_2));
192 dyn_params_handler_ = node->add_on_set_parameters_callback(
198 return nav2_util::CallbackReturn::SUCCESS;
201 nav2_util::CallbackReturn
204 RCLCPP_INFO(get_logger(),
"Deactivating");
206 action_server_pose_->deactivate();
207 action_server_poses_->deactivate();
208 plan_publisher_->on_deactivate();
217 costmap_ros_->deactivate();
219 PlannerMap::iterator it;
220 for (it = planners_.begin(); it != planners_.end(); ++it) {
221 it->second->deactivate();
224 dyn_params_handler_.reset();
229 return nav2_util::CallbackReturn::SUCCESS;
232 nav2_util::CallbackReturn
235 RCLCPP_INFO(get_logger(),
"Cleaning up");
237 action_server_pose_.reset();
238 action_server_poses_.reset();
239 plan_publisher_.reset();
242 costmap_ros_->cleanup();
244 PlannerMap::iterator it;
245 for (it = planners_.begin(); it != planners_.end(); ++it) {
246 it->second->cleanup();
250 costmap_thread_.reset();
252 return nav2_util::CallbackReturn::SUCCESS;
255 nav2_util::CallbackReturn
258 RCLCPP_INFO(get_logger(),
"Shutting down");
259 return nav2_util::CallbackReturn::SUCCESS;
266 if (action_server ==
nullptr || !action_server->is_server_active()) {
267 RCLCPP_DEBUG(get_logger(),
"Action server unavailable or inactive. Stopping.");
278 while (!costmap_ros_->isCurrent()) {
287 if (action_server->is_cancel_requested()) {
288 RCLCPP_INFO(get_logger(),
"Goal was canceled. Canceling planning action.");
289 action_server->terminate_all();
299 typename std::shared_ptr<const typename T::Goal> goal)
301 if (action_server->is_preempt_requested()) {
302 goal = action_server->accept_pending_goal();
309 typename std::shared_ptr<const typename T::Goal> goal,
310 geometry_msgs::msg::PoseStamped & start)
312 if (goal->use_start) {
314 }
else if (!costmap_ros_->getRobotPose(start)) {
315 action_server->terminate_current();
325 geometry_msgs::msg::PoseStamped & curr_start,
326 geometry_msgs::msg::PoseStamped & curr_goal)
328 if (!costmap_ros_->transformPoseToGlobalFrame(curr_start, curr_start) ||
329 !costmap_ros_->transformPoseToGlobalFrame(curr_goal, curr_goal))
332 get_logger(),
"Could not transform the start or goal pose in the costmap frame");
333 action_server->terminate_current();
343 const geometry_msgs::msg::PoseStamped & goal,
344 const nav_msgs::msg::Path & path,
345 const std::string & planner_id)
347 if (path.poses.size() == 0) {
349 get_logger(),
"Planning algorithm %s failed to generate a valid"
350 " path to (%.2f, %.2f)", planner_id.c_str(),
351 goal.pose.position.x, goal.pose.position.y);
352 action_server->terminate_current();
358 "Found valid path of size %zu to (%.2f, %.2f)",
359 path.poses.size(), goal.pose.position.x,
360 goal.pose.position.y);
368 std::lock_guard<std::mutex> lock(dynamic_params_lock_);
370 auto start_time = this->now();
373 auto goal = action_server_poses_->get_current_goal();
374 auto result = std::make_shared<ActionThroughPoses::Result>();
375 nav_msgs::msg::Path concat_path;
386 if (goal->goals.size() == 0) {
389 "Compute path through poses requested a plan with no viapoint poses, returning.");
390 action_server_poses_->terminate_current();
394 geometry_msgs::msg::PoseStamped start;
400 geometry_msgs::msg::PoseStamped curr_start, curr_goal;
401 for (
unsigned int i = 0; i != goal->goals.size(); i++) {
408 curr_start = concat_path.poses.back();
409 curr_start.header = concat_path.header;
411 curr_goal = goal->goals[i];
419 nav_msgs::msg::Path curr_path =
getPlan(curr_start, curr_goal, goal->planner_id);
422 if (!
validatePath(action_server_poses_, curr_goal, curr_path, goal->planner_id)) {
427 concat_path.poses.insert(
428 concat_path.poses.end(), curr_path.poses.begin(), curr_path.poses.end());
429 concat_path.header = curr_path.header;
433 result->path = concat_path;
436 auto cycle_duration = this->now() - start_time;
437 result->planning_time = cycle_duration;
439 if (max_planner_duration_ && cycle_duration.seconds() > max_planner_duration_) {
442 "Planner loop missed its desired rate of %.4f Hz. Current loop rate is %.4f Hz",
443 1 / max_planner_duration_, 1 / cycle_duration.seconds());
446 action_server_poses_->succeeded_current(result);
447 }
catch (std::exception & ex) {
450 "%s plugin failed to plan through %zu points with final goal (%.2f, %.2f): \"%s\"",
451 goal->planner_id.c_str(), goal->goals.size(), goal->goals.back().pose.position.x,
452 goal->goals.back().pose.position.y, ex.what());
453 action_server_poses_->terminate_current();
460 std::lock_guard<std::mutex> lock(dynamic_params_lock_);
462 auto start_time = this->now();
465 auto goal = action_server_pose_->get_current_goal();
466 auto result = std::make_shared<ActionToPose::Result>();
478 geometry_msgs::msg::PoseStamped start;
484 geometry_msgs::msg::PoseStamped goal_pose = goal->goal;
489 result->path =
getPlan(start, goal_pose, goal->planner_id);
491 if (!
validatePath(action_server_pose_, goal_pose, result->path, goal->planner_id)) {
498 auto cycle_duration = this->now() - start_time;
499 result->planning_time = cycle_duration;
501 if (max_planner_duration_ && cycle_duration.seconds() > max_planner_duration_) {
504 "Planner loop missed its desired rate of %.4f Hz. Current loop rate is %.4f Hz",
505 1 / max_planner_duration_, 1 / cycle_duration.seconds());
508 action_server_pose_->succeeded_current(result);
509 }
catch (std::exception & ex) {
511 get_logger(),
"%s plugin failed to plan calculation to (%.2f, %.2f): \"%s\"",
512 goal->planner_id.c_str(), goal->goal.pose.position.x,
513 goal->goal.pose.position.y, ex.what());
514 action_server_pose_->terminate_current();
520 const geometry_msgs::msg::PoseStamped & start,
521 const geometry_msgs::msg::PoseStamped & goal,
522 const std::string & planner_id)
525 get_logger(),
"Attempting to a find path from (%.2f, %.2f) to "
526 "(%.2f, %.2f).", start.pose.position.x, start.pose.position.y,
527 goal.pose.position.x, goal.pose.position.y);
529 if (planners_.find(planner_id) != planners_.end()) {
530 return planners_[planner_id]->createPlan(start, goal);
532 if (planners_.size() == 1 && planner_id.empty()) {
534 get_logger(),
"No planners specified in action call. "
535 "Server will use only plugin %s in server."
536 " This warning will appear once.", planner_ids_concat_.c_str());
537 return planners_[planners_.begin()->first]->createPlan(start, goal);
540 get_logger(),
"planner %s is not a valid planner. "
541 "Planner names are: %s", planner_id.c_str(),
542 planner_ids_concat_.c_str());
546 return nav_msgs::msg::Path();
552 auto msg = std::make_unique<nav_msgs::msg::Path>(path);
553 if (plan_publisher_->is_activated() && plan_publisher_->get_subscription_count() > 0) {
554 plan_publisher_->publish(std::move(msg));
559 const std::shared_ptr<nav2_msgs::srv::IsPathValid::Request> request,
560 std::shared_ptr<nav2_msgs::srv::IsPathValid::Response> response)
562 response->is_valid =
true;
564 if (request->path.poses.empty()) {
565 response->is_valid =
false;
569 geometry_msgs::msg::PoseStamped current_pose;
570 unsigned int closest_point_index = 0;
571 if (costmap_ros_->getRobotPose(current_pose)) {
572 float current_distance = std::numeric_limits<float>::max();
573 float closest_distance = current_distance;
574 geometry_msgs::msg::Point current_point = current_pose.pose.position;
575 for (
unsigned int i = 0; i < request->path.poses.size(); ++i) {
576 geometry_msgs::msg::Point path_point = request->path.poses[i].pose.position;
578 current_distance = nav2_util::geometry_utils::euclidean_distance(
582 if (current_distance < closest_distance) {
583 closest_point_index = i;
584 closest_distance = current_distance;
592 std::unique_lock<nav2_costmap_2d::Costmap2D::mutex_t> lock(*(costmap_->getMutex()));
596 bool use_radius = costmap_ros_->getUseRadius();
598 unsigned int cost = nav2_costmap_2d::FREE_SPACE;
600 for (
unsigned int i = closest_point_index; i < request->path.poses.size(); ++i) {
601 auto & position = request->path.poses[i].pose.position;
603 if (costmap_->
worldToMap(position.x, position.y, mx, my)) {
604 cost = costmap_->
getCost(mx, my);
606 cost = nav2_costmap_2d::LETHAL_OBSTACLE;
609 nav2_costmap_2d::Footprint footprint = costmap_ros_->getRobotFootprint();
610 auto theta = tf2::getYaw(request->path.poses[i].pose.orientation);
611 cost =
static_cast<unsigned int>(collision_checker_->footprintCostAtPose(
612 position.x, position.y, theta, footprint));
616 (cost == nav2_costmap_2d::LETHAL_OBSTACLE ||
617 cost == nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE))
619 response->is_valid =
false;
621 }
else if (cost == nav2_costmap_2d::LETHAL_OBSTACLE) {
622 response->is_valid =
false;
629 rcl_interfaces::msg::SetParametersResult
632 std::lock_guard<std::mutex> lock(dynamic_params_lock_);
633 rcl_interfaces::msg::SetParametersResult result;
635 for (
auto parameter : parameters) {
636 const auto & type = parameter.get_type();
637 const auto & name = parameter.get_name();
639 if (type == ParameterType::PARAMETER_DOUBLE) {
640 if (name ==
"expected_planner_frequency") {
641 if (parameter.as_double() > 0) {
642 max_planner_duration_ = 1 / parameter.as_double();
646 "The expected planner frequency parameter is %.4f Hz. The value should to be greater"
647 " than 0.0 to turn on duration overrrun warning messages", parameter.as_double());
648 max_planner_duration_ = 0.0;
654 result.successful =
true;
660 #include "rclcpp_components/register_node_macro.hpp"
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
An action server implements the behavior tree's ComputePathToPose interface and hosts various plugins...
void publishPlan(const nav_msgs::msg::Path &path)
Publish a path for visualization purposes.
void computePlan()
The action server callback which calls planner to get the path ComputePathToPose.
void getPreemptedGoalIfRequested(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, typename std::shared_ptr< const typename T::Goal > goal)
Check if an action server has a preemption request and replaces the goal with the new preemption goal...
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Reset member variables.
void waitForCostmap()
Wait for costmap to be valid with updated sensor data or repopulate after a clearing recovery....
~PlannerServer()
A destructor for nav2_planner::PlannerServer.
bool isServerInactive(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server)
Check if an action server is valid / active.
void isPathValid(const std::shared_ptr< nav2_msgs::srv::IsPathValid::Request > request, std::shared_ptr< nav2_msgs::srv::IsPathValid::Response > response)
The service callback to determine if the path is still valid.
void computePlanThroughPoses()
The action server callback which calls planner to get the path ComputePathThroughPoses.
bool transformPosesToGlobalFrame(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, geometry_msgs::msg::PoseStamped &curr_start, geometry_msgs::msg::PoseStamped &curr_goal)
Transform start and goal poses into the costmap global frame for path planning plugins to utilize.
bool getStartPose(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, typename std::shared_ptr< const typename T::Goal > goal, geometry_msgs::msg::PoseStamped &start)
Get the starting pose from costmap or message, if valid.
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
bool isCancelRequested(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server)
Check if an action server has a cancellation request pending.
nav_msgs::msg::Path getPlan(const geometry_msgs::msg::PoseStamped &start, const geometry_msgs::msg::PoseStamped &goal, const std::string &planner_id)
Method to get plan from the desired plugin.
nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configure member variables and initializes planner.
bool validatePath(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, const geometry_msgs::msg::PoseStamped &curr_goal, const nav_msgs::msg::Path &path, const std::string &planner_id)
Validate that the path contains a meaningful path.
nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activate member variables.
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivate member variables.
std::shared_ptr< nav2_util::LifecycleNode > shared_from_this()
Get a shared pointer of this.
void createBond()
Create bond connection to lifecycle manager.
void destroyBond()
Destroy bond connection to lifecycle manager.
An action server wrapper to make applications simpler using Actions.