Robot Planning and its Applications Project
ompl_planning.hpp
Go to the documentation of this file.
1 
7 #include <ompl/base/SpaceInformation.h>
8 #include <ompl/base/objectives/PathLengthOptimizationObjective.h>
9 #include <ompl/base/objectives/StateCostIntegralObjective.h>
10 #include <ompl/base/objectives/MaximizeMinClearanceObjective.h>
11 #include <ompl/base/spaces/RealVectorStateSpace.h>
12 #include "ompl/util/Console.h"
13 #include <ompl/geometric/planners/prm/PRMstar.h>
14 #include <ompl/geometric/planners/rrt/RRTstar.h>
15 #include <ompl/base/PlannerDataGraph.h>
16 
17 
18 #include <boost/program_options.hpp>
19 #include <boost/algorithm/string.hpp>
20 
21 #include <boost/graph/adjacency_list.hpp>
22 #include <boost/geometry.hpp>
23 #include <boost/geometry/geometries/point_xy.hpp>
24 #include <boost/geometry/geometries/polygon.hpp>
25 #include <boost/geometry/geometries/linestring.hpp>
26 
27 #include <boost/graph/dijkstra_shortest_paths.hpp>
28 
29 #include <memory>
30 
31 #include <fstream>
32 
33 #include "utils.hpp"
34 
35 #include<iostream>
36 using namespace std;
37 namespace ob = ompl::base;
38 namespace og = ompl::geometric;
39 namespace bg = boost::geometry;
40 typedef bg::model::d2::point_xy<double> boost_point;
41 typedef bg::model::polygon <boost_point> boost_polygon;
42 typedef boost::geometry::model::linestring<boost_point> boost_linestring;
43 
46 {
51 };
52 
55 {
64 };
65 
72 ob::OptimizationObjectivePtr getPathLengthObjective(const ob::SpaceInformationPtr& si)
73 {
74  return std::make_shared<ob::PathLengthOptimizationObjective>(si);
75 }
76 
84 ob::OptimizationObjectivePtr getThresholdPathLengthObj(const ob::SpaceInformationPtr& si)
85 {
86  auto obj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
87  obj->setCostThreshold(ob::Cost(1.51));
88  return obj;
89 }
90 
91 
92 class ClearanceObjective : public ob::StateCostIntegralObjective
93 {
94 public:
95  ClearanceObjective(const ob::SpaceInformationPtr& si) :
96  ob::StateCostIntegralObjective(si, true)
97  {
98  }
99 
100  // Our requirement is to maximize path clearance from obstacles,
101  // but we want to represent the objective as a path cost
102  // minimization. Therefore, we set each state's cost to be the
103  // reciprocal of its clearance, so that as state clearance
104  // increases, the state cost decreases.
105  ob::Cost stateCost(const ob::State* s) const override
106  {
107  return ob::Cost(1 / (si_->getStateValidityChecker()->clearance(s) +
108  std::numeric_limits<double>::min()));
109  }
110 };
111 
118 ob::OptimizationObjectivePtr getClearanceObjective(const ob::SpaceInformationPtr& si)
119 {
120  return std::make_shared<ClearanceObjective>(si);
121 }
122 
129 ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(const ob::SpaceInformationPtr& si)
130 {
131  auto obj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
132  obj->setCostToGoHeuristic(&ob::goalRegionCostToGo);
133  return obj;
134 }
135 
143 ob::OptimizationObjectivePtr getBalancedObjective2(const ob::SpaceInformationPtr& si)
144 {
145  auto lengthObj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
146  auto clearObj(std::make_shared<ClearanceObjective>(si));
147 
148  return 10.0*lengthObj + clearObj;
149 }
150 
156 ob::OptimizationObjectivePtr getBalancedObjective1(const ob::SpaceInformationPtr& si)
157 {
158  auto lengthObj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
159  auto clearObj(std::make_shared<ClearanceObjective>(si));
160  auto opt(std::make_shared<ob::MultiOptimizationObjective>(si));
161  opt->addObjective(lengthObj, 10.0);
162  opt->addObjective(clearObj, 1.0);
163 
164  return ob::OptimizationObjectivePtr(opt);
165 }
166 
174 ob::PlannerPtr allocatePlanner(ob::SpaceInformationPtr si, optimalPlanner plannerType)
175 {
176  switch (plannerType)
177  {
178  case PLANNER_PRMSTAR:
179  {
180  return std::make_shared<og::PRMstar>(si);
181  break;
182  }
183  case PLANNER_RRTSTAR:
184  {
185  return std::make_shared<og::RRTstar>(si);
186  break;
187  }
188  default:
189  {
190  OMPL_ERROR("Planner-type enum is not implemented in allocation function.");
191  return ob::PlannerPtr(); // Address compiler warning re: no return value.
192  break;
193  }
194  }
195 }
196 
197 
205 ob::OptimizationObjectivePtr allocateObjective(const ob::SpaceInformationPtr& si, planningObjective objectiveType)
206 {
207  switch (objectiveType)
208  {
210  return getClearanceObjective(si);
211  break;
213  return getPathLengthObjective(si);
214  break;
216  return getThresholdPathLengthObj(si);
217  break;
219  return getBalancedObjective1(si);
220  break;
221  default:
222  OMPL_ERROR("Optimization-objective enum is not implemented in allocation function.");
223  return ob::OptimizationObjectivePtr();
224  break;
225  }
226 }
227 
235  boost_polygon boost_poly;
236 
237  for(auto &iter : poly){
238  bg::append(boost_poly.outer(), boost_point(iter.x, iter.y));
239  }
240 
241  bg::append(boost_poly.outer(), boost_point(poly[0].x, poly[0].y));
242 
243  return boost_poly;
244 }
245 
249 class ValidityChecker : public ob::StateValidityChecker
250 {
251 public:
252 
253  std::vector<Polygon> obstacles;
258  ValidityChecker(const ob::SpaceInformationPtr& si,std::vector<Polygon> obstacle_list ) :
259  ob::StateValidityChecker(si) {obstacles = obstacle_list;}
260 
268  bool isValid(const ob::State* state) const override
269  {
270  //return this->clearance(state) > 0.0;
271  const ob::RealVectorStateSpace::StateType* state2D = state->as<ob::RealVectorStateSpace::StateType>();
272  double x = state2D->values[0];
273  double y = state2D->values[1];
274  boost_point centerPoint(x, y);
275  for (Polygon polygon : obstacles) {
277  if (boost::geometry::within(centerPoint, Poly))
278  return false;
279  }
280  return true;
281  }
282 
283 
284 };
285 
ob::OptimizationObjectivePtr getBalancedObjective2(const ob::SpaceInformationPtr &si)
get balanced objective between Path length optimization and Object clearance
Definition: ompl_planning.hpp:143
bool isValid(const ob::State *state) const override
to check if state is valid of not
Definition: ompl_planning.hpp:268
optimalPlanner
Choice of Optimal planner.
Definition: ompl_planning.hpp:45
ValidityChecker(const ob::SpaceInformationPtr &si, std::vector< Polygon > obstacle_list)
Constructor.
Definition: ompl_planning.hpp:258
PRM STAR implementation.
Definition: ompl_planning.hpp:48
ob::OptimizationObjectivePtr getThresholdPathLengthObj(const ob::SpaceInformationPtr &si)
get path length objective with threshold using the space information configured
Definition: ompl_planning.hpp:84
Path length objective.
Definition: ompl_planning.hpp:59
bg::model::d2::point_xy< double > boost_point
Definition: ompl_planning.hpp:40
planningObjective
An enum of the supported optimization objectives.
Definition: ompl_planning.hpp:54
ob::OptimizationObjectivePtr getClearanceObjective(const ob::SpaceInformationPtr &si)
get Obstace clearance objective using the space information configured
Definition: ompl_planning.hpp:118
ob::OptimizationObjectivePtr allocateObjective(const ob::SpaceInformationPtr &si, planningObjective objectiveType)
Allocate objective.
Definition: ompl_planning.hpp:205
ob::PlannerPtr allocatePlanner(ob::SpaceInformationPtr si, optimalPlanner plannerType)
Allocate planner.
Definition: ompl_planning.hpp:174
ClearanceObjective(const ob::SpaceInformationPtr &si)
Definition: ompl_planning.hpp:95
ob::OptimizationObjectivePtr getBalancedObjective1(const ob::SpaceInformationPtr &si)
get balanced objective between Path length optimization, Object clearance and Multi Optimization ...
Definition: ompl_planning.hpp:156
ob::Cost stateCost(const ob::State *s) const override
Definition: ompl_planning.hpp:105
Path length with threshold based objective.
Definition: ompl_planning.hpp:61
boost_polygon convertPolygonToBoostPolygon(const Polygon &poly)
Convert Polygon To Boost polygon.
Definition: ompl_planning.hpp:234
std::vector< Point > Polygon
Polygon(which is a representation of multiple points)
Definition: utils.hpp:58
ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(const ob::SpaceInformationPtr &si)
get Path length objective with go to heuristic cost using the space information configured ...
Definition: ompl_planning.hpp:129
Path clearance objective.
Definition: ompl_planning.hpp:57
Class to check if new random state is valid or not.
Definition: ompl_planning.hpp:249
ob::OptimizationObjectivePtr getPathLengthObjective(const ob::SpaceInformationPtr &si)
get path length objective using the space information configured
Definition: ompl_planning.hpp:72
Weighted combination obejctive of all the above three or two.
Definition: ompl_planning.hpp:63
std::vector< Polygon > obstacles
Definition: ompl_planning.hpp:253
bg::model::polygon< boost_point > boost_polygon
Definition: ompl_planning.hpp:41
boost::geometry::model::linestring< boost_point > boost_linestring
Definition: ompl_planning.hpp:42
Definition: ompl_planning.hpp:92
RRT Star implementation.
Definition: ompl_planning.hpp:50