00001
00002 import roslib
00003 roslib.load_manifest('cob_component_test')
00004
00005 import sys
00006 import time
00007 import unittest
00008 import rospy
00009 import rostest
00010 import actionlib
00011 import tf
00012
00013 from simple_script_server import *
00014 from gazebo.msg import *
00015 from geometry_msgs.msg import *
00016
00017 from math import *
00018
00019 class UnitTest(unittest.TestCase):
00020 def __init__(self, *args):
00021 super(UnitTest, self).__init__(*args)
00022 rospy.init_node('grasp_test')
00023 self.message_received = False
00024 self.sss=simple_script_server()
00025
00026 def setUp(self):
00027 self.errors = []
00028
00029 def test_grasp(self):
00030
00031
00032 try:
00033
00034 if not rospy.has_param('~test_duration'):
00035 self.fail('Parameter test_duration does not exist on ROS Parameter Server')
00036 test_duration = rospy.get_param('~test_duration')
00037
00038
00039 if not rospy.has_param('~grasp_object'):
00040 self.fail('Parameter grasp_object does not exist on ROS Parameter Server')
00041 grasp_object = rospy.get_param('~grasp_object')
00042
00043 except KeyError, e:
00044 self.fail('Parameters not set properly')
00045
00046 print """
00047 Test duration: %s
00048 Object to grasp: %s"""%(test_duration, grasp_object)
00049
00050
00051 sub_model_states = rospy.Subscriber("/gazebo/model_states", ModelStates, self.cb_model_states)
00052 sub_link_states = rospy.Subscriber("/gazebo/link_states", LinkStates, self.cb_link_states)
00053
00054
00055 self.listener = tf.TransformListener(True, rospy.Duration(10.0))
00056
00057
00058 self.sss.sleep(1)
00059 if grasp_object not in self.model_states.name:
00060 self.fail(grasp_object + " not spawned correctly")
00061
00062
00063 components = ['arm', 'tray', 'sdh', 'torso', 'base']
00064 for comp in components:
00065 init_comp = self.sss.init(comp)
00066 if init_comp.get_error_code() != 0:
00067 error_msg = self.get_error_msg(init_comp)
00068 self.fail(error_msg)
00069
00070
00071 handle_base = self.sss.move('base', 'kitchen', False)
00072 handle_tray = self.sss.move('tray', 'down', False)
00073 handle_arm = self.sss.move('arm', 'pregrasp', False)
00074 handle_sdh = self.sss.move('sdh', 'cylopen', False)
00075 handle_torso = self.sss.move('torso', 'home', False)
00076 handle_base.wait()
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 obj_index = self.model_states.name.index(grasp_object)
00093
00094
00095 self.arm_7_link_index = self.link_states.name.index("arm_7_link")
00096
00097
00098 grasp_obj = self.trans_into_arm_7_link(self.model_states.pose[obj_index].position)
00099
00100
00101
00102 grasp_obj.point.z = grasp_obj.point.z - 0.17
00103
00104
00105
00106 handle_arm1 = self.sss.move_cart_rel("arm", [[grasp_obj.point.x + 0.05, grasp_obj.point.y, grasp_obj.point.z - 0.2], [0.0, 0.0, 0.0]])
00107
00108 handle_arm2 = self.sss.move_cart_rel("arm", [[0.0, 0.0, 0.2], [0.0, 0.0, 0.0]])
00109
00110 handle_sdh = self.sss.move("sdh", "cylclosed")
00111
00112 handle_arm3 = self.sss.move_cart_rel("arm", [[0.2, -0.1, -0.3], [0.0, 0.0, 0.0]])
00113
00114
00115 if not self.at_des_pos(self.link_states.pose[self.arm_7_link_index].position, self.model_states.pose[obj_index].position, 0.5, "sdh in kitchen"):
00116 err_msgs = self.gen_error_msgs("sdh in kitchen", handle_arm1, handle_arm2, handle_sdh, handle_arm3)
00117 self.fail(err_msgs)
00118
00119
00120 handle_arm = self.sss.move("arm", "grasp-to-tray", False)
00121
00122 handle_tray = self.sss.move("tray", "up")
00123 handle_arm.wait()
00124
00125
00126 if not self.at_des_pos(self.link_states.pose[self.arm_7_link_index].position, self.model_states.pose[obj_index].position, 0.5, "sdh over tray"):
00127 err_msgs = self.gen_error_msgs("sdh over tray", handle_arm, handle_tray)
00128 self.fail(err_msgs)
00129
00130
00131
00132 dist_to_tray = self.model_states.pose[obj_index].position.z - 0.84 - 0.1 - 0.03
00133 handle_arm = self.sss.move_cart_rel("arm", [[-dist_to_tray, 0.0, 0.0], [0, 0, 0]])
00134 handle_sdh = self.sss.move("sdh", "cylopen")
00135
00136
00137 handle_base = self.sss.move('base', [0, -0.5, 0])
00138
00139
00140 des_pos_world = Point()
00141 des_pos_world.x = 0.3
00142 des_pos_world.y = -0.4
00143 des_pos_world.z = 0.84 + 0.1
00144 if not self.at_des_pos(des_pos_world, self.model_states.pose[obj_index].position, 0.5, "tray"):
00145 err_msgs = self.gen_error_msgs("tray", handle_arm, handle_sdh, handle_base)
00146 self.fail(err_msgs)
00147
00148
00149
00150 grasp_obj = self.trans_into_arm_7_link(self.model_states.pose[obj_index].position)
00151
00152 grasp_obj.point.z = grasp_obj.point.z - 0.17
00153 handle_arm = self.sss.move_cart_rel("arm", [[grasp_obj.point.x, grasp_obj.point.y, grasp_obj.point.z], [0, 0, 0]])
00154 handle_sdh = self.sss.move("sdh", "cylclosed")
00155
00156
00157 if not self.at_des_pos(self.link_states.pose[self.arm_7_link_index].position, self.model_states.pose[obj_index].position, 0.5, "sdh over tray"):
00158 err_msgs = self.gen_error_msgs("sdh over tray", handle_arm, handle_sdh)
00159 self.fail(err_msgs)
00160
00161
00162
00163 handle_arm1 = self.sss.move("arm", "overtray")
00164 handle_arm2 = self.sss.move_cart_rel("arm", [[0.0, 0.0, -0.2], [0, 0, 0]])
00165 handle_arm3 = self.sss.move("arm", [[1.5620375327333056, -0.59331108071630467, -2.9678321245253576, -0.96655272071376164, 1.2160753390569674, 1.4414846837499029, -2.2174714029417704]])
00166 des_pos_world.x = 0.0
00167 des_pos_world.y = -1.2
00168 des_pos_world.z = 0.56 + 0.1
00169 des_pos_arm_7_link = self.trans_into_arm_7_link(des_pos_world)
00170
00171 x_dist_obj_arm_7_link = self.trans_into_arm_7_link(self.model_states.pose[obj_index].position).point.x
00172 handle_arm4 = self.sss.move_cart_rel("arm", [[(des_pos_arm_7_link.point.x - x_dist_obj_arm_7_link + 0.03), des_pos_arm_7_link.point.y, des_pos_arm_7_link.point.z], [0.0, 0.0, 0.0]])
00173 handle_sdh = self.sss.move("sdh", "cylopen")
00174
00175
00176 des_pos_world.z = 0.55
00177 if not self.at_des_pos(des_pos_world, self.model_states.pose[obj_index].position, 0.3, "table"):
00178 err_msgs = self.gen_error_msgs("table", handle_arm1, handle_arm2, handle_arm3, handle_sdh, handle_arm4)
00179 self.fail(err_msgs)
00180
00181 self.sss.move_cart_rel("arm", [[0.5, 0.0, 0.0], [0.0, 0.0, 0.0]])
00182 self.sss.move("arm", "folded")
00183
00184
00185
00186 def cb_model_states(self, msg):
00187 self.model_states = msg
00188
00189 def cb_link_states(self, msg):
00190 self.link_states = msg
00191
00192 def at_des_pos(self, des_pos, act_pos, tolerance, pos_name):
00193
00194 distance = self.calc_dist(des_pos, act_pos)
00195 print >> sys.stdout, "Distance to '", pos_name, "': ", distance
00196 if distance >= tolerance:
00197 return False
00198 else:
00199 return True
00200
00201 def calc_dist(self, des_pos, act_pos):
00202
00203 distance = sqrt((des_pos.x - act_pos.x)**2 + (des_pos.y - act_pos.y)**2 + (des_pos.z - act_pos.z)**2)
00204 return distance
00205
00206
00207 def trans_into_arm_7_link(self, coord_world):
00208
00209 coord_arm_7_link = PointStamped()
00210 coord_arm_7_link.header.stamp = rospy.Time.now()
00211 coord_arm_7_link.header.frame_id = "/map"
00212 coord_arm_7_link.point = coord_world
00213 self.sss.sleep(2)
00214
00215 if not self.sss.parse:
00216 coord_arm_7_link = self.listener.transformPoint('/arm_7_link', coord_arm_7_link)
00217 return coord_arm_7_link
00218
00219 def gen_error_msgs(self, des_pos, *handles):
00220
00221 error_msgs = "Object not at desired position: " + des_pos
00222 for handle in handles:
00223 if self.get_error_msg(handle) != "worked":
00224 error_msgs = error_msgs + "\n" + self.get_error_msg(handle)
00225 return error_msgs
00226
00227 def get_error_msg(self, handle):
00228
00229 err_code = handle.get_error_code()
00230 if err_code == 0:
00231 error_msg = "worked"
00232 else:
00233 error_msg = "Error: could not " + handle.function_name + " " + handle.component_name
00234 if err_code == 1:
00235 error_msg = error_msg + "\n" + "service call failed"
00236 elif err_code == 2:
00237 error_msg = error_msg + "\n" + "parameters are not on parameter server"
00238 elif err_code == 3:
00239 error_msg = error_msg + "\n" + "parameter type or dimension is wrong"
00240 elif err_code == 4:
00241 error_msg = error_msg + "\n" + "server or service is not available"
00242 elif err_code == 10:
00243 error_msg = error_msg + "\n" + "exceeded timeout"
00244 elif err_code == 11:
00245 error_msg = error_msg + "\n" + "didn't reach goal position"
00246 elif err_code == 12:
00247 error_msg = error_msg + "\n" + "no object detected"
00248 return error_msg
00249
00250 if __name__ == '__main__':
00251 try:
00252 rostest.run('rostest', 'grasp_test', UnitTest, sys.argv)
00253 except KeyboardInterrupt, e:
00254 pass
00255 print "exiting"