moveit2
The MoveIt Motion Planning Framework for ROS 2.
planning_pipeline.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
41 #include <boost/tokenizer.hpp>
42 #include <boost/algorithm/string/join.hpp>
43 #include <sstream>
44 
45 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit.ros_planning.planning_pipeline");
46 
47 const std::string planning_pipeline::PlanningPipeline::DISPLAY_PATH_TOPIC = "display_planned_path";
48 const std::string planning_pipeline::PlanningPipeline::MOTION_PLAN_REQUEST_TOPIC = "motion_plan_request";
49 const std::string planning_pipeline::PlanningPipeline::MOTION_CONTACTS_TOPIC = "display_contacts";
50 
51 planning_pipeline::PlanningPipeline::PlanningPipeline(const moveit::core::RobotModelConstPtr& model,
52  const std::shared_ptr<rclcpp::Node>& node,
53  const std::string& parameter_namespace,
54  const std::string& planner_plugin_param_name,
55  const std::string& adapter_plugins_param_name)
56  : node_(node), parameter_namespace_(parameter_namespace), robot_model_(model)
57 {
58  std::string planner_plugin_fullname = parameter_namespace_ + "." + planner_plugin_param_name;
59  if (parameter_namespace_.empty())
60  planner_plugin_fullname = planner_plugin_param_name;
61  if (node_->has_parameter(planner_plugin_fullname))
62  {
63  node_->get_parameter(planner_plugin_fullname, planner_plugin_name_);
64  }
65 
66  std::string adapter_plugins_fullname = parameter_namespace_ + "." + adapter_plugins_param_name;
67  if (parameter_namespace_.empty())
68  adapter_plugins_fullname = adapter_plugins_param_name;
69 
70  std::string adapters;
71  if (node_->has_parameter(adapter_plugins_fullname))
72  {
73  node_->get_parameter(adapter_plugins_fullname, adapters);
74  boost::char_separator<char> sep(" ");
75  boost::tokenizer<boost::char_separator<char>> tok(adapters, sep);
76  for (boost::tokenizer<boost::char_separator<char>>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
77  adapter_plugin_names_.push_back(*beg);
78  }
79 
80  configure();
81 }
82 
83 planning_pipeline::PlanningPipeline::PlanningPipeline(const moveit::core::RobotModelConstPtr& model,
84  const std::shared_ptr<rclcpp::Node>& node,
85  const std::string& parameter_namespace,
86  const std::string& planner_plugin_name,
87  const std::vector<std::string>& adapter_plugin_names)
88  : node_(node)
89  , parameter_namespace_(parameter_namespace)
90  , planner_plugin_name_(planner_plugin_name)
91  , adapter_plugin_names_(adapter_plugin_names)
92  , robot_model_(model)
93 {
94  configure();
95 }
96 
97 void planning_pipeline::PlanningPipeline::configure()
98 {
99  check_solution_paths_ = false; // this is set to true below
100  publish_received_requests_ = false;
101  display_computed_motion_plans_ = false; // this is set to true below
102 
103  // load the planning plugin
104  try
105  {
106  planner_plugin_loader_ = std::make_unique<pluginlib::ClassLoader<planning_interface::PlannerManager>>(
107  "moveit_core", "planning_interface::PlannerManager");
108  }
109  catch (pluginlib::PluginlibException& ex)
110  {
111  RCLCPP_FATAL(LOGGER, "Exception while creating planning plugin loader %s", ex.what());
112  }
113 
114  std::vector<std::string> classes;
115  if (planner_plugin_loader_)
116  classes = planner_plugin_loader_->getDeclaredClasses();
117  if (planner_plugin_name_.empty() && classes.size() == 1)
118  {
119  planner_plugin_name_ = classes[0];
120  RCLCPP_INFO(
121  LOGGER,
122  "No '~planning_plugin' parameter specified, but only '%s' planning plugin is available. Using that one.",
123  planner_plugin_name_.c_str());
124  }
125  if (planner_plugin_name_.empty() && classes.size() > 1)
126  {
127  planner_plugin_name_ = classes[0];
128  RCLCPP_INFO(
129  LOGGER,
130  "Multiple planning plugins available. You should specify the '~planning_plugin' parameter. Using '%s' for "
131  "now.",
132  planner_plugin_name_.c_str());
133  }
134  try
135  {
136  planner_instance_ = planner_plugin_loader_->createUniqueInstance(planner_plugin_name_);
137  if (!planner_instance_->initialize(robot_model_, node_, parameter_namespace_))
138  throw std::runtime_error("Unable to initialize planning plugin");
139  RCLCPP_INFO(LOGGER, "Using planning interface '%s'", planner_instance_->getDescription().c_str());
140  }
141  catch (pluginlib::PluginlibException& ex)
142  {
143  std::string classes_str = boost::algorithm::join(classes, ", ");
144  RCLCPP_ERROR(LOGGER,
145  "Exception while loading planner '%s': %s"
146  "Available plugins: %s",
147  planner_plugin_name_.c_str(), ex.what(), classes_str.c_str());
148  }
149 
150  // load the planner request adapters
151  if (!adapter_plugin_names_.empty())
152  {
153  std::vector<planning_request_adapter::PlanningRequestAdapterConstPtr> ads;
154  try
155  {
156  adapter_plugin_loader_ =
157  std::make_unique<pluginlib::ClassLoader<planning_request_adapter::PlanningRequestAdapter>>(
158  "moveit_core", "planning_request_adapter::PlanningRequestAdapter");
159  }
160  catch (pluginlib::PluginlibException& ex)
161  {
162  RCLCPP_ERROR(LOGGER, "Exception while creating planning plugin loader %s", ex.what());
163  }
164 
165  if (adapter_plugin_loader_)
166  for (const std::string& adapter_plugin_name : adapter_plugin_names_)
167  {
168  planning_request_adapter::PlanningRequestAdapterPtr ad;
169  try
170  {
171  ad = adapter_plugin_loader_->createUniqueInstance(adapter_plugin_name);
172  }
173  catch (pluginlib::PluginlibException& ex)
174  {
175  RCLCPP_ERROR(LOGGER, "Exception while loading planning adapter plugin '%s': %s", adapter_plugin_name.c_str(),
176  ex.what());
177  }
178  if (ad)
179  {
180  ad->initialize(node_, parameter_namespace_);
181  ads.push_back(std::move(ad));
182  }
183  }
184  if (!ads.empty())
185  {
186  adapter_chain_ = std::make_unique<planning_request_adapter::PlanningRequestAdapterChain>();
187  for (planning_request_adapter::PlanningRequestAdapterConstPtr& ad : ads)
188  {
189  RCLCPP_INFO(LOGGER, "Using planning request adapter '%s'", ad->getDescription().c_str());
190  adapter_chain_->addAdapter(ad);
191  }
192  }
193  }
194  displayComputedMotionPlans(true);
195  checkSolutionPaths(true);
196 }
197 
199 {
200  if (display_computed_motion_plans_ && !flag)
201  display_path_publisher_.reset();
202  else if (!display_computed_motion_plans_ && flag)
203  {
204  display_path_publisher_ = node_->create_publisher<moveit_msgs::msg::DisplayTrajectory>(DISPLAY_PATH_TOPIC, 10);
205  }
206  display_computed_motion_plans_ = flag;
207 }
208 
210 {
211  if (publish_received_requests_ && !flag)
212  received_request_publisher_.reset();
213  else if (!publish_received_requests_ && flag)
214  {
215  received_request_publisher_ =
216  node_->create_publisher<moveit_msgs::msg::MotionPlanRequest>(MOTION_PLAN_REQUEST_TOPIC, 10);
217  }
218  publish_received_requests_ = flag;
219 }
220 
222 {
223  if (check_solution_paths_ && !flag)
224  contacts_publisher_.reset();
225  else if (!check_solution_paths_ && flag)
226  {
227  contacts_publisher_ = node_->create_publisher<visualization_msgs::msg::MarkerArray>(MOTION_CONTACTS_TOPIC, 10);
228  }
229  check_solution_paths_ = flag;
230 }
231 
232 bool planning_pipeline::PlanningPipeline::generatePlan(const planning_scene::PlanningSceneConstPtr& planning_scene,
235 {
236  std::vector<std::size_t> dummy;
237  return generatePlan(planning_scene, req, res, dummy);
238 }
239 
240 bool planning_pipeline::PlanningPipeline::generatePlan(const planning_scene::PlanningSceneConstPtr& planning_scene,
243  std::vector<std::size_t>& adapter_added_state_index) const
244 {
245  // broadcast the request we are about to work on, if needed
246  if (publish_received_requests_)
247  received_request_publisher_->publish(req);
248  adapter_added_state_index.clear();
249 
250  if (!planner_instance_)
251  {
252  RCLCPP_ERROR(LOGGER, "No planning plugin loaded. Cannot plan.");
253  return false;
254  }
255 
256  bool solved = false;
257  try
258  {
259  if (adapter_chain_)
260  {
261  solved = adapter_chain_->adaptAndPlan(planner_instance_, planning_scene, req, res, adapter_added_state_index);
262  if (!adapter_added_state_index.empty())
263  {
264  std::stringstream ss;
265  for (std::size_t added_index : adapter_added_state_index)
266  ss << added_index << " ";
267  RCLCPP_INFO(LOGGER, "Planning adapters have added states at index positions: [ %s]", ss.str().c_str());
268  }
269  }
270  else
271  {
272  planning_interface::PlanningContextPtr context =
273  planner_instance_->getPlanningContext(planning_scene, req, res.error_code_);
274  solved = context ? context->solve(res) : false;
275  }
276  }
277  catch (std::exception& ex)
278  {
279  RCLCPP_ERROR(LOGGER, "Exception caught: '%s'", ex.what());
280  return false;
281  }
282  bool valid = true;
283 
284  if (solved && res.trajectory_)
285  {
286  std::size_t state_count = res.trajectory_->getWayPointCount();
287  RCLCPP_DEBUG(LOGGER, "Motion planner reported a solution path with %ld states", state_count);
288  if (check_solution_paths_)
289  {
290  visualization_msgs::msg::MarkerArray arr;
291  visualization_msgs::msg::Marker m;
292  m.action = visualization_msgs::msg::Marker::DELETEALL;
293  arr.markers.push_back(m);
294 
295  std::vector<std::size_t> index;
296  if (!planning_scene->isPathValid(*res.trajectory_, req.path_constraints, req.group_name, false, &index))
297  {
298  // check to see if there is any problem with the states that are found to be invalid
299  // they are considered ok if they were added by a planning request adapter
300  bool problem = false;
301  for (std::size_t i = 0; i < index.size() && !problem; ++i)
302  {
303  bool found = false;
304  for (std::size_t added_index : adapter_added_state_index)
305  if (index[i] == added_index)
306  {
307  found = true;
308  break;
309  }
310  if (!found)
311  problem = true;
312  }
313  if (problem)
314  {
315  if (index.size() == 1 && index[0] == 0)
316  { // ignore cases when the robot starts at invalid location
317  RCLCPP_DEBUG(LOGGER, "It appears the robot is starting at an invalid state, but that is ok.");
318  }
319  else
320  {
321  valid = false;
322  res.error_code_.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN;
323 
324  // display error messages
325  std::stringstream ss;
326  for (std::size_t it : index)
327  ss << it << " ";
328 
329  RCLCPP_ERROR_STREAM(LOGGER, "Computed path is not valid. Invalid states at index locations: [ "
330  << ss.str() << "] out of " << state_count
331  << ". Explanations follow in command line. Contacts are published on "
332  << contacts_publisher_->get_topic_name());
333 
334  // call validity checks in verbose mode for the problematic states
335  for (std::size_t it : index)
336  {
337  // check validity with verbose on
338  const moveit::core::RobotState& robot_state = res.trajectory_->getWayPoint(it);
339  planning_scene->isStateValid(robot_state, req.path_constraints, req.group_name, true);
340 
341  // compute the contacts if any
344  c_req.contacts = true;
345  c_req.max_contacts = 10;
346  c_req.max_contacts_per_pair = 3;
347  c_req.verbose = false;
348  planning_scene->checkCollision(c_req, c_res, robot_state);
349  if (c_res.contact_count > 0)
350  {
351  visualization_msgs::msg::MarkerArray arr_i;
353  c_res.contacts);
354  arr.markers.insert(arr.markers.end(), arr_i.markers.begin(), arr_i.markers.end());
355  }
356  }
357  RCLCPP_ERROR(LOGGER, "Completed listing of explanations for invalid states.");
358  }
359  }
360  else
361  {
362  RCLCPP_DEBUG(LOGGER,
363  "Planned path was found to be valid, except for states that were added by planning request "
364  "adapters, but that is ok.");
365  }
366  }
367  else
368  RCLCPP_DEBUG(LOGGER, "Planned path was found to be valid when rechecked");
369  contacts_publisher_->publish(arr);
370  }
371  }
372 
373  // display solution path if needed
374  if (display_computed_motion_plans_ && solved)
375  {
376  moveit_msgs::msg::DisplayTrajectory disp;
377  disp.model_id = robot_model_->getName();
378  disp.trajectory.resize(1);
379  res.trajectory_->getRobotTrajectoryMsg(disp.trajectory[0]);
380  moveit::core::robotStateToRobotStateMsg(res.trajectory_->getFirstWayPoint(), disp.trajectory_start);
381  display_path_publisher_->publish(disp);
382  }
383 
384  if (!solved)
385  {
386  // This should alert the user if planning failed because of contradicting constraints.
387  // Could be checked more thoroughly, but it is probably not worth going to that length.
388  bool stacked_constraints = false;
389  if (req.path_constraints.position_constraints.size() > 1 || req.path_constraints.orientation_constraints.size() > 1)
390  stacked_constraints = true;
391  for (const auto& constraint : req.goal_constraints)
392  {
393  if (constraint.position_constraints.size() > 1 || constraint.orientation_constraints.size() > 1)
394  stacked_constraints = true;
395  }
396  if (stacked_constraints)
397  RCLCPP_WARN(LOGGER, "More than one constraint is set. If your move_group does not have multiple end "
398  "effectors/arms, this is "
399  "unusual. Are you using a move_group_interface and forgetting to call clearPoseTargets() or "
400  "equivalent?");
401  }
402 
403  return solved && valid;
404 }
405 
407 {
408  if (planner_instance_)
409  planner_instance_->terminate();
410 }
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
bool generatePlan(const planning_scene::PlanningSceneConstPtr &planning_scene, const planning_interface::MotionPlanRequest &req, planning_interface::MotionPlanResponse &res) const
Call the motion planner plugin and the sequence of planning request adapters (if any).
void publishReceivedRequests(bool flag)
Pass a flag telling the pipeline whether or not to publish the received motion planning requests on M...
PlanningPipeline(const moveit::core::RobotModelConstPtr &model, const std::shared_ptr< rclcpp::Node > &node, const std::string &parameter_namespace, const std::string &planning_plugin_param_name="planning_plugin", const std::string &adapter_plugins_param_name="request_adapters")
Given a robot model (model), a node handle (pipeline_nh), initialize the planning pipeline.
void checkSolutionPaths(bool flag)
Pass a flag telling the pipeline whether or not to re-check the solution paths reported by the planne...
void terminate() const
Request termination, if a generatePlan() function is currently computing plans.
static const std::string MOTION_PLAN_REQUEST_TOPIC
When motion planning requests are received and they are supposed to be automatically published,...
void displayComputedMotionPlans(bool flag)
Pass a flag telling the pipeline whether or not to publish the computed motion plans on DISPLAY_PATH_...
static const std::string MOTION_CONTACTS_TOPIC
When contacts are found in the solution path reported by a planner, they can be published as markers ...
static const std::string DISPLAY_PATH_TOPIC
When motion plans are computed and they are supposed to be automatically displayed,...
void getCollisionMarkersFromContacts(visualization_msgs::msg::MarkerArray &arr, const std::string &frame_id, const CollisionResult::ContactMap &con, const std_msgs::msg::ColorRGBA &color, const rclcpp::Duration &lifetime, const double radius=0.035)
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::msg::RobotState &robot_state, bool copy_attached_bodies=true)
Convert a MoveIt robot state to a robot state message.
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
This namespace includes the central class for representing planning contexts.
const rclcpp::Logger LOGGER
Definition: async_test.h:31
Representation of a collision checking request.
bool verbose
Flag indicating whether information about detected collisions should be reported.
bool contacts
If true, compute contacts. Otherwise only a binary collision yes/no is reported.
std::size_t max_contacts
Overall maximum number of contacts to compute.
std::size_t max_contacts_per_pair
Maximum number of contacts to compute per pair of bodies (multiple bodies may be in contact at differ...
Representation of a collision checking result.
std::size_t contact_count
Number of contacts returned.
robot_trajectory::RobotTrajectoryPtr trajectory_
moveit_msgs::msg::MoveItErrorCodes error_code_