cartesian_interface_backend.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 
00003 import numpy as np
00004 import cPickle as pkl
00005 
00006 import roslib
00007 roslib.load_manifest("hrl_ellipsoidal_control")
00008 import rospy
00009 from std_srvs.srv import Empty, EmptyResponse
00010 
00011 import hrl_geom.transformations as trans
00012 from hrl_pr2_arms.pr2_arm_jt import create_ep_arm, PR2ArmJTransposeTask
00013 from hrl_pr2_arms.pr2_arm_joint_traj import PR2ArmJointTraj
00014 from hrl_pr2_arms.pr2_controller_switcher import ControllerSwitcher
00015 from hrl_ellipsoidal_control.cartesian_controller import CartesianController
00016 from hrl_ellipsoidal_control.interface_backend import ControllerInterfaceBackend
00017 from hrl_ellipsoidal_control.ellipsoidal_interface_backend import ell_trans_params
00018 from hrl_ellipsoidal_control.ellipsoidal_interface_backend import ELL_LOCAL_VEL
00019 
00020 CART_TRANS_VEL = 0.02
00021 HORIZONTAL_STEP = 0.015
00022 VERTICAL_STEP = 0.014
00023 DEPTH_STEP = 0.014
00024 cart_trans_params = {
00025     'translate_up' : (0, 0, -VERTICAL_STEP),   'translate_down' : (0, 0, VERTICAL_STEP),
00026     'translate_right' : (0, -HORIZONTAL_STEP, 0), 'translate_left' : (0, HORIZONTAL_STEP, 0),
00027     'translate_in' : (DEPTH_STEP, 0, 0),      'translate_out' : (-DEPTH_STEP, 0, 0)}
00028 
00029 CART_ROT_VEL = 0.016
00030 ROLL_STEP = np.pi/12
00031 PITCH_STEP = np.pi/12
00032 YAW_STEP = np.pi/12
00033 cart_rot_params = {
00034     'rotate_x_pos' : (-ROLL_STEP, 0, 0), 'rotate_x_neg' : (ROLL_STEP, 0, 0),
00035     'rotate_y_pos' : (0, PITCH_STEP, 0), 'rotate_y_neg' : (0, -PITCH_STEP, 0),
00036     'rotate_z_pos' : (0, 0, -YAW_STEP),  'rotate_z_neg' : (0, 0, YAW_STEP)}
00037 
00038 
00039 class CartesianInterfaceBackend(ControllerInterfaceBackend):
00040     def __init__(self, use_service=True):
00041         super(CartesianInterfaceBackend, self).__init__("Cartesian Controller",
00042                                                         hidden_buttons=["reset_rotation"]
00043                                                         use_service=use_service)
00044         self.controller = CartesianController()
00045         self.button_distances = {}
00046         self.button_times = {}
00047         for button in cart_trans_params.keys() + cart_rot_params.keys() + ["reset_rotation"]:
00048             self.button_distances[button] = []
00049             self.button_times[button] = []
00050 
00051     def set_arm(self, cart_arm):
00052         self.cart_arm = cart_arm
00053         self.controller.set_arm(self.cart_arm)
00054 
00055     def run_controller(self, button_press):
00056         start_pos, _ = self.cart_arm.get_ep()
00057         start_time = rospy.get_time()
00058         quat_gripper_rot = trans.quaternion_from_euler(np.pi, 0, 0)
00059         if button_press in cart_trans_params:
00060             ell_change_trans_ep = ell_trans_params[button_press]
00061             change_trans_ep = cart_trans_params[button_press]
00062 
00063             # get number of samples:
00064             ell_f, rot_mat_f = self._parse_ell_move((ell_change_trans_ep, (0, 0, 0)), 
00065                                                     ((0, 0, 0), 0), 
00066                                                     quat_gripper_rot)
00067             traj = self._create_ell_trajectory(ell_f, rot_mat_f, orient_quat, velocity)
00068             num_samps = len(traj)
00069 
00070             # normalization occurs here:
00071             # TODO FIGURE THIS OUT? 
00072             ell_dist = self.controller._get_ell_equiv_dist((ell_change_trans_ep, (0, 0, 0)), 
00073                                                            ((0, 0, 0), 0), 
00074                                                            quat_gripper_rot, ELL_LOCAL_VEL)
00075             change_trans_ep = tuple(np.array(change_trans_ep)/np.sum(change_trans_ep) *
00076                                     ell_dist)
00077 
00078             self.controller.execute_cart_move((change_trans_ep, (0, 0, 0)), ((0, 0, 0), 0), 
00079                                                  quat_gripper_rot, CART_TRANS_VEL, num_samps=num_samps)
00080         elif button_press in cart_rot_params:
00081             change_rot_ep = cart_rot_params[button_press]
00082             self.controller.execute_cart_move(((0, 0, 0), change_rot_ep), ((0, 0, 0), 0), 
00083                                                  quat_gripper_rot, CART_ROT_VEL)
00084         elif button_press == "reset_rotation":
00085             self.controller.execute_cart_move(((0, 0, 0), np.mat(np.eye(3))), ((0, 0, 0), 1), 
00086                                                  quat_gripper_rot, CART_ROT_VEL)
00087         end_pos, _ = self.cart_arm.get_ep()
00088         end_time = rospy.get_time()
00089         dist = np.linalg.norm(end_pos - start_pos)
00090         time_diff = end_time - start_time
00091         print "%s, dist: %.3f, time_diff: %.2f" % (button_press, dist, time_diff)
00092         self.button_distances[button_press].append(dist)
00093         self.button_times[button_press].append(time_diff)
00094 
00095     def print_statistics(self):
00096         print self.button_distances
00097         print self.button_times
00098         print "MEANS:"
00099         for button in cart_trans_params.keys() + cart_rot_params.keys() + ["reset_rotation"]:
00100             print "%s, avg dist: %.3f, avg_time: %.3f, num_presses: %d" % (
00101                     button, 
00102                     np.mean(self.button_distances[button]), 
00103                     np.mean(self.button_times[button]), 
00104                     len(self.button_distances[button]))
00105 
00106     def save_statistics(self, filename):
00107         button_mean_distances = {}
00108         button_mean_times = {}
00109         button_num_presses = {}
00110         for button in cart_trans_params.keys() + cart_rot_params.keys() + ["reset_rotation"]:
00111             button_mean_distances[button] = np.mean(self.button_distances[button])
00112             button_mean_times[button] = np.mean(self.button_times[button])
00113             button_num_presses[button] = len(self.button_distances[button])
00114         stats = {
00115             "button_distances"      : self.button_distances,
00116             "button_times"          : self.button_times,
00117             "button_mean_distances" : button_mean_distances,
00118             "button_mean_times"     : button_mean_times,
00119             "button_num_presses"    : button_num_presses
00120         }
00121         f = file(filename, "w")
00122         pkl.dump(stats, f)
00123         f.close()
00124 
00125 
00126 def main():
00127     rospy.init_node("cartesian_controller_backend")
00128 
00129     cart_arm = create_ep_arm('l', PR2ArmJTransposeTask, 
00130                               controller_name='%s_cart_jt_task', 
00131                               end_link="%s_gripper_shaver45_frame")
00132 
00133     cart_backend = CartesianInterfaceBackend(cart_arm)
00134     cart_backend.disable_interface("Setting up arm.")
00135 
00136     if False:
00137         ctrl_switcher = ControllerSwitcher()
00138         ctrl_switcher.carefree_switch('l', '%s_arm_controller', None)
00139         rospy.sleep(1)
00140         joint_arm = create_ep_arm('l', PR2ArmJointTrajectory)
00141 
00142         setup_angles = [0, 0, np.pi/2, -np.pi/2, -np.pi, -np.pi/2, -np.pi/2]
00143         joint_arm.set_ep(setup_angles, 5)
00144         rospy.sleep(5)
00145 
00146         ctrl_switcher.carefree_switch('l', '%s_cart_jt_task', 
00147                                       "$(find hrl_rfh_fall_2011)/params/l_jt_task_shaver45.yaml") 
00148         rospy.sleep(1)
00149 
00150     if False:
00151         rospy.sleep(1)
00152         #cart_arm.set_posture(setup_angles)
00153         setup_angles = [0, 0, np.pi/2, -np.pi/2, -np.pi, -np.pi/2, -np.pi/2]
00154         cart_arm.set_posture(setup_angles)
00155         cart_arm.set_gains([200, 800, 800, 80, 80, 80], [15, 15, 15, 1.2, 1.2, 1.2])
00156         cart_backend.controller.reset_arm_orientation(8)
00157 
00158     cart_backend.enable_interface("Controller ready.")
00159     rospy.spin()
00160     cart_backend.print_statistics()
00161 
00162 if __name__ == "__main__":
00163     main()


hrl_ellipsoidal_control
Author(s): Kelsey Hawkins
autogenerated on Wed Nov 27 2013 11:41:49