arm_cart_control_backend.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 
00003 import numpy as np
00004 
00005 import roslib
00006 roslib.load_manifest("rospy")
00007 roslib.load_manifest("std_msgs")
00008 roslib.load_manifest("hrl_pr2_arms")
00009 import rospy
00010 from std_msgs.msg import String
00011 import tf.transformations as tf_trans
00012 
00013 from hrl_pr2_arms.pr2_arm import create_pr2_arm, PR2ArmJointTrajectory, PR2ArmCartesianPostureBase
00014 from arm_cart_vel_control import PR2ArmCartVelocityController
00015 from hrl_pr2_arms.pr2_controller_switcher import ControllerSwitcher
00016 
00017 MOVE_BUTTONS = {'translate_up' : (True, tf_trans.euler_matrix(0, -np.pi/2, 0)[:3,:3]), 
00018                 'translate_down' : (True, tf_trans.euler_matrix(0, np.pi/2, 0)[:3,:3]), 
00019                 'translate_left' : (True, tf_trans.euler_matrix(0, 0, np.pi/2)[:3,:3]), 
00020                 'translate_right' : (True, tf_trans.euler_matrix(0, 0, -np.pi/2)[:3,:3]), 
00021                 'translate_in' : (True, tf_trans.euler_matrix(0, 0, np.pi)[:3,:3]), 
00022                 'translate_out' : (True, tf_trans.euler_matrix(0, 0, 0)[:3,:3]),
00023                 'rotate_x_pos' : (False, tf_trans.euler_matrix(0, 0, 0)[:3,:3]),
00024                 'rotate_x_neg' : (False, tf_trans.euler_matrix(0, 0, np.pi)[:3,:3]),
00025                 'rotate_y_pos' : (False, tf_trans.euler_matrix(0, 0, np.pi/2)[:3,:3]),
00026                 'rotate_y_neg' : (False, tf_trans.euler_matrix(0, 0, -np.pi/2)[:3,:3]),
00027                 'rotate_z_pos' : (False, tf_trans.euler_matrix(0, -np.pi/2, 0)[:3,:3]),
00028                 'rotate_z_neg' : (False, tf_trans.euler_matrix(0, np.pi/2, 0)[:3,:3])}
00029 
00030 MONITOR_RATE = 20.
00031 MOVE_STATE_TOPIC = "/arm_control_gui/move_state"
00032 LOAD_ARM_TOPIC = "/arm_control_gui/load_arm"
00033 
00034 class ArmCartCtrlBackend(object):
00035     def __init__(self, r_arm, l_arm, monitor_rate, misses_allowed=5):
00036         self.misses_allowed = misses_allowed
00037         self.is_move_connected = False
00038         self.last_move_time = 0.
00039         self.misses = 0
00040         self.current_arm = "l"
00041         self.arm_switch = False
00042         self.topic_cmd = ""
00043         self.active_cmd = ""
00044 
00045         self.r_vel_ctrl = PR2ArmCartVelocityController(r_arm)
00046         self.l_vel_ctrl = PR2ArmCartVelocityController(l_arm)
00047         self.trans_vel = 0.01
00048         self.rot_vel = 0.10
00049 
00050         rospy.Subscriber(MOVE_STATE_TOPIC, String, self.move_state_cb)
00051         rospy.Subscriber(LOAD_ARM_TOPIC, String, self.load_arm_cb)
00052 
00053     def move_state_cb(self, msg):
00054         self.is_move_connected = True
00055         self.last_move_time = rospy.get_time()
00056         self.misses = 0
00057         self.topic_cmd = msg.data
00058 
00059     def load_arm_cb(self, msg):
00060         new_arm = msg.data
00061         if self.current_arm != new_arm:
00062             self.arm_switch = True
00063 
00064     def check_arm_switched(self):
00065         if self.arm_switch:
00066             self.arm_switch = False
00067             return True
00068         return False
00069 
00070     def check_move_state(self):
00071         if rospy.get_time() - self.last_move_time > 1. / MONITOR_RATE:
00072             self.misses += 1
00073             if self.misses > self.misses_allowed:
00074                 self.is_move_connected = False
00075         return self.is_move_connected
00076 
00077     def backend_loop(self):
00078         #vel_frame = tf_trans.euler_matrix(0, -np.pi/2, 0)[:3,:3]
00079         #vel_ctrl.move_velocity(velocity_rot=vel_frame, velocity=0.005, is_translation=False)
00080         
00081         vel_ctrl = self.r_vel_ctrl
00082         r = rospy.Rate(MONITOR_RATE)
00083         while not rospy.is_shutdown():
00084             if self.check_move_state():
00085                 if self.topic_cmd != self.active_cmd and self.topic_cmd in MOVE_BUTTONS:
00086                     self.active_cmd = self.topic_cmd
00087                     is_translation, vel_frame = MOVE_BUTTONS[self.topic_cmd]
00088                     if is_translation:
00089                         vel_val = self.trans_vel
00090                     else:
00091                         vel_val = self.rot_vel
00092 
00093                     def move_vel_thread(te):
00094                         vel_ctrl.move_velocity(velocity_rot=vel_frame, velocity=vel_val, 
00095                                                is_translation=is_translation)
00096                     move_vel_timer = rospy.Timer(rospy.Duration(0.05), move_vel_thread, oneshot=True)
00097                 if self.topic_cmd == "":
00098                     self.active_cmd = ""
00099                     vel_ctrl.stop_moving()
00100             else:
00101                 vel_ctrl.stop_moving()
00102 
00103             r.sleep()
00104 
00105 def main():
00106     rospy.init_node("arm_cart_control_backend")
00107     ctrl_switcher = ControllerSwitcher()
00108     if False:
00109         ctrl_switcher.carefree_switch('r', '%s_arm_controller', 
00110                            '$(find hrl_pr2_arms)/params/joint_traj_params_electric.yaml')
00111         rospy.sleep(0.5)
00112         ctrl_switcher.carefree_switch('r', '%s_joint_controller_low', 
00113                            '$(find hrl_pr2_arms)/params/joint_traj_params_electric_low.yaml')
00114         r_arm_js = create_pr2_arm('r', PR2ArmJointTrajectory, controller_name='%s_joint_controller_low')
00115         q = [-0.34781704,  0.27341079, -1.75392154, -2.08626393, -3.43756314, -1.82146607, -1.85187734]
00116         r_arm_js.set_ep(q, 3) 
00117         rospy.sleep(6)
00118     ctrl_switcher.carefree_switch('r', '%s_cart_low_rfh',
00119                                   '$(find kelsey_sandbox)/params/j_transpose_low_rfh.yaml')
00120 
00121     r_arm = create_pr2_arm('r', PR2ArmCartesianPostureBase, controller_name='%s_cart_low_rfh')
00122     r_arm.set_posture()
00123     rospy.sleep(0.2)
00124     l_arm = None
00125     #l_arm = create_pr2_arm('l', PR2ArmCartesianPostureBase)
00126     #l_arm.set_posture()
00127     rospy.sleep(0.2)
00128     cart_ctrl = ArmCartCtrlBackend(r_arm, l_arm, MONITOR_RATE)
00129     cart_ctrl.backend_loop()
00130 
00131 if __name__ == "__main__":
00132     main()


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:03