ompl_ros_ik_goal_sampleable_region.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, 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 Willow Garage, Inc. 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 
00037 #include <ompl_ros_interface/ik/ompl_ros_ik_goal_sampleable_region.h>
00038 
00039 namespace ompl_ros_interface
00040 {
00041 bool OmplRosIKSampleableRegion::initialize(const ompl::base::StateSpacePtr &state_space,
00042                                            const std::string &kinematics_solver_name,
00043                                            const std::string &group_name,
00044                                            const std::string &end_effector_name,
00045                                            const planning_environment::CollisionModelsInterface* cmi)
00046 {
00047   collision_models_interface_ = cmi;
00048   state_space_ = state_space;
00049   group_name_ = group_name;
00050   end_effector_name_ = end_effector_name;
00051 
00052   ros::NodeHandle node_handle("~");
00053 
00054   if(!kinematics_loader_.isClassAvailable(kinematics_solver_name))
00055   {
00056     ROS_ERROR("pluginlib does not have the class %s",kinematics_solver_name.c_str());
00057     return false;
00058   }
00059   
00060   try
00061   {
00062     kinematics_solver_ = kinematics_loader_.createClassInstance(kinematics_solver_name);
00063     return false;
00064   }
00065   catch(pluginlib::PluginlibException& ex)    //handle the class failing to load
00066   {
00067     ROS_ERROR("The plugin failed to load. Error: %s", ex.what());
00068     return false;
00069   }
00070   std::string base_name, tip_name;
00071   if(!node_handle.hasParam(group_name_+"/root_name"))
00072   {
00073     ROS_ERROR_STREAM("Kinematics solver has no root name " << base_name << " in param ns " << node_handle.getNamespace());
00074     throw new OMPLROSException();
00075   }
00076   node_handle.getParam(group_name_+"/root_name",base_name);
00077 
00078   if(!node_handle.hasParam(group_name_+"/tip_name"))
00079   {
00080     ROS_ERROR_STREAM("Kinematics solver has no root name " << tip_name << " in param ns " << node_handle.getNamespace());
00081     throw new OMPLROSException();
00082   }
00083   node_handle.getParam(group_name_+"/tip_name",tip_name);
00084 
00085   if(!kinematics_solver_->initialize(group_name,
00086                                      base_name,
00087                                      tip_name,
00088                                      .01))
00089   {
00090     ROS_ERROR("Could not initialize kinematics solver for group %s",group_name.c_str());
00091     return false;
00092   }
00093   //  scoped_state_.reset(new ompl::base::ScopedState<ompl::base::CompoundStateSpace>(state_space_));
00094   seed_state_.joint_state.name = kinematics_solver_->getJointNames();
00095   seed_state_.joint_state.position.resize(kinematics_solver_->getJointNames().size());
00096   solution_state_.joint_state.name = kinematics_solver_->getJointNames();
00097   solution_state_.joint_state.position.resize(kinematics_solver_->getJointNames().size());
00098   if(!ompl_ros_interface::getRobotStateToOmplStateMapping(seed_state_,scoped_state_,robot_state_to_ompl_state_mapping_))
00099     return false;
00100   if(!ompl_ros_interface::getOmplStateToRobotStateMapping(scoped_state_,seed_state_,ompl_state_to_robot_state_mapping_))
00101     return false;
00102 }  
00103 
00104 bool OmplRosIKSampleableRegion::configureOnRequest(const arm_navigation_msgs::GetMotionPlan::Request &request,
00105                                                    arm_navigation_msgs::GetMotionPlan::Response &response,
00106                                                    const unsigned int &max_sample_count)
00107 {
00108   max_sample_count_ = max_sample_count;
00109   ik_poses_.clear();
00110   arm_navigation_msgs::Constraints goal_constraints = request.motion_plan_request.goal_constraints;
00111 
00112   if(!collision_models_interface_->convertConstraintsGivenNewWorldTransform(*collision_models_interface_->getPlanningSceneState(),
00113                                                                             goal_constraints,
00114                                                                             kinematics_solver_->getBaseName())) {
00115     response.error_code.val = response.error_code.FRAME_TRANSFORM_FAILURE;
00116     return false;
00117   }
00118   if(!arm_navigation_msgs::constraintsToPoseStampedVector(goal_constraints, ik_poses_))
00119   {
00120     ROS_ERROR("Could not get poses from constraints");
00121     return false;
00122   }       
00123   if(ik_poses_.empty())
00124   {
00125     ROS_WARN("Could not setup goals for inverse kinematics sampling");
00126     return false;
00127   }
00128   for(unsigned int i=0; i < ik_poses_.size(); i++)
00129   {
00130     if(ik_poses_[i].header.frame_id != kinematics_solver_->getBaseName())
00131     {
00132       ROS_ERROR("Goals for inverse kinematic sampling in %s frame are not in kinematics frame: %s",
00133                 ik_poses_[i].header.frame_id.c_str(),
00134                 kinematics_solver_->getBaseName().c_str());
00135       return false;
00136     }
00137   }
00138   return true;
00139 }
00140 
00141 unsigned int OmplRosIKSampleableRegion::maxSampleCount(void) const 
00142 {
00143   return max_sample_count_;
00144 }
00145 
00146 void OmplRosIKSampleableRegion::sampleGoal(ompl::base::State *state) const
00147 
00148 {
00149   std::vector<arm_navigation_msgs::RobotState> sampled_states_vector;
00150   sampleGoals(1,sampled_states_vector);
00151   if(!sampled_states_vector.empty())
00152   {
00153     ompl_ros_interface::robotStateToOmplState(sampled_states_vector.front(),
00154                                               robot_state_to_ompl_state_mapping_,
00155                                               state);    
00156   }
00157 }
00158 
00159 void OmplRosIKSampleableRegion::sampleGoals(const unsigned int &number_goals,
00160                                             std::vector<arm_navigation_msgs::RobotState> &sampled_states_vector) const
00161 {
00162   arm_navigation_msgs::RobotState seed_state,solution_state;
00163   seed_state = seed_state_;
00164   solution_state = solution_state_; 
00165   ompl::base::ScopedState<ompl::base::CompoundStateSpace> scoped_state(state_space_);
00166   unsigned int ik_poses_counter = 0;
00167   for(unsigned int i=0; i < number_goals; i++)
00168   {    
00169     //sample a state at random
00170     scoped_state.random();
00171     ompl_ros_interface::omplStateToRobotState(scoped_state,
00172                                               ompl_state_to_robot_state_mapping_,
00173                                               seed_state);    
00174     int error_code;
00175     if(kinematics_solver_->getPositionIK(ik_poses_[ik_poses_counter].pose,
00176                                          seed_state.joint_state.position,
00177                                          solution_state.joint_state.position,
00178                                          error_code))
00179     {
00180       sampled_states_vector.push_back(solution_state);
00181       ik_poses_counter++;
00182       ik_poses_counter = ik_poses_counter%ik_poses_.size();
00183     }
00184   }
00185 }
00186 }


ompl_ros_interface
Author(s): Sachin Chitta
autogenerated on Thu Dec 12 2013 11:10:58