49 static const rclcpp::Logger
LOGGER =
50 rclcpp::get_logger(
"moveit_move_group_default_capabilities.move_action_capability");
60 auto node =
context_->moveit_cpp_->getNode();
61 execute_action_server_ = rclcpp_action::create_server<MGAction>(
63 [](
const rclcpp_action::GoalUUID& ,
const std::shared_ptr<const MGAction::Goal>& ) {
64 RCLCPP_INFO(LOGGER,
"Received request");
65 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
67 [](
const std::shared_ptr<MGActionGoal>& ) {
68 RCLCPP_INFO(LOGGER,
"Received request to cancel goal");
69 return rclcpp_action::CancelResponse::ACCEPT;
71 [
this](
const std::shared_ptr<MGActionGoal>& goal) {
return executeMoveCallback(goal); });
74 void MoveGroupMoveAction::executeMoveCallback(
const std::shared_ptr<MGActionGoal>& goal)
76 RCLCPP_INFO(LOGGER,
"executing..");
79 auto node =
context_->moveit_cpp_->getNode();
80 context_->planning_scene_monitor_->waitForCurrentRobotState(node->get_clock()->now());
81 context_->planning_scene_monitor_->updateFrameTransforms();
83 auto action_res = std::make_shared<MGAction::Result>();
84 if (goal->get_goal()->planning_options.plan_only || !
context_->allow_trajectory_execution_)
86 if (!goal->get_goal()->planning_options.plan_only)
87 RCLCPP_WARN(LOGGER,
"This instance of MoveGroup is not allowed to execute trajectories "
88 "but the goal request has plan_only set to false. "
89 "Only a motion plan will be computed anyway.");
90 executeMoveCallbackPlanOnly(goal, action_res);
93 executeMoveCallbackPlanAndExecute(goal, action_res);
98 goal->get_goal()->planning_options.plan_only));
99 if (action_res->error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
100 goal->succeed(action_res);
101 else if (action_res->error_code.val == moveit_msgs::msg::MoveItErrorCodes::PREEMPTED)
102 goal->canceled(action_res);
104 goal->abort(action_res);
106 setMoveState(
IDLE, goal);
107 preempt_requested_ =
false;
110 void MoveGroupMoveAction::executeMoveCallbackPlanAndExecute(
const std::shared_ptr<MGActionGoal>& goal,
111 std::shared_ptr<MGAction::Result>& action_res)
113 RCLCPP_INFO(LOGGER,
"Combined planning and execution request received for MoveGroup action. "
114 "Forwarding to planning and execution pipeline.");
122 for (std::size_t i = 0; i < goal->get_goal()->request.goal_constraints.size(); ++i)
123 if (lscene->isStateConstrained(
125 goal->get_goal()->request.path_constraints)))
127 RCLCPP_INFO(LOGGER,
"Goal constraints are already satisfied. No need to plan or execute any motions");
128 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
138 const moveit_msgs::msg::PlanningScene& planning_scene_diff =
140 goal->get_goal()->planning_options.planning_scene_diff :
143 opt.replan_ = goal->get_goal()->planning_options.replan;
144 opt.replan_attempts_ = goal->get_goal()->planning_options.replan_attempts;
145 opt.replan_delay_ = goal->get_goal()->planning_options.replan_delay;
146 opt.before_execution_callback_ = [
this] { startMoveExecutionCallback(); };
149 return planUsingPlanningPipeline(motion_plan_request,
plan);
153 if (preempt_requested_)
155 RCLCPP_INFO(LOGGER,
"Preempt requested before the goal is planned and executed.");
156 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
160 context_->plan_execution_->planAndExecute(
plan, planning_scene_diff,
opt);
162 convertToMsg(
plan.plan_components_, action_res->trajectory_start, action_res->planned_trajectory);
163 if (
plan.executed_trajectory_)
164 plan.executed_trajectory_->getRobotTrajectoryMsg(action_res->executed_trajectory);
165 action_res->error_code =
plan.error_code_;
168 void MoveGroupMoveAction::executeMoveCallbackPlanOnly(
const std::shared_ptr<MGActionGoal>& goal,
169 std::shared_ptr<MGAction::Result>& action_res)
171 RCLCPP_INFO(LOGGER,
"Planning request received for MoveGroup action. Forwarding to planning pipeline.");
175 const planning_scene::PlanningSceneConstPtr& the_scene =
177 static_cast<const planning_scene::PlanningSceneConstPtr&
>(lscene) :
178 lscene->diff(goal->get_goal()->planning_options.planning_scene_diff);
181 if (preempt_requested_)
183 RCLCPP_INFO(LOGGER,
"Preempt requested before the goal is planned.");
184 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
193 action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
201 catch (std::exception& ex)
203 RCLCPP_ERROR(LOGGER,
"Planning pipeline threw an exception: %s", ex.what());
204 res.
error_code_.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
224 res.
error_code_.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
233 catch (std::exception& ex)
235 RCLCPP_ERROR(LOGGER,
"Planning pipeline threw an exception: %s", ex.what());
236 res.
error_code_.val = moveit_msgs::msg::MoveItErrorCodes::FAILURE;
240 plan.plan_components_.resize(1);
242 plan.plan_components_[0].description_ =
"plan";
249 void MoveGroupMoveAction::startMoveExecutionCallback()
251 setMoveState(
MONITOR,
nullptr);
254 void MoveGroupMoveAction::startMoveLookCallback()
256 setMoveState(
LOOK,
nullptr);
259 void MoveGroupMoveAction::preemptMoveCallback()
261 preempt_requested_ =
true;
265 void MoveGroupMoveAction::setMoveState(
MoveGroupState state,
const std::shared_ptr<MGActionGoal>& goal)
271 auto move_feedback = std::make_shared<MGAction::Feedback>();
273 goal->publish_feedback(move_feedback);
278 #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.
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.
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
const rclcpp::Logger LOGGER
bool isTrajectoryEmpty(const moveit_msgs::msg::RobotTrajectory &trajectory)
A generic representation on what a computed motion plan looks like.
robot_trajectory::RobotTrajectoryPtr trajectory_
moveit_msgs::msg::MoveItErrorCodes error_code_