All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
ODEStatePropagator.cpp
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, Rice University
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Rice University nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include "ompl/extensions/ode/ODEStatePropagator.h"
00038 #include "ompl/extensions/ode/ODEStateSpace.h"
00039 #include "ompl/extensions/ode/ODEControlSpace.h"
00040 #include "ompl/util/Exception.h"
00041 #include "ompl/util/Console.h"
00042 
00043 ompl::control::ODEStatePropagator::ODEStatePropagator(const SpaceInformationPtr &si) : StatePropagator(si)
00044 {
00045     if (ODEStateSpace *oss = dynamic_cast<ODEStateSpace*>(si->getStateSpace().get()))
00046         env_ = oss->getEnvironment();
00047     else
00048         throw Exception("ODE State Space needed for ODEStatePropagator");
00049 }
00050 
00052 namespace ompl
00053 {
00054 
00055     struct CallbackParam
00056     {
00057         const control::ODEEnvironment *env;
00058         bool                           collision;
00059     };
00060 
00061     void nearCallback(void *data, dGeomID o1, dGeomID o2)
00062     {
00063         dBodyID b1 = dGeomGetBody(o1);
00064         dBodyID b2 = dGeomGetBody(o2);
00065 
00066         if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact)) return;
00067 
00068         CallbackParam *cp = reinterpret_cast<CallbackParam*>(data);
00069 
00070         const unsigned int maxContacts = cp->env->getMaxContacts(o1, o2);
00071 
00072         dContact *contact = (dContact*)alloca(maxContacts * sizeof(dContact));
00073 
00074         for (unsigned int i = 0; i < maxContacts; ++i)
00075             cp->env->setupContact(o1, o2, contact[i]);
00076 
00077         if (int numc = dCollide(o1, o2, maxContacts, &contact[0].geom, sizeof(dContact)))
00078         {
00079             for (int i = 0; i < numc; ++i)
00080             {
00081                 dJointID c = dJointCreateContact(cp->env->world_, cp->env->contactGroup_, contact + i);
00082                 dJointAttach(c, b1, b2);
00083                 bool valid = cp->env->isValidCollision(o1, o2, contact[i]);
00084                 if (!valid)
00085                     cp->collision = true;
00086                 if (cp->env->verboseContacts_)
00087                 {
00088                     static msg::Interface msg;
00089                     msg.debug((valid ? "Valid" : "Invalid") + std::string(" contact between ") +
00090                               cp->env->getGeomName(o1) + " and " + cp->env->getGeomName(o2));
00091                 }
00092             }
00093         }
00094     }
00095 }
00097 
00098 void ompl::control::ODEStatePropagator::propagate(const base::State *state, const Control* control, const double duration, base::State *result) const
00099 {
00100     env_->mutex_.lock();
00101 
00102     // place the ODE world at the start state
00103     si_->getStateSpace()->as<ODEStateSpace>()->writeState(state);
00104 
00105     // apply the controls
00106     env_->applyControl(control->as<RealVectorControlSpace::ControlType>()->values);
00107 
00108     // created contacts as needed
00109     CallbackParam cp = { env_.get(), false };
00110     for (unsigned int i = 0 ; i < env_->collisionSpaces_.size() ; ++i)
00111         dSpaceCollide(env_->collisionSpaces_[i],  &cp, &nearCallback);
00112 
00113     // propagate one step forward
00114     dWorldQuickStep(env_->world_, (const dReal)duration);
00115 
00116     // remove created contacts
00117     dJointGroupEmpty(env_->contactGroup_);
00118 
00119     // read the final state from the ODE world
00120     si_->getStateSpace()->as<ODEStateSpace>()->readState(result);
00121 
00122     env_->mutex_.unlock();
00123 
00124     // update the collision flag for the start state, if needed
00125     if (!(state->as<ODEStateSpace::StateType>()->collision & (1 << ODEStateSpace::STATE_COLLISION_KNOWN_BIT)))
00126     {
00127         if (cp.collision)
00128             state->as<ODEStateSpace::StateType>()->collision &= (1 << ODEStateSpace::STATE_COLLISION_VALUE_BIT);
00129         state->as<ODEStateSpace::StateType>()->collision &= (1 << ODEStateSpace::STATE_COLLISION_KNOWN_BIT);
00130     }
00131 }
00132 
00133 bool ompl::control::ODEStatePropagator::canPropagateBackward(void) const
00134 {
00135     return false;
00136 }