43#include <boost/algorithm/string/join.hpp>
44#include <rclcpp/logger.hpp>
45#include <rclcpp/logging.hpp>
46#include <rclcpp/node.hpp>
47#include <rclcpp/parameter_value.hpp>
48#include <rclcpp/rate.hpp>
49#include <rclcpp/utilities.hpp>
81 const rclcpp::Node::SharedPtr& node,
const planning_scene_monitor::PlanningSceneMonitorPtr&
planning_scene_monitor,
82 const trajectory_execution_manager::TrajectoryExecutionManagerPtr& trajectory_execution)
85 , trajectory_execution_manager_(trajectory_execution)
86 , logger_(
moveit::getLogger(
"moveit.ros.add_time_optimal_parameterization"))
88 if (!trajectory_execution_manager_)
90 trajectory_execution_manager_ = std::make_shared<trajectory_execution_manager::TrajectoryExecutionManager>(
91 node_, planning_scene_monitor_->getRobotModel(), planning_scene_monitor_->getStateMonitor());
94 default_max_replan_attempts_ = 5;
96 new_scene_update_ =
false;
99 planning_scene_monitor_->addUpdateCallback(
101 planningSceneUpdatedCallback(update_type);
120 plan.planning_scene_monitor = planning_scene_monitor_;
121 plan.planning_scene = planning_scene_monitor_->getPlanningScene();
122 planAndExecuteHelper(plan, opt);
126 const moveit_msgs::msg::PlanningScene& scene_diff,
131 planAndExecute(plan, opt);
135 plan.planning_scene_monitor = planning_scene_monitor_;
141 plan.planning_scene = lscene->diff(scene_diff);
143 planAndExecuteHelper(plan, opt);
147void plan_execution::PlanExecution::planAndExecuteHelper(
ExecutableMotionPlan& plan,
const Options& opt)
150 preempt_.checkAndClear();
152 bool preempt_requested =
false;
155 unsigned int max_replan_attempts =
156 opt.replan ? (opt.replan_attemps > 0 ? opt.replan_attemps : default_max_replan_attempts_) : 1;
157 unsigned int replan_attempts = 0;
158 bool previously_solved =
false;
165 RCLCPP_INFO(logger_,
"Planning attempt %u of at most %u", replan_attempts, max_replan_attempts);
167 if (opt.before_plan_callback_)
168 opt.before_plan_callback_();
170 new_scene_update_ =
false;
176 (!previously_solved || !opt.repair_plan_callback_) ?
177 opt.plan_callback(plan) :
178 opt.repair_plan_callback_(plan, trajectory_execution_manager_->getCurrentExpectedTrajectoryIndex());
180 preempt_requested = preempt_.checkAndClear();
181 if (preempt_requested)
186 if (plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::PLANNING_FAILED ||
187 plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN ||
188 plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::UNABLE_TO_AQUIRE_SENSOR_DATA)
190 if (plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::UNABLE_TO_AQUIRE_SENSOR_DATA &&
191 opt.replan_delay > 0.0)
193 auto replan_delay_seconds = std::chrono::duration<double>(opt.replan_delay);
194 rclcpp::sleep_for(std::chrono::duration_cast<std::chrono::nanoseconds>(replan_delay_seconds));
202 previously_solved =
true;
209 if (
plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
211 if (opt.before_execution_callback_)
212 opt.before_execution_callback_();
214 preempt_requested = preempt_.checkAndClear();
215 if (preempt_requested)
222 if (
plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::PREEMPTED)
223 preempt_requested =
true;
226 if (
plan.
error_code.val != moveit_msgs::msg::MoveItErrorCodes::MOTION_PLAN_INVALIDATED_BY_ENVIRONMENT_CHANGE)
233 if (opt.replan_delay > 0.0)
235 RCLCPP_INFO(logger_,
"Waiting for a %lf seconds before attempting a new plan ...", opt.replan_delay);
236 auto replan_delay_seconds = std::chrono::duration<double>(opt.replan_delay);
237 rclcpp::sleep_for(std::chrono::duration_cast<std::chrono::nanoseconds>(replan_delay_seconds));
238 RCLCPP_INFO(logger_,
"Done waiting");
242 preempt_requested = preempt_.checkAndClear();
243 if (preempt_requested)
246 }
while (replan_attempts < max_replan_attempts);
248 if (preempt_requested)
250 RCLCPP_DEBUG(logger_,
"PlanExecution was preempted");
251 plan.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
254 if (opt.done_callback_)
255 opt.done_callback_();
257 if (
plan.
error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
259 RCLCPP_DEBUG(logger_,
"PlanExecution finished successfully.");
263 RCLCPP_DEBUG(logger_,
"PlanExecution terminating with error code %d - '%s'",
plan.
error_code.val,
268bool plan_execution::PlanExecution::isRemainingPathValid(
const ExecutableMotionPlan& plan,
269 const std::pair<int, int>& path_segment)
271 if (path_segment.first >= 0 &&
272 plan.plan_components[path_segment.first].trajectory_monitoring)
281 plan.plan_components[path_segment.first].allowed_collision_matrix.get();
285 for (std::size_t i = std::max(path_segment.second - 1, 0); i < wpc; ++i)
290 plan.planning_scene->checkCollisionUnpadded(req, res, t.
getWayPoint(i), *acm);
294 plan.planning_scene->checkCollisionUnpadded(req, res, t.
getWayPoint(i));
300 RCLCPP_INFO(logger_,
"Trajectory component '%s' is invalid",
301 plan.plan_components[path_segment.first].description.c_str());
309 plan.planning_scene->checkCollisionUnpadded(req, res, t.
getWayPoint(i), *acm);
313 plan.planning_scene->checkCollisionUnpadded(req, res, t.
getWayPoint(i));
323 bool reset_preempted)
326 preempt_.checkAndClear();
328 if (!plan.planning_scene_monitor)
329 plan.planning_scene_monitor = planning_scene_monitor_;
330 if (!plan.planning_scene)
331 plan.planning_scene = planning_scene_monitor_->getPlanningScene();
333 moveit_msgs::msg::MoveItErrorCodes result;
336 execution_complete_ =
true;
338 if (!trajectory_execution_manager_)
340 RCLCPP_ERROR(logger_,
"No trajectory execution manager");
341 result.val = moveit_msgs::msg::MoveItErrorCodes::CONTROL_FAILED;
345 if (plan.plan_components.empty())
347 result.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
351 execution_complete_ =
false;
355 for (
size_t component_idx = 0; component_idx < plan.plan_components.size(); ++component_idx)
362 bool unwound =
false;
363 for (
int prev_component = component_idx - 1; prev_component >= 0; --prev_component)
367 if (plan.plan_components.at(prev_component).
trajectory &&
368 plan.plan_components.at(prev_component).
trajectory->getGroup() ==
369 plan.plan_components.at(prev_component).
trajectory->getGroup() &&
370 !plan.plan_components.at(prev_component).
trajectory->empty())
372 plan.plan_components.at(component_idx)
385 plan.plan_components[component_idx].
trajectory->unwind(
386 plan.planning_scene_monitor && plan.planning_scene_monitor->getStateMonitor() ?
387 *plan.planning_scene_monitor->getStateMonitor()->getCurrentState() :
388 plan.planning_scene->getCurrentState());
392 plan.plan_components[component_idx].
trajectory->unwind(plan.plan_components[prev].
trajectory->getLastWayPoint());
396 if (plan.plan_components[component_idx].
trajectory && !plan.plan_components[component_idx].
trajectory->empty())
397 prev = component_idx;
400 moveit_msgs::msg::RobotTrajectory msg;
401 plan.plan_components[component_idx].
trajectory->getRobotTrajectoryMsg(msg);
402 if (!trajectory_execution_manager_->push(msg, plan.plan_components[component_idx].controller_name))
404 trajectory_execution_manager_->clear();
405 RCLCPP_ERROR(logger_,
"Apparently trajectory initialization failed");
406 execution_complete_ =
true;
407 result.val = moveit_msgs::msg::MoveItErrorCodes::CONTROL_FAILED;
412 if (!trajectory_monitor_ && planning_scene_monitor_->getStateMonitor())
415 double sampling_frequency = 0.0;
416 node_->get_parameter_or(
"plan_execution.record_trajectory_state_frequency", sampling_frequency, 0.0);
417 trajectory_monitor_ = std::make_shared<planning_scene_monitor::TrajectoryMonitor>(
418 planning_scene_monitor_->getStateMonitor(), sampling_frequency);
422 if (trajectory_monitor_)
423 trajectory_monitor_->startTrajectoryMonitor();
426 trajectory_execution_manager_->execute(
428 [
this, &plan](std::size_t index) { successfulTrajectorySegmentExecution(plan, index); });
430 rclcpp::WallRate r(100);
431 path_became_invalid_ =
false;
432 bool preempt_requested =
false;
434 while (rclcpp::ok() && !execution_complete_ && !path_became_invalid_)
438 if (new_scene_update_)
440 new_scene_update_ =
false;
441 std::pair<int, int> current_index = trajectory_execution_manager_->getCurrentExpectedTrajectoryIndex();
442 if (!isRemainingPathValid(plan, current_index))
444 RCLCPP_INFO(logger_,
"Trajectory component '%s' is invalid after scene update",
445 plan.plan_components[current_index.first].description.c_str());
446 path_became_invalid_ =
true;
451 preempt_requested = preempt_.checkAndClear();
452 if (preempt_requested)
457 if (preempt_requested)
459 RCLCPP_INFO(logger_,
"Stopping execution due to preempt request");
460 trajectory_execution_manager_->stopExecution();
462 else if (path_became_invalid_)
464 RCLCPP_INFO(logger_,
"Stopping execution because the path to execute became invalid"
465 "(probably the environment changed)");
466 trajectory_execution_manager_->stopExecution();
468 else if (!execution_complete_)
470 RCLCPP_WARN(logger_,
"Stopping execution due to unknown reason."
471 "Possibly the node is about to shut down.");
472 trajectory_execution_manager_->stopExecution();
476 if (trajectory_monitor_)
478 trajectory_monitor_->stopTrajectoryMonitor();
479 plan.executed_trajectory =
480 std::make_shared<robot_trajectory::RobotTrajectory>(planning_scene_monitor_->getRobotModel(),
"");
481 trajectory_monitor_->swapTrajectory(*plan.executed_trajectory);
485 if (path_became_invalid_)
487 result.val = moveit_msgs::msg::MoveItErrorCodes::MOTION_PLAN_INVALIDATED_BY_ENVIRONMENT_CHANGE;
491 if (preempt_requested)
493 result.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
497 if (trajectory_execution_manager_->getLastExecutionStatus() ==
500 result.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
502 else if (trajectory_execution_manager_->getLastExecutionStatus() ==
505 result.val = moveit_msgs::msg::MoveItErrorCodes::TIMED_OUT;
509 result.val = moveit_msgs::msg::MoveItErrorCodes::CONTROL_FAILED;
516void plan_execution::PlanExecution::planningSceneUpdatedCallback(
521 new_scene_update_ =
true;
524void plan_execution::PlanExecution::doneWithTrajectoryExecution(
527 execution_complete_ =
true;
530void plan_execution::PlanExecution::successfulTrajectorySegmentExecution(
const ExecutableMotionPlan& plan,
533 if (
plan.plan_components.empty())
535 RCLCPP_WARN(logger_,
"Length of provided motion plan is zero.");
540 RCLCPP_DEBUG(logger_,
"Completed '%s'",
plan.plan_components[index].description.c_str());
541 if (
plan.plan_components[index].effect_on_success)
543 if (!
plan.plan_components[index].effect_on_success(&plan))
546 RCLCPP_ERROR(logger_,
"Execution of path-completion side-effect failed. Preempting.");
557 std::pair<int, int> next_index(
static_cast<int>(index), 0);
558 if (!isRemainingPathValid(plan, next_index))
560 RCLCPP_INFO(logger_,
"Upcoming trajectory component '%s' is invalid",
561 plan.plan_components[next_index.first].description.c_str());
562 path_became_invalid_ =
true;
Definition of a structure for the allowed collision matrix. All elements in the collision world are r...
void planAndExecute(ExecutableMotionPlan &plan, const Options &opt)
PlanExecution(const rclcpp::Node::SharedPtr &node, const planning_scene_monitor::PlanningSceneMonitorPtr &planning_scene_monitor, const trajectory_execution_manager::TrajectoryExecutionManagerPtr &trajectory_execution)
moveit_msgs::msg::MoveItErrorCodes executeAndMonitor(ExecutableMotionPlan &plan, bool reset_preempted=true)
Execute and monitor a previously created plan.
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
@ UPDATE_GEOMETRY
The geometry of the scene was updated. This includes receiving new octomaps, collision objects,...
@ UPDATE_TRANSFORMS
The maintained set of fixed transforms in the monitored scene was updated.
Maintain a sequence of waypoints and the time durations between these waypoints.
const std::string & getGroupName() const
std::size_t getWayPointCount() const
const moveit::core::RobotState & getWayPoint(std::size_t index) const
bool isEmpty(const moveit_msgs::msg::PlanningScene &msg)
Check if a message includes any information about a planning scene, or whether it is empty.
std::string errorCodeToString(const MoveItErrorCode &error_code)
Convenience function to translated error message into string.
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)
Main namespace for MoveIt.
This namespace includes functionality specific to the execution and monitoring of motion plans.
Representation of a collision checking request.
std::string group_name
The group name to check collisions for (optional; if empty, assume the complete robot)....
bool verbose
Flag indicating whether information about detected collisions should be reported.
Representation of a collision checking result.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW void clear()
Clear a previously stored result.
bool collision
True if collision was found, false otherwise.
The reported execution status.
A generic representation on what a computed motion plan looks like.
moveit::core::MoveItErrorCode error_code
robot_trajectory::RobotTrajectoryPtr trajectory