ellipsoidal_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_msgs.msg import String
00010 from std_srvs.srv import Empty, EmptyResponse
00011 
00012 import hrl_geom.transformations as trans
00013 from hrl_pr2_arms.pr2_arm_jt import create_ep_arm, PR2ArmJTransposeTask
00014 from hrl_pr2_arms.pr2_arm_joint_traj import PR2ArmJointTraj
00015 from hrl_pr2_arms.pr2_controller_switcher import ControllerSwitcher
00016 from hrl_ellipsoidal_control.ellipsoid_controller import EllipsoidController
00017 from hrl_ellipsoidal_control.interface_backend import ControllerInterfaceBackend
00018 from hrl_ellipsoidal_control.ellipsoidal_parameters import *
00019 
00020 class EllipsoidalInterfaceBackend(ControllerInterfaceBackend):
00021     def __init__(self, use_service=True):
00022         super(EllipsoidalInterfaceBackend, self).__init__("Ellipsoid Controller", 
00023                                                           use_service=use_service)
00024         self.controller = EllipsoidController()
00025         self.button_distances = {}
00026         self.button_times = {}
00027         for button in ell_trans_params.keys() + ell_rot_params.keys() + ["reset_rotation"]:
00028             self.button_distances[button] = []
00029             self.button_times[button] = []
00030 
00031     def set_arm(self, cart_arm):
00032         self.cart_arm = cart_arm
00033         self.controller.set_arm(self.cart_arm)
00034 
00035     def run_controller(self, button_press):
00036         start_pos, _ = self.cart_arm.get_ep()
00037         start_time = rospy.get_time()
00038         quat_gripper_rot = trans.quaternion_from_euler(np.pi, 0, 0)
00039         if button_press in ell_trans_params:
00040             change_trans_ep = ell_trans_params[button_press]
00041             self.controller.execute_ell_move((change_trans_ep, (0, 0, 0)), ((0, 0, 0), 0), 
00042                                                  quat_gripper_rot, ELL_LOCAL_VEL)
00043         elif button_press in ell_rot_params:
00044             change_rot_ep = ell_rot_params[button_press]
00045             self.controller.execute_ell_move(((0, 0, 0), change_rot_ep), ((0, 0, 0), 0), 
00046                                                  quat_gripper_rot, ELL_ROT_VEL)
00047         elif button_press == "reset_rotation":
00048             self.controller.execute_ell_move(((0, 0, 0), np.mat(np.eye(3))), ((0, 0, 0), 1), 
00049                                                  quat_gripper_rot, ELL_ROT_VEL)
00050         end_pos, _ = self.cart_arm.get_ep()
00051         end_time = rospy.get_time()
00052         dist = np.linalg.norm(end_pos - start_pos)
00053         time_diff = end_time - start_time
00054         print "%s, dist: %.3f, time_diff: %.2f" % (button_press, dist, time_diff)
00055         self.button_distances[button_press].append(dist)
00056         self.button_times[button_press].append(time_diff)
00057 
00058     def print_statistics(self):
00059         print self.button_distances
00060         print self.button_times
00061         print "MEANS:"
00062         for button in ell_trans_params.keys() + ell_rot_params.keys() + ["reset_rotation"]:
00063             print "%s, avg dist: %.3f, avg_time: %.3f, num_presses: %d" % (
00064                     button, 
00065                     np.mean(self.button_distances[button]), 
00066                     np.mean(self.button_times[button]), 
00067                     len(self.button_distances[button]))
00068 
00069     def save_statistics(self, filename):
00070         button_mean_distances = {}
00071         button_mean_times = {}
00072         button_num_presses = {}
00073         for button in ell_trans_params.keys() + ell_rot_params.keys() + ["reset_rotation"]:
00074             button_mean_distances[button] = np.mean(self.button_distances[button])
00075             button_mean_times[button] = np.mean(self.button_times[button])
00076             button_num_presses[button] = len(self.button_distances[button])
00077         stats = {
00078             "button_distances"      : self.button_distances,
00079             "button_times"          : self.button_times,
00080             "button_mean_distances" : button_mean_distances,
00081             "button_mean_times"     : button_mean_times,
00082             "button_num_presses"    : button_num_presses
00083         }
00084         f = file(filename, "w")
00085         pkl.dump(stats, f)
00086         f.close()
00087 
00088 def main():
00089     rospy.init_node("ellipsoidal_controller_backend")
00090 
00091 
00092     cart_arm = create_ep_arm('l', PR2ArmJTransposeTask, 
00093                              controller_name='%s_cart_jt_task', 
00094                              end_link="%s_gripper_shaver45_frame")
00095 
00096     ell_backend = EllipsoidalInterfaceBackend(cart_arm)
00097     ell_backend.disable_interface("Setting up arm.")
00098 
00099     if True:
00100         ctrl_switcher = ControllerSwitcher()
00101         ctrl_switcher.carefree_switch('l', '%s_arm_controller', None)
00102         rospy.sleep(1)
00103         joint_arm = create_ep_arm('l', PR2ArmJointTraj)
00104 
00105         setup_angles = [0, 0, np.pi/2, -np.pi/2, -np.pi, -np.pi/2, -np.pi/2]
00106         joint_arm.set_ep(setup_angles, 5)
00107         rospy.sleep(5)
00108 
00109         ctrl_switcher.carefree_switch('l', '%s_cart_jt_task', 
00110                                       "$(find hrl_rfh_fall_2011)/params/l_jt_task_shaver45.yaml") 
00111         rospy.sleep(1)
00112 
00113     if True:
00114         rospy.sleep(1)
00115         setup_angles = [0, 0, np.pi/2, -np.pi/2, -np.pi, -np.pi/2, -np.pi/2]
00116         cart_arm.set_posture(setup_angles)
00117         cart_arm.set_gains([200, 800, 800, 80, 80, 80], [15, 15, 15, 1.2, 1.2, 1.2])
00118         ell_backend.controller.reset_arm_orientation(8)
00119 
00120     ell_backend.enable_interface("Controller ready.")
00121     rospy.spin()
00122     ell_backend.print_statistics()
00123 
00124 if __name__ == "__main__":
00125     main()


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