/usr/share/ompl/demos/OpenDERigidBodyPlanning.cpp is in ompl-demos 1.0.0+ds2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | /*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2010, Rice University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Rice University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/* Author: Ioan Sucan */
#include <ompl/extensions/opende/OpenDESimpleSetup.h>
#include <ompl/base/goals/GoalRegion.h>
#include <ompl/config.h>
#include <iostream>
#include <ode/ode.h>
namespace ob = ompl::base;
namespace og = ompl::geometric;
namespace oc = ompl::control;
/// @cond IGNORE
class RigidBodyEnvironment : public oc::OpenDEEnvironment
{
public:
RigidBodyEnvironment(void) : oc::OpenDEEnvironment()
{
createWorld();
}
virtual ~RigidBodyEnvironment(void)
{
destroyWorld();
}
/**************************************************
* Implementation of functions needed by planning *
**************************************************/
virtual unsigned int getControlDimension(void) const
{
return 3;
}
virtual void getControlBounds(std::vector<double> &lower, std::vector<double> &upper) const
{
static double maxForce = 0.2;
lower.resize(3);
lower[0] = -maxForce;
lower[1] = -maxForce;
lower[2] = -maxForce;
upper.resize(3);
upper[0] = maxForce;
upper[1] = maxForce;
upper[2] = maxForce;
}
virtual void applyControl(const double *control) const
{
dBodyAddForce(boxBody, control[0], control[1], control[2]);
}
virtual bool isValidCollision(dGeomID /*geom1*/, dGeomID /*geom2*/, const dContact& /*contact*/) const
{
return false;
}
virtual void setupContact(dGeomID /*geom1*/, dGeomID /*geom2*/, dContact &contact) const
{
contact.surface.mode = dContactSoftCFM | dContactApprox1;
contact.surface.mu = 0.9;
contact.surface.soft_cfm = 0.2;
}
/**************************************************/
// OMPL does not require this function here; we implement it here
// for convenience. This function is only OpenDE code to create a
// simulation environment. At the end of the function, there is a
// call to setPlanningParameters(), which configures members of
// the base class needed by planners.
void createWorld(void);
// Clear all OpenDE objects
void destroyWorld(void);
// Set parameters needed by the base class (such as the bodies
// that make up to state of the system we are planning for)
void setPlanningParameters(void);
// the simulation world
dWorldID bodyWorld;
// the space for all objects
dSpaceID space;
// the car mass
dMass m;
// the body geom
dGeomID boxGeom;
// the body
dBodyID boxBody;
};
// Define the goal we want to reach
class RigidBodyGoal : public ob::GoalRegion
{
public:
RigidBodyGoal(const ob::SpaceInformationPtr &si) : ob::GoalRegion(si)
{
threshold_ = 0.5;
}
virtual double distanceGoal(const ob::State *st) const
{
const double *pos = st->as<oc::OpenDEStateSpace::StateType>()->getBodyPosition(0);
double dx = fabs(pos[0] - 30);
double dy = fabs(pos[1] - 55);
double dz = fabs(pos[2] - 35);
return sqrt(dx * dx + dy * dy + dz * dz);
}
};
// Define how we project a state
class RigidBodyStateProjectionEvaluator : public ob::ProjectionEvaluator
{
public:
RigidBodyStateProjectionEvaluator(const ob::StateSpace *space) : ob::ProjectionEvaluator(space)
{
}
virtual unsigned int getDimension(void) const
{
return 3;
}
virtual void defaultCellSizes(void)
{
cellSizes_.resize(3);
cellSizes_[0] = 1;
cellSizes_[1] = 1;
cellSizes_[2] = 1;
}
virtual void project(const ob::State *state, ob::EuclideanProjection &projection) const
{
const double *pos = state->as<oc::OpenDEStateSpace::StateType>()->getBodyPosition(0);
projection[0] = pos[0];
projection[1] = pos[1];
projection[2] = pos[2];
}
};
// Define our own space, to include a distance function we want and register a default projection
class RigidBodyStateSpace : public oc::OpenDEStateSpace
{
public:
RigidBodyStateSpace(const oc::OpenDEEnvironmentPtr &env) : oc::OpenDEStateSpace(env)
{
}
virtual double distance(const ob::State *s1, const ob::State *s2) const
{
const double *p1 = s1->as<oc::OpenDEStateSpace::StateType>()->getBodyPosition(0);
const double *p2 = s2->as<oc::OpenDEStateSpace::StateType>()->getBodyPosition(0);
double dx = fabs(p1[0] - p2[0]);
double dy = fabs(p1[1] - p2[1]);
double dz = fabs(p1[2] - p2[2]);
return sqrt(dx * dx + dy * dy + dz * dz);
}
virtual void registerProjections(void)
{
registerDefaultProjection(ob::ProjectionEvaluatorPtr(new RigidBodyStateProjectionEvaluator(this)));
}
};
/// @endcond
int main(int, char **)
{
// initialize OpenDE
dInitODE2(0);
// create the OpenDE environment
oc::OpenDEEnvironmentPtr env(new RigidBodyEnvironment());
// create the state space and the control space for planning
RigidBodyStateSpace *stateSpace = new RigidBodyStateSpace(env);
ob::StateSpacePtr stateSpacePtr = ob::StateSpacePtr(stateSpace);
// this will take care of setting a proper collision checker and the starting state for the planner as the initial OpenDE state
oc::OpenDESimpleSetup ss(stateSpacePtr);
// set the goal we would like to reach
ss.setGoal(ob::GoalPtr(new RigidBodyGoal(ss.getSpaceInformation())));
ob::RealVectorBounds bounds(3);
bounds.setLow(-200);
bounds.setHigh(200);
stateSpace->setVolumeBounds(bounds);
bounds.setLow(-20);
bounds.setHigh(20);
stateSpace->setLinearVelocityBounds(bounds);
stateSpace->setAngularVelocityBounds(bounds);
ss.setup();
ss.print();
if (ss.solve(10))
ss.getSolutionPath().asGeometric().printAsMatrix(std::cout);
dCloseODE();
return 0;
}
/// @cond IGNORE
/***********************************************
* Member function implementations *
***********************************************/
void RigidBodyEnvironment::createWorld(void)
{
// BEGIN SETTING UP AN OPENDE ENVIRONMENT
// ***********************************
bodyWorld = dWorldCreate();
space = dHashSpaceCreate(0);
dWorldSetGravity(bodyWorld, 0, 0, -0.981);
double lx = 0.2;
double ly = 0.2;
double lz = 0.1;
dMassSetBox(&m, 1, lx, ly, lz);
boxGeom = dCreateBox(space, lx, ly, lz);
boxBody = dBodyCreate(bodyWorld);
dBodySetMass(boxBody, &m);
dGeomSetBody(boxGeom, boxBody);
// *********************************
// END SETTING UP AN OPENDE ENVIRONMENT
setPlanningParameters();
}
void RigidBodyEnvironment::destroyWorld(void)
{
dSpaceDestroy(space);
dWorldDestroy(bodyWorld);
}
void RigidBodyEnvironment::setPlanningParameters(void)
{
// Fill in parameters for OMPL:
world_ = bodyWorld;
collisionSpaces_.push_back(space);
stateBodies_.push_back(boxBody);
stepSize_ = 0.05;
maxContacts_ = 3;
minControlSteps_ = 10;
maxControlSteps_ = 500;
}
/// @endcond
|