cartesian_manager.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import numpy as np
00004 import sys
00005 
00006 import roslib
00007 roslib.load_manifest("hrl_face_adls")
00008 
00009 import rospy
00010 from std_msgs.msg import Bool, Float32
00011 from geometry_msgs.msg import TwistStamped, PoseStamped
00012 
00013 from hrl_ellipsoidal_control.controller_base import CartesianStepController
00014 from pykdl_utils.joint_kinematics import create_joint_kin
00015 from hrl_geom.pose_converter import PoseConv
00016 from hrl_pr2_arms.pr2_arm_jt_task import create_ep_arm, PR2ArmJTransposeTask
00017 from hrl_pr2_arms.pr2_controller_switcher import ControllerSwitcher
00018 from face_adls_manager import async_call
00019 from hrl_face_adls.srv import EnableCartController, EnableCartControllerResponse
00020 
00021 VELOCITY = 0.03
00022 _, FLIP_PERSPECTIVE_ROT = PoseConv.to_pos_rot([0]*3, [0, 0, np.pi])
00023 
00024 class CartesianControllerManager(object):
00025     def __init__(self, arm_char):
00026         self.arm_char = arm_char
00027         self.arm = None
00028         self.kin = None
00029         self.cart_ctrl = CartesianStepController()
00030         self.ctrl_switcher = ControllerSwitcher()
00031         self.command_move_sub = rospy.Subscriber("/face_adls/%s_cart_move" % arm_char, TwistStamped, 
00032                                                  async_call(self.command_move_cb))
00033         self.command_absolute_sub = rospy.Subscriber("/face_adls/%s_cart_absolute" % arm_char, 
00034                                                      PoseStamped, async_call(self.command_absolute_cb))
00035         self.adjust_posture_sub = rospy.Subscriber("/face_adls/%s_cart_posture" % arm_char, 
00036                                                    Float32, self.adjust_posture_cb)
00037         def enable_controller_cb(req):
00038             if req.enable:
00039                 _, frame_rot = PoseConv.to_pos_rot([0]*3, 
00040                                             [req.frame_rot.x, req.frame_rot.y, req.frame_rot.z])
00041                 if req.velocity == 0:
00042                     req.velocity = 0.03
00043                 success = self.enable_controller(req.end_link, req.ctrl_params, req.ctrl_name,
00044                                                  frame_rot, velocity=req.velocity)
00045             else:
00046                 success = self.disable_controller()
00047             return EnableCartControllerResponse(success)
00048         self.controller_enabled_pub = rospy.Publisher('/face_adls/%s_cart_ctrl_enabled' % arm_char, 
00049                                                       Bool, latch=True)
00050         self.enable_controller_srv = rospy.Service("/face_adls/%s_enable_cart_ctrl" % arm_char, 
00051                                                    EnableCartController, enable_controller_cb)
00052 
00053     def enable_controller(self, end_link="%s_gripper_tool_frame",
00054                           ctrl_params="$(find hrl_face_adls)/params/%s_jt_task_tool.yaml",
00055                           ctrl_name="%s_cart_jt_task_tool",
00056                           frame_rot=FLIP_PERSPECTIVE_ROT, velocity=0.03):
00057 #frame_rot=np.mat(np.eye(3))):
00058         rospy.loginfo("[cartesian_manager] Enabling %s controller with end link %s" %
00059                       (ctrl_name, end_link))
00060 
00061         try:
00062             if '%s' in end_link:
00063                 end_link = end_link % self.arm_char
00064             if '%s' in ctrl_params:
00065                 ctrl_params = ctrl_params % self.arm_char
00066             if '%s' in ctrl_name:
00067                 ctrl_name = ctrl_name % self.arm_char
00068             self.ctrl_switcher.carefree_switch(self.arm_char, ctrl_name, ctrl_params, reset=False)
00069             rospy.sleep(0.2)
00070             cart_arm = create_ep_arm(self.arm_char, PR2ArmJTransposeTask, 
00071                                       controller_name=ctrl_name, 
00072                                       end_link=end_link, timeout=5)
00073             self.cart_ctrl.set_arm(cart_arm)
00074             self.arm = cart_arm
00075             rospy.sleep(0.1)
00076             self.cur_posture_angle = self.arm.get_joint_angles()[2]
00077             self.adjust_posture_cb(Float32(0.))
00078             self.frame_rot = frame_rot
00079             self.velocity = velocity
00080             self.controller_enabled_pub.publish(True)
00081         except Exception as e:
00082             print e
00083             return False
00084         rospy.loginfo("[cartesian_manager] Successfully enabled %s controller with end link %s" %
00085                       (ctrl_name, end_link))
00086         return True
00087 
00088     def disable_controller(self):
00089         rospy.loginfo("[cartesian_manager] Disabling cartesian controller.")
00090         self.cart_ctrl.set_arm(None)
00091         self.arm = None
00092         self.controller_enabled_pub.publish(False)
00093         return True
00094 
00095     def command_move_cb(self, msg):
00096         if self.arm is None:
00097             rospy.logwarn("[cartesian_manager] Cartesian controller not enabled.")
00098             return
00099         self.cart_ctrl.stop_moving(wait=True)
00100         if msg.header.frame_id == "":
00101             msg.header.frame_id = "torso_lift_link"
00102         if self.kin is None or msg.header.frame_id not in self.kin.get_link_names():
00103             self.kin = create_joint_kin("torso_lift_link", msg.header.frame_id)
00104         torso_pos_ep, torso_rot_ep = PoseConv.to_pos_rot(self.arm.get_ep())
00105         torso_B_ref = self.kin.forward(base_link="torso_lift_link", 
00106                                        end_link=msg.header.frame_id)
00107         _, torso_rot_ref = PoseConv.to_pos_rot(torso_B_ref)
00108         torso_rot_ref *= self.frame_rot
00109         ref_pos_off, ref_rot_off = PoseConv.to_pos_rot(msg)
00110         change_pos = torso_rot_ep.T * torso_rot_ref * ref_pos_off
00111         change_pos_xyz = change_pos.T.A[0]
00112         ep_rot_ref = torso_rot_ep.T * torso_rot_ref
00113         change_rot = ep_rot_ref * ref_rot_off * ep_rot_ref.T
00114         _, change_rot_rpy = PoseConv.to_pos_euler(np.mat([0]*3).T, change_rot)
00115         rospy.loginfo("[cartesian_manager] Command relative movement." + 
00116                       str((change_pos_xyz, change_rot_rpy)))
00117         self.cart_ctrl.execute_cart_move((change_pos_xyz, change_rot_rpy), ((0, 0, 0), 0), 
00118                                          velocity=self.velocity, blocking=True)
00119 
00120     def command_absolute_cb(self, msg):
00121         if self.arm is None:
00122             rospy.logwarn("[cartesian_manager] Cartesian controller not enabled.")
00123             return
00124         self.cart_ctrl.stop_moving(wait=True)
00125         if msg.header.frame_id == "":
00126             msg.header.frame_id = "torso_lift_link"
00127         if self.kin is None or msg.header.frame_id not in self.kin.get_link_names():
00128             self.kin = create_joint_kin("torso_lift_link", msg.header.frame_id)
00129         torso_pos_ep, torso_rot_ep = self.arm.get_ep()
00130         torso_B_ref = self.kin.forward(base_link="torso_lift_link", 
00131                                        end_link=msg.header.frame_id)
00132         ref_B_goal = PoseConv.to_homo_mat(msg)
00133         torso_B_goal = torso_B_ref * ref_B_goal
00134 
00135         change_pos, change_rot = PoseConv.to_pos_rot(torso_B_goal)
00136         change_pos_xyz = change_pos.T.A[0]
00137         rospy.loginfo("[cartesian_manager] Command absolute movement." + 
00138                       str((change_pos_xyz, change_rot)))
00139         self.cart_ctrl.execute_cart_move((change_pos_xyz, change_rot), ((1, 1, 1), 1), 
00140                                          velocity=self.velocity, blocking=True)
00141 
00142     def adjust_posture_cb(self, msg):
00143         rospy.loginfo("[cartesian_manager] Adjusting posture")
00144         new_posture = [None] * 7
00145         self.cur_posture_angle += msg.data
00146         new_posture[2] = self.cur_posture_angle
00147         self.arm.set_posture(new_posture)
00148 
00149 def main():
00150     rospy.init_node("cartesian_manager")
00151     arm_char = sys.argv[1]
00152     assert arm_char in ['r', 'l']
00153     cart_man = CartesianControllerManager(arm_char)
00154     if False:
00155         cart_man.enable_controller(
00156             end_link="%s_gripper_tool_frame" % arm_char, 
00157             ctrl_name="%s_cart_jt_task_tool" % arm_char,
00158             ctrl_params="$(find hrl_face_adls)/params/%s_jt_task_tool.yaml" % arm_char,
00159             frame_rot=FLIP_PERSPECTIVE_ROT)
00160         rospy.sleep(1)
00161         #t = PoseConv.to_twist_stamped_msg("l_gripper_tool_frame", (-0.15, 0, 0), (0, 0, 0))
00162         #t = PoseConv.to_twist_stamped_msg("torso_lift_link", (0, 0, 0), (-np.pi/6, 0, 0))
00163         t = PoseConv.to_twist_stamped_msg("torso_lift_link", (0, 0.1, 0), (0, 0, 0))
00164         cart_man.command_move_cb(t)
00165         return
00166 
00167     rospy.spin()
00168 
00169 if __name__ == "__main__":
00170     main()


hrl_face_adls
Author(s): Kelsey Hawkins, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab
autogenerated on Wed Nov 27 2013 11:47:39