grasp_tester_fast.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 *  Copyright (c) 2009, Willow Garage, Inc.
00004 *  All rights reserved.
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *   * Redistributions of source code must retain the above copyright
00011 *     notice, this list of conditions and the following disclaimer.
00012 *   * Redistributions in binary form must reproduce the above
00013 *     copyright notice, this list of conditions and the following
00014 *     disclaimer in the documentation and/or other materials provided
00015 *     with the distribution.
00016 *   * Neither the name of the Willow Garage nor the names of its
00017 *     contributors may be used to endorse or promote products derived
00018 *     from this software without specific prior written permission.
00019 *
00020 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00031 *  POSSIBILITY OF SUCH DAMAGE.
00032 *********************************************************************/
00033 
00034 // Author(s): E. Gil Jones
00035 
00036 #include <sstream>
00037 
00038 #include "object_manipulator/grasp_execution/grasp_tester_fast.h"
00039 
00040 #include "object_manipulator/tools/hand_description.h"
00041 #include "object_manipulator/tools/vector_tools.h"
00042 #include "object_manipulator/tools/mechanism_interface.h"
00043 
00044 //#include <demo_synchronizer/synchronizer_client.h>
00045 
00046 using object_manipulation_msgs::GraspResult;
00047 using arm_navigation_msgs::ArmNavigationErrorCodes;
00048 
00049 namespace object_manipulator {
00050 
00051     void  print_contacts(planning_environment::CollisionModels* cm, planning_models::KinematicState* state) {
00052         std::vector<arm_navigation_msgs::ContactInformation> contacts;
00053         cm->getAllCollisionsForState(*state, contacts,1);
00054         if(contacts.size() == 0) {
00055             ROS_WARN_STREAM("Collision reported but no contacts");
00056         }
00057         std::vector<std::string> names;
00058         for(unsigned int j = 0; j < contacts.size(); j++) {
00059             names.push_back(contacts[j].contact_body_1);
00060 
00061             ROS_DEBUG_STREAM_NAMED("manipulation", "Collision between " << contacts[j].contact_body_1
00062                              << " and " << contacts[j].contact_body_2);
00063         }
00064     }
00065 
00066     void visualize_grasps(const object_manipulation_msgs::PickupGoal &pickup_goal,
00067                           const std::vector<manipulation_msgs::Grasp> &grasps,
00068                           const std::vector<GraspExecutionInfo> &execution_info,
00069                           ros::Publisher &vis_marker_publisher) {
00070         /* display markers for all of the grasps */
00071         for(unsigned int i = 0; i < grasps.size(); i++)
00072         {
00073             float r, g, b;
00074             switch(execution_info[i].result_.result_code)
00075             {
00076             case GraspResult::GRASP_OUT_OF_REACH:
00077                 r = 1.0; g = 0.0; b = 0.0;
00078                 break;
00079             case GraspResult::GRASP_IN_COLLISION:
00080                 r = 1.0; g = 0.0; b = 0.0;
00081                 break;
00082             case GraspResult::GRASP_UNFEASIBLE:
00083                 r = 1.0; g = 0.0; b = 0.0;
00084                 break;
00085             case GraspResult::PREGRASP_OUT_OF_REACH:
00086                 r = 1.0; g = 0.0; b = 1.0;
00087                 break;
00088             case GraspResult::PREGRASP_IN_COLLISION:
00089                 r = 1.0; g = 0.0; b = 1.0;
00090                 break;
00091             case GraspResult::PREGRASP_UNFEASIBLE:
00092                 r = 1.0; g = 0.0; b = 1.0;
00093                 break;
00094             case GraspResult::LIFT_OUT_OF_REACH:
00095                 r = 0.0; g = 0.0; b = 1.0;
00096                 break;
00097             case GraspResult::LIFT_IN_COLLISION:
00098                 r = 0.0; g = 0.0; b = 1.0;
00099                 break;
00100             case GraspResult::LIFT_UNFEASIBLE:
00101                 r = 0.0; g = 0.0; b = 1.0;
00102                 break;
00103             case GraspResult::SUCCESS:
00104                 r = 0.0; g = 1.0; b = 0.0;
00105                 break;
00106             default:
00107                 r = 0.0; g = 0.0; b = 0.0;
00108             }
00109 
00110             visualization_msgs::Marker marker;
00111 
00112             marker.pose = grasps[i].grasp_pose.pose;
00113             marker.header.frame_id = grasps[i].grasp_pose.header.frame_id;
00114             marker.header.stamp = ros::Time::now();
00115             std::ostringstream marker_ns;
00116             marker_ns << "grasp " << i << " (" << execution_info[i].result_.result_code << ")";
00117             marker.ns = marker_ns.str();
00118 
00119             marker.action = visualization_msgs::Marker::ADD;
00120             marker.lifetime = ros::Duration();
00121 
00122             marker.type = visualization_msgs::Marker::ARROW;
00123             marker.scale.x = 0.1235;
00124             marker.scale.y = 0.06;
00125             marker.scale.z = 0.06;
00126             marker.color.r = r;
00127             marker.color.g = g;
00128             marker.color.b = b;
00129             marker.color.a = 1.0;
00130 
00131             marker.id = 0;
00132 
00133             vis_marker_publisher.publish(marker);
00134         }
00135     }
00136 
00137     GraspTesterFast::GraspTesterFast(planning_environment::CollisionModels* cm,
00138                                      const std::string& plugin_name)
00139                                          : GraspTester(),
00140                                          consistent_angle_(M_PI/12.0),
00141                                          num_points_(10),
00142                                          redundancy_(2),
00143                                          cm_(cm),
00144                                          state_(NULL),
00145                                          kinematics_loader_("kinematics_base","kinematics::KinematicsBase")
00146     {
00147         ros::NodeHandle nh;
00148 
00149         vis_marker_publisher_ = nh.advertise<visualization_msgs::Marker> ("grasp_executor_fast", 128);
00150         vis_marker_array_publisher_ = nh.advertise<visualization_msgs::MarkerArray> ("grasp_executor_fast_array", 128);
00151 
00152         const std::map<std::string, planning_models::KinematicModel::GroupConfig>& group_config_map =
00153                 getCollisionModels()->getKinematicModel()->getJointModelGroupConfigMap();
00154 
00155         for(std::map<std::string, planning_models::KinematicModel::GroupConfig>::const_iterator it = group_config_map.begin();
00156         it != group_config_map.end();
00157         it++) {
00158             kinematics::KinematicsBase* kinematics_solver = NULL;
00159             try
00160             {
00161                 kinematics_solver = kinematics_loader_.createClassInstance(plugin_name);
00162             }
00163             catch(pluginlib::PluginlibException& ex)
00164             {
00165                 ROS_ERROR("The plugin failed to load. Error1: %s", ex.what());    //handle the class failing to load
00166                 return;
00167             }
00168             if(!it->second.base_link_.empty() && !it->second.tip_link_.empty()) {
00169                 ik_solver_map_[it->first] = new arm_kinematics_constraint_aware::ArmKinematicsSolverConstraintAware(kinematics_solver,
00170                                                                                                                     getCollisionModels(),
00171                                                                                                                     it->first);
00172                 ik_solver_map_[it->first]->setSearchDiscretization(.025);
00173             }
00174         }
00175     }
00176 
00177     GraspTesterFast::~GraspTesterFast()
00178     {
00179         for(std::map<std::string, arm_kinematics_constraint_aware::ArmKinematicsSolverConstraintAware*>::iterator it = ik_solver_map_.begin();
00180         it != ik_solver_map_.end();
00181         it++) {
00182             delete it->second;
00183         }
00184     }
00185 
00186     planning_environment::CollisionModels* GraspTesterFast::getCollisionModels() {
00187         if(cm_ == NULL) {
00188             return &mechInterface().getCollisionModels();
00189         } else {
00190             return cm_;
00191         }
00192     }
00193 
00194     planning_models::KinematicState* GraspTesterFast::getPlanningSceneState() {
00195         if(state_ == NULL) {
00196             if(mechInterface().getPlanningSceneState() == NULL)
00197             {
00198                 ROS_ERROR("Planning scene was NULL!  Did you forget to set it somewhere?  Getting new planning scene");
00199                 const arm_navigation_msgs::OrderedCollisionOperations collision_operations;
00200                 const std::vector<arm_navigation_msgs::LinkPadding> link_padding;
00201                 mechInterface().getPlanningScene(collision_operations, link_padding);
00202             }
00203             return mechInterface().getPlanningSceneState();
00204         }
00205         else {
00206             return state_;
00207         }
00208     }
00209 
00211     std::vector<arm_navigation_msgs::LinkPadding>
00212             GraspTesterFast::linkPaddingForGrasp(const object_manipulation_msgs::PickupGoal &pickup_goal)
00213     {
00214         return concat(MechanismInterface::gripperPadding(pickup_goal.arm_name, 0.0),
00215                       pickup_goal.additional_link_padding);
00216     }
00217 
00218 
00219 
00220     void GraspTesterFast::getGroupLinks(const std::string& group_name,
00221                                         std::vector<std::string>& group_links)
00222     {
00223         group_links.clear();
00224         const planning_models::KinematicModel::JointModelGroup* jmg =
00225                 getCollisionModels()->getKinematicModel()->getModelGroup(group_name);
00226         if(jmg == NULL) return;
00227         group_links = jmg->getGroupLinkNames();
00228     }
00229 
00230     void GraspTesterFast::getGroupJoints(const std::string& group_name,
00231                                          std::vector<std::string>& group_joints)
00232     {
00233         if(ik_solver_map_.find(group_name) == ik_solver_map_.end()) {
00234             ROS_ERROR_STREAM("No group for solver " << group_name);
00235             return;
00236         }
00237         group_joints = ik_solver_map_[group_name]->getJointNames();
00238     }
00239 
00240     bool GraspTesterFast::getInterpolatedIK(const std::string& arm_name,
00241                                             const tf::Transform& first_pose,
00242                                             const tf::Vector3& direction,
00243                                             const double& distance,
00244                                             const std::vector<double>& ik_solution,
00245                                             const bool& reverse,
00246                                             const bool& premultiply,
00247                                             trajectory_msgs::JointTrajectory& traj) {
00248 
00249         std::map<std::string, double> ik_solution_map;
00250         for(unsigned int i = 0; i < traj.joint_names.size(); i++) {
00251             ik_solution_map[traj.joint_names[i]] = ik_solution[i];
00252         }
00253 
00254         getPlanningSceneState()->setKinematicState(ik_solution_map);
00255 
00256         geometry_msgs::Pose start_pose;
00257         tf::poseTFToMsg(first_pose, start_pose);
00258 
00259         arm_navigation_msgs::Constraints emp;
00260         arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00261         return ik_solver_map_[arm_name]->interpolateIKDirectional(start_pose,
00262                                                                   direction,
00263                                                                   distance,
00264                                                                   emp,
00265                                                                   getPlanningSceneState(),
00266                                                                   error_code,
00267                                                                   traj,
00268                                                                   redundancy_,
00269                                                                   consistent_angle_,
00270                                                                   reverse,
00271                                                                   premultiply,
00272                                                                   num_points_,
00273                                                                   ros::Duration(2.5),
00274                                                                   false);
00275     }
00276 
00277     /* this doesn't get called since testGrasps() does everything, but we still need to have
00278    this function since it is pure virtual in the base class */
00279     void GraspTesterFast::testGrasp(const object_manipulation_msgs::PickupGoal &pickup_goal,
00280                                     const manipulation_msgs::Grasp &grasp,
00281                                     GraspExecutionInfo &execution_info)  {}
00282 
00283     void GraspTesterFast::testGrasps(const object_manipulation_msgs::PickupGoal &pickup_goal,
00284                                      const std::vector<manipulation_msgs::Grasp> &grasps,
00285                                      std::vector<GraspExecutionInfo> &execution_info,
00286                                      bool return_on_first_hit)
00287 
00288     {
00289       ROS_DEBUG_STREAM_NAMED("manipulation","Grasp tester fast: testing a batch of " << grasps.size() << " grasps");
00290         ros::WallTime start = ros::WallTime::now();
00291         std::map<unsigned int, unsigned int> outcome_count;
00292         planning_environment::CollisionModels* cm = getCollisionModels();
00293         planning_models::KinematicState* state = getPlanningSceneState();
00294         std::map<std::string, double> planning_scene_state_values;
00295         state->getKinematicStateValues(planning_scene_state_values);
00296 
00297         std::vector<std::string> end_effector_links, arm_links;
00298         end_effector_links = handDescription().gripperTouchLinkNames(pickup_goal.arm_name);
00299         //getGroupLinks(handDescription().gripperCollisionName(pickup_goal.arm_name), end_effector_links);
00300         getGroupLinks(handDescription().armGroup(pickup_goal.arm_name), arm_links);
00301 
00302         collision_space::EnvironmentModel::AllowedCollisionMatrix original_acm = cm->getCurrentAllowedCollisionMatrix();
00303         // disable collisions for all links not in the arm we are using
00304         cm->disableCollisionsForNonUpdatedLinks(pickup_goal.arm_name); 
00305         collision_space::EnvironmentModel::AllowedCollisionMatrix group_disable_acm = cm->getCurrentAllowedCollisionMatrix();
00306         collision_space::EnvironmentModel::AllowedCollisionMatrix object_disable_acm = group_disable_acm;
00307         object_disable_acm.changeEntry(pickup_goal.collision_object_name, end_effector_links, true);
00308         collision_space::EnvironmentModel::AllowedCollisionMatrix object_support_disable_acm = object_disable_acm;
00309         if(pickup_goal.allow_gripper_support_collision)
00310         {
00311           ROS_DEBUG_NAMED("manipulation","Disabling collisions between gripper and support surface");
00312           if(pickup_goal.collision_support_surface_name == "\"all\"")
00313           {
00314             for(unsigned int i = 0; i < end_effector_links.size(); i++)
00315             {
00316               object_support_disable_acm.changeEntry(end_effector_links[i], true);
00317             }
00318           }
00319           else
00320           {
00321             ROS_DEBUG_NAMED("manipulation","not all");
00322             object_support_disable_acm.changeEntry(pickup_goal.collision_support_surface_name, end_effector_links, true);
00323           }
00324         }
00325 
00326         /* allows collisions between the object and the and effector */
00327         collision_space::EnvironmentModel::AllowedCollisionMatrix object_all_arm_disable_acm = object_disable_acm;
00328 
00329         /* allows collisions between the end effector and the object, as well as between the end effector and the support surface */
00330         collision_space::EnvironmentModel::AllowedCollisionMatrix object_support_all_arm_disable_acm = object_support_disable_acm;
00331 
00332         /* allows collisions between the arm and anything else */
00333         collision_space::EnvironmentModel::AllowedCollisionMatrix group_all_arm_disable_acm = group_disable_acm;
00334 
00335         //turning off collisions for the arm associated with this end effector
00336         for(unsigned int i = 0; i < arm_links.size(); i++) {
00337             object_all_arm_disable_acm.changeEntry(arm_links[i], true);
00338             object_support_all_arm_disable_acm.changeEntry(arm_links[i], true);
00339             group_all_arm_disable_acm.changeEntry(arm_links[i], true);
00340         }
00341         cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
00342 
00343         //first we apply link padding for grasp check
00344         cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00345 
00346         //setup that's not grasp specific
00347         std_msgs::Header target_header;
00348         target_header.frame_id = pickup_goal.target.reference_frame_id;
00349 
00350         execution_info.clear();
00351         execution_info.resize(grasps.size());
00352 
00353         tf::Vector3 pregrasp_dir;
00354         tf::vector3MsgToTF(doNegate(handDescription().approachDirection(pickup_goal.arm_name)), pregrasp_dir);
00355         pregrasp_dir.normalize();
00356 
00357         tf::Vector3 lift_dir;
00358         tf::vector3MsgToTF(pickup_goal.lift.direction.vector, lift_dir);
00359         lift_dir.normalize();
00360         tf::Vector3 distance_lift_dir = lift_dir*fabs(pickup_goal.lift.desired_distance);
00361         tf::Transform lift_trans(tf::Quaternion(0,0,0,1.0), distance_lift_dir);
00362 
00363         std::vector<tf::Transform> grasp_poses(grasps.size());
00364 
00365         ros::Rate debug_rate(0.2);
00366 
00367         //now this is grasp specific
00368         ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing grasp poses");
00369         for(unsigned int i = 0; i < grasps.size(); i++) {
00370 
00371             //check whether the grasp pose is ok (only checking hand, not arms)
00372             //using pre-grasp posture, cause grasp_posture only matters for closing the gripper
00373             std::map<std::string, double> pre_grasp_joint_vals;
00374             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00375                 pre_grasp_joint_vals[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00376             }
00377             state->setKinematicState(pre_grasp_joint_vals);
00378 
00379             //always true
00380             execution_info[i].result_.continuation_possible = true;
00381 
00382             geometry_msgs::PoseStamped grasp_world_pose_stamped;
00383             if(!cm->convertPoseGivenWorldTransform(*state,
00384                                                    cm->getWorldFrameId(),
00385                                                    grasps[i].grasp_pose.header,
00386                                                    grasps[i].grasp_pose.pose,
00387                                                    grasp_world_pose_stamped)) {
00388               ROS_WARN_STREAM("Can't convert into non-object frame from " << grasps[i].grasp_pose.header.frame_id);
00389               continue;
00390             }
00391             tf::poseMsgToTF(grasp_world_pose_stamped.pose, grasp_poses[i]);
00392 
00393             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),grasp_poses[i]);
00394 
00395             if(cm->isKinematicStateInCollision(*state)) {
00396               ROS_DEBUG_STREAM_NAMED("manipulation","Grasp in collision");
00397                 print_contacts(cm, state);
00398 
00399                 std_msgs::ColorRGBA col_pregrasp;
00400                 col_pregrasp.r = 0.0;
00401                 col_pregrasp.g = 1.0;
00402                 col_pregrasp.b = 1.0;
00403                 col_pregrasp.a = 1.0;
00404                 visualization_msgs::MarkerArray arr;
00405                 cm->getRobotMarkersGivenState(*state, arr, col_pregrasp,
00406                                               "grasp_in_collision",
00407                                               ros::Duration(0.0),
00408                                               &end_effector_links);
00409                 vis_marker_array_publisher_.publish(arr);
00410                 execution_info[i].result_.result_code = GraspResult::GRASP_IN_COLLISION;
00411                 outcome_count[GraspResult::GRASP_IN_COLLISION]++;
00412             } else {
00413                 execution_info[i].result_.result_code = 0;
00414             }
00415         }
00416 
00417         //now we revert link paddings for pre-grasp and lift checks
00418         cm->revertCollisionSpacePaddingToDefault();
00419 
00420         //first we do lift, with the hand in the grasp posture (collisions allowed between gripper and object)
00421         ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing lift poses");
00422         cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
00423         for(unsigned int i = 0; i < grasps.size(); i++) {
00424 
00425             if(execution_info[i].result_.result_code != 0) continue;
00426 
00427             std::map<std::string, double> grasp_joint_vals;
00428             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++) {
00429                 grasp_joint_vals[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00430             }
00431             state->setKinematicState(grasp_joint_vals);
00432 
00433             tf::Transform lift_pose = lift_trans*grasp_poses[i];
00434             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),lift_pose);
00435 
00436             if(cm->isKinematicStateInCollision(*state)) {
00437                 ROS_DEBUG_STREAM_NAMED("manipulation", "Lift in collision");
00438                 print_contacts(cm, state);
00439                 execution_info[i].result_.result_code = GraspResult::LIFT_IN_COLLISION;
00440                 outcome_count[GraspResult::LIFT_IN_COLLISION]++;
00441             }
00442         }
00443 
00444         //now we do pre-grasp not allowing object touch, but with arms disabled
00445         ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing pre-grasp poses");
00446         cm->setAlteredAllowedCollisionMatrix(group_all_arm_disable_acm);
00447         for(unsigned int i = 0; i < grasps.size(); i++) {
00448 
00449             if(execution_info[i].result_.result_code != 0) continue;
00450 
00451             //opening the gripper back to pre_grasp
00452             std::map<std::string, double> pre_grasp_joint_vals;
00453             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00454                 pre_grasp_joint_vals[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00455             }
00456             state->setKinematicState(pre_grasp_joint_vals);
00457 
00458             tf::Vector3 distance_pregrasp_dir = pregrasp_dir * fabs(grasps[0].approach.desired_distance);
00459             tf::Transform pre_grasp_trans(tf::Quaternion(0,0,0,1.0), distance_pregrasp_dir);
00460             tf::Transform pre_grasp_pose = grasp_poses[i]*pre_grasp_trans;
00461             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),pre_grasp_pose);
00462 
00463             if(cm->isKinematicStateInCollision(*state)) {
00464                 ROS_DEBUG_STREAM_NAMED("manipulation", "Pre-grasp in collision");
00465                 print_contacts(cm, state);
00466 
00467                 std_msgs::ColorRGBA col_pregrasp;
00468                 col_pregrasp.r = 1.0;
00469                 col_pregrasp.g = 0.0;
00470                 col_pregrasp.b = 1.0;
00471                 col_pregrasp.a = 1.0;
00472                 visualization_msgs::MarkerArray arr;
00473                 cm->getRobotMarkersGivenState(*state, arr, col_pregrasp,
00474                                               "pre_grasp_in_collision",
00475                                               ros::Duration(0.0),
00476                                               &end_effector_links);
00477                 vis_marker_array_publisher_.publish(arr);
00478 
00479                 execution_info[i].result_.result_code = GraspResult::PREGRASP_IN_COLLISION;
00480                 outcome_count[GraspResult::PREGRASP_IN_COLLISION]++;
00481                 continue;
00482             }
00483         }
00484 
00485         visualize_grasps(pickup_goal, grasps, execution_info, vis_marker_publisher_);
00486 
00487         std_msgs::Header world_header;
00488         world_header.frame_id = cm->getWorldFrameId();
00489         const std::vector<std::string>& joint_names = ik_solver_map_[pickup_goal.arm_name]->getJointNames();
00490 
00491         if(return_on_first_hit) {
00492           ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing InterpolatedIK and returning on first hit");
00493             bool last_ik_failed = false;
00494             for(unsigned int i = 0; i < grasps.size(); i++) {
00495 
00496                 if(execution_info[i].result_.result_code != 0) continue;
00497 
00498                 if(!last_ik_failed) {
00499                     //now we move to the ik portion, which requires re-enabling collisions for the arms
00500                     cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00501 
00502                     //and also reducing link paddings
00503                     cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00504                 }
00505                 //getting back to original state for seed
00506                 state->setKinematicState(planning_scene_state_values);
00507 
00508                 //adjusting planning scene state for pre-grasp
00509                 std::map<std::string, double> pre_grasp_values;
00510                 for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00511                     pre_grasp_values[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00512                 }
00513                 state->setKinematicState(pre_grasp_values);
00514 
00515                 //now call ik for grasp
00516                 geometry_msgs::Pose grasp_geom_pose;
00517                 tf::poseTFToMsg(grasp_poses[i], grasp_geom_pose);
00518                 geometry_msgs::PoseStamped base_link_grasp_pose;
00519                 cm->convertPoseGivenWorldTransform(*state,
00520                                                    ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
00521                                                    world_header,
00522                                                    grasp_geom_pose,
00523                                                    base_link_grasp_pose);
00524 
00525                 arm_navigation_msgs::Constraints emp;
00526                 sensor_msgs::JointState solution;
00527                 arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00528                 ROS_DEBUG_STREAM("X y z " << base_link_grasp_pose.pose.position.x << " "
00529                                 << base_link_grasp_pose.pose.position.y << " "
00530                                 << base_link_grasp_pose.pose.position.z);
00531                 if(!ik_solver_map_[pickup_goal.arm_name]->findConstraintAwareSolution(base_link_grasp_pose.pose,
00532                                                                                       emp,
00533                                                                                       state,
00534                                                                                       solution,
00535                                                                                       error_code,
00536                                                                                       false)) {
00537                     ROS_DEBUG_STREAM_NAMED("manipulation", "Grasp out of reach");
00538                     std_msgs::ColorRGBA col_pregrasp;
00539                     col_pregrasp.r = 0.0;
00540                     col_pregrasp.g = 1.0;
00541                     col_pregrasp.b = 1.0;
00542                     col_pregrasp.a = 1.0;
00543                     visualization_msgs::MarkerArray arr;
00544                     cm->getRobotMarkersGivenState(*state, arr, col_pregrasp,
00545                                                   "out_of_reach",
00546                                                   ros::Duration(0.0),
00547                                                   &end_effector_links);
00548                     vis_marker_array_publisher_.publish(arr);
00549 
00550                     execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
00551                     outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
00552                     last_ik_failed = true;
00553                     continue;
00554                 } else {
00555                     last_ik_failed = false;
00556                 }
00557 
00558                 std::map<std::string, double> ik_map_pre_grasp;
00559                 std::map<std::string, double> ik_map_grasp;
00560                 for(unsigned int j = 0; j < joint_names.size(); j++) {
00561                     ik_map_pre_grasp[joint_names[j]] = solution.position[j];
00562                 }
00563                 ik_map_grasp = ik_map_pre_grasp;
00564 
00565                 for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00566                     ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00567                 }
00568                 for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++) {
00569                     ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00570                 }
00571 
00572                 state->setKinematicState(ik_map_pre_grasp);
00573 
00574                 //now we solve interpolated ik
00575                 tf::Transform base_link_bullet_grasp_pose;
00576                 tf::poseMsgToTF(base_link_grasp_pose.pose, base_link_bullet_grasp_pose);
00577                 execution_info[i].approach_trajectory_.joint_names = joint_names;
00578                 //now we need to do interpolated ik
00579                 if(!getInterpolatedIK(pickup_goal.arm_name,
00580                                       base_link_bullet_grasp_pose,
00581                                       pregrasp_dir,
00582                                       grasps[i].approach.desired_distance,
00583                                       solution.position,
00584                                       true,
00585                                       false,
00586                                       execution_info[i].approach_trajectory_)) {
00587                     ROS_DEBUG_STREAM_NAMED("manipulation", "No interpolated IK for pre-grasp to grasp");
00588                     execution_info[i].result_.result_code = GraspResult::PREGRASP_UNFEASIBLE;
00589                     outcome_count[GraspResult::PREGRASP_UNFEASIBLE]++;
00590                     continue;
00591                 }
00592 
00593                 ROS_DEBUG_STREAM_NAMED("manipulation", "Last approach point is " <<
00594                                  execution_info[i].approach_trajectory_.points.back().positions[0] << " " <<
00595                                  execution_info[i].approach_trajectory_.points.back().positions[1] << " " <<
00596                                  execution_info[i].approach_trajectory_.points.back().positions[2] << " " <<
00597                                  execution_info[i].approach_trajectory_.points.back().positions[3] << " " <<
00598                                  execution_info[i].approach_trajectory_.points.back().positions[4] << " " <<
00599                                  execution_info[i].approach_trajectory_.points.back().positions[5] << " " <<
00600                                  execution_info[i].approach_trajectory_.points.back().positions[6]);
00601 
00602                 state->setKinematicState(ik_map_grasp);
00603                 execution_info[i].lift_trajectory_.joint_names = joint_names;
00604                 if(!getInterpolatedIK(pickup_goal.arm_name,
00605                                       base_link_bullet_grasp_pose,
00606                                       lift_dir,
00607                                       pickup_goal.lift.desired_distance,
00608                                       solution.position,
00609                                       false,
00610                                       true,
00611                                       execution_info[i].lift_trajectory_)) {
00612                     ROS_DEBUG_STREAM_NAMED("manipulation", "No interpolated IK for grasp to lift");
00613                     execution_info[i].result_.result_code = GraspResult::LIFT_UNFEASIBLE;
00614                     outcome_count[GraspResult::LIFT_UNFEASIBLE]++;
00615                     continue;
00616                 }
00617 
00618                 ROS_DEBUG_STREAM_NAMED("manipulation", "First lift point is " <<
00619                                  execution_info[i].lift_trajectory_.points.front().positions[0] << " " <<
00620                                  execution_info[i].lift_trajectory_.points.front().positions[1] << " " <<
00621                                  execution_info[i].lift_trajectory_.points.front().positions[2] << " " <<
00622                                  execution_info[i].lift_trajectory_.points.front().positions[3] << " " <<
00623                                  execution_info[i].lift_trajectory_.points.front().positions[4] << " " <<
00624                                  execution_info[i].lift_trajectory_.points.front().positions[5] << " " <<
00625                                  execution_info[i].lift_trajectory_.points.front().positions[6]);
00626 
00627                 //now we revert link paddings and object collisions and do a final check for the initial ik points
00628                 cm->revertCollisionSpacePaddingToDefault();
00629 
00630                 //the start of the approach needs to be collision-free according to the default collision matrix
00631                 cm->setAlteredAllowedCollisionMatrix(group_disable_acm);
00632 
00633                 if(execution_info[i].approach_trajectory_.points.empty()) {
00634                     ROS_WARN_STREAM("No result code and no points in approach trajectory");
00635                     continue;
00636                 }
00637 
00638                 std::map<std::string, double> pre_grasp_ik = ik_map_pre_grasp;
00639                 for(unsigned int j = 0; j < joint_names.size(); j++) {
00640                     pre_grasp_ik[joint_names[j]] = execution_info[i].approach_trajectory_.points[0].positions[j];
00641                 }
00642                 state->setKinematicState(pre_grasp_ik);
00643                 if(cm->isKinematicStateInCollision(*state)) {
00644                     ROS_DEBUG_STREAM_NAMED("manipulation", "Final pre-grasp check failed");
00645                     std::vector<arm_navigation_msgs::ContactInformation> contacts;
00646                     execution_info[i].result_.result_code = GraspResult::PREGRASP_OUT_OF_REACH;
00647                     outcome_count[GraspResult::PREGRASP_OUT_OF_REACH]++;
00648                     continue;
00649                 }
00650 
00651                 //the object will be attached to the gripper for the lift, so we don't care if the object collides with the hand
00652                 cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00653 
00654                 if(execution_info[i].lift_trajectory_.points.empty()) {
00655                     ROS_WARN_STREAM("No result code and no points in lift trajectory");
00656                     continue;
00657                 }
00658                 std::map<std::string, double> lift_ik = ik_map_pre_grasp;
00659                 for(unsigned int j = 0; j < joint_names.size(); j++) {
00660                     lift_ik[joint_names[j]] = execution_info[i].lift_trajectory_.points.back().positions[j];
00661                 }
00662                 state->setKinematicState(lift_ik);
00663                 if(cm->isKinematicStateInCollision(*state)) {
00664                     ROS_DEBUG_STREAM_NAMED("manipulation", "Final lift check failed");
00665                     execution_info[i].result_.result_code = GraspResult::LIFT_OUT_OF_REACH;
00666                     outcome_count[GraspResult::LIFT_OUT_OF_REACH]++;
00667                     continue;
00668                 } else {
00669                   ROS_DEBUG_STREAM_NAMED("manipulation","Everything successful");
00670                     execution_info[i].result_.result_code = GraspResult::SUCCESS;
00671                     execution_info.resize(i+1);
00672                     outcome_count[GraspResult::SUCCESS]++;
00673                     break;
00674                 }
00675             }
00676             for(std::map<unsigned int, unsigned int>::iterator it = outcome_count.begin();
00677             it != outcome_count.end();
00678             it++) {
00679                 ROS_INFO_STREAM("Outcome " << it->first << " count " << it->second);
00680             }
00681             cm->setAlteredAllowedCollisionMatrix(original_acm);
00682             ROS_INFO_STREAM("Took " << (ros::WallTime::now()-start).toSec());
00683             return;
00684         }
00685 
00686 
00687         //now we move to the ik portion, which requires re-enabling collisions for the arms
00688         cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00689 
00690         //and also reducing link paddings
00691         cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00692         ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing InterpolatedIK");
00693         for(unsigned int i = 0; i < grasps.size(); i++) {
00694 
00695             if(execution_info[i].result_.result_code != 0) continue;
00696 
00697             //getting back to original state for seed
00698             state->setKinematicState(planning_scene_state_values);
00699 
00700             //adjusting planning scene state for pre-grasp
00701             std::map<std::string, double> pre_grasp_values;
00702             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00703                 pre_grasp_values[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00704             }
00705             state->setKinematicState(pre_grasp_values);
00706 
00707             //now call ik for grasp
00708             geometry_msgs::Pose grasp_geom_pose;
00709             tf::poseTFToMsg(grasp_poses[i], grasp_geom_pose);
00710             geometry_msgs::PoseStamped base_link_grasp_pose;
00711             cm->convertPoseGivenWorldTransform(*state,
00712                                                ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
00713                                                world_header,
00714                                                grasp_geom_pose,
00715                                                base_link_grasp_pose);
00716 
00717             arm_navigation_msgs::Constraints emp;
00718             sensor_msgs::JointState solution;
00719             arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00720             if(!ik_solver_map_[pickup_goal.arm_name]->findConstraintAwareSolution(base_link_grasp_pose.pose,
00721                                                                                   emp,
00722                                                                                   state,
00723                                                                                   solution,
00724                                                                                   error_code,
00725                                                                                   false)) {
00726                 ROS_DEBUG_STREAM_NAMED("manipulation", "Grasp out of reach");
00727                 print_contacts(cm, state);
00728                 execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
00729                 outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
00730                 continue;
00731             }
00732 
00733             std::map<std::string, double> ik_map_pre_grasp;
00734             std::map<std::string, double> ik_map_grasp;
00735             for(unsigned int j = 0; j < joint_names.size(); j++) {
00736                 ik_map_pre_grasp[joint_names[j]] = solution.position[j];
00737             }
00738             ik_map_grasp = ik_map_pre_grasp;
00739 
00740             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00741                 ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00742             }
00743             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++) {
00744                 ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00745             }
00746 
00747             state->setKinematicState(ik_map_pre_grasp);
00748 
00749             //now we solve interpolated ik
00750             tf::Transform base_link_bullet_grasp_pose;
00751             tf::poseMsgToTF(base_link_grasp_pose.pose, base_link_bullet_grasp_pose);
00752 
00753             /* try to do interpolated IK from grasp back to pregrasp */
00754             execution_info[i].approach_trajectory_.joint_names = joint_names;
00755             if(!getInterpolatedIK(pickup_goal.arm_name,
00756                                   base_link_bullet_grasp_pose,
00757                                   pregrasp_dir,
00758                                   grasps[i].approach.desired_distance,
00759                                   solution.position,
00760                                   true,
00761                                   false,
00762                                   execution_info[i].approach_trajectory_)) {
00763                 ROS_DEBUG_STREAM_NAMED("manipulation", "No interpolated IK for pre-grasp to grasp");
00764                 execution_info[i].result_.result_code = GraspResult::PREGRASP_UNFEASIBLE;
00765                 outcome_count[GraspResult::PREGRASP_UNFEASIBLE]++;
00766                 continue;
00767             }
00768 
00769             /* try to do interpolated IK for the lift */
00770             state->setKinematicState(ik_map_grasp);
00771             execution_info[i].lift_trajectory_.joint_names = joint_names;
00772             if(!getInterpolatedIK(pickup_goal.arm_name,
00773                                   base_link_bullet_grasp_pose,
00774                                   lift_dir,
00775                                   pickup_goal.lift.desired_distance,
00776                                   solution.position,
00777                                   false,
00778                                   true,
00779                                   execution_info[i].lift_trajectory_)) {
00780                 ROS_DEBUG_STREAM_NAMED("manipulation","No interpolated IK for grasp to lift");
00781                 execution_info[i].result_.result_code = GraspResult::LIFT_UNFEASIBLE;
00782                 outcome_count[GraspResult::LIFT_UNFEASIBLE]++;
00783                 continue;
00784             }
00785         }
00786 
00787         //now we revert link paddings and object collisions and do a final check for the initial ik points
00788         cm->revertCollisionSpacePaddingToDefault();
00789 
00790         cm->setAlteredAllowedCollisionMatrix(group_disable_acm);
00791         ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing grasp start pose after InterpolatedIK");
00792         for(unsigned int i = 0; i < grasps.size(); i++) {
00793             if(execution_info[i].result_.result_code != 0) continue;
00794 
00795             if(execution_info[i].approach_trajectory_.points.empty()) {
00796                 ROS_WARN_STREAM("No result code and no points in approach trajectory");
00797                 continue;
00798             }
00799 
00800             std::map<std::string, double> ik_map_pre_grasp;
00801             for(unsigned int j = 0; j < joint_names.size(); j++) {
00802                 ik_map_pre_grasp[joint_names[j]] = execution_info[i].approach_trajectory_.points[0].positions[j];
00803             }
00804             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00805                 ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00806             }
00807             state->setKinematicState(ik_map_pre_grasp);
00808             if(cm->isKinematicStateInCollision(*state)) {
00809                 ROS_DEBUG_STREAM_NAMED("manipulation","Final pre-grasp check failed");
00810                 print_contacts(cm, state);
00811 
00812                 std::vector<arm_navigation_msgs::ContactInformation> contacts;
00813                 cm->getAllCollisionsForState(*state, contacts,1);
00814                 std::vector<std::string> names;
00815                 for(unsigned int j = 0; j < contacts.size(); j++) {
00816                     names.push_back(contacts[j].contact_body_1);
00817                 }
00818 
00819                 std_msgs::ColorRGBA col_pregrasp;
00820                 col_pregrasp.r = 0.0;
00821                 col_pregrasp.g = 0.0;
00822                 col_pregrasp.b = 1.0;
00823                 col_pregrasp.a = 1.0;
00824                 visualization_msgs::MarkerArray arr;
00825                 cm->getRobotPaddedMarkersGivenState(*state, arr, col_pregrasp,
00826                                                     "padded",
00827                                                     ros::Duration(0.0),
00828                                                     &names);
00829                 vis_marker_array_publisher_.publish(arr);
00830                 execution_info[i].result_.result_code = GraspResult::PREGRASP_OUT_OF_REACH;
00831                 outcome_count[GraspResult::PREGRASP_OUT_OF_REACH]++;
00832                 continue;
00833             }
00834 
00835         }
00836 
00837         //now we need to disable collisions with the object for lift
00838         cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00839         ROS_DEBUG_NAMED("manipulation", "grasp_tester_fast: testing lift end pose after InterpolatedIK");
00840         for(unsigned int i = 0; i < grasps.size(); i++) {
00841 
00842             if(execution_info[i].result_.result_code != 0) continue;
00843 
00844             if(execution_info[i].lift_trajectory_.points.empty()) {
00845                 ROS_WARN_STREAM("No result code and no points in lift trajectory");
00846                 continue;
00847             }
00848             std::map<std::string, double> ik_map_grasp;
00849             for(unsigned int j = 0; j < joint_names.size(); j++) {
00850                 ik_map_grasp[joint_names[j]] = execution_info[i].lift_trajectory_.points.back().positions[j];
00851             }
00852             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++) {
00853                 ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00854             }
00855             state->setKinematicState(ik_map_grasp);
00856             if(cm->isKinematicStateInCollision(*state)) {
00857                 ROS_DEBUG_STREAM_NAMED("manipulation","Final lift check failed");
00858                 print_contacts(cm, state);
00859                 execution_info[i].result_.result_code = GraspResult::LIFT_OUT_OF_REACH;
00860                 outcome_count[GraspResult::LIFT_OUT_OF_REACH]++;
00861                 continue;
00862             } else {
00863                 ROS_DEBUG_STREAM_NAMED("manipulation", "Everything successful");
00864                 execution_info[i].result_.result_code = GraspResult::SUCCESS;
00865                 outcome_count[GraspResult::SUCCESS]++;
00866             }
00867         }
00868         cm->setAlteredAllowedCollisionMatrix(original_acm);
00869 
00870         visualize_grasps(pickup_goal, grasps, execution_info, vis_marker_publisher_);
00871 
00872         ROS_DEBUG_STREAM("Took " << (ros::WallTime::now()-start).toSec());
00873 
00874         for(std::map<unsigned int, unsigned int>::iterator it = outcome_count.begin();
00875         it != outcome_count.end();
00876         it++) {
00877             ROS_INFO_STREAM("Outcome " << it->first << " count " << it->second);
00878         }
00879     }
00880 
00881 
00882 } //namespace object_manipulator


object_manipulator
Author(s): Matei Ciocarlie and Kaijen Hsiao
autogenerated on Mon Oct 6 2014 02:59:50