00001
00002 import roslib
00003 roslib.load_manifest('pr2_playpen')
00004
00005 import rospy
00006 import tf
00007 import math
00008 import tf.transformations as tr
00009 import numpy as np
00010 import hrl_lib.tf_utils as tfu
00011
00012 from geometry_msgs.msg import PoseStamped
00013
00014 from teleop_controllers.msg import JTTeleopControllerState
00015 from actionlib_msgs.msg import *
00016 from pr2_controllers_msgs.msg import *
00017 import actionlib
00018
00019
00020
00021
00022 def limit_range(numb, lower, upper):
00023 if lower > upper:
00024 raise RuntimeError('lower bound > upper bound, wa?')
00025 return max(min(numb, upper), lower)
00026
00027
00028 class ControlPR2Arm:
00029
00030 def __init__(self, pr2_control_topic,
00031 desired_pose_topic,
00032 current_pose_topic,
00033 gripper_control_topic,
00034 gripper_tip_frame,
00035 center_in_torso_frame,
00036 tfbroadcast,
00037 tflistener):
00038
00039 self.zero_out_forces = True
00040 self.desired_pose_topic = desired_pose_topic
00041 self.X_BOUNDS = [.3, 1.5]
00042 self.center_in_torso_frame = center_in_torso_frame
00043 self.tflistener = tflistener
00044 self.tfbroadcast = tfbroadcast
00045 self.gripper_tip_frame = gripper_tip_frame
00046 self.current_pose_topic = current_pose_topic
00047 self.prev_dt = 0.0
00048 self.tip_t = np.zeros((3,1))
00049 self.tip_q = np.zeros((4,1))
00050 self.gripper_server = actionlib.SimpleActionClient(gripper_control_topic+'/gripper_action', Pr2GripperCommandAction)
00051 self.gripper_server.wait_for_server()
00052 self.rate = rospy.Rate(2.0)
00053
00054 success = False
00055 while not success and (not rospy.is_shutdown()):
00056 self.rate.sleep()
00057 try:
00058 print "need to implement tf stuff still"
00059 success = True
00060 except tf.LookupException, e:
00061 pass
00062 except tf.ConnectivityException, e:
00063 pass
00064 rospy.loginfo('Finished linking frame (but not really)')
00065
00066 self.pr2_pub = rospy.Publisher(pr2_control_topic, PoseStamped)
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 def cmd_pose(self, goal_tip_t, goal_tip_q):
00082
00083
00084
00085
00086
00087
00088 cur_ps = rospy.wait_for_message(self.current_pose_topic, PoseStamped)
00089 print "this is the cur_ps :", cur_ps
00090
00091 cur_tip_t = np.array([cur_ps.pose.position.x, cur_ps.pose.position.y, cur_ps.pose.position.z])
00092 cur_tip_q = np.array([cur_ps.pose.orientation.x, cur_ps.pose.orientation.y,
00093 cur_ps.pose.orientation.z, cur_ps.pose.orientation.w])
00094 diff_pos = goal_tip_t - cur_tip_t
00095 max_speed = 0.1
00096 factor = np.ceil(np.max(np.abs(diff_pos))/(max_speed*self.rate.sleep_dur.to_sec()))
00097 dp = diff_pos/factor
00098 dq = (goal_tip_q-cur_tip_q)/factor
00099
00100 ps = PoseStamped()
00101
00102 ps.header.frame_id = '/torso_lift_link'
00103 tip_t = cur_tip_t + 2*dp
00104 tip_q = cur_tip_q + 2*dq
00105 for i in xrange(factor):
00106 ps.header.stamp = rospy.get_rostime()
00107 tip_t = tip_t + dp
00108 tip_q = tip_q + dq
00109 ps.pose.position.x = tip_t[0]
00110 ps.pose.position.y = tip_t[1]
00111 ps.pose.position.z = tip_t[2]
00112 ps.pose.orientation.x = tip_q[0]
00113 ps.pose.orientation.y = tip_q[1]
00114 ps.pose.orientation.z = tip_q[2]
00115 ps.pose.orientation.w = tip_q[3]
00116
00117
00118
00119
00120
00121 self.pr2_pub.publish(ps)
00122 print ps
00123 self.rate.sleep()
00124
00125 ps.header.stamp = rospy.get_rostime()
00126 ps.pose.position.x = goal_tip_t[0]
00127 ps.pose.position.y = goal_tip_t[1]
00128 ps.pose.position.z = goal_tip_t[2]
00129 ps.pose.orientation.x = goal_tip_q[0]
00130 ps.pose.orientation.y = goal_tip_q[1]
00131 ps.pose.orientation.z = goal_tip_q[2]
00132 ps.pose.orientation.w = goal_tip_q[3]
00133 self.pr2_pub.publish(ps)
00134 self.rate.sleep()
00135
00136 class PR2Playpen:
00137 def __init__(self):
00138 rospy.init_node('playpen_simple_grasper')
00139 self.tfbroadcast = tf.TransformBroadcaster()
00140 self.tflistener = tf.TransformListener()
00141
00142 self.left_controller = ControlPR2Arm(
00143 pr2_control_topic = 'l_cart/command_pose',
00144 desired_pose_topic = 'l_cart/state/x_desi',
00145 current_pose_topic = 'l_cart/state/x',
00146 gripper_control_topic = 'l_gripper_controller',
00147 gripper_tip_frame = 'l_gripper_tool_frame',
00148 center_in_torso_frame = [1.2, .3, -1],
00149 tfbroadcast=self.tfbroadcast,
00150 tflistener=self.tflistener)
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 self.right_controller = ControlPR2Arm(
00162 pr2_control_topic = 'r_cart/command_pose',
00163 desired_pose_topic = 'r_cart/state/x_desi',
00164 current_pose_topic = 'r_cart/state/x',
00165 gripper_control_topic = 'r_gripper_controller',
00166 gripper_tip_frame = 'r_gripper_tool_frame',
00167 center_in_torso_frame = [1.2, -.3, -1],
00168 tfbroadcast=self.tfbroadcast,
00169 tflistener=self.tflistener)
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 def run(self):
00182 rate = rospy.Rate(100.0)
00183 rospy.loginfo('running...')
00184 rospy.spin()
00185
00186
00187 if __name__ == '__main__':
00188 o = PR2Playpen()
00189
00190
00191 tip_t_r = np.array([0.23, -0.6, -0.05])
00192 tip_q_r = np.array([-0.51, 0.54, 0.48, 0.46])
00193 tip_t_l = np.array([0.23, 0.6, -0.05])
00194 tip_q_l = np.array([-0.51, 0.54, 0.48, 0.46])
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 cur_time = rospy.Time.to_sec(rospy.Time.now())
00208
00209 while rospy.Time.to_sec(rospy.Time.now())-cur_time < 6:
00210 o.right_controller.cmd_pose(np.array([0.62, 0.0, 0.16]), np.array([0.5, 0.48, -0.48, 0.53]))
00211
00212 cur_time = rospy.Time.to_sec(rospy.Time.now())
00213 while rospy.Time.to_sec(rospy.Time.now())-cur_time < 6:
00214 o.right_controller.cmd_pose(np.array([0.600+0.06, 0.106, -0.32]), np.array([0.408, 0.560, -0.421, 0.585]))
00215
00216 o.right_controller.gripper_server.send_goal(Pr2GripperCommandGoal(
00217 Pr2GripperCommand(position = 0.0, max_effort = 20)),
00218 done_cb = None, feedback_cb = None)
00219 rospy.sleep(10)
00220
00221 cur_time = rospy.Time.to_sec(rospy.Time.now())
00222
00223 while rospy.Time.to_sec(rospy.Time.now())-cur_time < 6:
00224 o.right_controller.cmd_pose(np.array([0.62, 0.0, 0.16]), np.array([0.5, 0.48, -0.48, 0.53]))
00225
00226
00227 while not rospy.is_shutdown():
00228 o.left_controller.cmd_pose(tip_t_l, tip_q_l)
00229 o.right_controller.cmd_pose(tip_t_r, tip_q_r)
00230
00231