Robowflex  v0.1
Making MoveIt Easy
ompl_interface.cpp
Go to the documentation of this file.
1 #include <moveit/ompl_interface/model_based_planning_context.h>
2 
5 #include <robowflex_library/io.h>
9 
11 
12 using namespace robowflex;
13 
15  : Planner(robot, name)
16 {
17 }
18 
19 bool OMPL::OMPLInterfacePlanner::initialize(const std::string &config_file, const OMPL::Settings settings)
20 {
21  if (!loadOMPLConfig(handler_, config_file, configs_))
22  return false;
23 
24  interface_.reset(new ompl_interface::OMPLInterface(robot_->getModel(), handler_.getHandle()));
25 
26  settings.setParam(handler_);
27 
28  interface_->simplifySolutions(settings.simplify_solutions);
29  hybridize_ = settings.hybridize_solutions;
30  interpolate_ = settings.interpolate_solutions;
31 
32  auto &pcm = interface_->getPlanningContextManager();
33  pcm.setMaximumSolutionSegmentLength(settings.maximum_waypoint_distance);
34  pcm.setMinimumWaypointCount(settings.minimum_waypoint_count);
35 
36  pcm.setMaximumPlanningThreads(settings.max_planning_threads);
37  pcm.setMaximumGoalSamples(settings.max_goal_samples);
38  pcm.setMaximumStateSamplingAttempts(settings.max_state_sampling_attempts);
39  pcm.setMaximumGoalSamplingAttempts(settings.max_goal_sampling_attempts);
40 
42  pcm.setMaximumSolutionSegmentLength(settings.max_solution_segment_length);
43 
44  return true;
45 }
46 
47 ompl_interface::ModelBasedPlanningContextPtr OMPL::OMPLInterfacePlanner::getPlanningContext(
48  const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) const
49 {
50  return interface_->getPlanningContext(scene->getSceneConst(), request);
51 }
52 
55 {
56  refreshContext(scene, request, true);
57 }
58 
61 {
63  response.error_code_.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
64 
65  refreshContext(scene, request);
66  if (not ss_)
67  return response;
68 
69  if (pre_plan_callback_)
70  pre_plan_callback_(context_, scene, request);
71 
72  context_->solve(response);
73  return response;
74 }
75 
77  const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) const
78 {
79  refreshContext(scene, request);
80  if (not ss_)
81  return {};
82 
83  const auto &planner = ss_->getPlanner();
84 
85 #if ROBOWFLEX_AT_LEAST_KINETIC
86  return planner->getPlannerProgressProperties();
87 
88  // As in Indigo they are boost::function
89 #else
91  for (const auto &pair : planner->getPlannerProgressProperties())
92  {
93  auto function = pair.second;
94  ret[pair.first] = [function] { return function(); };
95  }
96 
97  return ret;
98 #endif
99 }
100 
103  bool force) const
104 {
105  const auto &scene_id = scene->getKey();
106  const auto &request_hash = IO::getMessageMD5(request);
107 
108  bool same_scene = compareIDs(scene_id, last_scene_id_);
109  bool same_request = request_hash == last_request_hash_;
110 
111  if (not force and ss_ and same_scene and same_request)
112  {
113  RBX_INFO("Reusing Cached Context!");
114  return;
115  }
116 
117  context_ = getPlanningContext(scene, request);
118  if (not context_)
119  {
120  RBX_ERROR("Context was not set!");
121  ss_ = nullptr;
122  return;
123  }
124 
125 #if ROBOWFLEX_AT_LEAST_MELODIC
126  context_->setInterpolation(interpolate_);
127  context_->setHybridize(hybridize_);
128 #endif
129 
130  ss_ = context_->getOMPLSimpleSetup();
131 
132  last_scene_id_ = scene_id;
133  last_request_hash_ = request_hash;
134 
135  RBX_INFO("Refreshed Context!");
136 }
137 
138 ompl::geometric::SimpleSetupPtr OMPL::OMPLInterfacePlanner::getLastSimpleSetup() const
139 {
140  return ss_;
141 }
142 
144 {
145  return configs_;
146 }
147 
148 ompl_interface::OMPLInterface &OMPL::OMPLInterfacePlanner::getInterface() const
149 {
150  if (!interface_)
151  {
152  RBX_WARN("Interface is not initialized before call to OMPLInterfacePlanner::initialize.");
153  }
154  return *interface_;
155 }
156 
158 {
159  pre_plan_callback_ = prePlanCallback;
160 }
ompl_interface::OMPLInterface & getInterface() const
Access the OMPLInterface directly, to customize the planning process.
void setPrePlanCallback(const PrePlanCallback &prePlanCallback)
Set a callback, to be called right before a planning session for last-minute configuration or externa...
void preRun(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) override
This function is called before benchmarking.
bool initialize(const std::string &config_file="", const OMPL::Settings settings=Settings())
Initialize planning pipeline.
ompl_interface::ModelBasedPlanningContextPtr getPlanningContext(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) const
Returns the planning context used for this motion planning request.
planning_interface::MotionPlanResponse plan(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) override
Plan a motion given a request and a scene. Uses the planning pipeline's generatePlan() method,...
std::map< std::string, Planner::ProgressProperty > getProgressProperties(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) const override
Retrieve the planner progress property map for this planner given a specific request.
void refreshContext(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request, bool force=false) const
Refreshes the internal planning context.
std::vector< std::string > getPlannerConfigs() const override
Return all planner configurations offered by this planner. Any of the configurations returned can be ...
ompl::geometric::SimpleSetupPtr getLastSimpleSetup() const
Get the last OMPL simple setup used in planning.
OMPLInterfacePlanner(const RobotPtr &robot, const std::string &name="")
Constructor.
Settings descriptor for settings provided by the default MoveIt! OMPL planning pipeline.
void setParam(IO::Handler &handler) const
Sets member variables on the parameter server using handler.
int minimum_waypoint_count
Minimum number of waypoints in output path.
int max_goal_samples
Maximum number of successful goal samples to keep.
int max_state_sampling_attempts
Maximum number of attempts to sample a new state.
int max_planning_threads
Maximum number of threads used to service a request.
bool interpolate_solutions
Whether or not planner should interpolate solutions.
bool hybridize_solutions
Whether or not planner should hybridize solutions.
double max_solution_segment_length
Maximum solution segment length.
double maximum_waypoint_distance
Maximum distance between waypoints in path.
bool simplify_solutions
Whether or not planner should simplify solutions.
int max_goal_sampling_attempts
Maximum number of attempts to sample a goal.
An abstract interface to a motion planning algorithm.
A shared pointer wrapper for robowflex::Robot.
A const shared pointer wrapper for robowflex::Scene.
#define RBX_WARN(fmt,...)
Output a warning logging message.
Definition: log.h:110
#define RBX_ERROR(fmt,...)
Output a error logging message.
Definition: log.h:102
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118
moveit_msgs::MotionPlanRequest MotionPlanRequest
Functions for loading and animating robots in Blender.
std::string getMessageMD5(T &msg)
Compute MD5 hash of message.
bool loadOMPLConfig(IO::Handler &handler, const std::string &config_file, std::vector< std::string > &configs)
Loads an OMPL configuration YAML file onto the parameter server.
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
bool compareIDs(const ID &a, const ID &b)
Compare two ID objects.
Definition: id.cpp:47
Functions for loading and animating scenes in Blender.
moveit_msgs::MoveItErrorCodes error_code_