cob_pick_place_action.cpp
Go to the documentation of this file.
00001 
00035 #include <ros/ros.h>
00036 #include <iostream>
00037 #include <fstream>
00038 #include <math.h>
00039 
00040 #include <cob_pick_place_action/cob_pick_place_action.h>
00041 #include "geometric_shapes/shapes.h"
00042 #include "geometric_shapes/shape_messages.h"
00043 #include "geometric_shapes/mesh_operations.h"
00044 #include "geometric_shapes/shape_operations.h"
00045 
00046 #include <algorithm>
00047 
00048 void CobPickPlaceActionServer::initialize()
00049 {
00050         pub_co = nh_.advertise<moveit_msgs::CollisionObject>("collision_object", 10);
00051         pub_ao = nh_.advertise<moveit_msgs::AttachedCollisionObject>("attached_collision_object", 10);
00052         
00053         //ToDo: Generate this mapping from GraspTable.txt?
00054         map_classid_to_classname[11]="sauerkraut";
00055         map_classid_to_classname[13]="fruittea";
00056         map_classid_to_classname[16]="orangemarmelade";
00057         map_classid_to_classname[18]="yellowsaltcube";
00058         map_classid_to_classname[27]="hotpot";
00059         map_classid_to_classname[30]="bluesaltcube";
00060         map_classid_to_classname[31]="yellowsaltcylinder";
00061         map_classid_to_classname[44]="knaeckebrot";
00062         map_classid_to_classname[46]="liviosunfloweroil";
00063         map_classid_to_classname[50]="instantsoup";
00064         map_classid_to_classname[52]="hotpot2";
00065         map_classid_to_classname[57]="mueslibars";
00066         map_classid_to_classname[65]="fruitdrink";
00067         map_classid_to_classname[99]="ruskwholemeal";
00068         map_classid_to_classname[101]="koalacandy";
00069         map_classid_to_classname[103]="instanttomatosoup";
00070         map_classid_to_classname[106]="bakingsoda";
00071         map_classid_to_classname[107]="sweetener";
00072         map_classid_to_classname[109]="chocoicing";
00073         map_classid_to_classname[119]="tomatoherbsauce";
00074         map_classid_to_classname[122]="peanuts";
00075         map_classid_to_classname[126]="herbsalt";
00076         map_classid_to_classname[128]="bathdetergent";
00077         
00078         
00079         static const std::string COB_PICKUP_ACTION_NAME = "cob_pick_action";
00080         as_pick.reset(new actionlib::SimpleActionServer<cob_pick_place_action::CobPickAction>(nh_, COB_PICKUP_ACTION_NAME, boost::bind(&CobPickPlaceActionServer::pick_goal_cb, this, _1), false));
00081         as_pick->start();
00082         
00083         static const std::string COB_PLACE_ACTION_NAME = "cob_place_action";
00084         as_place.reset(new actionlib::SimpleActionServer<cob_pick_place_action::CobPlaceAction>(nh_, COB_PLACE_ACTION_NAME, boost::bind(&CobPickPlaceActionServer::place_goal_cb, this, _1), false));
00085         as_place->start();
00086         
00087         last_grasp_valid = false;
00088         last_object_name.clear();
00089         
00090         //~ Grasp Table Initializations################################
00091         std::string path = ros::package::getPath("cob_pick_place_action")+std::string("/files/GraspTable.txt");
00092         GraspTableIniFile = const_cast<char*>(path.c_str());  
00093         m_GraspTable = new GraspTable();
00094         int error = m_GraspTable->Init(GraspTableIniFile);
00095         
00096         if(error<0)
00097                 ROS_ERROR("Failed to initialize GraspTables");
00098         
00099         
00100         static const std::string QUERY_GRASPS_OR_ACTION_NAME = "query_grasps";
00101         ac_grasps_or.reset(new actionlib::SimpleActionClient<cob_grasp_generation::QueryGraspsAction>(nh_, QUERY_GRASPS_OR_ACTION_NAME, true));
00102         ROS_INFO("Waiting for action server \"%s\" to start...", QUERY_GRASPS_OR_ACTION_NAME.c_str());
00103         ac_grasps_or->waitForServer(); //will wait for infinite time
00104         ROS_INFO("Action server \"%s\" started.", QUERY_GRASPS_OR_ACTION_NAME.c_str());
00105 }
00106 
00107 void CobPickPlaceActionServer::run()
00108 {
00109         ROS_INFO("cob_pick_action...spinning");
00110         ros::spin();
00111 }
00112 
00113 void CobPickPlaceActionServer::pick_goal_cb(const cob_pick_place_action::CobPickGoalConstPtr &goal)
00114 {
00115         ROS_INFO("PickGoalCB: Received new goal: Trying to pick %s", goal->object_name.c_str());
00116         bool success = false;
00117         
00118         ROS_DEBUG_STREAM(*(goal.get()));
00119 
00121         insertObject(goal->object_name, goal->object_class, goal->object_pose);
00122         
00124         std::vector<moveit_msgs::Grasp> grasps;
00125         if(goal->grasp_database=="KIT")
00126         {
00127         ROS_INFO("Using KIT grasp table");
00128                 if(goal->grasp_id!=0)
00129                 {
00130                         ROS_INFO("Using specific grasp_id: %d", goal->grasp_id);
00131                         fillSingleGraspKIT(goal->object_class, goal->grasp_id, goal->object_pose, grasps);
00132                 }
00133                 else
00134                 {
00135                         ROS_INFO("Using all grasps");
00136                         fillAllGraspsKIT(goal->object_class, goal->object_pose, grasps);
00137                 }
00138         }
00139         else if(goal->grasp_database=="OpenRAVE")
00140         {
00141         ROS_INFO("Using OpenRAVE grasp table");
00142                 fillGraspsOR(goal->object_class, goal->grasp_id, goal->object_pose, grasps);
00143         }
00144         else if(goal->grasp_database=="ALL")
00145         {
00146         ROS_INFO("Using all available databases");
00147                 std::vector<moveit_msgs::Grasp> grasps_OR, grasps_KIT;
00148                 fillAllGraspsKIT(goal->object_class, goal->object_pose, grasps_KIT);
00149                 fillGraspsOR(goal->object_class, goal->grasp_id, goal->object_pose, grasps_OR);
00150                 
00151                 grasps = grasps_KIT;
00152                 std::vector<moveit_msgs::Grasp>::iterator it = grasps.end();
00153                 grasps.insert(it, grasps_OR.begin(), grasps_OR.end());
00154         }
00155         else
00156                 ROS_ERROR("Grasp_Database %s not supported! Please use \"KIT\" or \"OpenRAVE\" instead", goal->grasp_database.c_str());
00157 
00158         
00159         ROS_INFO("PickGoalCB: Found %d grasps for this object", grasps.size());
00160         for(unsigned int i=0; i<grasps.size(); i++)
00161         {
00162                 ROS_DEBUG_STREAM("Grasp "<< i << ": " << grasps[i]);
00163         }
00164         
00165         if(!(goal->support_surface.empty()))
00166         {
00167                 ROS_INFO("Setting SupportSurface to %s", goal->support_surface.c_str());
00168                 group.setSupportSurfaceName(goal->support_surface);
00169         }
00170         group.setPlanningTime(300.0);   //default is 5.0 s
00171         
00172         
00174         success = group.pick(goal->object_name, grasps);
00175         
00177         cob_pick_place_action::CobPickResult result;
00178         std::string response;
00179         if(success)
00180         {
00181                 ROS_INFO("Pick was successfull!");
00182                 result.success.data=true;
00183                 response="Hurray!!! Pickup COMPLETE";
00184                 as_pick->setSucceeded(result, response);
00185                 last_grasp_valid = true;
00186                 last_object_name = goal->object_name;
00187         }
00188         else
00189         {
00190                 ROS_INFO("Pick failed!");
00191                 result.success.data=false;
00192                 response="Alas!!! Pickup FAILED";
00193                 as_pick->setAborted(result, response);
00194                 last_grasp_valid = false;
00195                 last_object_name.clear();
00196         }
00197 }
00198 
00199 void CobPickPlaceActionServer::place_goal_cb(const cob_pick_place_action::CobPlaceGoalConstPtr &goal)
00200 {
00201         ROS_INFO("PlaceCB: Received new goal: Trying to Place %s", goal->object_name.c_str());
00202         cob_pick_place_action::CobPlaceResult result;
00203         std::string response;
00204         bool success = false;
00205         
00206         if(!last_grasp_valid || last_object_name.empty() || last_object_name != goal->object_name)
00207         {
00208                 ROS_ERROR("Object %s cannot be placed", goal->object_name.c_str());
00209                 result.success.data = false;
00210                 response = "Object has not been picked before";
00211                 as_place->setAborted(result, response);
00212                 last_grasp_valid = false;
00213                 last_object_name.clear();
00214                 return; 
00215         }
00216         
00217         if(goal->destinations.empty())
00218         {
00219                 ROS_ERROR("Object %s cannot be placed", goal->object_name.c_str());
00220                 result.success.data = false;
00221                 response = "No destinations given";
00222                 as_place->setAborted(result, response);
00223                 last_grasp_valid = false;
00224                 last_object_name.clear();
00225                 return; 
00226         }
00227         
00228         std::vector<moveit_msgs::PlaceLocation> locations;
00229         trajectory_msgs::JointTrajectory pre_grasp_posture;             //use cyl_open by default
00230         pre_grasp_posture.header.stamp = ros::Time::now();
00231         pre_grasp_posture.joint_names.resize(7);
00232         pre_grasp_posture.joint_names[0] = "sdh_knuckle_joint";
00233         pre_grasp_posture.joint_names[1] = "sdh_finger_12_joint";
00234         pre_grasp_posture.joint_names[2] = "sdh_finger_13_joint";
00235         pre_grasp_posture.joint_names[3] = "sdh_finger_22_joint";
00236         pre_grasp_posture.joint_names[4] = "sdh_finger_23_joint";
00237         pre_grasp_posture.joint_names[5] = "sdh_thumb_2_joint";
00238         pre_grasp_posture.joint_names[6] = "sdh_thumb_3_joint";
00239         pre_grasp_posture.points.resize(7);
00240         
00241         trajectory_msgs::JointTrajectoryPoint point;
00242         point.positions.resize(1);
00243         point.velocities.push_back(0.0);
00244         point.accelerations.push_back(0.0);
00245         point.time_from_start = ros::Duration(3.0);
00246         
00247         point.positions[0] = 0.0;
00248         pre_grasp_posture.points.push_back(point);
00249         point.positions[0] = -0.9854;
00250         pre_grasp_posture.points.push_back(point);
00251         point.positions[0] = 0.9472;
00252         pre_grasp_posture.points.push_back(point);
00253         point.positions[0] = -0.9854;
00254         pre_grasp_posture.points.push_back(point);
00255         point.positions[0] = 0.9472;
00256         pre_grasp_posture.points.push_back(point);
00257         point.positions[0] = -0.9854;
00258         pre_grasp_posture.points.push_back(point);
00259         point.positions[0] = 0.9472;
00260         pre_grasp_posture.points.push_back(point);
00261         
00262         for(unsigned int i=0; i<goal->destinations.size(); i++)
00263         {
00264                 moveit_msgs::PlaceLocation place_location;
00265                 
00266                 place_location.id = "Last_"+goal->object_name+"_grasp";
00267                 place_location.post_place_posture = pre_grasp_posture;
00268                 place_location.place_pose = goal->destinations[i];
00269                 place_location.pre_place_approach.direction.header.frame_id = "/base_footprint";
00270                 place_location.pre_place_approach.direction.vector.z = -1.0;
00271                 place_location.pre_place_approach.min_distance = 0.1;
00272                 place_location.pre_place_approach.desired_distance = 0.15;
00273                 place_location.post_place_retreat.direction.header.frame_id = "/base_footprint";
00274                 place_location.post_place_retreat.direction.vector.z = 1.0;
00275                 place_location.post_place_retreat.min_distance = 0.1;
00276                 place_location.post_place_retreat.desired_distance = 0.15;
00277                 
00278                 locations.push_back(place_location);
00279         }
00280     
00281     if(!(goal->support_surface.empty()))
00282         {
00283                 ROS_INFO("Setting SupportSurface to %s", goal->support_surface.c_str());
00284                 group.setSupportSurfaceName(goal->support_surface);
00285         }
00286         group.setPlanningTime(300.0);   //default is 5.0 s
00287         
00288         success = group.place(goal->object_name, locations);
00289 
00291         if(success)
00292         {
00293                 result.success.data=true;
00294                 response="PLACE SUCCEEDED";
00295                 as_place->setSucceeded(result, response);
00296                 last_grasp_valid = false;
00297                 last_object_name.clear();
00298         }
00299         else
00300         {
00301                 result.success.data=false;
00302                 response="PLACE FAILED";
00303                 as_place->setAborted(result, response);
00304                 last_grasp_valid = false;
00305                 last_object_name.clear();
00306         }
00307 }
00308 
00309 void CobPickPlaceActionServer::insertObject(std::string object_name, unsigned int object_class, geometry_msgs::PoseStamped object_pose)
00310 {
00311         ROS_INFO("Adding object to MoveIt! environment..");
00312         
00313         moveit_msgs::CollisionObject co;
00314         co.header.stamp = object_pose.header.stamp;//ros::Time::now();
00315         co.header.frame_id = object_pose.header.frame_id;
00316         
00317         // remove object
00318         co.id = object_name;
00319         co.operation = co.REMOVE;
00320         pub_co.publish(co);
00321         
00322         // add object as Mesh
00323         co.id = object_name;
00324         co.operation = co.ADD;
00325         
00326         std::string mesh_name = map_classid_to_classname[object_class];
00327         std::transform(mesh_name.begin(), mesh_name.end(), mesh_name.begin(), ::tolower);
00328         
00329         boost::scoped_ptr<shapes::Mesh> mesh;
00330         mesh.reset(shapes::createMeshFromResource("package://cob_pick_place_action/files/meshes/"+mesh_name+".stl"));
00331         shapes::ShapeMsg shape_msg;
00332         shapes::constructMsgFromShape(mesh.get(), shape_msg);    
00333         co.meshes.push_back(boost::get<shape_msgs::Mesh>(shape_msg));
00334         co.mesh_poses.push_back(object_pose.pose);
00335         pub_co.publish(co);
00336         
00337         ros::Duration(1.0).sleep();
00338 }
00339 
00340 
00341 
00342 void CobPickPlaceActionServer::fillAllGraspsKIT(unsigned int objectClassId, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
00343 {  
00344         grasps.clear(); 
00345         Grasp *current_grasp = NULL;
00346         
00347         m_GraspTable->ResetReadPtr(objectClassId);
00348         unsigned int grasp_index = 0;
00349         
00350         current_grasp = m_GraspTable->GetNextGrasp(objectClassId);
00351         
00352         while(current_grasp)
00353         {
00354                 ROS_DEBUG("GraspIndex: %d",grasp_index);
00355                 
00356                 convertGraspKIT(current_grasp, object_pose, grasps);
00357                 
00358                 current_grasp = m_GraspTable->GetNextGrasp(objectClassId);
00359                 grasp_index++;
00360         }
00361 }
00362 
00363 void CobPickPlaceActionServer::fillSingleGraspKIT(unsigned int objectClassId, unsigned int grasp_id, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
00364 {
00365         grasps.clear(); 
00366         Grasp *current_grasp = NULL;
00367         
00368         current_grasp = m_GraspTable->GetGrasp(objectClassId, grasp_id);
00369         
00370         if(current_grasp)
00371         {
00372                 ROS_INFO("GraspIndex %d found",grasp_id);
00373                 
00374                 convertGraspKIT(current_grasp, object_pose, grasps);
00375         }
00376         else
00377                 ROS_ERROR("Grasp %d NOT found", grasp_id);
00378 }
00379 
00380 void CobPickPlaceActionServer::convertGraspKIT(Grasp* current_grasp, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
00381 {
00382     bool debug = true;
00383         moveit_msgs::Grasp g;
00384         
00385         //HandPreGraspConfig
00386         std::vector<double> current_hand_pre_config = current_grasp->GetHandPreGraspConfig();
00387         sensor_msgs::JointState pre_grasp_posture;
00388         pre_grasp_posture.position.clear();
00389         for (unsigned int i=0; i<current_hand_pre_config.size(); i++)
00390         {
00391                 pre_grasp_posture.position.push_back(current_hand_pre_config[i]);
00392         }
00393         g.pre_grasp_posture = MapHandConfiguration(pre_grasp_posture);
00394         
00395         //HandGraspConfig
00396         std::vector<double> current_hand_config = current_grasp->GetHandOptimalGraspConfig();
00397         sensor_msgs::JointState grasp_posture;
00398         grasp_posture.position.clear();
00399         for (unsigned int i=0; i<current_hand_config.size(); i++)
00400         {
00401                 grasp_posture.position.push_back(current_hand_config[i]);
00402         }
00403         g.grasp_posture= MapHandConfiguration(grasp_posture);
00404         
00405         //~~~ TCPGraspPose ~~~
00407         
00408         // O_from_SDH
00409         std::vector<double> current_grasp_pose = current_grasp->GetTCPGraspPose();
00410         tf::Transform transform_grasp_O_from_SDH = tf::Transform(
00411                 tf::createQuaternionFromRPY(current_grasp_pose[3], current_grasp_pose[4], current_grasp_pose[5] ),
00412                 0.001*tf::Vector3(current_grasp_pose[0],current_grasp_pose[1],current_grasp_pose[2]));
00413         //debug
00414     if (debug)
00415     {
00416         geometry_msgs::Transform msg_grasp_O_from_SDH;
00417         tf::transformTFToMsg(transform_grasp_O_from_SDH, msg_grasp_O_from_SDH);
00418         ROS_DEBUG_STREAM("msg_grasp_O_from_SDH:" << msg_grasp_O_from_SDH);
00419     }
00420         
00421         // HEADER_from_O (given)
00422         tf::Transform transform_HEADER_from_O = tf::Transform(
00423                 tf::Quaternion(object_pose.pose.orientation.x, object_pose.pose.orientation.y, object_pose.pose.orientation.z, object_pose.pose.orientation.w),
00424                 tf::Vector3(object_pose.pose.position.x, object_pose.pose.position.y, object_pose.pose.position.z));
00425         //debug
00426     if (debug)
00427     {
00428         geometry_msgs::Transform msg_HEADER_from_O;
00429         tf::transformTFToMsg(transform_HEADER_from_O, msg_HEADER_from_O);
00430         ROS_DEBUG_STREAM("msg_HEADER_from_O:" << msg_HEADER_from_O);
00431     }
00432         
00433         // FOOTPRINT_from_ARM7
00434         tf::Transform transform_grasp_FOOTPRINT_from_ARM7 = transformPose(transform_grasp_O_from_SDH, transform_HEADER_from_O, object_pose.header.frame_id);
00435         //debug
00436     if (debug)
00437     {
00438         geometry_msgs::Transform msg_grasp_FOOTPRINT_from_ARM7;
00439         tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_grasp_FOOTPRINT_from_ARM7);
00440         ROS_DEBUG_STREAM("msg_grasp_FOOTPRINT_from_ARM7:" << msg_grasp_FOOTPRINT_from_ARM7);
00441     }
00442         
00443         // convert to PoseStamped
00444         geometry_msgs::Transform msg_transform_grasp_FOOTPRINT_from_ARM7;
00445         tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_transform_grasp_FOOTPRINT_from_ARM7);
00446         geometry_msgs::PoseStamped msg_pose_grasp_FOOTPRINT_from_ARM7;
00447         msg_pose_grasp_FOOTPRINT_from_ARM7.header.stamp = ros::Time::now();
00448         msg_pose_grasp_FOOTPRINT_from_ARM7.header.frame_id = "/base_footprint";
00449         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.x = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.x;
00450         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.y = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.y;
00451         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.z = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.z;
00452         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.orientation = msg_transform_grasp_FOOTPRINT_from_ARM7.rotation;
00453     
00454     if (debug)
00455     {
00456         ROS_DEBUG_STREAM("msg_pose_grasp_FOOTPRINT_from_ARM7:" << msg_pose_grasp_FOOTPRINT_from_ARM7);
00457     }
00458         
00459         g.grasp_pose = msg_pose_grasp_FOOTPRINT_from_ARM7;
00460         
00461         
00462         //~~~ ApproachDirection ~~~
00463         // O_from_SDH
00464         std::vector<double> current_pre_grasp_pose = current_grasp->GetTCPPreGraspPose();
00465         tf::Transform transform_pre_O_from_SDH = tf::Transform(
00466                 tf::createQuaternionFromRPY(current_pre_grasp_pose[3], current_pre_grasp_pose[4], current_pre_grasp_pose[5] ),
00467                 0.001*tf::Vector3(current_pre_grasp_pose[0],current_pre_grasp_pose[1],current_pre_grasp_pose[2]));
00468         //debug
00469     if (debug)
00470     {
00471         geometry_msgs::Transform msg_pre_O_from_SDH;
00472         tf::transformTFToMsg(transform_pre_O_from_SDH, msg_pre_O_from_SDH);
00473         ROS_DEBUG_STREAM("msg_pre_O_from_SDH:" << msg_pre_O_from_SDH);
00474     }
00475         
00476         // FOOTPRINT_from_ARM7
00477         tf::Transform transform_pre_FOOTPRINT_from_ARM7 = transformPose(transform_pre_O_from_SDH, transform_HEADER_from_O, object_pose.header.frame_id);
00478         //debug
00479     if (debug)
00480     {
00481         geometry_msgs::Transform msg_pre_FOOTPRINT_from_ARM7;
00482         tf::transformTFToMsg(transform_pre_FOOTPRINT_from_ARM7, msg_pre_FOOTPRINT_from_ARM7);
00483         ROS_DEBUG_STREAM("msg_pre_FOOTPRINT_from_ARM7:" << msg_pre_FOOTPRINT_from_ARM7);
00484     }
00485         
00486         // convert to PoseStamped
00487         geometry_msgs::Transform msg_transform_pre_FOOTPRINT_from_ARM7;
00488         tf::transformTFToMsg(transform_pre_FOOTPRINT_from_ARM7, msg_transform_pre_FOOTPRINT_from_ARM7);
00489         geometry_msgs::PoseStamped msg_pose_pre_FOOTPRINT_from_ARM7;
00490         msg_pose_pre_FOOTPRINT_from_ARM7.header.stamp = ros::Time::now();
00491         msg_pose_pre_FOOTPRINT_from_ARM7.header.frame_id = "/base_footprint";
00492         msg_pose_pre_FOOTPRINT_from_ARM7.pose.position.x = msg_transform_pre_FOOTPRINT_from_ARM7.translation.x;
00493         msg_pose_pre_FOOTPRINT_from_ARM7.pose.position.y = msg_transform_pre_FOOTPRINT_from_ARM7.translation.y;
00494         msg_pose_pre_FOOTPRINT_from_ARM7.pose.position.z = msg_transform_pre_FOOTPRINT_from_ARM7.translation.z;
00495         msg_pose_pre_FOOTPRINT_from_ARM7.pose.orientation = msg_transform_pre_FOOTPRINT_from_ARM7.rotation;
00496     if (debug)
00497     {
00498         ROS_DEBUG_STREAM("msg_pose_pre_FOOTPRINT_from_ARM7:" << msg_pose_pre_FOOTPRINT_from_ARM7);
00499     }
00500         
00501         g.pre_grasp_approach = calculateApproachDirection(msg_pose_grasp_FOOTPRINT_from_ARM7.pose, msg_pose_pre_FOOTPRINT_from_ARM7.pose);
00502         
00503         //~~~ RetreatDirection ~~~
00504         g.post_grasp_retreat.direction.header.frame_id = g.pre_grasp_approach.direction.header.frame_id;
00505         g.post_grasp_retreat.direction.vector.x = -g.pre_grasp_approach.direction.vector.x;
00506         g.post_grasp_retreat.direction.vector.y = -g.pre_grasp_approach.direction.vector.y;
00507         g.post_grasp_retreat.direction.vector.z = -g.pre_grasp_approach.direction.vector.z + 0.5; //also lift the object a little bit
00508         
00509         g.post_grasp_retreat.min_distance = g.pre_grasp_approach.min_distance;
00510         g.post_grasp_retreat.desired_distance = g.pre_grasp_approach.desired_distance;
00511         
00512         g.post_place_retreat = g.post_grasp_retreat;
00513         
00514         grasps.push_back(g);
00515 }
00516 
00517 
00518 
00519 
00520 void CobPickPlaceActionServer::fillGraspsOR(unsigned int objectClassId, unsigned int grasp_id, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
00521 {
00522         bool finished_before_timeout;
00523         grasps.clear();
00524         
00525         //ToDo: resolve object_class_name from objectClassId
00526         if(map_classid_to_classname.find(objectClassId) == map_classid_to_classname.end())
00527         {
00528                 ROS_ERROR("Unable to resolve class_name for class_id %d", objectClassId);
00529                 return;
00530         }
00531         
00532         
00533         cob_grasp_generation::QueryGraspsGoal goal_query_grasps;
00534         goal_query_grasps.object_name = map_classid_to_classname.find(objectClassId)->second;
00535         goal_query_grasps.grasp_id = grasp_id;
00536         goal_query_grasps.num_grasps = 0;
00537         goal_query_grasps.threshold = 0;//0.012;
00538         
00539         ac_grasps_or->sendGoal(goal_query_grasps);
00540         
00541         ROS_INFO("Querying grasps...");
00542         finished_before_timeout = ac_grasps_or->waitForResult(ros::Duration(70.0));
00543         actionlib::SimpleClientGoalState state_grasps_or = ac_grasps_or->getState();
00544         boost::shared_ptr<const cob_grasp_generation::QueryGraspsResult> result_query_grasps = ac_grasps_or->getResult();
00545         if (finished_before_timeout)
00546         {
00547                 ROS_INFO("Action finished: %s",state_grasps_or.toString().c_str());
00548                 
00549                 ROS_INFO("Found %d grasps for this object", result_query_grasps.get()->grasp_list.size());
00550                 for(unsigned int i=0; i<result_query_grasps.get()->grasp_list.size(); i++)
00551                 {
00552                         ROS_DEBUG_STREAM("Grasp "<< i << ": " << result_query_grasps.get()->grasp_list[i]);
00553                 }
00554                 
00555                 for(unsigned int i=0; i<result_query_grasps.get()->grasp_list.size(); i++)
00556                 {
00557                         moveit_msgs::Grasp current_grasp;
00558                         //~~~ HandGraspConfig ~~~
00559                         current_grasp.grasp_posture = result_query_grasps.get()->grasp_list[i].grasp_posture;
00560                         for(unsigned int k=0; k<current_grasp.grasp_posture.points.size(); k++)
00561                         {
00562                                 if(current_grasp.grasp_posture.points[k].positions[0] < -1.5707) current_grasp.grasp_posture.points[k].positions[0] = -1.5707;
00563                                 if(current_grasp.grasp_posture.points[k].positions[0] >  1.5707) current_grasp.grasp_posture.points[k].positions[0] =  1.5707;
00564                         }
00565                         //~~~ HandPreGraspConfig ~~~
00566                         current_grasp.pre_grasp_posture = result_query_grasps.get()->grasp_list[i].pre_grasp_posture;
00567                         
00568                         //~~~ TCPGraspPose ~~~
00570                         
00571                         // O_from_SDH
00572                         geometry_msgs::Pose current_grasp_pose = result_query_grasps.get()->grasp_list[i].grasp_pose.pose;
00573                         tf::Transform transform_grasp_O_from_SDH = tf::Transform(
00574                                 tf::Quaternion(current_grasp_pose.orientation.x, current_grasp_pose.orientation.y, current_grasp_pose.orientation.z, current_grasp_pose.orientation.w),
00575                                 tf::Vector3(current_grasp_pose.position.x, current_grasp_pose.position.y, current_grasp_pose.position.z));
00576                         
00577                         //debug
00578                         geometry_msgs::Transform msg_grasp_O_from_SDH;
00579                         tf::transformTFToMsg(transform_grasp_O_from_SDH, msg_grasp_O_from_SDH);
00580                         ROS_DEBUG_STREAM("msg_grasp_O_from_SDH:" << msg_grasp_O_from_SDH);
00581                         
00582                         // HEADER_from_O (given)
00583                         tf::Transform transform_HEADER_from_O = tf::Transform(
00584                                 tf::Quaternion(object_pose.pose.orientation.x, object_pose.pose.orientation.y, object_pose.pose.orientation.z, object_pose.pose.orientation.w),
00585                                 tf::Vector3(object_pose.pose.position.x, object_pose.pose.position.y, object_pose.pose.position.z));
00586                         //debug
00587                         geometry_msgs::Transform msg_HEADER_from_O;
00588                         tf::transformTFToMsg(transform_HEADER_from_O, msg_HEADER_from_O);
00589                         ROS_DEBUG_STREAM("msg_HEADER_from_O:" << msg_HEADER_from_O);
00590                         
00591                         // FOOTPRINT_from_ARM7
00592                         tf::Transform transform_grasp_FOOTPRINT_from_ARM7 = transformPose(transform_grasp_O_from_SDH, transform_HEADER_from_O, object_pose.header.frame_id);
00593                         //debug
00594                         geometry_msgs::Transform msg_grasp_FOOTPRINT_from_ARM7;
00595                         tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_grasp_FOOTPRINT_from_ARM7);
00596                         ROS_DEBUG_STREAM("msg_grasp_FOOTPRINT_from_ARM7:" << msg_grasp_FOOTPRINT_from_ARM7);
00597                         
00598                         // convert to PoseStamped
00599                         geometry_msgs::Transform msg_transform_grasp_FOOTPRINT_from_ARM7;
00600                         tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_transform_grasp_FOOTPRINT_from_ARM7);
00601                         geometry_msgs::PoseStamped msg_pose_grasp_FOOTPRINT_from_ARM7;
00602                         msg_pose_grasp_FOOTPRINT_from_ARM7.header.stamp = ros::Time::now();
00603                         msg_pose_grasp_FOOTPRINT_from_ARM7.header.frame_id = "/base_footprint";
00604                         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.x = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.x;
00605                         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.y = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.y;
00606                         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.z = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.z;
00607                         msg_pose_grasp_FOOTPRINT_from_ARM7.pose.orientation = msg_transform_grasp_FOOTPRINT_from_ARM7.rotation;
00608                         ROS_DEBUG_STREAM("msg_pose_grasp_FOOTPRINT_from_ARM7:" << msg_pose_grasp_FOOTPRINT_from_ARM7);
00609                         
00610                         current_grasp.grasp_pose = msg_pose_grasp_FOOTPRINT_from_ARM7;
00611                         
00612                         //~~~ ApproachDirection ~~~
00613                         //current_grasp.pre_grasp_approach.direction.header.frame_id = "/base_footprint";
00614                         //current_grasp.pre_grasp_approach.direction.vector.x = 0.0;
00615                         //current_grasp.pre_grasp_approach.direction.vector.y = 0.0;
00616                         //current_grasp.pre_grasp_approach.direction.vector.z = -1.0;
00617                         //current_grasp.pre_grasp_approach.min_distance = 0.18;
00618                         //current_grasp.pre_grasp_approach.desired_distance = 0.28;
00619                         
00620                         current_grasp.pre_grasp_approach.direction.header.frame_id = "/arm_7_link";
00621                         current_grasp.pre_grasp_approach.direction.vector.x = 0.0;
00622                         current_grasp.pre_grasp_approach.direction.vector.y = 0.0;
00623                         current_grasp.pre_grasp_approach.direction.vector.z = 1.0;
00624                         current_grasp.pre_grasp_approach.min_distance = 0.18;
00625                         current_grasp.pre_grasp_approach.desired_distance = 0.28;
00626                         
00627                         //current_grasp.pre_grasp_approach.direction.header.frame_id = "/base_footprint";
00628                         //current_grasp.pre_grasp_approach.direction.vector.x = -msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.x+object_pose.pose.position.x;
00629                         //current_grasp.pre_grasp_approach.direction.vector.y = -msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.y+object_pose.pose.position.y;
00630                         //current_grasp.pre_grasp_approach.direction.vector.z = -msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.z+object_pose.pose.position.z;
00631                         //current_grasp.pre_grasp_approach.min_distance = 0.18;
00632                         //current_grasp.pre_grasp_approach.desired_distance = 0.28;
00633                         
00634                         //~~~ RetreatDirection ~~~
00635                         current_grasp.post_grasp_retreat.direction.header.frame_id = "/base_footprint";
00636                         current_grasp.post_grasp_retreat.direction.vector.x = 0.0;
00637                         current_grasp.post_grasp_retreat.direction.vector.y = 0.0;
00638                         current_grasp.post_grasp_retreat.direction.vector.z = 1.0;
00639                         current_grasp.post_grasp_retreat.min_distance = 0.05;
00640                         current_grasp.post_grasp_retreat.desired_distance = 0.1;
00641                         
00642                         // current_grasp.post_grasp_retreat.direction.header.frame_id = "/arm_7_link";
00643                         // current_grasp.post_grasp_retreat.direction.vector.x = 0.0;
00644                         // current_grasp.post_grasp_retreat.direction.vector.y = 0.0;
00645                         // current_grasp.post_grasp_retreat.direction.vector.z = -1.0;
00646                         // current_grasp.post_grasp_retreat.min_distance = 0.1;
00647                         // current_grasp.post_grasp_retreat.desired_distance = 0.15;
00648                         
00649                         current_grasp.post_place_retreat = current_grasp.post_grasp_retreat;
00650                         
00651                         grasps.push_back(current_grasp);
00652                 }
00653                 
00654         }
00655         else
00656                 ROS_ERROR("Grasps not queried within timeout");
00657 }
00658 
00659 
00660 
00661 
00662 
00663 
00664 trajectory_msgs::JointTrajectory CobPickPlaceActionServer::MapHandConfiguration(sensor_msgs::JointState table_config)
00665 {
00666         trajectory_msgs::JointTrajectory grasp_configuration;
00667         grasp_configuration.points.resize(7);
00668         
00669         trajectory_msgs::JointTrajectoryPoint point;
00670         point.positions.resize(1);
00671         point.velocities.push_back(0.0);
00672         point.accelerations.push_back(0.0);
00673         point.time_from_start = ros::Duration(3.0);
00674         
00675         point.positions[0] = table_config.position[0];
00676         grasp_configuration.points.push_back(point);
00677         point.positions[0] = table_config.position[2];
00678         grasp_configuration.points.push_back(point);
00679         point.positions[0] = table_config.position[3];
00680         grasp_configuration.points.push_back(point);
00681         point.positions[0] = table_config.position[5];
00682         grasp_configuration.points.push_back(point);
00683         point.positions[0] = table_config.position[6];
00684         grasp_configuration.points.push_back(point);
00685         point.positions[0] = table_config.position[11];
00686         grasp_configuration.points.push_back(point);
00687         point.positions[0] = table_config.position[12];
00688         grasp_configuration.points.push_back(point);
00689         
00690         grasp_configuration.joint_names.resize(7);
00691         grasp_configuration.joint_names[0]=("sdh_knuckle_joint");
00692         grasp_configuration.joint_names[1]=("sdh_thumb_2_joint");
00693         grasp_configuration.joint_names[2]=("sdh_thumb_3_joint");
00694         grasp_configuration.joint_names[3]=("sdh_finger_12_joint");
00695         grasp_configuration.joint_names[4]=("sdh_finger_13_joint");
00696         grasp_configuration.joint_names[5]=("sdh_finger_22_joint");
00697         grasp_configuration.joint_names[6]=("sdh_finger_23_joint");    
00698 
00699         //cut joint_values according to joint_limits
00700         for(unsigned int i=0; i<grasp_configuration.points.size();i++)
00701         {
00702                 if(grasp_configuration.points[i].positions[0]>1.57079){grasp_configuration.points[i].positions[0]=1.57079;}
00703                 if(grasp_configuration.points[i].positions[0]<-1.57079){grasp_configuration.points[i].positions[0]=-1.57079;}
00704         }
00705         return grasp_configuration;
00706 }
00707 
00708 tf::Transform CobPickPlaceActionServer::transformPose(tf::Transform transform_O_from_SDH, tf::Transform transform_HEADER_from_O, std::string object_frame_id)
00709 {
00710     bool debug = false;
00711         // SDH_from_ARM7
00712         tf::StampedTransform transform_SDH_from_ARM7;
00713         try
00714         {
00715                 ros::Time now = ros::Time::now();
00716                 tf_listener_.waitForTransform("/sdh_palm_link", "/arm_7_link", now, ros::Duration(10.0));
00717                 tf_listener_.lookupTransform("/sdh_palm_link", "/arm_7_link", now, transform_SDH_from_ARM7);
00718         }
00719         catch (tf::TransformException ex)
00720         {
00721                 ROS_ERROR("%s",ex.what());
00722         }
00723         //debug
00724         geometry_msgs::TransformStamped msg_SDH_from_ARM7;
00725         tf::transformStampedTFToMsg(transform_SDH_from_ARM7, msg_SDH_from_ARM7);
00726     
00727     if (debug)
00728         ROS_DEBUG_STREAM("msg_SDH_from_ARM7:" << msg_SDH_from_ARM7);
00729         
00730         // O_from_ARM7 = O_from_SDH * SDH_from_ARM7
00731         tf::Transform transform_O_from_ARM7 = transform_O_from_SDH * transform_SDH_from_ARM7;
00732         
00733         // FOOTPRINT_from_HEADER
00734         tf::StampedTransform transform_FOOTPRINT_from_HEADER;
00735         try
00736         {
00737                 ros::Time now = ros::Time::now();
00738                 tf_listener_.waitForTransform("/base_footprint", object_frame_id, now, ros::Duration(10.0));
00739                 tf_listener_.lookupTransform("/base_footprint", object_frame_id, now, transform_FOOTPRINT_from_HEADER);
00740         }
00741         catch (tf::TransformException ex)
00742         {
00743                 ROS_ERROR("%s",ex.what());
00744         }
00745         //debug
00746         geometry_msgs::TransformStamped msg_FOOTPRINT_from_HEADER;
00747         tf::transformStampedTFToMsg(transform_FOOTPRINT_from_HEADER, msg_FOOTPRINT_from_HEADER);
00748     
00749     if (debug)
00750         ROS_DEBUG_STREAM("msg_FOOTPRINT_from_HEADER:" << msg_FOOTPRINT_from_HEADER);
00751         
00752         // FOOTPRINT_from_O = FOOTPRINT_from_HEADER * HEADER_from_O
00753         tf::Transform transform_FOOTPRINT_from_O = transform_FOOTPRINT_from_HEADER * transform_HEADER_from_O;
00754         
00755         // FOOTPRINT_from_ARM7 = FOOTPRINT_from_O * O_from_ARM7
00756         tf::Transform transform_FOOTPRINT_from_ARM7 = transform_FOOTPRINT_from_O * transform_O_from_ARM7;
00757         
00758         return transform_FOOTPRINT_from_ARM7;
00759 }
00760 
00761 moveit_msgs::GripperTranslation CobPickPlaceActionServer::calculateApproachDirection(geometry_msgs::Pose msg_pose_grasp_FOOTPRINT_from_ARM7, geometry_msgs::Pose msg_pose_pre_FOOTPRINT_from_ARM7)
00762 {
00763         double finger_length = 0.18; //this is the lenght of the sdh ('home' configuration)
00764         moveit_msgs::GripperTranslation approach;
00765         approach.direction.header.frame_id = "/base_footprint";
00766         //~ dis=sqrt((x1-x0)^2+(y1-y0)^2+(z1-z0)^2)
00767         //~ direction.x= (x1-x0)/dis and likewise
00768         
00769         double distance = sqrt(
00770                 pow((msg_pose_grasp_FOOTPRINT_from_ARM7.position.x-msg_pose_pre_FOOTPRINT_from_ARM7.position.x),2) +
00771                 pow((msg_pose_grasp_FOOTPRINT_from_ARM7.position.y-msg_pose_pre_FOOTPRINT_from_ARM7.position.y),2) +
00772                 pow((msg_pose_grasp_FOOTPRINT_from_ARM7.position.z-msg_pose_pre_FOOTPRINT_from_ARM7.position.z),2));
00773         
00774         ROS_DEBUG("Distance between pre-grasp and grasp: %f", distance);
00775         
00776         approach.direction.vector.x = (msg_pose_grasp_FOOTPRINT_from_ARM7.position.x-msg_pose_pre_FOOTPRINT_from_ARM7.position.x)/distance;
00777         approach.direction.vector.y = (msg_pose_grasp_FOOTPRINT_from_ARM7.position.y-msg_pose_pre_FOOTPRINT_from_ARM7.position.y)/distance;
00778         approach.direction.vector.z = (msg_pose_grasp_FOOTPRINT_from_ARM7.position.z-msg_pose_pre_FOOTPRINT_from_ARM7.position.z)/distance;
00779         
00780         //min_distance means that we want to follow at least this length along the apporach_vector
00781         if(distance < finger_length)
00782         {
00783                 approach.min_distance = finger_length;
00784         }
00785         else
00786         {
00787                 approach.min_distance = distance;
00788         }
00789         approach.desired_distance = approach.min_distance + 0.1;
00790         
00791         return approach;
00792 }
00793 
00794 
00795 
00796 
00797 
00798 
00799 
00800 
00801 
00802 
00803 
00804 
00805 
00806 
00807 
00808 int main(int argc, char **argv)
00809 {
00810         ros::init (argc, argv, "cob_pick_place_action_server_node");
00811         CobPickPlaceActionServer *cob_pick_place_action_server = new CobPickPlaceActionServer();
00812         
00813         cob_pick_place_action_server->initialize();
00814         cob_pick_place_action_server->run();
00815         
00816         return 0;
00817 }


cob_pick_place_action
Author(s): Felix Messmer
autogenerated on Wed Aug 26 2015 11:01:29