pr2_arm_joint_traj.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 #
00003 # Class for interacting with a PR2 JointTrajectory realtime controller.
00004 #
00005 # Copyright (c) 2012, Georgia Tech Research Corporation
00006 # All rights reserved.
00007 # 
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions are met:
00010 #     * Redistributions of source code must retain the above copyright
00011 #       notice, this list of conditions and the following disclaimer.
00012 #     * Redistributions in binary form must reproduce the above copyright
00013 #       notice, this list of conditions and the following disclaimer in the
00014 #       documentation and/or other materials provided with the distribution.
00015 #     * Neither the name of the Georgia Tech Research Corporation nor the
00016 #       names of its contributors may be used to endorse or promote products
00017 #       derived from this software without specific prior written permission.
00018 # 
00019 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00020 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00025 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00026 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00027 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 #
00030 # Author: Kelsey Hawkins
00031 
00032 import numpy as np
00033 import copy
00034 
00035 import roslib
00036 roslib.load_manifest('hrl_pr2_arms')
00037 
00038 import rospy
00039 import actionlib
00040 from pr2_controllers_msgs.msg import JointTrajectoryAction, JointTrajectoryGoal 
00041 from pr2_controllers_msgs.msg import JointTrajectoryControllerState 
00042 from trajectory_msgs.msg import JointTrajectoryPoint
00043 
00044 from ep_arm_base import EPArmBase, create_ep_arm
00045 
00046 ##
00047 # Class for interacting with the JointTrajectory controller on the PR2.
00048 # Controller type: robot_mechanism_controllers/JointTrajectoryActionController
00049 # The equilibrium points are lists of joint angles.
00050 class PR2ArmJointTraj(EPArmBase):
00051     def __init__(self, arm_side, urdf, base_link='torso_lift_link', end_link='%s_gripper_tool_frame', 
00052                  controller_name='/%s_arm_controller', kdl_tree=None, timeout=1.):
00053         super(PR2ArmJointTraj, self).__init__(arm_side, urdf, base_link, end_link, 
00054                                               controller_name, kdl_tree, timeout)
00055         self.joint_action_client = actionlib.SimpleActionClient(
00056                                        self.controller_name + '/joint_trajectory_action',
00057                                        JointTrajectoryAction)
00058 
00059         self._state_sub = rospy.Subscriber(self.controller_name + '/state', 
00060                                            JointTrajectoryControllerState, self._ctrl_state_cb)
00061         if self.wait_for_joint_angles(0):
00062             if not self.wait_for_ep(timeout):
00063                 rospy.logwarn("[pr2_arm_joint_traj] Timed out waiting for EP.")
00064             elif not self.joint_action_client.wait_for_server(rospy.Duration(timeout)):
00065                 rospy.logwarn("[pr2_arm_joint_traj] JointTrajectoryAction action server timed out.")
00066 
00067     def _ctrl_state_cb(self, ctrl_state):
00068         self._save_ep(np.array(ctrl_state.desired.positions))
00069         with self.ctrl_state_lock:
00070             self.ctrl_state_dict["frame"] = ctrl_state.header.frame_id
00071             self.ctrl_state_dict["x_desi"] = np.array(ctrl_state.desired.positions)
00072             self.ctrl_state_dict["xd_desi"] = np.array(ctrl_state.desired.velocities)
00073             self.ctrl_state_dict["xdd_desi"] = np.array(ctrl_state.desired.accelerations)
00074             self.ctrl_state_dict["x_act"] = np.array(ctrl_state.actual.positions)
00075             self.ctrl_state_dict["xd_act"] = np.array(ctrl_state.actual.velocities)
00076             self.ctrl_state_dict["xdd_act"] = np.array(ctrl_state.actual.accelerations)
00077             self.ctrl_state_dict["x_err"] = np.array(ctrl_state.error.positions)
00078             self.ctrl_state_dict["xd_err"] = np.array(ctrl_state.error.velocities)
00079             self.ctrl_state_dict["xdd_err"] = np.array(ctrl_state.error.accelerations)
00080 
00081     ##
00082     # Returns the current equilibrium point
00083     # @return equilibrium point
00084     def get_ep(self, wrapped=False):
00085         with self.ep_lock:
00086             ret_ep = copy.copy(self.ep)
00087         if wrapped:
00088             return self.wrap_angles(ret_ep)
00089         else:
00090             return ret_ep
00091             
00092     ##
00093     # Commands joint angles to a single position
00094     # @param jep List of joint params to command the the arm to reach
00095     # @param duration Length of time command should take
00096     # @param delay Time to wait before starting joint command
00097     def set_ep(self, jep, duration, delay=0.0):
00098         jtg = JointTrajectoryGoal()
00099         jtg.trajectory.header.stamp = rospy.Time.now() + rospy.Duration(delay)
00100         jtg.trajectory.joint_names = self.get_joint_names()
00101         jtp = JointTrajectoryPoint()
00102         jtp.positions = list(jep)
00103         jtp.time_from_start = rospy.Duration(duration)
00104         jtg.trajectory.points.append(jtp)
00105         self.joint_action_client.send_goal(jtg)
00106 
00107     ##
00108     # Interpolates from one ep to the other using the trajectory specified by t.
00109     # @param ep_a The starting EP.
00110     # @param ep_b The ending EP.
00111     # @param t_vals List of values in [0, 1] representing where in the linear interpolation
00112     #               of ep_a to ep_b to place that point.
00113     # @return np.array of size len(t_vals)xlen(ep_*) trajectory.
00114     def interpolate_ep(self, ep_a, ep_b, t_vals):
00115         linspace_list = [[ep_a[i] + (ep_b[i] - ep_a[i]) * t for t in t_vals] 
00116                          for i in range(len(ep_a))]
00117         return np.dstack(linspace_list)[0]


hrl_pr2_arms
Author(s): Kelsey Hawkins, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab
autogenerated on Wed Nov 27 2013 11:41:30