plan_with_sensing.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 
40 #include <boost/algorithm/string/join.hpp>
41 
42 #include <dynamic_reconfigure/server.h>
43 #include <moveit_ros_planning/SenseForPlanDynamicReconfigureConfig.h>
44 
45 namespace plan_execution
46 {
47 using namespace moveit_ros_planning;
48 
49 class PlanWithSensing::DynamicReconfigureImpl
50 {
51 public:
52  DynamicReconfigureImpl(PlanWithSensing* owner)
53  : owner_(owner), dynamic_reconfigure_server_(ros::NodeHandle("~/sense_for_plan"))
54  {
55  dynamic_reconfigure_server_.setCallback(
56  [this](const auto& config, uint32_t level) { dynamicReconfigureCallback(config, level); });
57  }
58 
59 private:
60  void dynamicReconfigureCallback(const SenseForPlanDynamicReconfigureConfig& config, uint32_t /*level*/)
61  {
62  owner_->setMaxSafePathCost(config.max_safe_path_cost);
63  owner_->setMaxCostSources(config.max_cost_sources);
64  owner_->setMaxLookAttempts(config.max_look_attempts);
65  owner_->setDiscardOverlappingCostSources(config.discard_overlapping_cost_sources);
66  }
67 
68  PlanWithSensing* owner_;
69  dynamic_reconfigure::Server<SenseForPlanDynamicReconfigureConfig> dynamic_reconfigure_server_;
70 };
71 } // namespace plan_execution
72 
74  const trajectory_execution_manager::TrajectoryExecutionManagerPtr& trajectory_execution)
75  : node_handle_("~"), trajectory_execution_manager_(trajectory_execution)
76 {
79 
82 
83  // by default we do not display path cost sources
85 
86  // load the sensor manager plugin, if needed
87  if (node_handle_.hasParam("moveit_sensor_manager"))
88  {
89  try
90  {
91  sensor_manager_loader_ = std::make_unique<pluginlib::ClassLoader<moveit_sensor_manager::MoveItSensorManager>>(
92  "moveit_core", "moveit_sensor_manager::MoveItSensorManager");
93  }
95  {
96  ROS_ERROR_STREAM("Exception while creating sensor manager plugin loader: " << ex.what());
97  }
99  {
100  std::string manager;
101  if (node_handle_.getParam("moveit_sensor_manager", manager))
102  try
103  {
104  sensor_manager_ = sensor_manager_loader_->createUniqueInstance(manager);
105  }
107  {
108  ROS_ERROR_STREAM("Exception while loading sensor manager '" << manager << "': " << ex.what());
109  }
110  }
111  if (sensor_manager_)
112  {
113  std::vector<std::string> sensors;
114  sensor_manager_->getSensorsList(sensors);
115  ROS_INFO_STREAM("PlanWithSensing is aware of the following sensors: " << boost::algorithm::join(sensors, ", "));
116  }
117  }
118 
119  // start the dynamic-reconfigure server
120  reconfigure_impl_ = new DynamicReconfigureImpl(this);
121 }
122 
124 {
125  delete reconfigure_impl_;
126 }
127 
129 {
130  if (flag && !display_cost_sources_)
131  // publisher for cost sources
132  cost_sources_publisher_ =
133  node_handle_.advertise<visualization_msgs::MarkerArray>("display_cost_sources", 100, true);
134  else if (!flag && display_cost_sources_)
135  cost_sources_publisher_.shutdown();
136  display_cost_sources_ = flag;
137 }
138 
140  const ExecutableMotionPlanComputationFn& motion_planner,
141  unsigned int max_look_attempts, double max_safe_path_cost)
142 {
143  if (max_safe_path_cost <= std::numeric_limits<double>::epsilon())
144  max_safe_path_cost = default_max_safe_path_cost_;
145 
146  if (max_look_attempts == 0)
147  max_look_attempts = default_max_look_attempts_;
148 
149  double previous_cost = 0.0;
150  unsigned int look_attempts = 0;
151 
152  // this flag is set to true when all conditions for looking around are met, and the command is sent.
153  // the intention is for the planning loop not to terminate when having just looked around
154  bool just_looked_around = false;
155 
156  // this flag indicates whether the last lookAt() operation failed. If this operation fails once, we assume that
157  // maybe some information was gained anyway (the sensor moved part of the way) and we try to plan one more time.
158  // If we have two sensor pointing failures in a row, we fail
159  bool look_around_failed = false;
160 
161  // there can be a maximum number of looking attempts as well that lead to replanning, if the cost
162  // of the path is above a maximum threshold.
163  do
164  {
165  bool solved = motion_planner(plan);
166  if (!solved || plan.error_code_.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
167  return solved;
168 
169  // determine the sources of cost for this path
170  std::set<collision_detection::CostSource> cost_sources;
171  {
173  // planning_scene_monitor_ is
174  // null; there just will be
175  // no locking done
176  for (std::size_t i = 0; i < plan.plan_components_.size(); ++i)
177  {
178  std::set<collision_detection::CostSource> cost_sources_i;
179  plan.planning_scene_->getCostSources(*plan.plan_components_[i].trajectory_, max_cost_sources_,
180  plan.plan_components_[i].trajectory_->getGroupName(), cost_sources_i,
181  discard_overlapping_cost_sources_);
182  cost_sources.insert(cost_sources_i.begin(), cost_sources_i.end());
183  if (cost_sources.size() > max_cost_sources_)
184  {
185  std::set<collision_detection::CostSource> other;
186  other.swap(cost_sources);
187  std::size_t j = 0;
188  for (std::set<collision_detection::CostSource>::iterator it = other.begin(); j < max_cost_sources_; ++it, ++j)
189  cost_sources.insert(*it);
190  }
191  }
192  }
193 
194  // display the costs if needed
195  if (display_cost_sources_)
196  {
197  visualization_msgs::MarkerArray arr;
198  collision_detection::getCostMarkers(arr, plan.planning_scene_->getPlanningFrame(), cost_sources);
199  cost_sources_publisher_.publish(arr);
200  }
201 
202  double cost = collision_detection::getTotalCost(cost_sources);
203  ROS_DEBUG("The total cost of the trajectory is %lf.", cost);
204  if (previous_cost > 0.0)
205  ROS_DEBUG("The change in the trajectory cost is %lf after the perception step.", cost - previous_cost);
206  if (cost > max_safe_path_cost && look_attempts < max_look_attempts)
207  {
208  ++look_attempts;
209  ROS_INFO("The cost of the trajectory is %lf, which is above the maximum safe cost of %lf. Attempt %u (of at most "
210  "%u) at looking around.",
211  cost, max_safe_path_cost, look_attempts, max_look_attempts);
212 
213  bool looked_at_result = lookAt(cost_sources, plan.planning_scene_->getPlanningFrame());
214  if (looked_at_result)
215  ROS_INFO("Sensor was successfully actuated. Attempting to recompute a motion plan.");
216  else
217  {
218  if (look_around_failed)
219  ROS_WARN("Looking around seems to keep failing. Giving up.");
220  else
221  ROS_WARN("Looking around seems to have failed. Attempting to recompute a motion plan anyway.");
222  }
223  if (looked_at_result || !look_around_failed)
224  {
225  previous_cost = cost;
226  just_looked_around = true;
227  }
228  look_around_failed = !looked_at_result;
229  // if we are unable to look, let this loop continue into the next if statement
230  if (just_looked_around)
231  continue;
232  }
233 
234  if (cost > max_safe_path_cost)
235  {
236  plan.error_code_.val = moveit_msgs::MoveItErrorCodes::UNABLE_TO_AQUIRE_SENSOR_DATA;
237  return true;
238  }
239  else
240  return true;
241  } while (true);
242 
243  return false;
244 }
245 
246 bool plan_execution::PlanWithSensing::lookAt(const std::set<collision_detection::CostSource>& cost_sources,
247  const std::string& frame_id)
248 {
249  if (!sensor_manager_)
250  {
251  ROS_WARN("It seems looking around would be useful, but no MoveIt Sensor Manager is loaded. Did you set "
252  "~moveit_sensor_manager ?");
253  return false;
254  }
255 
256  if (before_look_callback_)
257  before_look_callback_();
258 
259  std::vector<std::string> names;
260  sensor_manager_->getSensorsList(names);
261  geometry_msgs::PointStamped point;
262  for (const std::string& name : names)
263  if (collision_detection::getSensorPositioning(point.point, cost_sources))
264  {
265  point.header.stamp = ros::Time::now();
266  point.header.frame_id = frame_id;
267  ROS_DEBUG_STREAM("Pointing sensor " << name << " to:\n" << point);
268  moveit_msgs::RobotTrajectory sensor_trajectory;
269  if (sensor_manager_->pointSensorTo(name, point, sensor_trajectory))
270  {
271  if (!trajectory_processing::isTrajectoryEmpty(sensor_trajectory))
272  return trajectory_execution_manager_->push(sensor_trajectory) &&
273  trajectory_execution_manager_->executeAndWait();
274  else
275  return true;
276  }
277  }
278  return false;
279 }
plan_execution::ExecutableMotionPlan::planning_scene_
planning_scene::PlanningSceneConstPtr planning_scene_
Definition: plan_representation.h:108
collision_detection::getTotalCost
double getTotalCost(const std::set< CostSource > &cost_sources)
plan_execution::PlanWithSensing::sensor_manager_
moveit_sensor_manager::MoveItSensorManagerPtr sensor_manager_
Definition: plan_with_sensing.h:153
plan_execution::ExecutableMotionPlan::planning_scene_monitor_
planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_
Definition: plan_representation.h:107
plan_execution::PlanWithSensing::PlanWithSensing
PlanWithSensing(const trajectory_execution_manager::TrajectoryExecutionManagerPtr &trajectory_execution)
Definition: plan_with_sensing.cpp:73
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
plan_execution::PlanWithSensing::reconfigure_impl_
DynamicReconfigureImpl * reconfigure_impl_
Definition: plan_with_sensing.h:165
collision_tools.h
plan_execution::PlanWithSensing::lookAt
bool lookAt(const std::set< collision_detection::CostSource > &cost_sources, const std::string &frame_id)
Definition: plan_with_sensing.cpp:246
trajectory_processing::isTrajectoryEmpty
bool isTrajectoryEmpty(const moveit_msgs::RobotTrajectory &trajectory)
ros
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
plan_execution::PlanWithSensing::sensor_manager_loader_
std::unique_ptr< pluginlib::ClassLoader< moveit_sensor_manager::MoveItSensorManager > > sensor_manager_loader_
Definition: plan_with_sensing.h:152
ROS_DEBUG
#define ROS_DEBUG(...)
plan_with_sensing.h
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
plan_execution::PlanWithSensing::discard_overlapping_cost_sources_
double discard_overlapping_cost_sources_
Definition: plan_with_sensing.h:157
pluginlib::PluginlibException
plan_execution::PlanWithSensing::default_max_safe_path_cost_
double default_max_safe_path_cost_
Definition: plan_with_sensing.h:155
name
std::string name
plan_execution::ExecutableMotionPlan::plan_components_
std::vector< ExecutableTrajectory > plan_components_
Definition: plan_representation.h:110
plan_execution::PlanWithSensing::display_cost_sources_
bool display_cost_sources_
Definition: plan_with_sensing.h:160
plan_execution::PlanWithSensing::computePlan
bool computePlan(ExecutableMotionPlan &plan, const ExecutableMotionPlanComputationFn &motion_planner, unsigned int max_look_attempts, double max_safe_path_cost)
Definition: plan_with_sensing.cpp:139
plan_execution::PlanWithSensing::default_max_look_attempts_
unsigned int default_max_look_attempts_
Definition: plan_with_sensing.h:154
ros::NodeHandle::hasParam
bool hasParam(const std::string &key) const
ROS_WARN
#define ROS_WARN(...)
plan_execution::PlanWithSensing::displayCostSources
void displayCostSources(bool flag)
Definition: plan_with_sensing.cpp:128
collision_detection::getCostMarkers
void getCostMarkers(visualization_msgs::MarkerArray &arr, const std::string &frame_id, std::set< CostSource > &cost_sources)
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
plan_execution::PlanWithSensing::~PlanWithSensing
~PlanWithSensing()
Definition: plan_with_sensing.cpp:123
plan_execution::ExecutableMotionPlan::error_code_
moveit_msgs::MoveItErrorCodes error_code_
An error code reflecting what went wrong (if anything)
Definition: plan_representation.h:116
point
std::chrono::system_clock::time_point point
plan_execution
This namespace includes functionality specific to the execution and monitoring of motion plans.
Definition: plan_execution.h:50
collision_detection::getSensorPositioning
bool getSensorPositioning(geometry_msgs::Point &point, const std::set< CostSource > &cost_sources)
plan_execution::ExecutableMotionPlan
A generic representation on what a computed motion plan looks like.
Definition: plan_representation.h:105
planning_scene_monitor::LockedPlanningSceneRO
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
Definition: planning_scene_monitor.h:646
trajectory_tools.h
ROS_INFO
#define ROS_INFO(...)
plan_execution::PlanWithSensing::max_cost_sources_
unsigned int max_cost_sources_
Definition: plan_with_sensing.h:158
plan_execution::ExecutableMotionPlanComputationFn
boost::function< bool(ExecutableMotionPlan &)> ExecutableMotionPlanComputationFn
The signature of a function that can compute a motion plan.
Definition: plan_representation.h:120
plan_execution::PlanWithSensing::node_handle_
ros::NodeHandle node_handle_
Definition: plan_with_sensing.h:149
ros::Time::now
static Time now()


planning
Author(s): Ioan Sucan , Sachin Chitta
autogenerated on Thu Nov 21 2024 03:24:18