model_based_joint_state_space.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2012, Willow Garage, Inc.
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 Willow Garage 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 <moveit/ompl_interface/parameterization/model_based_joint_state_space.h>
00038 
00039 ompl_interface::ModelBasedJointStateSpace::ModelBasedJointStateSpace(const robot_model::JointModel *joint_model,
00040                                                                      const robot_model::JointModel::Bounds &joint_bounds) :
00041   ompl::base::StateSpace(), joint_model_(joint_model), joint_bounds_(joint_bounds)
00042 {
00043   // set the state space name
00044   setName(joint_model_->getName());
00045 
00046   if (joint_bounds_.size() != joint_model_->getVariableBounds().size())
00047   {
00048     logError("Joint '%s' from group has incorrect bounds specified. Using the default bounds instead.",  joint_model_->getName().c_str());
00049     joint_bounds_ = joint_model_->getVariableBounds();
00050   }
00051 }
00052 
00053 ompl_interface::ModelBasedJointStateSpace::ModelBasedJointStateSpace(const robot_model::JointModel *joint_model) :
00054   ompl::base::StateSpace(), joint_model_(joint_model)
00055 {
00056   // set the state space name
00057   setName(joint_model_->getName());
00058   joint_bounds_ = joint_model_->getVariableBounds();
00059 }
00060 
00061 ompl_interface::ModelBasedJointStateSpace::~ModelBasedJointStateSpace()
00062 {
00063 }
00064 
00065 ompl::base::State* ompl_interface::ModelBasedJointStateSpace::allocState() const
00066 {
00067   StateType *st = new StateType();
00068   st->joint_state = new robot_state::JointState(joint_model_);
00069   return st;
00070 }
00071 
00072 void ompl_interface::ModelBasedJointStateSpace::freeState(ompl::base::State *state) const
00073 {
00074   delete state->as<StateType>()->joint_state;
00075   delete state->as<StateType>();
00076 }
00077 
00078 unsigned int ompl_interface::ModelBasedJointStateSpace::getDimension() const
00079 {
00080   return joint_model_->getStateSpaceDimension();
00081 }
00082 
00083 double ompl_interface::ModelBasedJointStateSpace::getMaximumExtent() const
00084 {
00085   return joint_model_->getMaximumExtent(joint_bounds_);
00086 }
00087 
00088 void ompl_interface::ModelBasedJointStateSpace::enforceBounds(ompl::base::State *state) const
00089 {
00090   joint_model_->enforceBounds(state->as<StateType>()->joint_state->getVariableValues(), joint_bounds_);
00091 }
00092 
00093 bool ompl_interface::ModelBasedJointStateSpace::satisfiesBounds(const ompl::base::State *state) const
00094 {
00095   return joint_model_->satisfiesBounds(state->as<StateType>()->joint_state->getVariableValues(), joint_bounds_, std::numeric_limits<double>::epsilon());
00096 }
00097 
00098 void ompl_interface::ModelBasedJointStateSpace::copyState(ompl::base::State *destination, const ompl::base::State *source) const
00099 {
00100   *destination->as<StateType>()->joint_state = *source->as<StateType>()->joint_state;
00101 }
00102 
00103 double ompl_interface::ModelBasedJointStateSpace::distance(const ompl::base::State *state1, const ompl::base::State *state2) const
00104 {
00105   return state1->as<StateType>()->joint_state->distance(state2->as<StateType>()->joint_state);
00106 }
00107 
00108 bool ompl_interface::ModelBasedJointStateSpace::equalStates(const ompl::base::State *state1, const ompl::base::State *state2) const
00109 {
00110   return state1->as<StateType>()->joint_state->getVariableValues() == state2->as<StateType>()->joint_state->getVariableValues();
00111 }
00112 
00113 void ompl_interface::ModelBasedJointStateSpace::interpolate(const ompl::base::State *from, const ompl::base::State *to, const double t, ompl::base::State *state) const
00114 {
00115   from->as<StateType>()->joint_state->interpolate(to->as<StateType>()->joint_state, t, state->as<StateType>()->joint_state);
00116   propagateJointStateUpdate(state);
00117 }
00118 
00119 unsigned int ompl_interface::ModelBasedJointStateSpace::getSerializationLength() const
00120 {
00121   return sizeof(double) * joint_model_->getVariableCount();
00122 }
00123 
00124 void ompl_interface::ModelBasedJointStateSpace::serialize(void *serialization, const ompl::base::State *state) const
00125 {
00126   memcpy(serialization, &state->as<StateType>()->joint_state->getVariableValues()[0], joint_model_->getVariableCount() * sizeof(double));
00127 }
00128 
00129 void ompl_interface::ModelBasedJointStateSpace::deserialize(ompl::base::State *state, const void *serialization) const
00130 {
00131   memcpy(&state->as<StateType>()->joint_state->getVariableValues()[0], serialization, joint_model_->getVariableCount() * sizeof(double));
00132   propagateJointStateUpdate(state);
00133 }
00134 
00135 void ompl_interface::ModelBasedJointStateSpace::setPlanningVolume(double minX, double maxX, double minY, double maxY, double minZ, double maxZ)
00136 {
00137   if (joint_model_->getType() == robot_model::JointModel::PLANAR)
00138   {
00139     joint_bounds_[0].first = minX;
00140     joint_bounds_[0].second = maxX;
00141     joint_bounds_[1].first = minY;
00142     joint_bounds_[1].second = maxY;
00143   }
00144   else
00145     if (joint_model_->getType() == robot_model::JointModel::FLOATING)
00146     {
00147       joint_bounds_[0].first = minX;
00148       joint_bounds_[0].second = maxX;
00149       joint_bounds_[1].first = minY;
00150       joint_bounds_[1].second = maxY;
00151       joint_bounds_[2].first = minZ;
00152       joint_bounds_[2].second = maxZ;
00153     }
00154 }
00155 
00156 double* ompl_interface::ModelBasedJointStateSpace::getValueAddressAtIndex(ompl::base::State *state, const unsigned int index) const
00157 {
00158   if (index >= joint_model_->getVariableCount())
00159     return NULL;
00160   return &state->as<StateType>()->joint_state->getVariableValues()[index];
00161 }
00162 
00163 void ompl_interface::ModelBasedJointStateSpace::printState(const ompl::base::State *state, std::ostream &out) const
00164 {
00165   out << "JointState(" << joint_model_->getName() << ") = [ ";
00166   for (std::size_t j = 0 ; j < state->as<StateType>()->joint_state->getVariableValues().size() ; ++j)
00167     out << state->as<StateType>()->joint_state->getVariableValues()[j] << " ";
00168   out << "]" << std::endl;
00169 }
00170 
00171 void ompl_interface::ModelBasedJointStateSpace::printSettings(std::ostream &out) const
00172 {
00173   out << "ModelBasedJointStateSpace '" << getName() << "' at " << this << std::endl;
00174 }
00175 
00176 void ompl_interface::ModelBasedJointStateSpace::propagateJointStateUpdate(ompl::base::State *state) const
00177 {
00178   // update the transform the joint applies
00179   joint_model_->updateTransform(state->as<StateType>()->joint_state->getVariableValues(), state->as<StateType>()->joint_state->getVariableTransform());
00180 
00181   // propagate updates to mimic joints
00182   state->as<StateType>()->joint_state->updateMimicJoints();
00183 }
00184 
00185 ompl::base::StateSamplerPtr ompl_interface::ModelBasedJointStateSpace::allocDefaultStateSampler() const
00186 {
00187   class DefaultStateSampler : public ompl::base::StateSampler
00188   {
00189   public:
00190 
00191     DefaultStateSampler(const ompl::base::StateSpace *space) : ompl::base::StateSampler(space),
00192                                                                values_(space->as<ModelBasedJointStateSpace>()->getJointModel()->getVariableCount())
00193     {
00194     }
00195 
00196     virtual void sampleUniform(ompl::base::State *state)
00197     {
00198       std::vector<double> &values = state->as<ModelBasedJointStateSpace::StateType>()->joint_state->getVariableValues();
00199       values.clear(); // clear first since getRandomValues() does .push_back()
00200 
00201       // generate random values accorrding to JointModel
00202       space_->as<ModelBasedJointStateSpace>()->getJointModel()->getVariableRandomValues(moveit_rng_, values, space_->as<ModelBasedJointStateSpace>()->getJointBounds());
00203 
00204       // propagate transforms
00205       space_->as<ModelBasedJointStateSpace>()->propagateJointStateUpdate(state);
00206     }
00207 
00208     virtual void sampleUniformNear(ompl::base::State *state, const ompl::base::State *near, const double distance)
00209     {
00210       std::vector<double> &values = state->as<ModelBasedJointStateSpace::StateType>()->joint_state->getVariableValues();
00211       values.clear(); // clear first since getRandomValues() does .push_back()
00212 
00213       // generate random values accorrding to JointModel
00214       space_->as<ModelBasedJointStateSpace>()->getJointModel()->getVariableRandomValuesNearBy(moveit_rng_, values, space_->as<ModelBasedJointStateSpace>()->getJointBounds(),
00215                                                                                               near->as<ModelBasedJointStateSpace::StateType>()->joint_state->getVariableValues(),
00216                                                                                               distance);
00217       // propagate transforms
00218       space_->as<ModelBasedJointStateSpace>()->propagateJointStateUpdate(state);
00219     }
00220 
00221     virtual void sampleGaussian(ompl::base::State *state, const ompl::base::State *mean, const double stdDev)
00222     {
00223       sampleUniformNear(state, mean, rng_.gaussian(0.0, stdDev));
00224     }
00225 
00226   protected:
00227     std::vector<double> values_;
00228     random_numbers::RandomNumberGenerator moveit_rng_;
00229   };
00230 
00231   return ompl::base::StateSamplerPtr(static_cast<ompl::base::StateSampler*>(new DefaultStateSampler(this)));
00232 }


ompl
Author(s): Ioan Sucan
autogenerated on Mon Oct 6 2014 11:12:03