00001
00002 import roslib
00003 roslib.load_manifest('cob_component_test')
00004
00005
00006 import sys
00007 import time
00008 import unittest
00009 import rospy
00010 import rostest
00011 import actionlib
00012 import tf
00013
00014
00015 from simple_script_server import *
00016 from gazebo.msg import *
00017 from geometry_msgs.msg import *
00018
00019 from math import *
00020
00021 class UnitTest(unittest.TestCase):
00022 def __init__(self, *args):
00023 super(UnitTest, self).__init__(*args)
00024 rospy.init_node('grasp_test')
00025 self.message_received = False
00026 self.sss=simple_script_server()
00027
00028 def setUp(self):
00029 self.errors = []
00030
00031 def test_grasp(self):
00032
00033
00034 try:
00035
00036 if not rospy.has_param('~test_duration'):
00037 self.fail('Parameter test_duration does not exist on ROS Parameter Server')
00038 test_duration = rospy.get_param('~test_duration')
00039
00040
00041 if not rospy.has_param('~grasp_object'):
00042 self.fail('Parameter grasp_object does not exist on ROS Parameter Server')
00043 grasp_object = rospy.get_param('~grasp_object')
00044
00045 except KeyError, e:
00046 self.fail('Parameters not set properly')
00047
00048 print """
00049 Test duration: %s
00050 Object to grasp: %s"""%(test_duration, grasp_object)
00051
00052
00053 sub_model_states = rospy.Subscriber("/gazebo/model_states", ModelStates, self.cb_model_states)
00054 sub_link_states = rospy.Subscriber("/gazebo/link_states", LinkStates, self.cb_link_states)
00055
00056
00057 self.listener = tf.TransformListener(True, rospy.Duration(10.0))
00058
00059
00060 components_to_init = ['arm', 'tray', 'sdh', 'torso', 'base']
00061 for component in components_to_init:
00062 init_component = self.sss.init(component)
00063 if init_component.get_error_code() != 0:
00064 error_msg = 'Could not initialize ' + component
00065 self.fail(error_msg)
00066
00067
00068 if grasp_object not in self.model_states.name:
00069 self.fail(grasp_object + " not spawned correctly")
00070
00071
00072
00073
00074
00075 self.arm_7_link_index = self.link_states.name.index("arm_7_link")
00076
00077
00078 handle_base = self.sss.move('base', 'kitchen', False)
00079 self.sss.move('tray', 'down', False)
00080 self.sss.move('arm', 'pregrasp', False)
00081 self.sss.move('sdh', 'cylopen', False)
00082 handle_base.wait()
00083
00084
00085 self.sss.detect('milk')
00086 milk_box = PoseStamped()
00087 milk_box = self.sss.get_object_pose('milk')
00088
00089
00090 self.sss.sleep(2)
00091 grasp_obj = self.trans_into_arm_7_link(self.model_states.pose[obj_index].position)
00092
00093
00094 pregrasp_distance = 0.03
00095 grasp_offset = 0.17
00096
00097
00098
00099 self.sss.move_cart_rel("arm", [[0.0, 0.0, 0.2], [0.0, 0.0, 0.0]])
00100
00101
00102 self.sss.move("sdh", "cylclose")
00103
00104 self.sss.move_cart_rel("arm", [[0.2, -0.1, -0.2], [0.0, 0.0, 0.0]])
00105
00106
00107 self.check_pos(self.link_states.pose[self.arm_7_link_index].position, self.model_states.pose[obj_index].position, 0.5, "sdh in kitchen")
00108
00109
00110 handle_arm = self.sss.move("arm", "grasp-to-tray", False)
00111
00112 self.sss.move("tray", "up")
00113 handle_arm.wait()
00114
00115
00116 self.check_pos(self.link_states.pose[self.arm_7_link_index].position, self.model_states.pose[obj_index].position, 0.5, "sdh over tray")
00117
00118
00119 self.sss.move_cart_rel("arm", [[-0.05, 0.0, 0.0], [0, 0, 0]])
00120 self.sss.move("sdh", "cylopen")
00121
00122
00123 self.sss.move('base', [0, -0.5, 0])
00124
00125
00126 des_pos_world = Point()
00127 des_pos_world.x = 0.3
00128 des_pos_world.y = -0.4
00129 des_pos_world.z = 0.885
00130 self.check_pos(des_pos_world, self.model_states.pose[obj_index].position, 0.5, "tray")
00131
00132
00133 self.sss.move("sdh", "cylclose")
00134
00135
00136 self.sss.move("arm", "overtray")
00137 self.sss.move_cart_rel("arm", [[0.0, 0.0, -0.2], [0, 0, 0]])
00138 self.sss.move("arm", [[1.4272207239645427, -0.86918345596744029, -2.6785592907972724, -0.83566556448023821, 0.93072293274776374, 1.2925104647818602, -2.3042322384962883]])
00139 des_pos_world.x = 0.0
00140 des_pos_world.y = -1.2
00141 des_pos_world.z = 0.56
00142 des_pos_arm_7_link = self.trans_into_arm_7_link(des_pos_world)
00143
00144 x_dist_obj_arm_7_link = self.trans_into_arm_7_link(self.model_states.pose[obj_index].position).point.x
00145 self.sss.move_cart_rel("arm", [[(des_pos_arm_7_link.point.x - x_dist_obj_arm_7_link + 0.01), des_pos_arm_7_link.point.y, des_pos_arm_7_link.point.z], [0.0, 0.0, 0.0]])
00146 self.sss.move("sdh", "cylopen")
00147
00148
00149 des_pos_world.z = 0.55
00150 self.check_pos(des_pos_world, self.model_states.pose[obj_index].position, 0.2, "table")
00151
00152 self.sss.move_cart_rel("arm", [[0.5, 0.0, 0.0], [0.0, 0.0, 0.0]])
00153 self.sss.move("arm", "folded")
00154
00155
00156
00157
00158 def cb_model_states(self, msg):
00159 self.model_states = msg
00160
00161 def cb_link_states(self, msg):
00162 self.link_states = msg
00163
00164 def check_pos(self, des_pos, act_pos, tolerance, pos_name):
00165
00166 distance = self.calc_dist(des_pos, act_pos)
00167 if distance >= tolerance:
00168 error_msg = "Object not at desired position '" + pos_name + "'"
00169 self.fail(error_msg)
00170 else:
00171 print >> sys.stderr, "Object in/on '", pos_name, "'"
00172 print >> sys.stderr, "Distance to '", pos_name, "': ", distance
00173
00174 def calc_dist(self, des_pos, act_pos):
00175
00176 distance = sqrt((des_pos.x - act_pos.x)**2 + (des_pos.y - act_pos.y)**2 + (des_pos.z - act_pos.z)**2)
00177 return distance
00178
00179
00180 def trans_into_arm_7_link(self, coord_world):
00181
00182 coord_arm_7_link = PointStamped()
00183 coord_arm_7_link.header.stamp = rospy.Time.now()
00184 coord_arm_7_link.header.frame_id = "/map"
00185 coord_arm_7_link.point = coord_world
00186 self.sss.sleep(2)
00187
00188 if not self.sss.parse:
00189 coord_arm_7_link = self.listener.transformPoint('/arm_7_link', coord_arm_7_link)
00190
00191
00192 return coord_arm_7_link
00193
00194
00195 if __name__ == '__main__':
00196 try:
00197 rostest.run('rostest', 'grasp_test', UnitTest, sys.argv)
00198 except KeyboardInterrupt, e:
00199 pass
00200 print "exiting"