Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 import roslib
00059 roslib.load_manifest('srs_grasping')
00060 import rospy
00061 import time
00062 import grasping_functions
00063
00064 from srs_grasping.srv import *
00065 from srs_msgs.msg import *
00066 from tf.transformations import *
00067 from geometry_msgs.msg import *
00068
00069
00070 class get_pregrasps():
00071
00072 def __init__(self):
00073 self.ik_loop_reply = 1
00074
00075 rospy.loginfo("Waiting /get_db_grasps service...");
00076 rospy.wait_for_service('/get_db_grasps')
00077 self.client = rospy.ServiceProxy('/get_db_grasps', GetDBGrasps)
00078 rospy.loginfo("/get_pregrasps service is ready.");
00079 print "---------------------------------------------------------------------------";
00080
00081 def get_pregrasps(self, req):
00082 x = time.time();
00083 rospy.loginfo("/get_pregrasps service has been called...");
00084
00085 obj_id = req.object_id;
00086 obj_pose = req.object_pose;
00087 pregrasp_offsets = req.pregrasp_offsets;
00088 num_configurations = (1, req.num_configurations)[req.num_configurations>0];
00089
00090
00091 req = GetDBGraspsRequest();
00092 req.object_id = obj_id;
00093 grasp_configuration = (self.client(req)).grasp_configuration;
00094
00095
00096 rotacion = grasping_functions.graspingutils.rotation_matrix(obj_pose);
00097
00098 resp = GetPreGraspResponse();
00099 resp.side = []
00100 resp.mside = []
00101 resp.top = []
00102 resp.front = []
00103
00104 for i in range(0,len(grasp_configuration)):
00105 pre_trans = rotacion * grasping_functions.graspingutils.matrix_from_pose(grasp_configuration[i].pre_grasp.pose);
00106 grasp_trans = rotacion * grasping_functions.graspingutils.matrix_from_pose(grasp_configuration[i].grasp.pose);
00107
00108 t = translation_from_matrix(pre_trans);
00109 q = quaternion_from_matrix(pre_trans);
00110 tg = translation_from_matrix(grasp_trans);
00111 qg = quaternion_from_matrix(grasp_trans);
00112
00113 pre = Pose();
00114 pre.position.x = t[0];
00115 pre.position.y = t[1];
00116 pre.position.z = t[2];
00117 pre.orientation.x = q[0];
00118 pre.orientation.y = q[1];
00119 pre.orientation.z = q[2];
00120 pre.orientation.w = q[3];
00121
00122 g = Pose();
00123 g.position.x = tg[0];
00124 g.position.y = tg[1];
00125 g.position.z = tg[2];
00126 g.orientation.x = qg[0];
00127 g.orientation.y = qg[1];
00128 g.orientation.z = qg[2];
00129 g.orientation.w = qg[3];
00130
00131
00132 category = grasping_functions.graspingutils.get_grasp_category(pre.position, g.position);
00133 pre = grasping_functions.graspingutils.set_pregrasp_offsets(category, pre, pregrasp_offsets);
00134
00135
00136 aux = DBGrasp();
00137 aux.object_id = obj_id;
00138 aux.hand_type = "SDH";
00139 aux.sdh_joint_values = grasp_configuration[i].sdh_joint_values;
00140 aux.pre_grasp.pose = pre;
00141 aux.grasp.pose = g;
00142 aux.category = grasping_functions.graspingutils.get_grasp_category(pre.position, g.position);
00143
00144 if aux.category == "TOP":
00145 if len(resp.top) < num_configurations:
00146 resp.top.append(aux);
00147 elif aux.category == "FRONT":
00148 if len(resp.front) < num_configurations:
00149 resp.front.append(aux);
00150 elif aux.category == "SIDE":
00151 if len(resp.side) < num_configurations:
00152 resp.side.append(aux);
00153 elif aux.category == "-SIDE":
00154 if len(resp.mside) < num_configurations:
00155 resp.mside.append(aux);
00156 else:
00157 continue
00158
00159 if len(resp.top)==num_configurations and len(resp.side)==num_configurations and len(resp.mside)==num_configurations and len(resp.front)==num_configurations:
00160 break;
00161
00162 rospy.loginfo("/get_pregrasps call has finished.");
00163 print "Time employed: " + str(time.time() - x);
00164 print "---------------------------------------";
00165 return resp;
00166
00167
00168 def get_pregrasps_server(self):
00169 s = rospy.Service('/get_pregrasps', GetPreGrasp, self.get_pregrasps);
00170
00171
00172
00173 if __name__ == '__main__':
00174 rospy.init_node('get_pregrasps');
00175 SCRIPT = get_pregrasps();
00176 SCRIPT.get_pregrasps_server();
00177 rospy.spin();