Aikido
OMPLConfigurationToConfigurationPlanner-impl.hpp
Go to the documentation of this file.
1 #ifndef AIKIDO_PLANNER_OMPL_DETAIL_OMPLCONFIGURATIONTOCONFIGURATION_IMPL_HPP_
2 #define AIKIDO_PLANNER_OMPL_DETAIL_OMPLCONFIGURATIONTOCONFIGURATION_IMPL_HPP_
3 
4 #include <utility>
5 
16 
17 namespace aikido {
18 namespace planner {
19 namespace ompl {
20 
26 
27 //==============================================================================
28 template <class PlannerType>
32  common::RNG* rng,
36  constraint::ConstTestablePtr boundsConstraint,
37  constraint::ProjectablePtr boundsProjector,
38  double maxDistanceBetweenValidityChecks)
39  : ConfigurationToConfigurationPlanner(std::move(stateSpace), rng)
40 {
41  if (!interpolator)
42  interpolator
43  = std::make_shared<const aikido::statespace::GeodesicInterpolator>(
44  mStateSpace);
45 
46  if (!dmetric)
47  dmetric = std::move(createDistanceMetric(mStateSpace));
48 
49  // TODO (avk): constraint namespace works with MetaskeletonStateSpace only.
50  const auto metaskeletonStatespace
51  = std::dynamic_pointer_cast<const MetaSkeletonStateSpace>(mStateSpace);
52 
53  if (metaskeletonStatespace)
54  {
55  if (!sampler)
56  {
57  if (!rng)
58  throw std::invalid_argument(
59  "[OMPLPlanner] Both of sampler and rng cannot be nullptr.");
60 
61  sampler = std::move(
62  createSampleableBounds(metaskeletonStatespace, rng->clone()));
63  }
64 
65  if (!boundsConstraint)
66  boundsConstraint
67  = std::move(createTestableBounds(metaskeletonStatespace));
68 
69  if (!boundsProjector)
70  boundsProjector
71  = std::move(createProjectableBounds(metaskeletonStatespace));
72  }
73  else
74  {
75  if (!sampler || !boundsConstraint || !boundsProjector)
76  throw std::invalid_argument(
77  "[OMPLPlanner] Either statespace must be metaSkeletonStateSpace or "
78  "all of sampler, boundsContraint and boundsProjector must be "
79  "provided.");
80  }
81 
82  // Geometric State space
83  auto sspace = ompl_make_shared<GeometricStateSpace>(
85  std::move(interpolator),
86  std::move(dmetric),
87  std::move(sampler),
88  std::move(boundsConstraint),
89  std::move(boundsProjector),
90  maxDistanceBetweenValidityChecks);
91 
92  // Space Information
93  auto si = ompl_make_shared<::ompl::base::SpaceInformation>(sspace);
94 
95  // Setup the planner
96  mPlanner = ompl_make_shared<PlannerType>(si);
97 }
98 
99 //==============================================================================
100 template <class PlannerType>
103  const SolvableProblem& problem, Result* result)
104 {
105  auto si = mPlanner->getSpaceInformation();
106 
107  // Only geometric statespaces are supported.
108  assert(ompl_dynamic_pointer_cast<GeometricStateSpace>(si->getStateSpace()));
109  auto sspace
110  = ompl_static_pointer_cast<GeometricStateSpace>(si->getStateSpace());
111 
112  // Set validity checker.
113  std::vector<constraint::ConstTestablePtr> constraints{
114  problem.getConstraint(), sspace->getBoundsConstraint()};
115  auto conjunctionConstraint
116  = std::make_shared<constraint::TestableIntersection>(
117  mStateSpace, std::move(constraints));
118  ::ompl::base::StateValidityCheckerPtr vchecker
119  = ompl_make_shared<StateValidityChecker>(si, conjunctionConstraint);
120  si->setStateValidityChecker(vchecker);
121 
122  ::ompl::base::MotionValidatorPtr mvalidator
123  = ompl_make_shared<MotionValidator>(
124  si, sspace->getMaxDistanceBetweenValidityChecks());
125  si->setMotionValidator(mvalidator);
126 
127  // Define the OMPL problem.
128  auto pdef = ompl_make_shared<::ompl::base::ProblemDefinition>(si);
129 
130  // Provide the problem definition with start and goal configuations.
131  auto start = sspace->allocState(problem.getStartState());
132  auto goal = sspace->allocState(problem.getGoalState());
133  pdef->setStartAndGoalStates(start, goal);
134 
135  // pdef clones and maintains start and goal internally.
136  sspace->freeState(start);
137  sspace->freeState(goal);
138 
139  // Solve the planning problem.
140  mPlanner->setProblemDefinition(pdef);
141  mPlanner->setup();
142 
143  // TODO (avk): Introduce other termination conditions for planners (as in
144  // OMPL).
145  auto solved = mPlanner->solve(::ompl::base::plannerNonTerminatingCondition());
146 
147  if (solved)
148  {
149  auto returnTraj = std::make_shared<trajectory::Interpolated>(
150  mStateSpace, sspace->getInterpolator());
151 
152  // Get the path
153  auto path = ompl_dynamic_pointer_cast<::ompl::geometric::PathGeometric>(
154  pdef->getSolutionPath());
155  if (!path)
156  {
157  throw std::invalid_argument(
158  "Path is not of type PathGeometric. Cannot convert to aikido "
159  "Trajectory.");
160  }
161 
162  for (std::size_t idx = 0; idx < path->getStateCount(); ++idx)
163  {
164  assert(
166  path->getState(idx)));
167  const auto* st
169  path->getState(idx));
170  returnTraj->addWaypoint(idx, st->mState);
171  }
172  // Clear the planner internal data.
173  mPlanner->clear();
174  return returnTraj;
175  }
176 
177  if (result)
178  result->setMessage("Problem could not be solved.");
179  mPlanner->clear();
180  return nullptr;
181 }
182 
183 //==============================================================================
184 template <class PlannerType>
187 {
188  return mPlanner;
189 }
190 
191 } // namespace ompl
192 } // namespace planner
193 } // namespace aikido
194 
195 #endif // AIKIDO_PLANNER_OMPL_DETAIL_OMPLCONFIGURATIONTOCONFIGURATION_IMPL_HPP_
aikido::planner::ompl::GeometricStateSpace::StateType
Wraps an aikido::statespace::StateSpace::State in an OMPL StateType.
Definition: GeometricStateSpace.hpp:28
aikido::distance::createDistanceMetric
std::unique_ptr< DistanceMetric > createDistanceMetric(statespace::ConstStateSpacePtr _sspace)
Creates a DistanceMetric that is appropriate for the statespace.
aikido::constraint::dart::createSampleableBounds
std::unique_ptr< Sampleable > createSampleableBounds(std::shared_ptr< const statespace::dart::JointStateSpace > _stateSpace, std::unique_ptr< common::RNG > _rng)
Create a Sampleabe constraint that can be used to sample values for the joint that are guarenteed to ...
Planner.hpp
JointStateSpaceHelpers.hpp
OMPLConfigurationToConfigurationPlanner.hpp
aikido
Format of serialized trajectory in YAML.
Definition: algorithm.hpp:4
aikido::constraint::ProjectablePtr
std::shared_ptr< Projectable > ProjectablePtr
Definition: Projectable.hpp:33
aikido::distance::DistanceMetricPtr
std::shared_ptr< DistanceMetric > DistanceMetricPtr
Definition: DistanceMetric.hpp:10
aikido::planner::Planner::Result::setMessage
void setMessage(const std::string &message)
Sets message.
aikido::statespace::ConstStateSpacePtr
std::shared_ptr< const StateSpace > ConstStateSpacePtr
Definition: StateSpace.hpp:15
aikido::planner::ConfigurationToConfigurationPlanner
Base planner class for ConfigurationToConfiguration planning problem.
Definition: ConfigurationToConfigurationPlanner.hpp:12
aikido::constraint::SampleablePtr
std::shared_ptr< Sampleable > SampleablePtr
Definition: Sampleable.hpp:16
aikido::statespace::ConstInterpolatorPtr
std::shared_ptr< const Interpolator > ConstInterpolatorPtr
Definition: Interpolator.hpp:12
MotionValidator.hpp
aikido::trajectory::TrajectoryPtr
std::shared_ptr< Trajectory > TrajectoryPtr
Definition: Trajectory.hpp:13
aikido::statespace::dart::MetaSkeletonStateSpace
StateSpace of a DART MetaSkeleton.
Definition: MetaSkeletonStateSpace.hpp:26
FrameTestable.hpp
TestableIntersection.hpp
aikido::planner::ompl::OMPLConfigurationToConfigurationPlanner::mPlanner
::ompl::base::PlannerPtr mPlanner
Pointer to the underlying OMPL Planner.
Definition: OMPLConfigurationToConfigurationPlanner.hpp:83
aikido::planner::ompl::OMPLConfigurationToConfigurationPlanner::getOMPLPlanner
::ompl::base::PlannerPtr getOMPLPlanner()
Returns the underlying OMPL planner used.
Definition: OMPLConfigurationToConfigurationPlanner-impl.hpp:186
aikido::common::RNG
Implementation of the C++11 "random engine" concept that uses virtual function calls to erase the typ...
Definition: RNG.hpp:24
aikido::common::RNG::clone
virtual std::unique_ptr< RNG > clone() const =0
Create a copy of this RNG, including its internal state.
aikido::constraint::dart::createTestableBounds
std::unique_ptr< Testable > createTestableBounds(std::shared_ptr< const statespace::dart::JointStateSpace > _stateSpace)
Create a Testable constraint that can be used to determine if the value of a joint is within the join...
aikido::planner::PlannerPtr
std::shared_ptr< Planner > PlannerPtr
Definition: Planner.hpp:14
defaults.hpp
FrameDifferentiable.hpp
aikido::planner::ompl::OMPLConfigurationToConfigurationPlanner::OMPLConfigurationToConfigurationPlanner
OMPLConfigurationToConfigurationPlanner(statespace::ConstStateSpacePtr stateSpace, common::RNG *rng=nullptr, statespace::ConstInterpolatorPtr interpolator=nullptr, distance::DistanceMetricPtr dmetric=nullptr, constraint::SampleablePtr sampler=nullptr, constraint::ConstTestablePtr boundsConstraint=nullptr, constraint::ProjectablePtr boundsProjector=nullptr, double maxDistanceBtwValidityChecks=0.1)
Constructor.
Definition: OMPLConfigurationToConfigurationPlanner-impl.hpp:30
aikido::planner::ompl::OMPLConfigurationToConfigurationPlanner::plan
trajectory::TrajectoryPtr plan(const SolvableProblem &problem, Result *result=nullptr) override
Plans a trajectory from start state to goal state by using an interpolator to interpolate between the...
Definition: OMPLConfigurationToConfigurationPlanner-impl.hpp:102
GeometricStateSpace.hpp
GeodesicInterpolator.hpp
aikido::planner::Planner::mStateSpace
statespace::ConstStateSpacePtr mStateSpace
State space associated with this planner.
Definition: Planner.hpp:51
aikido::planner::Planner::Result
Base class for planning result of various planning problems.
Definition: Planner.hpp:58
aikido::constraint::dart::createProjectableBounds
std::unique_ptr< Projectable > createProjectableBounds(std::shared_ptr< const statespace::dart::JointStateSpace > _stateSpace)
Create a Projectable that can be used to project the value of a joint back within joint limits.
aikido::constraint::ConstTestablePtr
std::shared_ptr< const Testable > ConstTestablePtr
Definition: Testable.hpp:13