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


manipulation
Author(s): Ioan Sucan , Sachin Chitta
autogenerated on Mon Jul 24 2017 02:21:50