00001
00002
00003 import roslib
00004 roslib.load_manifest( 'rospy' )
00005 roslib.load_manifest( 'kinematics_msgs' )
00006 roslib.load_manifest( 'geometry_msgs' )
00007
00008 import rospy
00009 from kinematics_msgs.srv import \
00010 GetPositionIK, GetPositionIKRequest
00011 from geometry_msgs.msg import \
00012 Pose, Point, Quaternion
00013
00014 import numpy as np
00015 import dynamics_utils as dynutils
00016
00017
00018
00019
00020
00021 if __name__== '__main__':
00022 node_name = "wave_arm"
00023 rospy.loginfo( 'register node %s ...'%node_name )
00024 rospy.init_node( node_name )
00025 rospy.loginfo( 'node %s up and running!'%node_name )
00026
00027
00028 joint_names = dynutils.get_joint_names( 'l_arm_controller' )
00029 ik_service_name = 'pr2_left_arm_kinematics/get_ik'
00030 rospy.loginfo( 'wait for service %s ...'%ik_service_name )
00031 rospy.wait_for_service( ik_service_name )
00032 ik_service = rospy.ServiceProxy( ik_service_name, GetPositionIK )
00033 rospy.loginfo( 'ik service %s is up and running!'%ik_service_name )
00034
00035
00036 time = 10.0;
00037 n = int(time * 200)
00038 dt = time / n;
00039 alphas = np.linspace( 0, 2 * np.pi, n + 1 )
00040 alphas = alphas[0:-1]
00041 ys = np.cos( alphas ) * 0.4
00042 zs = -np.sin( 2 * alphas ) * 0.2
00043
00044
00045 rospy.loginfo( 'creating trajectory ...' )
00046 trajectory = []
00047 req = GetPositionIKRequest()
00048 req.timeout = rospy.Duration(5.0)
00049 req.ik_request.ik_link_name = "l_wrist_roll_link"
00050 req.ik_request.pose_stamped.header.frame_id = "torso_lift_link"
00051 req.ik_request.ik_seed_state.joint_state.name = joint_names
00052 req.ik_request.ik_seed_state.joint_state.position = [0.61309537, 0.45494851, 0.03, -1.03480809, 2.23232079, -0.79696399, -2.44271129]
00053 joint_positions = []
00054
00055 for (y, z) in zip( ys, zs ):
00056 pose = Pose( position = Point( 0.6, 0.20 + y, 0.0 + z ),
00057 orientation = Quaternion( 0.0, 0.0, 0.0, 1.0 ) )
00058 req.ik_request.pose_stamped.pose = pose
00059
00060
00061 try:
00062
00063 res = ik_service( req )
00064
00065 if res.error_code.val == res.error_code.SUCCESS:
00066 joint_positions = np.asarray( res.solution.joint_state.position )
00067 else:
00068 print "IK failed, error code : ", res.error_code.val
00069 break
00070 except rospy.ServiceException, e:
00071 print "service call failed: %s"%e
00072 break
00073
00074 trajectory.append( joint_positions )
00075 req.ik_request.ik_seed_state.joint_state.position = joint_positions
00076
00077 rospy.loginfo( 'done!' )
00078
00079 pos = np.asarray( trajectory )
00080 dynutils.wrap_trajectory( pos )
00081
00082 vel = dynutils.compute_derivative( pos, dt )
00083 acc = dynutils.compute_derivative( vel, dt )
00084
00085
00086
00087
00088
00089 dynutils.move_arm( [ -np.pi / 3,
00090 np.pi / 3,
00091 0,
00092 -3 * np.pi/4,
00093 0,
00094 0,
00095 0], arm = 'r' )
00096
00097 l_jt_client = dynutils.init_jt_client(arm = 'l')
00098 dynutils.move_arm( pos[0,:],
00099 time_from_start = 5.0,
00100 client = l_jt_client )
00101
00102
00103 last_call = rospy.Time().now() + rospy.Duration().from_sec( .5 );
00104 dynutils.track_trajectory(pos, vel, acc, dt, arm = 'l', client = l_jt_client, stamp = last_call )
00105
00106 while ( not rospy.is_shutdown() ):
00107 dur = rospy.Time().now() - last_call;
00108 if ( dur.to_sec() > time - (time / 2.0) ):
00109 last_call += rospy.Duration().from_sec( time );
00110 dynutils.track_trajectory(pos, vel, acc, dt, arm = 'l', client = l_jt_client, stamp = last_call )