67 auto node =
context_->moveit_cpp_->getNode();
68 execute_action_server_ = rclcpp_action::create_server<MGAction>(
70 [](
const rclcpp_action::GoalUUID& ,
const std::shared_ptr<const MGAction::Goal>& ) {
71 RCLCPP_INFO(getLogger(),
"MoveGroupMoveAction: Received request");
72 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
74 [
this](
const std::shared_ptr<MGActionGoal>& ) {
75 RCLCPP_INFO(getLogger(),
"MoveGroupMoveAction: Received request to cancel goal");
76 preemptMoveCallback();
77 return rclcpp_action::CancelResponse::ACCEPT;
79 [
this](
const std::shared_ptr<MGActionGoal>& goal) {
80 std::thread{ [
this](
const std::shared_ptr<move_group::MGActionGoal>& goal) { executeMoveCallback(goal); }, goal }
85void MoveGroupMoveAction::executeMoveCallback(
const std::shared_ptr<MGActionGoal>& goal)
88 RCLCPP_INFO(getLogger(),
"executing..");
91 auto node =
context_->moveit_cpp_->getNode();
92 context_->planning_scene_monitor_->waitForCurrentRobotState(node->get_clock()->now());
93 context_->planning_scene_monitor_->updateFrameTransforms();
95 auto action_res = std::make_shared<MGAction::Result>();
96 if (goal->get_goal()->planning_options.plan_only || !
context_->allow_trajectory_execution_)
98 if (!goal->get_goal()->planning_options.plan_only)
100 RCLCPP_WARN(getLogger(),
"This instance of MoveGroup is not allowed to execute trajectories "
101 "but the goal request has plan_only set to false. "
102 "Only a motion plan will be computed anyway.");
104 executeMoveCallbackPlanOnly(goal, action_res);
107 executeMoveCallbackPlanAndExecute(goal, action_res);
112 goal->get_goal()->planning_options.plan_only));
113 if (action_res->error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
115 goal->succeed(action_res);
117 else if (action_res->error_code.val == moveit_msgs::msg::MoveItErrorCodes::PREEMPTED)
119 goal->canceled(action_res);
123 goal->abort(action_res);
126 setMoveState(
IDLE, goal_);
127 preempt_requested_ =
false;
131void MoveGroupMoveAction::executeMoveCallbackPlanAndExecute(
const std::shared_ptr<MGActionGoal>& goal,
132 std::shared_ptr<MGAction::Result>& action_res)
134 RCLCPP_INFO(
getLogger(),
"Combined planning and execution request received for MoveGroup action. "
135 "Forwarding to planning and execution pipeline.");
143 for (std::size_t i = 0; i < goal->get_goal()->request.goal_constraints.size(); ++i)
145 if (lscene->isStateConstrained(
147 goal->get_goal()->request.path_constraints)))
149 RCLCPP_INFO(
getLogger(),
"Goal constraints are already satisfied. No need to plan or execute any motions");
150 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
158 const moveit_msgs::msg::MotionPlanRequest& motion_plan_request =
161 const moveit_msgs::msg::PlanningScene& planning_scene_diff =
163 goal->get_goal()->planning_options.planning_scene_diff :
166 opt.
replan = goal->get_goal()->planning_options.replan;
167 opt.
replan_attemps = goal->get_goal()->planning_options.replan_attempts;
168 opt.
replan_delay = goal->get_goal()->planning_options.replan_delay;
172 return planUsingPlanningPipeline(motion_plan_request, plan);
176 if (preempt_requested_)
178 RCLCPP_INFO(
getLogger(),
"Preempt requested before the goal is planned and executed.");
179 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
183 context_->plan_execution_->planAndExecute(plan, planning_scene_diff, opt);
185 convertToMsg(
plan.plan_components, action_res->trajectory_start, action_res->planned_trajectory);
186 if (
plan.executed_trajectory)
187 plan.executed_trajectory->getRobotTrajectoryMsg(action_res->executed_trajectory);
191void MoveGroupMoveAction::executeMoveCallbackPlanOnly(
const std::shared_ptr<MGActionGoal>& goal,
192 std::shared_ptr<MGAction::Result>& action_res)
194 RCLCPP_INFO(
getLogger(),
"Planning request received for MoveGroup action. Forwarding to planning pipeline.");
198 const planning_scene::PlanningSceneConstPtr& the_scene =
200 static_cast<const planning_scene::PlanningSceneConstPtr&
>(lscene) :
201 lscene->diff(goal->get_goal()->planning_options.planning_scene_diff);
204 if (preempt_requested_)
206 RCLCPP_INFO(
getLogger(),
"Preempt requested before the goal is planned.");
207 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
216 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
224 RCLCPP_ERROR(
getLogger(),
"Generating a plan with planning pipeline failed.");
225 res.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
228 catch (std::exception& ex)
230 RCLCPP_ERROR(
getLogger(),
"Planning pipeline threw an exception: %s", ex.what());
231 res.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
251 res.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
260 catch (std::exception& ex)
262 RCLCPP_ERROR(
getLogger(),
"Planning pipeline threw an exception: %s", ex.what());
263 res.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
267 plan.plan_components.resize(1);
269 plan.plan_components[0].description =
"plan";
276void MoveGroupMoveAction::startMoveExecutionCallback()
281void MoveGroupMoveAction::startMoveLookCallback()
283 setMoveState(
LOOK, goal_);
286void MoveGroupMoveAction::preemptMoveCallback()
288 preempt_requested_ =
true;
292void MoveGroupMoveAction::setMoveState(
MoveGroupState state,
const std::shared_ptr<MGActionGoal>& goal)
298 auto move_feedback = std::make_shared<MGAction::Feedback>();
300 goal->publish_feedback(move_feedback);
305#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(cached_ik_kinematics_plugin::CachedIKKinematicsPlugin< kdl_kinematics_plugin::KDLKinematicsPlugin >, kinematics::KinematicsBase)
moveit_msgs::msg::PlanningScene clearSceneRobotState(const moveit_msgs::msg::PlanningScene &scene) const
planning_interface::MotionPlanRequest clearRequestStartState(const planning_interface::MotionPlanRequest &request) const
void convertToMsg(const std::vector< plan_execution::ExecutableTrajectory > &trajectory, moveit_msgs::msg::RobotState &first_state_msg, std::vector< moveit_msgs::msg::RobotTrajectory > &trajectory_msg) const
MoveGroupContextPtr context_
std::string getActionResultString(const moveit_msgs::msg::MoveItErrorCodes &error_code, bool planned_trajectory_empty, bool plan_only)
std::string stateToStr(MoveGroupState state) const
planning_pipeline::PlanningPipelinePtr resolvePlanningPipeline(const std::string &pipeline_id) const
void initialize() override
Representation of a robot's state. This includes position, velocity, acceleration and effort.
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
rclcpp::Logger getLogger()
moveit_msgs::msg::Constraints mergeConstraints(const moveit_msgs::msg::Constraints &first, const moveit_msgs::msg::Constraints &second)
Merge two sets of constraints into one.
bool isEmpty(const moveit_msgs::msg::PlanningScene &msg)
Check if a message includes any information about a planning scene, or whether it is empty.
planning_interface::MotionPlanResponse plan(std::shared_ptr< moveit_cpp::PlanningComponent > &planning_component, std::shared_ptr< moveit_cpp::PlanningComponent::PlanRequestParameters > &single_plan_parameters, std::shared_ptr< moveit_cpp::PlanningComponent::MultiPipelinePlanRequestParameters > &multi_plan_parameters, std::shared_ptr< planning_scene::PlanningScene > &planning_scene, std::optional< const moveit::planning_pipeline_interfaces::SolutionSelectionFunction > solution_selection_function, std::optional< moveit::planning_pipeline_interfaces::StoppingCriterionFunction > stopping_criterion_callback)
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
bool isTrajectoryEmpty(const moveit_msgs::msg::RobotTrajectory &trajectory)
Checks if a robot trajectory is empty.
A generic representation on what a computed motion plan looks like.
double replan_delay
The amount of time to wait in between replanning attempts (in seconds)
unsigned int replan_attemps
ExecutableMotionPlanComputationFn plan_callback
Callback for computing motion plans. This callback must always be specified.
std::function< void()> before_execution_callback_
bool replan
Flag indicating whether replanning is allowed.
Response to a planning query.
moveit::core::MoveItErrorCode error_code
robot_trajectory::RobotTrajectoryPtr trajectory