00001
00002
00003 import numpy as np
00004 from collections import deque
00005 from threading import Lock
00006
00007 import roslib
00008 roslib.load_manifest("hrl_pr2_arms")
00009 import tf.transformations as tf_trans
00010
00011 import rospy
00012 from hrl_pr2_arms.pr2_controller_switcher import ControllerSwitcher
00013 from hrl_pr2_arms.pr2_arm import create_pr2_arm, PR2ArmJointTrajectory, PR2ArmCartesianPostureBase
00014 from hrl_generic_arms.controllers import PIDController
00015
00016 rate = 100.
00017
00018
00019 kp_proportional = 0.084
00020 kp_integral = 0.35
00021 kp_init_integral = 0.005
00022 kp_constant = 0.002
00023 kp_max_ctrl = 0.30
00024
00025 kr_proportional = 0.160
00026 kr_integral = 0.200
00027 kr_init_integral = 0.05
00028 kr_constant = 0.020
00029 kr_max_ctrl = 0.40
00030
00031 class PR2ArmCartVelocityController(object):
00032 def __init__(self, arm, velocity_rot=None):
00033 self.arm = arm
00034 if velocity_rot is None:
00035 self.velocity_rot = np.mat(np.eye(3))
00036 else:
00037 self.velocity_rot = velocity_rot
00038 self._is_moving = False
00039 self.lock = Lock()
00040
00041 def update_velocity_rot(self, velocity_rot):
00042 with self.lock:
00043 self.velocity_rot = np.mat(velocity_rot)
00044
00045 def move_velocity(self, velocity=0.015, is_translation=True, velocity_rot=None):
00046 if velocity_rot is not None:
00047 self.update_velocity_rot(velocity_rot)
00048 pos_i_des, rot_i_des = self.arm.get_ep()
00049 pos_i_act, rot_i_act = self.arm.get_end_effector_pose()
00050
00051
00052 pos_err = pos_i_act - pos_i_des
00053 rot_err = rot_i_act.T * rot_i_des
00054
00055 if is_translation:
00056 pos_vel_des = velocity
00057 pid_ctrl = PIDController(rate=rate, k_p=kp_proportional, k_i=kp_integral,
00058 i_max=None, init_integ=np.sign(pos_vel_des) * kp_init_integral,
00059 saturation=kp_max_ctrl,
00060 feed_forward=np.sign(pos_vel_des) * kp_constant,
00061 name="arm_vel")
00062 else:
00063 rot_vel_des = velocity
00064 pid_ctrl = PIDController(rate=rate, k_p=kr_proportional, k_i=kr_integral,
00065 i_max=None, init_integ=np.sign(rot_vel_des) * kr_init_integral,
00066 saturation=kr_max_ctrl,
00067 feed_forward=np.sign(rot_vel_des) * kr_constant,
00068 name="arm_vel")
00069 vels = deque([np.array([0]*6)]*40)
00070 r = rospy.Rate(rate)
00071 self._is_moving = True
00072 while not rospy.is_shutdown() and self._is_moving:
00073 with self.lock:
00074 vel_rot = self.velocity_rot.copy()
00075 cur_pos, cur_rot = self.arm.get_end_effector_pose()
00076
00077
00078 xd_act = self.arm.get_controller_state()['xd_act']
00079 vels.append(xd_act)
00080 vels.popleft()
00081 vel_filt = np.mat(np.mean(vels, 0)).T
00082 x_vel_filt = (vel_rot.T * vel_filt[:3,0])[0,0]
00083 roll_vel_filt = (vel_rot.T * vel_filt[3:,0])[0,0]
00084
00085 if is_translation:
00086
00087 pos_ctrl = pid_ctrl.update_state(pos_vel_des - x_vel_filt)
00088 pos_des = vel_rot * (np.mat([pos_ctrl, 0, 0]).T +
00089 np.mat(np.diag([1, 0, 0])) * vel_rot.T * (cur_pos - pos_err) +
00090 np.mat(np.diag([0, 1, 1])) * vel_rot.T * pos_i_des)
00091
00092 rot_des = rot_i_des
00093
00094 if not is_translation:
00095 rot_des_vel_frame = np.mat(np.eye(4))
00096 rot_des_vel_frame[:3,:3] = cur_rot * rot_err * vel_rot
00097 roll_des_vel_frame, r2, r3 = tf_trans.euler_from_matrix(rot_des_vel_frame)
00098
00099
00100 rot_ctrl = pid_ctrl.update_state(rot_vel_des + roll_vel_filt)
00101 print roll_vel_filt, rot_vel_des, rot_vel_des - roll_vel_filt
00102
00103 roll_ctrl_des = roll_des_vel_frame + rot_ctrl
00104 r1, pitch_i_des, yaw_i_des = tf_trans.euler_from_matrix(rot_i_des * vel_rot)
00105 rot_des = np.mat(tf_trans.euler_matrix(roll_ctrl_des, pitch_i_des, yaw_i_des)[:3,:3]) * vel_rot.T
00106
00107 pos_des = pos_i_des
00108
00109 self.arm.set_ep((pos_des, rot_des), 1)
00110 r.sleep()
00111 self.arm.set_ep(self.arm.get_ep(), 1)
00112
00113 def stop_moving(self):
00114 self._is_moving = False
00115
00116 def main():
00117 rospy.init_node("arm_cart_vel_control")
00118 if True:
00119 ctrl_switcher = ControllerSwitcher()
00120 ctrl_switcher.carefree_switch('r', '%s_arm_controller',
00121 '$(find hrl_pr2_arms)/params/joint_traj_params_electric.yaml')
00122 rospy.sleep(0.5)
00123 ctrl_switcher.carefree_switch('r', '%s_joint_controller_low',
00124 '$(find hrl_pr2_arms)/params/joint_traj_params_electric_low.yaml')
00125 r_arm_js = create_pr2_arm('r', PR2ArmJointTrajectory, controller_name='%s_joint_controller_low')
00126 q = [-0.34781704, 0.27341079, -1.75392154, -2.08626393, -3.43756314, -1.82146607, -1.85187734]
00127 r_arm_js.set_ep(q, 3)
00128 rospy.sleep(6)
00129 ctrl_switcher.carefree_switch('r', '%s_cart_low_rfh',
00130 '$(find kelsey_sandbox)/params/j_transpose_low_rfh.yaml')
00131
00132 r_arm = create_pr2_arm('r', PR2ArmCartesianPostureBase)
00133 r_arm.set_posture()
00134 rospy.sleep(0.2)
00135 vel_ctrl = PR2ArmCartVelocityController(r_arm)
00136
00137
00138 vel_frame = tf_trans.euler_matrix(0, 0, np.pi/2)[:3,:3]
00139 vel_ctrl.move_velocity(velocity_rot=vel_frame, velocity=0.10, is_translation=False)
00140
00141
00142 if __name__ == "__main__":
00143 main()