Robowflex  v0.1
Making MoveIt Easy
planner.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #include <ompl/geometric/planners/est/BiEST.h>
4 #include <ompl/geometric/planners/est/EST.h>
5 #include <ompl/geometric/planners/est/ProjEST.h>
6 #include <ompl/geometric/planners/fmt/BFMT.h>
7 #include <ompl/geometric/planners/fmt/FMT.h>
8 #include <ompl/geometric/planners/kpiece/BKPIECE1.h>
9 #include <ompl/geometric/planners/kpiece/KPIECE1.h>
10 #include <ompl/geometric/planners/kpiece/LBKPIECE1.h>
11 #include <ompl/geometric/planners/pdst/PDST.h>
12 #include <ompl/geometric/planners/prm/LazyPRM.h>
13 #include <ompl/geometric/planners/prm/LazyPRMstar.h>
14 #include <ompl/geometric/planners/prm/PRM.h>
15 #include <ompl/geometric/planners/prm/PRMstar.h>
16 #include <ompl/geometric/planners/prm/SPARS.h>
17 #include <ompl/geometric/planners/prm/SPARStwo.h>
18 #include <ompl/geometric/planners/rrt/LazyRRT.h>
19 #include <ompl/geometric/planners/rrt/RRT.h>
20 #include <ompl/geometric/planners/rrt/RRTConnect.h>
21 #include <ompl/geometric/planners/rrt/RRTstar.h>
22 
26 
27 #include <robowflex_dart/planner.h>
28 #include <robowflex_dart/robot.h>
30 #include <robowflex_dart/world.h>
31 
32 using namespace robowflex::darts;
33 
35  : robowflex::Planner(robot, name)
36  , dart_robot_(std::make_shared<Robot>(robot))
37  , world_(std::make_shared<World>())
38 {
39  world_->addRobot(dart_robot_);
40  setupPlanners();
41  builder = std::make_shared<PlanBuilder>(world_);
42 }
43 
45 {
46  // rrts
47  planner_allocators_.emplace("rrt::RRT", makePlanner<ompl::geometric::RRT>());
48  planner_allocators_.emplace("rrt::RRTConnect", makePlanner<ompl::geometric::RRTConnect>());
49  planner_allocators_.emplace("rrt::RRTConnectIntermediate",
50  makePlanner<ompl::geometric::RRTConnect>(true));
51  planner_allocators_.emplace("rrt::RRT*", makePlanner<ompl::geometric::RRTstar>());
52  planner_allocators_.emplace("rrt::LazyRRT", makePlanner<ompl::geometric::LazyRRT>());
53 
54  // ests
55  planner_allocators_.emplace("est::EST", makePlanner<ompl::geometric::EST>());
56  planner_allocators_.emplace("est::BiEST", makePlanner<ompl::geometric::BiEST>());
57  planner_allocators_.emplace("est::ProjEST", makePlanner<ompl::geometric::ProjEST>());
58 
59  // prms
60  planner_allocators_.emplace("prm::PRM", makePlanner<ompl::geometric::PRM>());
61  planner_allocators_.emplace("prm::PRM*", makePlanner<ompl::geometric::PRMstar>());
62  planner_allocators_.emplace("prm::LazyPRM", makePlanner<ompl::geometric::LazyPRM>());
63  planner_allocators_.emplace("prm::LazyPRM*", makePlanner<ompl::geometric::LazyPRMstar>());
64  planner_allocators_.emplace("spars::SPARS", makePlanner<ompl::geometric::SPARS>());
65  planner_allocators_.emplace("spars::SPARS2", makePlanner<ompl::geometric::SPARStwo>());
66 
67  // fmts
68  planner_allocators_.emplace("fmt::FMT", makePlanner<ompl::geometric::FMT>());
69  planner_allocators_.emplace("fmt::BFMT", makePlanner<ompl::geometric::BFMT>());
70 
71  // pdst / kpiece
72  planner_allocators_.emplace("pdst::PDST", makePlanner<ompl::geometric::PDST>());
73  planner_allocators_.emplace("kpiece::KPIECE", makePlanner<ompl::geometric::KPIECE1>());
74  planner_allocators_.emplace("kpiece::BKPIECE", makePlanner<ompl::geometric::BKPIECE1>());
75  planner_allocators_.emplace("kpiece::LBKPIECE", makePlanner<ompl::geometric::LBKPIECE1>());
76 }
77 
80 {
81  // remove old scene, if one exists
82  if (dart_scene_)
83  world_->removeStructure(dart_scene_);
84 
85  // convert scene representation
86  scene_ = scene;
87  dart_scene_ = std::make_shared<Structure>("scene", scene);
88  world_->addStructure(dart_scene_);
89 
90  // setup planning request and get desired goal
91  goal_ = builder->fromMessage(robot_->getName(), request);
92 
93  // find and setup planner
94  auto it = planner_allocators_.find(request.planner_id);
95  if (it == planner_allocators_.end())
96  throw std::runtime_error("Invalid planner ID");
97 
98  builder->ss->setPlanner(it->second());
99  builder->setup();
100 }
101 
104 {
105  // if scene and preRun scene mismatch, recompute
106  if (scene != scene_)
107  preRun(scene, request);
108 
109  // start sampling if necessary
110  auto gls = std::dynamic_pointer_cast<ompl::base::GoalLazySamples>(goal_);
111  if (gls)
112  gls->startSampling();
113 
114  // solve the plan, time the request
115  auto start = std::chrono::steady_clock::now();
116  ompl::base::PlannerStatus solved = builder->ss->solve(request.allowed_planning_time);
117  auto end = std::chrono::steady_clock::now();
118 
119  auto time = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
120 
121  // kill sampling
122  if (gls)
123  gls->stopSampling();
124 
125  // extract request
127  response.planning_time_ = 1e6 * (double)time;
128  if (solved == ompl::base::PlannerStatus::EXACT_SOLUTION)
129  response.error_code_.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
130  else
131  response.error_code_.val = moveit_msgs::MoveItErrorCodes::FAILURE;
132 
133  // extract trajectory
134  auto path = builder->getSolutionPath();
135 
136  response.trajectory_ =
137  std::make_shared<robot_trajectory::RobotTrajectory>(robot_->getModel(), request.group_name);
138 
139  for (const auto &s : path.getStates())
140  {
141  // set world state
142  builder->rspace->setWorldState(world_, s);
143 
144  // copy over world state to moveit state
145  auto ms = robot_->allocState();
146  dart_robot_->setMoveItStateFromState(*ms);
147 
148  response.trajectory_->addSuffixWayPoint(ms, 0);
149  }
150 
151  // compute time parameterization
153 
154  return response;
155 }
156 
158 {
159  std::vector<std::string> configs;
160  for (const auto &pair : planner_allocators_)
161  configs.emplace_back(pair.first);
162 
163  return configs;
164 }
An abstract interface to a motion planning algorithm.
A shared pointer wrapper for robowflex::Robot.
A const shared pointer wrapper for robowflex::Scene.
bool computeTimeParameterization(double max_velocity=1., double max_acceleration=1.)
Computes the time parameterization of a path according to a desired max velocity or acceleration.
Definition: trajectory.cpp:91
std::vector< std::string > getPlannerConfigs() const override
Return all planner configurations offered by this planner. Any of the configurations returned can be ...
Definition: planner.cpp:157
void setupPlanners()
Setup planner allocators.
Definition: planner.cpp:44
RobotPtr dart_robot_
DART version of the robot.
Definition: planner.h:93
DARTPlanner(const robowflex::RobotPtr &robot, const std::string &name="DARTPlanner")
Constructor. Takes in a robot description and an optional namespace name. If name is specified,...
Definition: planner.cpp:34
PlanBuilderPtr builder
DART Motion Plan Builder.
Definition: planner.h:59
StructurePtr dart_scene_
DART version of current planning request scene.
Definition: planner.h:92
void preRun(const robowflex::SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) override
This function is called before benchmarking. Here, it is used to setup the DART scene before the solv...
Definition: planner.cpp:78
planning_interface::MotionPlanResponse plan(const robowflex::SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) override
Plan a motion given a request and a scene.
Definition: planner.cpp:102
WorldPtr world_
DART world containing robot and scene.
Definition: planner.h:94
ompl::base::GoalPtr goal_
Current motion planning goal.
Definition: planner.h:95
SceneConstPtr scene_
Current planning request scene.
Definition: planner.h:91
std::map< std::string, PlannerAllocator > planner_allocators_
Named planner allocators.
Definition: planner.h:90
A sampleable goal region for OMPL for a set of TSRs. Samples goals in a separate thread using a clone...
Wrapper for a dart::simulation::World, which contains a set of skeletons (i.e., Robots and Structures...
Definition: world.h:58
T emplace_back(T... args)
moveit_msgs::MotionPlanRequest MotionPlanRequest
Functions for loading and animating robots in Blender.
DART-based robot modeling and planning.
Definition: acm.h:16
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
Functions for loading and animating scenes in Blender.
moveit_msgs::MoveItErrorCodes error_code_
robot_trajectory::RobotTrajectoryPtr trajectory_