38 #include <moveit_msgs/msg/move_it_error_codes.hpp>
45 const rclcpp::Logger
LOGGER = rclcpp::get_logger(
"global_planner_component");
46 const auto JOIN_THREAD_TIMEOUT = std::chrono::seconds(1);
51 using namespace std::chrono_literals;
55 : node_{ std::make_shared<
rclcpp::Node>(
"global_planner_component",
options) }
57 if (!initializeGlobalPlanner())
59 throw std::runtime_error(
"Failed to initialize Global Planner Component");
63 bool GlobalPlannerComponent::initializeGlobalPlanner()
66 std::string global_planning_action_name =
"";
67 node_->declare_parameter(
"global_planning_action_name",
"");
68 node_->get_parameter<std::string>(
"global_planning_action_name", global_planning_action_name);
69 if (global_planning_action_name.empty())
71 RCLCPP_ERROR(LOGGER,
"global_planning_action_name was not defined");
74 cb_group_ = node_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
75 global_planning_request_server_ = rclcpp_action::create_server<moveit_msgs::action::GlobalPlanner>(
76 node_, global_planning_action_name,
78 [
this](
const rclcpp_action::GoalUUID& ,
79 const std::shared_ptr<const moveit_msgs::action::GlobalPlanner::Goal>& ) {
80 RCLCPP_INFO(LOGGER,
"Received global planning goal request");
82 if (long_callback_thread_.joinable())
85 auto future = std::async(std::launch::async, &std::thread::join, &long_callback_thread_);
86 if (future.wait_for(JOIN_THREAD_TIMEOUT) == std::future_status::timeout)
88 RCLCPP_WARN(LOGGER,
"Another goal was running. Rejecting the new hybrid planning goal.");
89 return rclcpp_action::GoalResponse::REJECT;
91 if (!global_planner_instance_->reset())
93 throw std::runtime_error(
"Failed to reset the global planner while aborting current global planning");
96 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
99 [
this](
const std::shared_ptr<rclcpp_action::ServerGoalHandle<moveit_msgs::action::GlobalPlanner>>& ) {
100 RCLCPP_INFO(LOGGER,
"Received request to cancel global planning goal");
101 if (long_callback_thread_.joinable())
103 long_callback_thread_.join();
105 if (!global_planner_instance_->reset())
107 throw std::runtime_error(
"Failed to reset the global planner while aborting current global planning");
109 return rclcpp_action::CancelResponse::ACCEPT;
112 [
this](std::shared_ptr<rclcpp_action::ServerGoalHandle<moveit_msgs::action::GlobalPlanner>> goal_handle) {
114 if (long_callback_thread_.joinable())
116 long_callback_thread_.join();
117 global_planner_instance_->reset();
119 long_callback_thread_ = std::thread(&GlobalPlannerComponent::globalPlanningRequestCallback,
this, goal_handle);
121 rcl_action_server_get_default_options(), cb_group_);
123 global_trajectory_pub_ = node_->create_publisher<moveit_msgs::msg::MotionPlanResponse>(
"global_trajectory", 1);
126 planner_plugin_name_ = node_->declare_parameter<std::string>(
"global_planner_name",
UNDEFINED);
130 global_planner_plugin_loader_ = std::make_unique<pluginlib::ClassLoader<GlobalPlannerInterface>>(
131 "moveit_hybrid_planning",
"moveit::hybrid_planning::GlobalPlannerInterface");
133 catch (pluginlib::PluginlibException& ex)
135 RCLCPP_ERROR(LOGGER,
"Exception while creating global planner plugin loader: '%s'", ex.what());
140 global_planner_instance_ = global_planner_plugin_loader_->createUniqueInstance(planner_plugin_name_);
142 catch (pluginlib::PluginlibException& ex)
144 RCLCPP_ERROR(LOGGER,
"Exception while loading global planner '%s': '%s'", planner_plugin_name_.c_str(), ex.what());
149 if (!global_planner_instance_->initialize(node_))
151 RCLCPP_ERROR(LOGGER,
"Unable to initialize global planner plugin '%s'", planner_plugin_name_.c_str());
154 RCLCPP_INFO(LOGGER,
"Using global planner plugin '%s'", planner_plugin_name_.c_str());
158 void GlobalPlannerComponent::globalPlanningRequestCallback(
159 const std::shared_ptr<rclcpp_action::ServerGoalHandle<moveit_msgs::action::GlobalPlanner>>& goal_handle)
162 moveit_msgs::msg::MotionPlanResponse planning_solution = global_planner_instance_->plan(goal_handle);
165 auto result = std::make_shared<moveit_msgs::action::GlobalPlanner::Result>();
166 result->response = planning_solution;
168 if (planning_solution.error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
171 global_trajectory_pub_->publish(planning_solution);
172 goal_handle->succeed(result);
176 goal_handle->abort(result);
180 global_planner_instance_->reset();
185 #include <rclcpp_components/register_node_macro.hpp>
GlobalPlannerComponent(const rclcpp::NodeOptions &options)
Constructor.
const std::string UNDEFINED
const rclcpp::Logger LOGGER