reachable_valid_pose_filter.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 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/pick_place/reachable_valid_pose_filter.h>
00038 #include <moveit/kinematic_constraints/utils.h>
00039 #include <eigen_conversions/eigen_msg.h>
00040 #include <boost/bind.hpp>
00041 #include <ros/console.h>
00042 
00043 pick_place::ReachableAndValidPoseFilter::ReachableAndValidPoseFilter(const planning_scene::PlanningSceneConstPtr &scene,
00044                                                                      const collision_detection::AllowedCollisionMatrixConstPtr &collision_matrix,
00045                                                                      const constraint_samplers::ConstraintSamplerManagerPtr &constraints_sampler_manager) :
00046   ManipulationStage("reachable & valid pose filter"),
00047   planning_scene_(scene),
00048   collision_matrix_(collision_matrix),
00049   constraints_sampler_manager_(constraints_sampler_manager)
00050 {
00051 }
00052 
00053 namespace
00054 {
00055 bool isStateCollisionFree(const planning_scene::PlanningScene *planning_scene,
00056                           const collision_detection::AllowedCollisionMatrix *collision_matrix,
00057                           bool verbose,
00058                           const pick_place::ManipulationPlan *manipulation_plan,
00059                           robot_state::RobotState *state,
00060                           const robot_model::JointModelGroup *group,
00061                           const double *joint_group_variable_values) 
00062 { 
00063   collision_detection::CollisionRequest req;
00064   req.verbose = verbose;
00065   req.group_name = manipulation_plan->shared_data_->planning_group_->getName();
00066   
00067   state->setJointGroupPositions(group, joint_group_variable_values);
00068   
00069   if (manipulation_plan->approach_posture_.points.empty())
00070   {  
00071     state->update();
00072     collision_detection::CollisionResult res;
00073     planning_scene->checkCollision(req, res, *state, *collision_matrix);
00074     if (res.collision)
00075       return false;
00076   }
00077   else 
00078     // apply approach posture for the end effector (we always apply it here since it could be the case the sampler changes this posture)
00079     for (std::size_t i = 0 ; i < manipulation_plan->approach_posture_.points.size() ; ++i)
00080     {
00081       state->setVariablePositions(manipulation_plan->approach_posture_.joint_names, manipulation_plan->approach_posture_.points[i].positions);
00082       state->update();
00083       collision_detection::CollisionResult res;
00084       planning_scene->checkCollision(req, res, *state, *collision_matrix);
00085       if (res.collision)
00086         return false;
00087     }
00088   return planning_scene->isStateFeasible(*state);
00089 }
00090 }
00091 
00092 bool pick_place::ReachableAndValidPoseFilter::isEndEffectorFree(const ManipulationPlanPtr &plan, robot_state::RobotState &token_state) const
00093 {
00094   tf::poseMsgToEigen(plan->goal_pose_.pose, plan->transformed_goal_pose_);
00095   plan->transformed_goal_pose_ = planning_scene_->getFrameTransform(token_state, plan->goal_pose_.header.frame_id) * plan->transformed_goal_pose_;
00096   token_state.updateStateWithLinkAt(plan->shared_data_->ik_link_, plan->transformed_goal_pose_);
00097   collision_detection::CollisionRequest req;
00098   req.verbose = verbose_;
00099   collision_detection::CollisionResult res;
00100   req.group_name = plan->shared_data_->end_effector_group_->getName();
00101   planning_scene_->checkCollision(req, res, token_state, *collision_matrix_);
00102   return res.collision == false;
00103 }
00104 
00105 bool pick_place::ReachableAndValidPoseFilter::evaluate(const ManipulationPlanPtr &plan) const
00106 {
00107   // initialize with scene state
00108   robot_state::RobotStatePtr token_state(new robot_state::RobotState(planning_scene_->getCurrentState()));
00109   if (isEndEffectorFree(plan, *token_state))
00110   {
00111     // update the goal pose message if anything has changed; this is because the name of the frame in the input goal pose
00112     // can be that of objects in the collision world but most components are unaware of those transforms,
00113     // so we convert to a frame that is certainly known
00114     if (robot_state::Transforms::sameFrame(planning_scene_->getPlanningFrame(), plan->goal_pose_.header.frame_id))
00115     {
00116       tf::poseEigenToMsg(plan->transformed_goal_pose_, plan->goal_pose_.pose);
00117       plan->goal_pose_.header.frame_id = planning_scene_->getPlanningFrame();
00118     }
00119 
00120     // convert the pose we want to reach to a set of constraints
00121     plan->goal_constraints_ = kinematic_constraints::constructGoalConstraints(plan->shared_data_->ik_link_->getName(), plan->goal_pose_);
00122 
00123     const std::string &planning_group = plan->shared_data_->planning_group_->getName();
00124 
00125     // construct a sampler for the specified constraints; this can end up calling just IK, but it is more general
00126     // and allows for robot-specific samplers, producing samples that also change the base position if needed, etc
00127     plan->goal_sampler_ = constraints_sampler_manager_->selectSampler(planning_scene_, planning_group, plan->goal_constraints_);
00128     if (plan->goal_sampler_)
00129     {
00130       plan->goal_sampler_->setGroupStateValidityCallback(boost::bind(&isStateCollisionFree, planning_scene_.get(), collision_matrix_.get(),
00131                                                                      verbose_, plan.get(), _1, _2, _3));
00132       plan->goal_sampler_->setVerbose(verbose_);
00133       if (plan->goal_sampler_->sample(*token_state, plan->shared_data_->max_goal_sampling_attempts_))
00134       {
00135         plan->possible_goal_states_.push_back(token_state);
00136         return true;
00137       }
00138       else
00139         if (verbose_)
00140           ROS_INFO("Sampler failed to produce a state");
00141     }
00142     else
00143       ROS_ERROR_THROTTLE(1, "No sampler was constructed");
00144   }
00145   plan->error_code_.val = moveit_msgs::MoveItErrorCodes::GOAL_IN_COLLISION;
00146   return false;
00147 }
00148 


manipulation
Author(s): Ioan Sucan , Sachin Chitta , Sachin Chitta
autogenerated on Wed Aug 26 2015 12:44:04