pr2_arm_controller.py
Go to the documentation of this file.
00001 #!/usr/bin/env python  
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 #import threading
00012 from geometry_msgs.msg import PoseStamped
00013 #from phantom_omni.msg import OmniFeedback
00014 from teleop_controllers.msg import JTTeleopControllerState
00015 from actionlib_msgs.msg import *
00016 from pr2_controllers_msgs.msg import *
00017 import actionlib
00018 ###COULD use the accelerometer in a smart way too if want to....
00019 #from pr2_msgs.msg import AccelerometerState
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, # = 'l_cart/command_pose',
00031             desired_pose_topic, 
00032             current_pose_topic, 
00033             gripper_control_topic,  #'l_gripper_controller'
00034             gripper_tip_frame, #'l_gripper_tool_frame'
00035             center_in_torso_frame, #=[1,.3,-1], 
00036             tfbroadcast, #=None,
00037             tflistener): #=None):
00038 
00039         self.zero_out_forces = True
00040         self.desired_pose_topic = desired_pose_topic
00041         self.X_BOUNDS = [.3, 1.5] #Bound translation of PR2 arms in the X direction (in torso link frame)
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 ########################I will need a static transform like this in the launch file from the calibration#############
00070     # Link this omni to pr2 frame
00071 #     def send_transform_to_link_omni_and_pr2_frame(self):
00072 #         self.tfbroadcast.sendTransform(self.center_in_torso_frame,
00073 #                          tuple(tf.transformations.quaternion_from_euler(0, 0, 0)),
00074 #                          rospy.Time.now(),
00075 #                          self.omni_name,
00076 #                          "/torso_lift_link")
00077 ####################################################################################################################
00078 ####################################################################################################################
00079     ##
00080     # Callback for pose of omni
00081     def cmd_pose(self, goal_tip_t, goal_tip_q):
00082             #Get the omni's tip pose in the PR2's torso frame
00083 #         tip_omni, msg_frame = tfu.posestamped_as_matrix(msg)
00084 #         self.torso_T_playpen(tip_omni, msg_frame)
00085         # tip_t = self.tip_t
00086         # tip_q = self.tip_q
00087         #Publish new arm pose
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  # m/s max desired speed of the arm
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         ##########do a comparison here and break up the trajectory into chunks###########
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             # while des_ps != ps:
00117             #     self.pr2_pub.publish(ps)
00118             #     rospy.sleep(5)
00119             #     des_ps = rospy.wait_for_message(self.desired_pose_topic, PoseStamped)
00120             #     print "this is desired", des_ps
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 #             if ff == True:
00152 #                 self.left_feedback = ForceFeedbackFilter(wrench_topic = '/l_cart/state',
00153 #                                              dest_frame = '/omni2_sensable',
00154 #                                              wrench_frame = '/l_gripper_tool_frame', 
00155 #                                              force_feedback_topic = 'omni2_force_feedback',
00156 #                                              tflistener = self.tflistener,
00157 #                                              kp_name = '/l_cart/cart_gains/trans/p',
00158 #                                              kd_name = '/l_cart/cart_gains/trans/d')
00159 #                 self.ff_list.append(self.left_feedback)
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 #             if ff == True:
00171 #                 self.right_feedback = ForceFeedbackFilter(wrench_topic = '/r_cart/state',
00172 #                       dest_frame = '/omni1_sensable',
00173 #                       wrench_frame = '/r_gripper_tool_frame', 
00174 #                       force_feedback_topic = 'omni1_force_feedback',
00175 #                       tflistener = self.tflistener,
00176 #                       kp_name = '/r_cart/cart_gains/trans/p',
00177 #                       kd_name = '/r_cart/cart_gains/trans/d')
00178 #                 self.ff_list.append(self.right_feedback)
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 #    o.run()
00190 ####Default out of workspace positions
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     # o.right_controller.gripper_server.send_goal(Pr2GripperCommandGoal(
00197     #                                             Pr2GripperCommand(position = 0.09, max_effort = 40)), 
00198     #                                             done_cb = None, feedback_cb = None)
00199 
00200 #    o.right_controller.cmd_pose(np.array([0.62, 0.0, 0.16]), np.array([0.5, 0.48, -0.48, 0.53]))
00201 #    while not rospy.is_shutdown():
00202 
00203     # o.left_controller.cmd_pose(tip_t_l, tip_q_l)
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 #    o.left_controller.cmd_pose(tip_t_l, tip_q_l)
00231 #    o.right_controller.cmd_pose(tip_t_r, tip_q_r)


pr2_playpen
Author(s): Marc Killpack / mkillpack3@gatech.edu, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 12:18:32