Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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)
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
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
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 }