GraspSequenceValidator.cpp
Go to the documentation of this file.
00001 /*
00002  * GraspSequenceValidator.cpp
00003  *
00004  *  Created on: Oct 31, 2012
00005  *      Author: coky
00006  */
00007 
00008 #include  <object_manipulation_tools/manipulation_utils/GraspSequenceValidator.h>
00009 #include <object_manipulator/grasp_execution/grasp_tester_fast.h>
00010 #include "object_manipulator/tools/hand_description.h"
00011 #include "object_manipulator/tools/vector_tools.h"
00012 #include "object_manipulator/tools/mechanism_interface.h"
00013 
00014 
00015 using object_manipulation_msgs::GraspResult;
00016 using arm_navigation_msgs::ArmNavigationErrorCodes;
00017 using namespace object_manipulator;
00018 
00019 GraspSequenceValidator::GraspSequenceValidator(planning_environment::CollisionModels* cm,
00020                                    const std::string& plugin_name)
00021   : GraspTester(),// GraspTesterFast(),
00022     consistent_angle_(M_PI/12.0),
00023     num_points_(10),
00024     redundancy_(2),
00025     cm_(cm),
00026     state_(NULL),
00027     kinematics_loader_("kinematics_base","kinematics::KinematicsBase")
00028 {
00029   ros::NodeHandle nh;
00030 
00031   vis_marker_publisher_ = nh.advertise<visualization_msgs::Marker> ("grasp_executor_fast", 128);
00032   vis_marker_array_publisher_ = nh.advertise<visualization_msgs::MarkerArray> ("grasp_executor_fast_array", 128);
00033 
00034   const std::map<std::string, planning_models::KinematicModel::GroupConfig>& group_config_map =
00035     getCollisionModels()->getKinematicModel()->getJointModelGroupConfigMap();
00036 
00037   for(std::map<std::string, planning_models::KinematicModel::GroupConfig>::const_iterator it = group_config_map.begin();
00038       it != group_config_map.end();
00039       it++)
00040   {
00041     kinematics::KinematicsBase* kinematics_solver = NULL;
00042     try
00043     {
00044       kinematics_solver = kinematics_loader_.createClassInstance(plugin_name);
00045     }
00046     catch(pluginlib::PluginlibException& ex)
00047     {
00048       ROS_ERROR("The plugin failed to load. Error1: %s", ex.what());    //handle the class failing to load
00049       return;
00050     }
00051     if(!it->second.base_link_.empty() && !it->second.tip_link_.empty()) {
00052       ik_solver_map_[it->first] = new arm_kinematics_constraint_aware::ArmKinematicsSolverConstraintAware(kinematics_solver,
00053                                                                                                           getCollisionModels(),
00054                                                                                                           it->first);
00055       ik_solver_map_[it->first]->setSearchDiscretization(.025);
00056     }
00057   }
00058 }
00059 
00060 GraspSequenceValidator::~GraspSequenceValidator()
00061 {
00062 
00063 }
00064 
00065 planning_environment::CollisionModels* GraspSequenceValidator::getCollisionModels()
00066 {
00067   if(cm_ == NULL)
00068   {
00069     return &mechInterface().getCollisionModels();
00070   }
00071   else
00072   {
00073     return cm_;
00074   }
00075 }
00076 
00077 planning_models::KinematicState* GraspSequenceValidator::getPlanningSceneState()
00078 {
00079   if(state_ == NULL) {
00080     if(mechInterface().getPlanningSceneState() == NULL)
00081     {
00082       ROS_ERROR("Planning scene was NULL!  Did you forget to set it somewhere?  Getting new planning scene");
00083       const arm_navigation_msgs::OrderedCollisionOperations collision_operations;
00084       const std::vector<arm_navigation_msgs::LinkPadding> link_padding;
00085       mechInterface().getPlanningScene(collision_operations, link_padding);
00086     }
00087     return mechInterface().getPlanningSceneState();
00088   }
00089   else {
00090     return state_;
00091   }
00092 }
00093 
00095 std::vector<arm_navigation_msgs::LinkPadding>
00096 GraspSequenceValidator::linkPaddingForGrasp(const object_manipulation_msgs::PickupGoal &pickup_goal)
00097 {
00098   return concat(MechanismInterface::gripperPadding(pickup_goal.arm_name, 0.0),
00099                 pickup_goal.additional_link_padding);
00100 }
00101 
00102 
00103 
00104 void GraspSequenceValidator::getGroupLinks(const std::string& group_name,
00105                                     std::vector<std::string>& group_links)
00106 {
00107   group_links.clear();
00108   const planning_models::KinematicModel::JointModelGroup* jmg =
00109     getCollisionModels()->getKinematicModel()->getModelGroup(group_name);
00110   if(jmg == NULL) return;
00111   group_links = jmg->getGroupLinkNames();
00112 }
00113 
00114 void GraspSequenceValidator::getGroupJoints(const std::string& group_name,
00115                                     std::vector<std::string>& group_joints)
00116 {
00117   if(ik_solver_map_.find(group_name) == ik_solver_map_.end()) {
00118     ROS_ERROR_STREAM("No group for solver " << group_name);
00119     return;
00120   }
00121   group_joints = ik_solver_map_[group_name]->getJointNames();
00122 }
00123 
00124 bool GraspSequenceValidator::getInterpolatedIK(const std::string& arm_name,
00125                                         const tf::Transform& first_pose,
00126                                         const tf::Vector3& direction,
00127                                         const double& distance,
00128                                         const std::vector<double>& ik_solution,
00129                                         const bool& reverse,
00130                                         const bool& premultiply,
00131                                         trajectory_msgs::JointTrajectory& traj)
00132 {
00133 
00134   std::map<std::string, double> ik_solution_map;
00135   for(unsigned int i = 0; i < traj.joint_names.size(); i++)
00136   {
00137     ik_solution_map[traj.joint_names[i]] = ik_solution[i];
00138   }
00139 
00140   getPlanningSceneState()->setKinematicState(ik_solution_map);
00141 
00142   geometry_msgs::Pose start_pose;
00143   tf::poseTFToMsg(first_pose, start_pose);
00144 
00145   arm_navigation_msgs::Constraints emp;
00146   arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00147 
00148 
00149   return ik_solver_map_[arm_name]->interpolateIKDirectional(start_pose,
00150                                                             direction,
00151                                                             distance,
00152                                                             emp,
00153                                                             getPlanningSceneState(),
00154                                                             error_code,
00155                                                             traj,
00156                                                             redundancy_,
00157                                                             consistent_angle_,
00158                                                             reverse,
00159                                                             premultiply,
00160                                                             num_points_,
00161                                                             ros::Duration(2.5),
00162                                                             false);
00163 }
00164 
00165 bool GraspSequenceValidator::getInterpolatedIK(const std::string& arm_name,
00166                                         const tf::Transform& initialTransform,
00167                                         const tf::Quaternion &rot,
00168                                         const std::vector<double>& ikSolutionAtStart,
00169                                         std::vector<double>& ikSolutionAtEnd,
00170                                         trajectory_msgs::JointTrajectory& traj)
00171 {
00172 
00173           // updating arm kinematic state (joint configuration at initial transform)
00174           std::map<std::string, double> ik_solution_map;
00175           for(unsigned int i = 0; i < traj.joint_names.size(); i++)
00176           {
00177             ik_solution_map[traj.joint_names[i]] = ikSolutionAtStart[i];
00178           }
00179           getPlanningSceneState()->setKinematicState(ik_solution_map);
00180 
00181           // final pose (rotate initial transform by the rotation requested)
00182           geometry_msgs::Pose end_pose;
00183           tf::Transform endTransform =   initialTransform * tf::Transform(tf::Transform(rot,tf::Vector3(0.0f,0.0f,0.0f)));
00184           tf::poseTFToMsg(endTransform,end_pose);
00185 
00186           // solving ik at final pose
00187           arm_navigation_msgs::Constraints emp;
00188           arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00189           sensor_msgs::JointState jointStateAtEnd;
00190           if(!ik_solver_map_[arm_name]->getPositionIK(end_pose,getPlanningSceneState(),jointStateAtEnd,error_code))
00191           {
00192                   return false;
00193           }
00194           ikSolutionAtEnd.clear();
00195           ikSolutionAtEnd.assign(jointStateAtEnd.position.begin(),jointStateAtEnd.position.end());
00196 
00197           // computing increments
00198           std::vector<double> increments;
00199           for(unsigned int i = 0; i < ikSolutionAtStart.size(); i++)
00200           {
00201                   const double &start = ikSolutionAtStart[i];
00202                   const double &end = ikSolutionAtEnd[i];
00203                   double increment = (end - start)/double(num_points_ - 1);
00204                   increments.push_back(increment);
00205           }
00206 
00207           // interpolating
00208           traj.points.clear();
00209           for(unsigned int i = 0; i < (unsigned int)num_points_; i++)
00210           {
00211                   trajectory_msgs::JointTrajectoryPoint point;
00212                   for(unsigned int j = 0; j < increments.size(); j++)
00213                   {
00214                           double jointVal = ikSolutionAtStart[j] + i*increments[j];
00215                           point.positions.push_back(jointVal);
00216                           point.velocities.push_back(0.0f);
00217                           point.velocities.push_back(0.0f);
00218                   }
00219                   traj.points.push_back(point);
00220           }
00221 
00222           return true;
00223 }
00224 
00225 
00226 void GraspSequenceValidator::testGrasps(const object_manipulation_msgs::PickupGoal &pickup_goal,
00227                                  const std::vector<object_manipulation_msgs::Grasp> &grasps,
00228                                  std::vector<GraspExecutionInfo> &execution_info,
00229                                  bool return_on_first_hit)
00230 
00231 {
00232   ros::WallTime start = ros::WallTime::now();
00233 
00234   std::map<unsigned int, unsigned int> outcome_count;
00235 
00236   planning_environment::CollisionModels* cm = getCollisionModels();
00237   planning_models::KinematicState* state = getPlanningSceneState();
00238 
00239   std::map<std::string, double> planning_scene_state_values;
00240   state->getKinematicStateValues(planning_scene_state_values);
00241 
00242   std::vector<std::string> end_effector_links, arm_links;
00243   getGroupLinks(handDescription().gripperCollisionName(pickup_goal.arm_name), end_effector_links);
00244   getGroupLinks(handDescription().armGroup(pickup_goal.arm_name), arm_links);
00245 
00246   std::stringstream ss;
00247   if(!end_effector_links.empty())
00248   {
00249           ss<<"\n\tUsing gripper collision group:";
00250           for(std::size_t i = 0; i < end_effector_links.size();i++)
00251           {
00252                   ss<<"\n\t"<<end_effector_links[i];
00253           }
00254 
00255           ROS_INFO_STREAM(ss.str());
00256   }
00257   else
00258   {
00259           ROS_WARN_STREAM("Collision for the end-effector links will not be disabled");
00260   }
00261 
00262   collision_space::EnvironmentModel::AllowedCollisionMatrix original_acm = cm->getCurrentAllowedCollisionMatrix();
00263   cm->disableCollisionsForNonUpdatedLinks(pickup_goal.arm_name);
00264   collision_space::EnvironmentModel::AllowedCollisionMatrix group_disable_acm = cm->getCurrentAllowedCollisionMatrix();
00265   collision_space::EnvironmentModel::AllowedCollisionMatrix object_disable_acm = group_disable_acm;
00266   object_disable_acm.changeEntry(pickup_goal.collision_object_name, end_effector_links, true);
00267   collision_space::EnvironmentModel::AllowedCollisionMatrix object_support_disable_acm = object_disable_acm;
00268   if(pickup_goal.allow_gripper_support_collision)
00269   {
00270     if(pickup_goal.collision_support_surface_name == "\"all\"")
00271     {
00272       for(unsigned int i = 0; i < end_effector_links.size(); i++)
00273       {
00274           object_support_disable_acm.changeEntry(end_effector_links[i], true);
00275       }
00276     }
00277     else
00278     {
00279       ROS_INFO("not all");
00280       object_support_disable_acm.changeEntry(pickup_goal.collision_support_surface_name, end_effector_links, true);
00281     }
00282   }
00283 
00284   collision_space::EnvironmentModel::AllowedCollisionMatrix object_all_arm_disable_acm = object_disable_acm;
00285   collision_space::EnvironmentModel::AllowedCollisionMatrix object_support_all_arm_disable_acm = object_support_disable_acm;
00286   collision_space::EnvironmentModel::AllowedCollisionMatrix group_all_arm_disable_acm = group_disable_acm;
00287 
00288   //turning off collisions for the arm associated with this end effector
00289   for(unsigned int i = 0; i < arm_links.size(); i++)
00290   {
00291     object_all_arm_disable_acm.changeEntry(arm_links[i], true);
00292     object_support_all_arm_disable_acm.changeEntry(arm_links[i], true);
00293     group_all_arm_disable_acm.changeEntry(arm_links[i], true);
00294   }
00295 
00296   cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
00297 
00298   //first we apply link padding for grasp check
00299   cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00300 
00301   //setup that's not grasp specific
00302   std_msgs::Header target_header;
00303   target_header.frame_id = pickup_goal.target.reference_frame_id;
00304 
00305   bool in_object_frame = false;
00306   tf::Transform obj_pose(tf::Quaternion(0,0,0,1.0), tf::Vector3(0.0,0.0,0.0));
00307   if(pickup_goal.target.reference_frame_id == pickup_goal.collision_object_name)
00308   {
00309     in_object_frame = true;
00310     geometry_msgs::PoseStamped obj_world_pose_stamped;
00311     cm->convertPoseGivenWorldTransform(*state,
00312                                        cm->getWorldFrameId(),
00313                                        pickup_goal.target.potential_models[0].pose.header,
00314                                        pickup_goal.target.potential_models[0].pose.pose,
00315                                        obj_world_pose_stamped);
00316     tf::poseMsgToTF(obj_world_pose_stamped.pose, obj_pose);
00317   }
00318 
00319   execution_info.clear();
00320   execution_info.resize(grasps.size());
00321 
00322   tf::Vector3 pregrasp_dir;
00323   tf::vector3MsgToTF(doNegate(handDescription().approachDirection(pickup_goal.arm_name)), pregrasp_dir);
00324   pregrasp_dir.normalize();
00325 
00326   tf::Vector3 lift_dir;
00327   tf::vector3MsgToTF(pickup_goal.lift.direction.vector, lift_dir);
00328   lift_dir.normalize();
00329   tf::Vector3 distance_lift_dir = lift_dir*fabs(pickup_goal.lift.desired_distance);
00330   tf::Transform lift_trans(tf::Quaternion(0,0,0,1.0), distance_lift_dir);
00331 
00332   std::vector<tf::Transform> grasp_poses(grasps.size());
00333 
00334   //now this is grasp specific
00335   for(unsigned int i = 0; i < grasps.size(); i++)
00336   {
00337 
00338     //check whether the grasp pose is ok (only checking hand, not arms)
00339     //using pre-grasp posture, cause grasp_posture only matters for closing the gripper
00340     std::map<std::string, double> pre_grasp_joint_vals;
00341     for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++) {
00342       pre_grasp_joint_vals[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00343     }
00344     state->setKinematicState(pre_grasp_joint_vals);
00345 
00346     //always true
00347     execution_info[i].result_.continuation_possible = true;
00348 
00349     if(!in_object_frame) {
00350       geometry_msgs::PoseStamped grasp_world_pose_stamped;
00351       if(!cm->convertPoseGivenWorldTransform(*state,
00352                                              cm->getWorldFrameId(),
00353                                              target_header,
00354                                              grasps[i].grasp_pose,
00355                                              grasp_world_pose_stamped))
00356       {
00357         ROS_WARN_STREAM("Can't convert into non-object frame " << target_header.frame_id);
00358         continue;
00359       }
00360 
00361       tf::poseMsgToTF(grasp_world_pose_stamped.pose, grasp_poses[i]);
00362 
00363     }
00364     else
00365     {
00366       tf::Transform gp;
00367       tf::poseMsgToTF(grasps[i].grasp_pose, gp);
00368       grasp_poses[i] = obj_pose*gp;
00369     }
00370     state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),grasp_poses[i]);
00371 
00372     if(cm->isKinematicStateInCollision(*state))
00373     {
00374       ROS_INFO_STREAM("Grasp in collision");
00375       execution_info[i].result_.result_code = GraspResult::GRASP_IN_COLLISION;
00376       outcome_count[GraspResult::GRASP_IN_COLLISION]++;
00377     }
00378     else
00379     {
00380       execution_info[i].result_.result_code = 0;
00381     }
00382   }
00383 
00384   //now we revert link paddings for pre-grasp and lift checks
00385   cm->revertCollisionSpacePaddingToDefault();
00386 
00387   //first we do lift, with the hand in the grasp posture (collisions allowed between gripper and object)
00388   cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
00389   for(unsigned int i = 0; i < grasps.size(); i++)
00390   {
00391 
00392     if(execution_info[i].result_.result_code != 0) continue;
00393 
00394     std::map<std::string, double> grasp_joint_vals;
00395     for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
00396     {
00397       grasp_joint_vals[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00398     }
00399     state->setKinematicState(grasp_joint_vals);
00400 
00401     tf::Transform lift_pose = lift_trans*grasp_poses[i];
00402     state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),lift_pose);
00403 
00404     if(cm->isKinematicStateInCollision(*state))
00405     {
00406       ROS_DEBUG_STREAM("Lift in collision");
00407       execution_info[i].result_.result_code = GraspResult::LIFT_IN_COLLISION;
00408       outcome_count[GraspResult::LIFT_IN_COLLISION]++;
00409     }
00410   }
00411 
00412   //now we do pre-grasp not allowing object touch, but with arms disabled
00413   cm->setAlteredAllowedCollisionMatrix(group_all_arm_disable_acm);
00414 
00415   for(unsigned int i = 0; i < grasps.size(); i++)
00416   {
00417 
00418     if(execution_info[i].result_.result_code != 0) continue;
00419 
00420     //opening the gripper back to pre_grasp
00421     std::map<std::string, double> pre_grasp_joint_vals;
00422     for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00423     {
00424       pre_grasp_joint_vals[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00425     }
00426 
00427     state->setKinematicState(pre_grasp_joint_vals);
00428 
00429     tf::Vector3 distance_pregrasp_dir = pregrasp_dir*fabs(grasps[0].desired_approach_distance);
00430     tf::Transform pre_grasp_trans(tf::Quaternion(0,0,0,1.0), distance_pregrasp_dir);
00431     tf::Transform pre_grasp_pose = grasp_poses[i]*pre_grasp_trans;
00432     //tf::Transform pre_grasp_pose = pre_grasp_trans * grasp_poses[i]
00433     state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),pre_grasp_pose);
00434 
00435     if(cm->isKinematicStateInCollision(*state))
00436     {
00437       ROS_DEBUG_STREAM("Pre-grasp in collision");
00438       execution_info[i].result_.result_code = GraspResult::PREGRASP_IN_COLLISION;
00439       outcome_count[GraspResult::PREGRASP_IN_COLLISION]++;
00440       continue;
00441     }
00442   }
00443 
00444   std_msgs::Header world_header;
00445   world_header.frame_id = cm->getWorldFrameId();
00446   const std::vector<std::string>& joint_names = ik_solver_map_[pickup_goal.arm_name]->getJointNames();
00447 
00448   if(return_on_first_hit)
00449   {
00450 
00451     bool last_ik_failed = false;
00452     for(unsigned int i = 0; i < grasps.size(); i++)
00453     {
00454 
00455       if(execution_info[i].result_.result_code != 0) continue;
00456 
00457       if(!last_ik_failed)
00458       {
00459         //now we move to the ik portion, which requires re-enabling collisions for the arms
00460         cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00461 
00462         //and also reducing link paddings
00463         cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00464 
00465       }
00466       //getting back to original state for seed
00467       state->setKinematicState(planning_scene_state_values);
00468 
00469       //adjusting planning scene state for pre-grasp
00470       std::map<std::string, double> pre_grasp_values;
00471       for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00472       {
00473         pre_grasp_values[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00474       }
00475 
00476       state->setKinematicState(pre_grasp_values);
00477 
00478       //now call ik for grasp
00479       geometry_msgs::Pose grasp_geom_pose;
00480       tf::poseTFToMsg(grasp_poses[i], grasp_geom_pose);
00481       geometry_msgs::PoseStamped base_link_grasp_pose;
00482       cm->convertPoseGivenWorldTransform(*state,
00483                                          ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
00484                                          world_header,
00485                                          grasp_geom_pose,
00486                                          base_link_grasp_pose);
00487 
00488       arm_navigation_msgs::Constraints emp;
00489       sensor_msgs::JointState solution;
00490       arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00491       ROS_INFO_STREAM("X y z " << base_link_grasp_pose.pose.position.x << " "
00492                       << base_link_grasp_pose.pose.position.y << " "
00493                       << base_link_grasp_pose.pose.position.z);
00494       if(!ik_solver_map_[pickup_goal.arm_name]->findConstraintAwareSolution(base_link_grasp_pose.pose,
00495                                                                             emp,
00496                                                                             state,
00497                                                                             solution,
00498                                                                             error_code,
00499                                                                             false)) {
00500         ROS_DEBUG_STREAM("Grasp out of reach");
00501 
00502         execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
00503         outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
00504         last_ik_failed = true;
00505         continue;
00506       }
00507       else
00508       {
00509         last_ik_failed = false;
00510       }
00511 
00512       std::map<std::string, double> ik_map_pre_grasp;
00513       std::map<std::string, double> ik_map_grasp;
00514       for(unsigned int j = 0; j < joint_names.size(); j++)
00515       {
00516         ik_map_pre_grasp[joint_names[j]] = solution.position[j];
00517       }
00518 
00519       ik_map_grasp = ik_map_pre_grasp;
00520 
00521       for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00522       {
00523         ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00524       }
00525       for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
00526       {
00527         ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00528       }
00529 
00530       state->setKinematicState(ik_map_pre_grasp);
00531 
00532       //now we solve interpolated ik
00533       tf::Transform base_link_bullet_grasp_pose;
00534       tf::poseMsgToTF(base_link_grasp_pose.pose, base_link_bullet_grasp_pose);
00535       execution_info[i].approach_trajectory_.joint_names = joint_names;
00536       //now we need to do interpolated ik
00537       if(!getInterpolatedIK(pickup_goal.arm_name,
00538                             base_link_bullet_grasp_pose,
00539                             pregrasp_dir,
00540                             grasps[i].desired_approach_distance,
00541                             solution.position,
00542                             true,
00543                             true,
00544                             execution_info[i].approach_trajectory_))
00545       {
00546         ROS_DEBUG_STREAM("No interpolated IK for pre-grasp to grasp");
00547         execution_info[i].result_.result_code = GraspResult::PREGRASP_UNFEASIBLE;
00548         outcome_count[GraspResult::PREGRASP_UNFEASIBLE]++;
00549         continue;
00550       }
00551 
00552       ROS_DEBUG_STREAM("Last approach point is " <<
00553                        execution_info[i].approach_trajectory_.points.back().positions[0] << " " <<
00554                        execution_info[i].approach_trajectory_.points.back().positions[1] << " " <<
00555                        execution_info[i].approach_trajectory_.points.back().positions[2] << " " <<
00556                        execution_info[i].approach_trajectory_.points.back().positions[3] << " " <<
00557                        execution_info[i].approach_trajectory_.points.back().positions[4] << " " <<
00558                        execution_info[i].approach_trajectory_.points.back().positions[5] << " " <<
00559                        execution_info[i].approach_trajectory_.points.back().positions[6]);
00560 
00561       state->setKinematicState(ik_map_grasp);
00562       execution_info[i].lift_trajectory_.joint_names = joint_names;
00563       if(!getInterpolatedIK(pickup_goal.arm_name,
00564                             base_link_bullet_grasp_pose,
00565                             lift_dir,
00566                             pickup_goal.lift.desired_distance,
00567                             solution.position,
00568                             false,
00569                             true,
00570                             execution_info[i].lift_trajectory_))
00571       {
00572         ROS_DEBUG_STREAM("No interpolated IK for grasp to lift");
00573         execution_info[i].result_.result_code = GraspResult::LIFT_UNFEASIBLE;
00574         outcome_count[GraspResult::LIFT_UNFEASIBLE]++;
00575         continue;
00576       }
00577 
00578       ROS_DEBUG_STREAM("First lift point is " <<
00579                        execution_info[i].lift_trajectory_.points.front().positions[0] << " " <<
00580                        execution_info[i].lift_trajectory_.points.front().positions[1] << " " <<
00581                        execution_info[i].lift_trajectory_.points.front().positions[2] << " " <<
00582                        execution_info[i].lift_trajectory_.points.front().positions[3] << " " <<
00583                        execution_info[i].lift_trajectory_.points.front().positions[4] << " " <<
00584                        execution_info[i].lift_trajectory_.points.front().positions[5] << " " <<
00585                        execution_info[i].lift_trajectory_.points.front().positions[6]);
00586 
00587       //now we revert link paddings and object collisions and do a final check for the initial ik points
00588       cm->revertCollisionSpacePaddingToDefault();
00589 
00590       //the start of the approach needs to be collision-free according to the default collision matrix
00591       cm->setAlteredAllowedCollisionMatrix(group_disable_acm);
00592 
00593       if(execution_info[i].approach_trajectory_.points.empty())
00594       {
00595         ROS_WARN_STREAM("No result code and no points in approach trajectory");
00596         continue;
00597       }
00598 
00599       std::map<std::string, double> pre_grasp_ik = ik_map_pre_grasp;
00600       for(unsigned int j = 0; j < joint_names.size(); j++)
00601       {
00602         pre_grasp_ik[joint_names[j]] = execution_info[i].approach_trajectory_.points[0].positions[j];
00603       }
00604       state->setKinematicState(pre_grasp_ik);
00605       if(cm->isKinematicStateInCollision(*state))
00606       {
00607         ROS_DEBUG_STREAM("Final pre-grasp check failed");
00608         std::vector<arm_navigation_msgs::ContactInformation> contacts;
00609         execution_info[i].result_.result_code = GraspResult::PREGRASP_OUT_OF_REACH;
00610         outcome_count[GraspResult::PREGRASP_OUT_OF_REACH]++;
00611         continue;
00612       }
00613 
00614       //the object will be attached to the gripper for the lift, so we don't care if the object collides with the hand
00615       cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00616 
00617       if(execution_info[i].lift_trajectory_.points.empty())
00618       {
00619         ROS_WARN_STREAM("No result code and no points in lift trajectory");
00620         continue;
00621       }
00622       std::map<std::string, double> lift_ik = ik_map_pre_grasp;
00623       for(unsigned int j = 0; j < joint_names.size(); j++)
00624       {
00625        lift_ik[joint_names[j]] = execution_info[i].lift_trajectory_.points.back().positions[j];
00626       }
00627       state->setKinematicState(lift_ik);
00628       if(cm->isKinematicStateInCollision(*state))
00629       {
00630         ROS_DEBUG_STREAM("Final lift check failed");
00631         execution_info[i].result_.result_code = GraspResult::LIFT_OUT_OF_REACH;
00632         outcome_count[GraspResult::LIFT_OUT_OF_REACH]++;
00633         continue;
00634       }
00635       else
00636       {
00637         ROS_INFO_STREAM("Everything successful");
00638         execution_info[i].result_.result_code = GraspResult::SUCCESS;
00639         execution_info.resize(i+1);
00640         outcome_count[GraspResult::SUCCESS]++;
00641         break;
00642       }
00643     }
00644 
00645     for(std::map<unsigned int, unsigned int>::iterator it = outcome_count.begin();
00646         it != outcome_count.end();
00647         it++)
00648     {
00649       ROS_INFO_STREAM("Outcome " << it->first << " count " << it->second);
00650     }
00651 
00652     cm->setAlteredAllowedCollisionMatrix(original_acm);
00653     ROS_INFO_STREAM("Took " << (ros::WallTime::now()-start).toSec());
00654     return;
00655 
00656   }
00657 
00658   //now we move to the ik portion, which requires re-enabling collisions for the arms
00659   cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00660 
00661   //and also reducing link paddings
00662   cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00663 
00664   for(unsigned int i = 0; i < grasps.size(); i++)
00665   {
00666 
00667     if(execution_info[i].result_.result_code != 0) continue;
00668 
00669     //getting back to original state for seed
00670     state->setKinematicState(planning_scene_state_values);
00671 
00672     //adjusting planning scene state for pre-grasp
00673     std::map<std::string, double> pre_grasp_values;
00674     for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00675     {
00676       pre_grasp_values[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00677     }
00678 
00679     state->setKinematicState(pre_grasp_values);
00680 
00681     //now call ik for grasp
00682     geometry_msgs::Pose grasp_geom_pose;
00683     tf::poseTFToMsg(grasp_poses[i], grasp_geom_pose);
00684     geometry_msgs::PoseStamped base_link_grasp_pose;
00685     cm->convertPoseGivenWorldTransform(*state,
00686                                        ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
00687                                        world_header,
00688                                        grasp_geom_pose,
00689                                        base_link_grasp_pose);
00690 
00691     arm_navigation_msgs::Constraints emp;
00692     sensor_msgs::JointState solution;
00693     arm_navigation_msgs::ArmNavigationErrorCodes error_code;
00694     if(!ik_solver_map_[pickup_goal.arm_name]->findConstraintAwareSolution(base_link_grasp_pose.pose,
00695                                                                           emp,
00696                                                                           state,
00697                                                                           solution,
00698                                                                           error_code,
00699                                                                           false))
00700     {
00701       ROS_DEBUG_STREAM("Grasp out of reach");
00702       execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
00703       outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
00704       continue;
00705     }
00706 
00707     std::map<std::string, double> ik_map_pre_grasp;
00708     std::map<std::string, double> ik_map_grasp;
00709     for(unsigned int j = 0; j < joint_names.size(); j++)
00710     {
00711       ik_map_pre_grasp[joint_names[j]] = solution.position[j];
00712     }
00713     ik_map_grasp = ik_map_pre_grasp;
00714 
00715     for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00716     {
00717       ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00718     }
00719 
00720     for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
00721     {
00722       ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00723     }
00724 
00725     state->setKinematicState(ik_map_pre_grasp);
00726 
00727     //now we solve interpolated ik
00728     tf::Transform base_link_bullet_grasp_pose;
00729     tf::poseMsgToTF(base_link_grasp_pose.pose, base_link_bullet_grasp_pose);
00730     //now we need to do interpolated ik
00731     execution_info[i].approach_trajectory_.joint_names = joint_names;
00732     if(!getInterpolatedIK(pickup_goal.arm_name,
00733                           base_link_bullet_grasp_pose,
00734                           pregrasp_dir,
00735                           grasps[i].desired_approach_distance,
00736                           solution.position,
00737                           true,
00738                           false,
00739                           execution_info[i].approach_trajectory_))
00740     {
00741       ROS_DEBUG_STREAM("No interpolated IK for pre-grasp to grasp");
00742       execution_info[i].result_.result_code = GraspResult::PREGRASP_UNFEASIBLE;
00743       outcome_count[GraspResult::PREGRASP_UNFEASIBLE]++;
00744       continue;
00745     }
00746 
00747     state->setKinematicState(ik_map_grasp);
00748     execution_info[i].lift_trajectory_.joint_names = joint_names;
00749     if(!getInterpolatedIK(pickup_goal.arm_name,
00750                           base_link_bullet_grasp_pose,
00751                           lift_dir,
00752                           pickup_goal.lift.desired_distance,
00753                           solution.position,
00754                           false,
00755                           true,
00756                           execution_info[i].lift_trajectory_))
00757     {
00758 
00759       ROS_DEBUG_STREAM("No interpolated IK for grasp to lift");
00760       execution_info[i].result_.result_code = GraspResult::LIFT_UNFEASIBLE;
00761       outcome_count[GraspResult::LIFT_UNFEASIBLE]++;
00762       continue;
00763 
00764     }
00765   }
00766 
00767   //now we revert link paddings and object collisions and do a final check for the initial ik points
00768   cm->revertCollisionSpacePaddingToDefault();
00769 
00770   cm->setAlteredAllowedCollisionMatrix(group_disable_acm);
00771 
00772   for(unsigned int i = 0; i < grasps.size(); i++)
00773   {
00774 
00775     if(execution_info[i].result_.result_code != 0) continue;
00776 
00777     if(execution_info[i].approach_trajectory_.points.empty()) {
00778       ROS_WARN_STREAM("No result code and no points in approach trajectory");
00779       continue;
00780     }
00781 
00782     std::map<std::string, double> ik_map_pre_grasp;
00783     for(unsigned int j = 0; j < joint_names.size(); j++)
00784     {
00785       ik_map_pre_grasp[joint_names[j]] = execution_info[i].approach_trajectory_.points[0].positions[j];
00786     }
00787 
00788     for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00789     {
00790       ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00791     }
00792 
00793     state->setKinematicState(ik_map_pre_grasp);
00794     if(cm->isKinematicStateInCollision(*state))
00795     {
00796       execution_info[i].result_.result_code = GraspResult::PREGRASP_OUT_OF_REACH;
00797       outcome_count[GraspResult::PREGRASP_OUT_OF_REACH]++;
00798       continue;
00799     }
00800 
00801   }
00802 
00803   //now we need to disable collisions with the object for lift
00804   cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
00805 
00806   for(unsigned int i = 0; i < grasps.size(); i++)
00807   {
00808 
00809     if(execution_info[i].result_.result_code != 0) continue;
00810 
00811     if(execution_info[i].lift_trajectory_.points.empty())
00812     {
00813       ROS_WARN_STREAM("No result code and no points in lift trajectory");
00814       continue;
00815     }
00816 
00817     std::map<std::string, double> ik_map_grasp;
00818     for(unsigned int j = 0; j < joint_names.size(); j++)
00819     {
00820       ik_map_grasp[joint_names[j]] = execution_info[i].lift_trajectory_.points.back().positions[j];
00821     }
00822 
00823     for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
00824     {
00825       ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
00826     }
00827 
00828     state->setKinematicState(ik_map_grasp);
00829     if(cm->isKinematicStateInCollision(*state))
00830     {
00831       ROS_DEBUG_STREAM("Final lift check failed");
00832       execution_info[i].result_.result_code = GraspResult::LIFT_OUT_OF_REACH;
00833       outcome_count[GraspResult::LIFT_OUT_OF_REACH]++;
00834       continue;
00835     }
00836     else
00837     {
00838       ROS_DEBUG_STREAM("Everything successful");
00839       execution_info[i].result_.result_code = GraspResult::SUCCESS;
00840       outcome_count[GraspResult::SUCCESS]++;
00841     }
00842   }
00843   cm->setAlteredAllowedCollisionMatrix(original_acm);
00844 
00845   ROS_INFO_STREAM("Took " << (ros::WallTime::now()-start).toSec());
00846 
00847   for(std::map<unsigned int, unsigned int>::iterator it = outcome_count.begin();
00848       it != outcome_count.end();
00849       it++)
00850   {
00851     ROS_INFO_STREAM("Outcome " << it->first << " count " << it->second);
00852   }
00853 }
00854 
00855 void GraspSequenceValidator::testGrasps(const object_manipulation_msgs::PickupGoal &pickup_goal,
00856                         const std::vector<object_manipulation_msgs::Grasp> &grasps,double twistAngle,
00857                         std::vector<GraspSequenceDetails> &execution_info,
00858                         bool return_on_first_hit)
00859 {
00860           ros::WallTime start = ros::WallTime::now();
00861 
00862           std::map<unsigned int, unsigned int> outcome_count;
00863 
00864           planning_environment::CollisionModels* cm = getCollisionModels();
00865           planning_models::KinematicState* state = getPlanningSceneState();
00866 
00867           std::map<std::string, double> planning_scene_state_values;
00868           state->getKinematicStateValues(planning_scene_state_values);
00869 
00870           std::vector<std::string> end_effector_links, arm_links;
00871           getGroupLinks(handDescription().gripperCollisionName(pickup_goal.arm_name), end_effector_links);
00872           getGroupLinks(handDescription().armGroup(pickup_goal.arm_name), arm_links);
00873 
00874           collision_space::EnvironmentModel::AllowedCollisionMatrix original_acm = cm->getCurrentAllowedCollisionMatrix();
00875           cm->disableCollisionsForNonUpdatedLinks(pickup_goal.arm_name);
00876           collision_space::EnvironmentModel::AllowedCollisionMatrix group_disable_acm = cm->getCurrentAllowedCollisionMatrix();
00877           collision_space::EnvironmentModel::AllowedCollisionMatrix object_disable_acm = group_disable_acm;
00878           object_disable_acm.changeEntry(pickup_goal.collision_object_name, end_effector_links, true);
00879           collision_space::EnvironmentModel::AllowedCollisionMatrix object_support_disable_acm = object_disable_acm;
00880           if(pickup_goal.allow_gripper_support_collision)
00881           {
00882             if(pickup_goal.collision_support_surface_name == "\"all\"")
00883             {
00884               for(unsigned int i = 0; i < end_effector_links.size(); i++)
00885               {
00886                   object_support_disable_acm.changeEntry(end_effector_links[i], true);
00887               }
00888             }
00889             else
00890             {
00891               ROS_INFO("not all");
00892               object_support_disable_acm.changeEntry(pickup_goal.collision_support_surface_name, end_effector_links, true);
00893             }
00894           }
00895 
00896           collision_space::EnvironmentModel::AllowedCollisionMatrix object_all_arm_disable_acm = object_disable_acm;
00897           collision_space::EnvironmentModel::AllowedCollisionMatrix object_support_all_arm_disable_acm = object_support_disable_acm;
00898           collision_space::EnvironmentModel::AllowedCollisionMatrix group_all_arm_disable_acm = group_disable_acm;
00899 
00900           //turning off collisions for the arm associated with this end effector
00901           for(unsigned int i = 0; i < arm_links.size(); i++)
00902           {
00903             object_all_arm_disable_acm.changeEntry(arm_links[i], true);
00904             object_support_all_arm_disable_acm.changeEntry(arm_links[i], true);
00905             group_all_arm_disable_acm.changeEntry(arm_links[i], true);
00906           }
00907 
00908           cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
00909 
00910           //first we apply link padding for grasp check
00911           cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
00912 
00913           //setup that's not grasp specific
00914           std_msgs::Header target_header;
00915           target_header.frame_id = pickup_goal.target.reference_frame_id;
00916 
00917           bool in_object_frame = false;
00918           tf::Transform obj_pose(tf::Quaternion(0,0,0,1.0), tf::Vector3(0.0,0.0,0.0));
00919           if(pickup_goal.target.reference_frame_id == pickup_goal.collision_object_name)
00920           {
00921             in_object_frame = true;
00922             geometry_msgs::PoseStamped obj_world_pose_stamped;
00923             cm->convertPoseGivenWorldTransform(*state,
00924                                                cm->getWorldFrameId(),
00925                                                pickup_goal.target.potential_models[0].pose.header,
00926                                                pickup_goal.target.potential_models[0].pose.pose,
00927                                                obj_world_pose_stamped);
00928             tf::poseMsgToTF(obj_world_pose_stamped.pose, obj_pose);
00929           }
00930 
00931           execution_info.clear();
00932           execution_info.resize(grasps.size());
00933 
00934           // defining parameters for grasp moves (approach, twist, and lift)
00935           // approach
00936           tf::Vector3 pregrasp_dir;
00937           tf::vector3MsgToTF(doNegate(handDescription().approachDirection(pickup_goal.arm_name)), pregrasp_dir);
00938           pregrasp_dir.normalize();
00939 
00940           // twist at pick location (rotate about approach direction)
00941           tf::Transform twistTransform = tf::Transform(tf::Quaternion(tf::Vector3(0.0f,0.0f,1.0f),twistAngle),tf::Vector3(0.0f,0.0f,0.0f));
00942 
00943           // lift transform to apply to grasp pose
00944           tf::Vector3 lift_dir;
00945           tf::vector3MsgToTF(pickup_goal.lift.direction.vector, lift_dir);
00946           lift_dir.normalize();
00947           tf::Vector3 distance_lift_dir = lift_dir*fabs(pickup_goal.lift.desired_distance);
00948           tf::Transform lift_trans(tf::Quaternion(0,0,0,1.0), distance_lift_dir);
00949 
00950           std::vector<tf::Transform> grasp_poses(grasps.size());
00951 
00952           //now this is grasp specific
00953           for(unsigned int i = 0; i < grasps.size(); i++)
00954           {
00955 
00956             //check whether the grasp pose is ok (only checking hand, not arms)
00957             //using pre-grasp posture, cause grasp_posture only matters for closing the gripper
00958             std::map<std::string, double> pre_grasp_joint_vals;
00959             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
00960             {
00961               pre_grasp_joint_vals[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
00962             }
00963 
00964             state->setKinematicState(pre_grasp_joint_vals);
00965 
00966             //always true
00967             execution_info[i].result_.continuation_possible = true;
00968 
00969             if(!in_object_frame)
00970             {
00971               geometry_msgs::PoseStamped grasp_world_pose_stamped;
00972 
00973               if(!cm->convertPoseGivenWorldTransform(*state,
00974                                                      cm->getWorldFrameId(),
00975                                                      target_header,
00976                                                      grasps[i].grasp_pose,
00977                                                      grasp_world_pose_stamped))
00978               {
00979                 ROS_WARN_STREAM("Can't convert into non-object frame " << target_header.frame_id);
00980                 continue;
00981               }
00982 
00983               tf::poseMsgToTF(grasp_world_pose_stamped.pose, grasp_poses[i]);
00984 
00985             }
00986             else
00987             {
00988               tf::Transform gp;
00989               tf::poseMsgToTF(grasps[i].grasp_pose, gp);
00990               grasp_poses[i] = obj_pose*gp;
00991             }
00992             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),grasp_poses[i]);
00993 
00994             if(cm->isKinematicStateInCollision(*state))
00995             {
00996               ROS_INFO_STREAM("Grasp in collision");
00997               execution_info[i].result_.result_code = GraspResult::GRASP_IN_COLLISION;
00998               outcome_count[GraspResult::GRASP_IN_COLLISION]++;
00999             }
01000             else
01001             {
01002               execution_info[i].result_.result_code = 0;
01003             }
01004           }
01005 
01006   /*
01007    * ****************************************************************************************
01008    * Checking collision at the start/end of each pick move
01009    */
01010           //now we revert link paddings for pre-grasp and lift checks
01011           cm->revertCollisionSpacePaddingToDefault();
01012 
01013           // check collision at twist pose
01014           cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
01015           for(unsigned int i = 0; i < grasps.size(); i++)
01016           {
01017 
01018             if(execution_info[i].result_.result_code != 0) continue;
01019 
01020             std::map<std::string, double> grasp_joint_vals;
01021             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
01022             {
01023               grasp_joint_vals[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
01024             }
01025             state->setKinematicState(grasp_joint_vals);
01026 
01027             tf::Transform twistPose = grasp_poses[i]*twistTransform;// twistTransform*grasp_poses[i];
01028             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),twistPose);
01029 
01030             if(cm->isKinematicStateInCollision(*state))
01031             {
01032               ROS_DEBUG_STREAM("Twist pose in collision");
01033               execution_info[i].result_.result_code = GraspResult::GRASP_IN_COLLISION;
01034               outcome_count[GraspResult::GRASP_IN_COLLISION]++;
01035             }
01036           }
01037 
01038           // check collision at lift pose, with the hand in the grasp posture (collisions allowed between gripper and object)
01039           cm->setAlteredAllowedCollisionMatrix(object_support_all_arm_disable_acm);
01040           for(unsigned int i = 0; i < grasps.size(); i++)
01041           {
01042 
01043             if(execution_info[i].result_.result_code != 0) continue;
01044 
01045             std::map<std::string, double> grasp_joint_vals;
01046             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
01047             {
01048               grasp_joint_vals[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
01049             }
01050             state->setKinematicState(grasp_joint_vals);
01051 
01052             tf::Transform lift_pose = lift_trans* grasp_poses[i]* twistTransform;
01053             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),lift_pose);
01054 
01055             if(cm->isKinematicStateInCollision(*state))
01056             {
01057               ROS_DEBUG_STREAM("Lift in collision");
01058               execution_info[i].result_.result_code = GraspResult::LIFT_IN_COLLISION;
01059               outcome_count[GraspResult::LIFT_IN_COLLISION]++;
01060             }
01061           }
01062 
01063           // checking collision at pre-grasp, arms are disabled but collision between objects is not allowed
01064           cm->setAlteredAllowedCollisionMatrix(group_all_arm_disable_acm);
01065 
01066           for(unsigned int i = 0; i < grasps.size(); i++)
01067           {
01068 
01069             if(execution_info[i].result_.result_code != 0) continue;
01070 
01071             //opening the gripper back to pre_grasp
01072             std::map<std::string, double> pre_grasp_joint_vals;
01073             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
01074             {
01075               pre_grasp_joint_vals[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
01076             }
01077 
01078             state->setKinematicState(pre_grasp_joint_vals);
01079 
01080             tf::Vector3 distance_pregrasp_dir = pregrasp_dir*fabs(grasps[0].desired_approach_distance);
01081             tf::Transform pre_grasp_trans(tf::Quaternion(0,0,0,1.0), distance_pregrasp_dir);
01082             tf::Transform pre_grasp_pose = grasp_poses[i]*pre_grasp_trans;
01083             state->updateKinematicStateWithLinkAt(handDescription().gripperFrame(pickup_goal.arm_name),pre_grasp_pose);
01084 
01085             if(cm->isKinematicStateInCollision(*state))
01086             {
01087               ROS_DEBUG_STREAM("Pre-grasp in collision");
01088               execution_info[i].result_.result_code = GraspResult::PREGRASP_IN_COLLISION;
01089               outcome_count[GraspResult::PREGRASP_IN_COLLISION]++;
01090               continue;
01091             }
01092           }
01093 
01094         /*
01095         * All collision checks at each pick move (pre-grasp, twist, lift) end here.  The next step is to find
01096         * the ik solution for each pose.
01097         * **************************************************************************************************
01098         */
01099 
01100           std_msgs::Header world_header;
01101           world_header.frame_id = cm->getWorldFrameId();
01102           const std::vector<std::string>& joint_names = ik_solver_map_[pickup_goal.arm_name]->getJointNames();
01103 
01104           if(return_on_first_hit)
01105           {
01106 
01107             bool last_ik_failed = false;
01108             for(unsigned int i = 0; i < grasps.size(); i++)
01109             {
01110 
01111               if(execution_info[i].result_.result_code != 0) continue;
01112 
01113               if(!last_ik_failed)
01114               {
01115                 //now we move to the ik portion, which requires re-enabling collisions for the arms
01116                 cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
01117 
01118                 //and also reducing link paddings
01119                 cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
01120 
01121               }
01122               //getting back to original state for seed
01123               state->setKinematicState(planning_scene_state_values);
01124 
01125               //adjusting planning scene state for pre-grasp
01126               std::map<std::string, double> pre_grasp_values;
01127               for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
01128               {
01129                 pre_grasp_values[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
01130               }
01131 
01132               state->setKinematicState(pre_grasp_values);
01133 
01134               // declaring geometry_msgs grasp poses relative to world coordinates
01135               geometry_msgs::Pose grasp_geom_pose; // grasp pose at pick location
01136               geometry_msgs::Pose graspTwistPose; // grasp pose after rotating wrist
01137               tf::poseTFToMsg(grasp_poses[i], grasp_geom_pose);
01138               tf::poseTFToMsg(grasp_poses[i] * twistTransform ,graspTwistPose);
01139 
01140           /*
01141            * ************************************************************************
01142            * Solving ik and interpolating trajectories
01143            * ************************************************************************
01144            */
01145 
01146               // solving ik for approach trajectory (pre-grasp to grasp pose)
01147               geometry_msgs::PoseStamped base_link_grasp_pose; // grasp pose relative to robot base
01148               cm->convertPoseGivenWorldTransform(*state,
01149                                                  ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
01150                                                  world_header,
01151                                                  grasp_geom_pose,
01152                                                  base_link_grasp_pose);
01153 
01154               arm_navigation_msgs::Constraints emp;
01155               sensor_msgs::JointState solution;
01156               arm_navigation_msgs::ArmNavigationErrorCodes error_code;
01157 
01158               ROS_INFO_STREAM("x y z " << base_link_grasp_pose.pose.position.x << " "
01159                               << base_link_grasp_pose.pose.position.y << " "
01160                               << base_link_grasp_pose.pose.position.z);
01161 
01162               if(!ik_solver_map_[pickup_goal.arm_name]->findConstraintAwareSolution(base_link_grasp_pose.pose,
01163                                                                                     emp,
01164                                                                                     state,
01165                                                                                     solution,
01166                                                                                     error_code,
01167                                                                                     false))
01168               {
01169 
01170                 ROS_DEBUG_STREAM("Grasp out of reach");
01171                 execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
01172                 outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
01173                 last_ik_failed = true;
01174                 continue;
01175 
01176               }
01177               else
01178               {
01179                 last_ik_failed = false;
01180               }
01181 
01182               std::map<std::string, double> ik_map_pre_grasp;
01183               std::map<std::string, double> ik_map_grasp;
01184               for(unsigned int j = 0; j < joint_names.size(); j++)
01185               {
01186                 ik_map_pre_grasp[joint_names[j]] = solution.position[j];
01187               }
01188 
01189               // updating grasp and pre-grasp maps
01190               ik_map_grasp = ik_map_pre_grasp;
01191               for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
01192               {
01193                 ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
01194               }
01195 
01196               for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
01197               {
01198                 ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
01199               }
01200               state->setKinematicState(ik_map_pre_grasp);
01201 
01202               //now we solve interpolated ik
01203               tf::Transform base_link_bullet_grasp_pose;
01204               tf::poseMsgToTF(base_link_grasp_pose.pose, base_link_bullet_grasp_pose);
01205               execution_info[i].approach_trajectory_.joint_names = joint_names;
01206 
01207               //now we need to do interpolated ik
01208               if(!getInterpolatedIK(pickup_goal.arm_name,
01209                                     base_link_bullet_grasp_pose,
01210                                     pregrasp_dir,
01211                                     grasps[i].desired_approach_distance,
01212                                     solution.position,
01213                                     true,
01214                                     false,
01215                                     execution_info[i].approach_trajectory_))
01216               {
01217                 ROS_DEBUG_STREAM("No interpolated IK for pre-grasp to grasp");
01218                 execution_info[i].result_.result_code = GraspResult::PREGRASP_UNFEASIBLE;
01219                 outcome_count[GraspResult::PREGRASP_UNFEASIBLE]++;
01220                 continue;
01221               }
01222 
01223               ROS_INFO_STREAM("Last approach point is " <<
01224                                execution_info[i].approach_trajectory_.points.back().positions[0] << " " <<
01225                                execution_info[i].approach_trajectory_.points.back().positions[1] << " " <<
01226                                execution_info[i].approach_trajectory_.points.back().positions[2] << " " <<
01227                                execution_info[i].approach_trajectory_.points.back().positions[3] << " " <<
01228                                execution_info[i].approach_trajectory_.points.back().positions[4] << " " <<
01229                                execution_info[i].approach_trajectory_.points.back().positions[5] << " " <<
01230                                execution_info[i].approach_trajectory_.points.back().positions[6]);
01231               ROS_INFO_STREAM("Total number of points for approach trajectory is "<<
01232                           execution_info[i].approach_trajectory_.points.size()) ;
01233 
01234               // end of approach trajectory
01235 
01236               // start of twist trajectory (will skip collision checks and other test)
01237               state->setKinematicState(ik_map_grasp); // this should set gripper at grasp (close) configuration
01238 
01239               // finding grasp twist pose relative to robot
01240               geometry_msgs::PoseStamped baseToTwistPose; // grasp pose relative to robot base
01241               cm->convertPoseGivenWorldTransform(*state,
01242                                                  ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
01243                                                  world_header,
01244                                                  graspTwistPose,
01245                                                  baseToTwistPose);
01246 
01247               // interpolating joint trajectory for twist move
01248               std::vector<double> &ikSolutionAtGrasp = solution.position;
01249               std::vector<double> ikSolutionAtTwist;
01250               execution_info[i].twist_trajectory_.joint_names = joint_names;
01251               ROS_INFO_STREAM("Finding ik solution for twist move");
01252               if(!getInterpolatedIK(
01253                           pickup_goal.arm_name,
01254                           base_link_bullet_grasp_pose,
01255                           twistTransform.getRotation(),
01256                           ikSolutionAtGrasp,ikSolutionAtTwist,execution_info[i].twist_trajectory_))
01257               {
01258                   ROS_INFO_STREAM("No ik solution found for twist move");
01259                   execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
01260                   outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
01261                   continue;
01262               }
01263               // end of twist trajectory
01264 
01265               //start of lift trajectory
01266               state->setKinematicState(ik_map_grasp);
01267               execution_info[i].lift_trajectory_.joint_names = joint_names;
01268               tf::Transform baseToTwistTransform;
01269               tf::poseMsgToTF(baseToTwistPose.pose, baseToTwistTransform);
01270 
01271               if(!getInterpolatedIK(pickup_goal.arm_name,
01272                           baseToTwistTransform,
01273                           lift_dir,
01274                           pickup_goal.lift.desired_distance,
01275                           ikSolutionAtTwist,
01276                           false,
01277                           true,
01278                           execution_info[i].lift_trajectory_))
01279               {
01280                 ROS_DEBUG_STREAM("No interpolated IK for grasp to lift");
01281                 execution_info[i].result_.result_code = GraspResult::LIFT_UNFEASIBLE;
01282                 outcome_count[GraspResult::LIFT_UNFEASIBLE]++;
01283                 continue;
01284               }
01285 
01286               ROS_DEBUG_STREAM("First lift point is " <<
01287                                execution_info[i].lift_trajectory_.points.front().positions[0] << " " <<
01288                                execution_info[i].lift_trajectory_.points.front().positions[1] << " " <<
01289                                execution_info[i].lift_trajectory_.points.front().positions[2] << " " <<
01290                                execution_info[i].lift_trajectory_.points.front().positions[3] << " " <<
01291                                execution_info[i].lift_trajectory_.points.front().positions[4] << " " <<
01292                                execution_info[i].lift_trajectory_.points.front().positions[5] << " " <<
01293                                execution_info[i].lift_trajectory_.points.front().positions[6]);
01294 
01295               // end of lift trajectory
01296 
01297               //now we revert link paddings and object collisions and do a final check for the initial ik points
01298               cm->revertCollisionSpacePaddingToDefault();
01299 
01300               //the start of the approach needs to be collision-free according to the default collision matrix
01301               cm->setAlteredAllowedCollisionMatrix(group_disable_acm);
01302 
01303               if(execution_info[i].approach_trajectory_.points.empty())
01304               {
01305                 ROS_WARN_STREAM("No result code and no points in approach trajectory");
01306                 continue;
01307               }
01308 
01309               std::map<std::string, double> pre_grasp_ik = ik_map_pre_grasp;
01310               for(unsigned int j = 0; j < joint_names.size(); j++)
01311               {
01312                 pre_grasp_ik[joint_names[j]] = execution_info[i].approach_trajectory_.points[0].positions[j];
01313               }
01314               state->setKinematicState(pre_grasp_ik);
01315               if(cm->isKinematicStateInCollision(*state))
01316               {
01317                 ROS_DEBUG_STREAM("Final pre-grasp check failed");
01318                 std::vector<arm_navigation_msgs::ContactInformation> contacts;
01319                 execution_info[i].result_.result_code = GraspResult::PREGRASP_OUT_OF_REACH;
01320                 outcome_count[GraspResult::PREGRASP_OUT_OF_REACH]++;
01321                 continue;
01322               }
01323 
01324               //the object will be attached to the gripper for the lift, so we don't care if the object collides with the hand
01325               cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
01326 
01327               if(execution_info[i].lift_trajectory_.points.empty())
01328               {
01329                 ROS_WARN_STREAM("No result code and no points in lift trajectory");
01330                 continue;
01331               }
01332 
01333               std::map<std::string, double> lift_ik = ik_map_pre_grasp;
01334               for(unsigned int j = 0; j < joint_names.size(); j++)
01335               {
01336                lift_ik[joint_names[j]] = execution_info[i].lift_trajectory_.points.back().positions[j];
01337               }
01338 
01339               state->setKinematicState(lift_ik);
01340               if(cm->isKinematicStateInCollision(*state))
01341               {
01342                 ROS_DEBUG_STREAM("Final lift check failed");
01343                 execution_info[i].result_.result_code = GraspResult::LIFT_OUT_OF_REACH;
01344                 outcome_count[GraspResult::LIFT_OUT_OF_REACH]++;
01345                 continue;
01346               }
01347               else
01348               {
01349                 ROS_INFO_STREAM("Everything successful");
01350                 execution_info[i].result_.result_code = GraspResult::SUCCESS;
01351                 execution_info.resize(i+1);
01352                 outcome_count[GraspResult::SUCCESS]++;
01353                 break;
01354               }
01355             }
01356 
01357             for(std::map<unsigned int, unsigned int>::iterator it = outcome_count.begin();
01358                 it != outcome_count.end();
01359                 it++)
01360             {
01361               ROS_INFO_STREAM("Outcome " << it->first << " count " << it->second);
01362             }
01363 
01364             cm->setAlteredAllowedCollisionMatrix(original_acm);
01365             ROS_INFO_STREAM("Took " << (ros::WallTime::now()-start).toSec());
01366             return;
01367 
01368           }
01369 
01370           //now we move to the ik portion, which requires re-enabling collisions for the arms
01371           cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
01372 
01373           //and also reducing link paddings
01374           cm->applyLinkPaddingToCollisionSpace(linkPaddingForGrasp(pickup_goal));
01375 
01376           for(unsigned int i = 0; i < grasps.size(); i++)
01377           {
01378 
01379             if(execution_info[i].result_.result_code != 0) continue;
01380 
01381             //getting back to original state for seed
01382             state->setKinematicState(planning_scene_state_values);
01383 
01384             //adjusting planning scene state for pre-grasp
01385             std::map<std::string, double> pre_grasp_values;
01386             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
01387             {
01388               pre_grasp_values[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
01389             }
01390 
01391             state->setKinematicState(pre_grasp_values);
01392 
01393             //now call ik for grasp
01394             geometry_msgs::Pose grasp_geom_pose;
01395             tf::poseTFToMsg(grasp_poses[i], grasp_geom_pose);
01396             geometry_msgs::PoseStamped base_link_grasp_pose;
01397             cm->convertPoseGivenWorldTransform(*state,
01398                                                ik_solver_map_[pickup_goal.arm_name]->getBaseName(),
01399                                                world_header,
01400                                                grasp_geom_pose,
01401                                                base_link_grasp_pose);
01402 
01403             arm_navigation_msgs::Constraints emp;
01404             sensor_msgs::JointState solution;
01405             arm_navigation_msgs::ArmNavigationErrorCodes error_code;
01406             if(!ik_solver_map_[pickup_goal.arm_name]->findConstraintAwareSolution(base_link_grasp_pose.pose,
01407                                                                                   emp,
01408                                                                                   state,
01409                                                                                   solution,
01410                                                                                   error_code,
01411                                                                                   false))
01412             {
01413               ROS_DEBUG_STREAM("Grasp out of reach");
01414               execution_info[i].result_.result_code = GraspResult::GRASP_OUT_OF_REACH;
01415               outcome_count[GraspResult::GRASP_OUT_OF_REACH]++;
01416               continue;
01417             }
01418 
01419             std::map<std::string, double> ik_map_pre_grasp;
01420             std::map<std::string, double> ik_map_grasp;
01421             for(unsigned int j = 0; j < joint_names.size(); j++)
01422             {
01423               ik_map_pre_grasp[joint_names[j]] = solution.position[j];
01424             }
01425             ik_map_grasp = ik_map_pre_grasp;
01426 
01427             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
01428             {
01429               ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
01430             }
01431 
01432             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
01433             {
01434               ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
01435             }
01436 
01437             state->setKinematicState(ik_map_pre_grasp);
01438 
01439             //now we solve interpolated ik
01440             tf::Transform base_link_bullet_grasp_pose;
01441             tf::poseMsgToTF(base_link_grasp_pose.pose, base_link_bullet_grasp_pose);
01442             //now we need to do interpolated ik
01443             execution_info[i].approach_trajectory_.joint_names = joint_names;
01444             if(!getInterpolatedIK(pickup_goal.arm_name,
01445                                   base_link_bullet_grasp_pose,
01446                                   pregrasp_dir,
01447                                   grasps[i].desired_approach_distance,
01448                                   solution.position,
01449                                   true,
01450                                   false,
01451                                   execution_info[i].approach_trajectory_))
01452             {
01453               ROS_DEBUG_STREAM("No interpolated IK for pre-grasp to grasp");
01454               execution_info[i].result_.result_code = GraspResult::PREGRASP_UNFEASIBLE;
01455               outcome_count[GraspResult::PREGRASP_UNFEASIBLE]++;
01456               continue;
01457             }
01458 
01459             state->setKinematicState(ik_map_grasp);
01460             execution_info[i].lift_trajectory_.joint_names = joint_names;
01461             if(!getInterpolatedIK(pickup_goal.arm_name,
01462                                   base_link_bullet_grasp_pose,
01463                                   lift_dir,
01464                                   pickup_goal.lift.desired_distance,
01465                                   solution.position,
01466                                   false,
01467                                   true,
01468                                   execution_info[i].lift_trajectory_))
01469             {
01470 
01471               ROS_DEBUG_STREAM("No interpolated IK for grasp to lift");
01472               execution_info[i].result_.result_code = GraspResult::LIFT_UNFEASIBLE;
01473               outcome_count[GraspResult::LIFT_UNFEASIBLE]++;
01474               continue;
01475 
01476             }
01477           }
01478 
01479           //now we revert link paddings and object collisions and do a final check for the initial ik points
01480           cm->revertCollisionSpacePaddingToDefault();
01481 
01482           cm->setAlteredAllowedCollisionMatrix(group_disable_acm);
01483 
01484           for(unsigned int i = 0; i < grasps.size(); i++)
01485           {
01486 
01487             if(execution_info[i].result_.result_code != 0) continue;
01488 
01489             if(execution_info[i].approach_trajectory_.points.empty()) {
01490               ROS_WARN_STREAM("No result code and no points in approach trajectory");
01491               continue;
01492             }
01493 
01494             std::map<std::string, double> ik_map_pre_grasp;
01495             for(unsigned int j = 0; j < joint_names.size(); j++)
01496             {
01497               ik_map_pre_grasp[joint_names[j]] = execution_info[i].approach_trajectory_.points[0].positions[j];
01498             }
01499 
01500             for(unsigned int j = 0; j < grasps[i].pre_grasp_posture.name.size(); j++)
01501             {
01502               ik_map_pre_grasp[grasps[i].pre_grasp_posture.name[j]] = grasps[i].pre_grasp_posture.position[j];
01503             }
01504 
01505             state->setKinematicState(ik_map_pre_grasp);
01506             if(cm->isKinematicStateInCollision(*state))
01507             {
01508               execution_info[i].result_.result_code = GraspResult::PREGRASP_OUT_OF_REACH;
01509               outcome_count[GraspResult::PREGRASP_OUT_OF_REACH]++;
01510               continue;
01511             }
01512 
01513           }
01514 
01515           //now we need to disable collisions with the object for lift
01516           cm->setAlteredAllowedCollisionMatrix(object_support_disable_acm);
01517 
01518           for(unsigned int i = 0; i < grasps.size(); i++)
01519           {
01520 
01521             if(execution_info[i].result_.result_code != 0) continue;
01522 
01523             if(execution_info[i].lift_trajectory_.points.empty())
01524             {
01525               ROS_WARN_STREAM("No result code and no points in lift trajectory");
01526               continue;
01527             }
01528 
01529             std::map<std::string, double> ik_map_grasp;
01530             for(unsigned int j = 0; j < joint_names.size(); j++)
01531             {
01532               ik_map_grasp[joint_names[j]] = execution_info[i].lift_trajectory_.points.back().positions[j];
01533             }
01534 
01535             for(unsigned int j = 0; j < grasps[i].grasp_posture.name.size(); j++)
01536             {
01537               ik_map_grasp[grasps[i].grasp_posture.name[j]] = grasps[i].grasp_posture.position[j];
01538             }
01539 
01540             state->setKinematicState(ik_map_grasp);
01541             if(cm->isKinematicStateInCollision(*state))
01542             {
01543               ROS_DEBUG_STREAM("Final lift check failed");
01544               execution_info[i].result_.result_code = GraspResult::LIFT_OUT_OF_REACH;
01545               outcome_count[GraspResult::LIFT_OUT_OF_REACH]++;
01546               continue;
01547             }
01548             else
01549             {
01550               ROS_DEBUG_STREAM("Everything successful");
01551               execution_info[i].result_.result_code = GraspResult::SUCCESS;
01552               outcome_count[GraspResult::SUCCESS]++;
01553             }
01554           }
01555           cm->setAlteredAllowedCollisionMatrix(original_acm);
01556 
01557           ROS_INFO_STREAM("Took " << (ros::WallTime::now()-start).toSec());
01558 
01559           for(std::map<unsigned int, unsigned int>::iterator it = outcome_count.begin();
01560               it != outcome_count.end();
01561               it++)
01562           {
01563             ROS_INFO_STREAM("Outcome " << it->first << " count " << it->second);
01564           }
01565 }


object_manipulation_tools
Author(s): Jnicho
autogenerated on Mon Oct 6 2014 00:56:17