48 constexpr
char LOGNAME[] =
"planning_component";
53 joint_model_group_ = moveit_cpp_->getRobotModel()->getJointModelGroup(group_name);
54 if (!joint_model_group_)
56 std::string error =
"Could not find joint model group '" + group_name +
"'.";
58 throw std::runtime_error(error);
60 plan_request_parameters_.load(nh_);
62 LOGNAME,
"Plan request parameters loaded with --"
63 <<
" planning_pipeline: " << plan_request_parameters_.planning_pipeline <<
","
64 <<
" planner_id: " << plan_request_parameters_.planner_id <<
","
65 <<
" planning_time: " << plan_request_parameters_.planning_time <<
","
66 <<
" planning_attempts: " << plan_request_parameters_.planning_attempts <<
","
67 <<
" max_velocity_scaling_factor: " << plan_request_parameters_.max_velocity_scaling_factor <<
","
68 <<
" max_acceleration_scaling_factor: " << plan_request_parameters_.max_acceleration_scaling_factor);
76 PlanningComponent::~PlanningComponent()
82 const std::vector<std::string> PlanningComponent::getNamedTargetStates()
84 if (joint_model_group_)
86 return joint_model_group_->getDefaultStateNames();
93 std::vector<std::string> empty;
97 const std::string& PlanningComponent::getPlanningGroupName()
const
102 bool PlanningComponent::setPathConstraints(
const moveit_msgs::Constraints& path_constraints)
104 current_path_constraints_ = path_constraints;
108 bool PlanningComponent::setTrajectoryConstraints(
const moveit_msgs::TrajectoryConstraints& trajectory_constraints)
110 current_trajectory_constraints_ = trajectory_constraints;
115 const bool store_solution)
118 if (!joint_model_group_)
121 plan_solution.error_code_ = moveit::core::MoveItErrorCode::INVALID_GROUP_NAME;
124 last_plan_solution_ = plan_solution;
126 return plan_solution;
131 moveit_cpp_->getPlanningSceneMonitorNonConst();
141 req.group_name = group_name_;
147 if (workspace_parameters_set_)
148 req.workspace_parameters = workspace_parameters_;
151 moveit::core::RobotStatePtr start_state = considered_start_state_;
153 start_state = moveit_cpp_->getCurrentState();
154 start_state->update();
159 if (current_goal_constraints_.empty())
162 plan_solution.error_code_ = moveit::core::MoveItErrorCode::INVALID_GOAL_CONSTRAINTS;
165 last_plan_solution_ = plan_solution;
167 return plan_solution;
169 req.goal_constraints = current_goal_constraints_;
172 req.path_constraints = current_path_constraints_;
175 req.trajectory_constraints = current_trajectory_constraints_;
179 const auto& pipelines = moveit_cpp_->getPlanningPipelines();
181 if (it == pipelines.end())
184 plan_solution.error_code_ = moveit::core::MoveItErrorCode::FAILURE;
187 last_plan_solution_ = plan_solution;
189 return plan_solution;
191 const planning_pipeline::PlanningPipelinePtr pipeline = it->second;
196 ROS_ERROR(
"Could not compute plan successfully");
199 last_plan_solution_ = plan_solution;
201 return plan_solution;
205 plan_solution.start_state_ = req.start_state;
222 last_plan_solution_ = plan_solution;
224 return plan_solution;
228 PlanningComponent::plan(
const MultiPipelinePlanRequestParameters& parameters,
229 const SolutionCallbackFunction& solution_selection_callback,
230 const StoppingCriterionFunction& stopping_criterion_callback)
233 PlanningComponent::PlanSolutions planning_solutions{ parameters.multi_plan_request_parameters.size() };
234 std::vector<std::thread> planning_threads;
235 planning_threads.reserve(parameters.multi_plan_request_parameters.size());
239 auto const hardware_concurrency = std::thread::hardware_concurrency();
240 if (parameters.multi_plan_request_parameters.size() > hardware_concurrency && hardware_concurrency != 0)
244 "More parallel planning problems defined ('%ld') than possible to solve concurrently with the hardware ('%d')",
245 parameters.multi_plan_request_parameters.size(), hardware_concurrency);
249 for (
auto const& plan_request_parameter : parameters.multi_plan_request_parameters)
251 auto planning_thread = std::thread([&]() {
255 plan_solution = plan(plan_request_parameter,
false);
257 catch (
const std::exception& e)
260 <<
"' threw exception '" << e.what() <<
"'");
261 plan_solution.error_code_ = moveit::core::MoveItErrorCode::FAILURE;
263 plan_solution.planner_id_ = plan_request_parameter.planner_id;
264 planning_solutions.pushBack(plan_solution);
266 if (stopping_criterion_callback !=
nullptr)
268 if (stopping_criterion_callback(planning_solutions, parameters))
272 "Stopping criterion met: Terminating planning pipelines that are still active");
273 for (
auto const& plan_request_parameter : parameters.multi_plan_request_parameters)
275 moveit_cpp_->terminatePlanningPipeline(plan_request_parameter.planning_pipeline);
280 planning_threads.push_back(std::move(planning_thread));
284 for (
auto& planning_thread : planning_threads)
286 if (planning_thread.joinable())
288 planning_thread.join();
293 last_plan_solution_ = solution_selection_callback(planning_solutions.getSolutions());
294 return last_plan_solution_;
299 return plan(plan_request_parameters_);
304 considered_start_state_ = std::make_shared<moveit::core::RobotState>(start_state);
308 moveit::core::RobotStatePtr PlanningComponent::getStartState()
310 if (considered_start_state_)
311 return considered_start_state_;
314 moveit::core::RobotStatePtr
s;
315 moveit_cpp_->getCurrentState(s, 1.0);
320 bool PlanningComponent::setStartState(
const std::string& start_state_name)
322 const auto& named_targets = getNamedTargetStates();
323 if (std::find(named_targets.begin(), named_targets.end(), start_state_name) == named_targets.end())
325 ROS_ERROR_NAMED(
LOGNAME,
"No predefined joint state found for target name '%s'", start_state_name.c_str());
329 start_state.setToDefaultValues(joint_model_group_, start_state_name);
330 return setStartState(start_state);
333 void PlanningComponent::setStartStateToCurrentState()
335 considered_start_state_.reset();
338 std::map<std::string, double> PlanningComponent::getNamedTargetStateValues(
const std::string& name)
341 std::map<std::string, double> positions;
342 joint_model_group_->getVariableDefaultPositions(name, positions);
346 void PlanningComponent::setWorkspace(
double minx,
double miny,
double minz,
double maxx,
double maxy,
double maxz)
348 workspace_parameters_.header.frame_id = moveit_cpp_->getRobotModel()->getModelFrame();
350 workspace_parameters_.min_corner.x = minx;
351 workspace_parameters_.min_corner.y = miny;
352 workspace_parameters_.min_corner.z = minz;
353 workspace_parameters_.max_corner.x = maxx;
354 workspace_parameters_.max_corner.y = maxy;
355 workspace_parameters_.max_corner.z = maxz;
356 workspace_parameters_set_ =
true;
359 void PlanningComponent::unsetWorkspace()
361 workspace_parameters_set_ =
false;
364 bool PlanningComponent::setGoal(
const std::vector<moveit_msgs::Constraints>& goal_constraints)
366 current_goal_constraints_ = goal_constraints;
376 bool PlanningComponent::setGoal(
const geometry_msgs::PoseStamped& goal_pose,
const std::string& link_name)
382 bool PlanningComponent::setGoal(
const std::string& goal_state_name)
384 const auto& named_targets = getNamedTargetStates();
385 if (std::find(named_targets.begin(), named_targets.end(), goal_state_name) == named_targets.end())
387 ROS_ERROR_NAMED(
LOGNAME,
"No predefined joint state found for target name '%s'", goal_state_name.c_str());
391 goal_state.setToDefaultValues(joint_model_group_, goal_state_name);
392 return setGoal(goal_state);
395 bool PlanningComponent::execute(
bool blocking)
397 if (!last_plan_solution_)
411 return moveit_cpp_->execute(group_name_, last_plan_solution_.trajectory_, blocking);
416 return last_plan_solution_;
423 auto const shortest_trajectory = std::min_element(solutions.begin(), solutions.end(),
427 if (solution_a && solution_b)
429 return robot_trajectory::path_length(*solution_a.trajectory_) <
430 robot_trajectory::path_length(*solution_b.trajectory_);
440 if (shortest_trajectory->trajectory_ !=
nullptr)
449 return *shortest_trajectory;
452 void PlanningComponent::clearContents()
454 considered_start_state_.reset();
456 current_goal_constraints_.clear();