$search
00001 import roslib; roslib.load_manifest("pr2_dremel_server") 00002 00003 import pr2_controllers_msgs.msg 00004 from pr2_controller_manager import pr2_controller_manager_interface 00005 from std_msgs.msg import * 00006 00007 00008 import pdb 00009 00010 import actionlib 00011 import sensor_msgs.msg 00012 import kinematics_msgs.msg 00013 import geometry_msgs 00014 import trajectory_msgs 00015 import rospy 00016 00017 arm_seeds = (-0.135, -0.183, -3.202, -1.598, 3.161, -1.318, -3.053) 00018 arm_seeds_without_shoulder = (-0.135, -3.202, -1.598, 3.161, -1.318, -3.053) 00019 00020 r_arm_joints = ['r_shoulder_pan_joint', 'r_shoulder_lift_joint', 'r_upper_arm_roll_joint', 'r_elbow_flex_joint', 'r_forearm_roll_joint', 'r_wrist_flex_joint', 'r_wrist_roll_joint'] 00021 r_arm_joints_without_shoulder = ['r_shoulder_pan_joint', 'r_elbow_flex_joint', 'r_upper_arm_roll_joint', 'r_forearm_roll_joint', 'r_wrist_flex_joint', 'r_wrist_roll_joint'] 00022 00023 class JointForceController: 00024 def __init__(self, joint_name): 00025 self.controller_name = joint_name + "_joint_force_controller" 00026 00027 rospy.set_param(self.controller_name + '/type', 'pr2_plotter/JointForceController') 00028 rospy.set_param(self.controller_name + '/joint', joint_name) 00029 rospy.set_param(self.controller_name + '/max_velocity', 0.2) 00030 00031 rospy.set_param(self.controller_name + '/position/pid', rospy.get_param("r_arm_controller/gains/r_shoulder_lift_joint") ) 00032 rospy.set_param(self.controller_name + '/velocity/pid', rospy.get_param("r_arm_controller/gains/r_shoulder_lift_joint") ) 00033 rospy.set_param(self.controller_name + '/velocity/pid/p', 5) 00034 rospy.set_param(self.controller_name + '/velocity/pid/i', 20) 00035 rospy.set_param(self.controller_name + '/velocity/pid/d', 0) 00036 rospy.set_param(self.controller_name + '/velocity/pid/i_clamp', 60) 00037 00038 pr2_controller_manager_interface.load_controller(self.controller_name) 00039 00040 self.force_pub = rospy.Publisher("%s/force" % self.controller_name, Float64) 00041 self.position_pub = rospy.Publisher("%s/position" % self.controller_name, Float64) 00042 self.running = False 00043 00044 def __del__(self): 00045 self.shutdown() 00046 00047 def shutdown(self): 00048 self.stop() 00049 self.load() 00050 pr2_controller_manager_interface.unload_controller(self.controller_name) 00051 00052 def stop(self): 00053 pr2_controller_manager_interface.stop_controller(self.controller_name) 00054 self.running = False 00055 00056 def load(self): 00057 pr2_controller_manager_interface.load_controller(self.controller_name) 00058 00059 def unload(self): 00060 pr2_controller_manager_interface.unload_controller(self.controller_name) 00061 00062 def start(self): 00063 pr2_controller_manager_interface.start_controller(self.controller_name) 00064 self.running = True 00065 00066 def set_position_pid(self, p, i, d): 00067 rospy.set_param(self.controller_name + '/position/pid/p', p ) 00068 rospy.set_param(self.controller_name + '/position/pid/i', i ) 00069 rospy.set_param(self.controller_name + '/position/pid/d', d ) 00070 00071 def set_velocity_pid(self, p, i, d): 00072 rospy.set_param(self.controller_name + '/velocity/pid/p', p ) 00073 rospy.set_param(self.controller_name + '/velocity/pid/i', i ) 00074 rospy.set_param(self.controller_name + '/velocity/pid/d', d ) 00075 00076 def force(self, force): 00077 if not self.running: 00078 self.start() 00079 self.force_pub.publish(Float64(force)) 00080 00081 def position(self, position): 00082 if not self.running: 00083 self.start() 00084 self.position_pub.publish(Float64(position)) 00085 00086 class ArmPositionController: 00087 def __init__(self, controller_name): 00088 self.controller_name = controller_name 00089 self.running = False 00090 00091 def stop(self): 00092 pr2_controller_manager_interface.stop_controller(self.controller_name) 00093 self.running = False 00094 00095 def start(self): 00096 pr2_controller_manager_interface.start_controller(self.controller_name) 00097 self.running = True 00098 00099 def load(self): 00100 pr2_controller_manager_interface.load_controller(self.controller_name) 00101 00102 class Arm: 00103 def __init__(self, controller, ikPath): 00104 self.controller = controller 00105 self.trajClient = actionlib.SimpleActionClient(controller+"/joint_trajectory_action", pr2_controllers_msgs.msg.JointTrajectoryAction) 00106 00107 self.joint_names = self.getJointNames() 00108 self.cAngles = self.getJointAngles() 00109 00110 self.ikPath = ikPath 00111 self._runIK = rospy.ServiceProxy(ikPath + "/get_ik", kinematics_msgs.srv.GetPositionIK) 00112 00113 # self._getIKInfo = rospy.ServiceProxy(ikPath+ "/get_ik_solver_info", kinematics_msgs.msg.KinematicSolverInfo) 00114 00115 def sendTraj(self, traj, wait = True): 00116 """ Send the joint trajectory goal to the action server""" 00117 if wait: 00118 self.trajClient.send_goal_and_wait(traj) 00119 else: 00120 self.trajClient.send_goal(traj) 00121 00122 def getState(self): 00123 """Returns the current state of action client 00124 """ 00125 return self.trajClient.get_state() 00126 00127 def getJointNames(self): 00128 """ Contacts param server to get controller joints """ 00129 return rospy.get_param(self.controller + "/joints"); 00130 00131 def getJointAngles(self): 00132 msg = rospy.wait_for_message("/joint_states", sensor_msgs.msg.JointState) 00133 angles = [] 00134 for name in self.joint_names: 00135 angles.append( msg.position[msg.name.index(name)]) 00136 return angles 00137 00138 def getArmSeedAngles(self): 00139 if 'r_shoulder_lift_joint' in self.joint_names: 00140 return arm_seeds 00141 else: 00142 return arm_seeds_without_shoulder 00143 00144 return angles 00145 def getIK(self, goalPose, link_name, seedAngles): 00146 """Calls inverse kinematics service for the arm. 00147 goalPose - Pose Stamped message showing desired position of link in coord system 00148 linkName - goal Pose target link name - (r_wrist_roll_link or l_wrist_roll_link) 00149 startAngles - seed angles for IK 00150 """ 00151 ikreq = kinematics_msgs.msg.PositionIKRequest() 00152 ikreq.ik_link_name =link_name 00153 ikreq.pose_stamped = goalPose 00154 ikreq.ik_seed_state.joint_state.name = self.joint_names 00155 ikreq.ik_seed_state.joint_state.position = seedAngles 00156 ikres = self._runIK(ikreq, rospy.Duration(5)) 00157 if (ikres.error_code.val != ikres.error_code.SUCCESS): #figure out which error code 00158 raise rospy.exceptions.ROSException("Could not find IK with " + self.ikPath + "\n\n" + ikres.__str__()) 00159 return ikres 00160 00161 def goToAngle(self, angles, time, wait = True): 00162 if (len(angles) != len(self.joint_names)): 00163 raise Exception("Wrong number of Angles. "+ len(angles) + "given. "+ len(self.joint_names) + "needed.") 00164 00165 trajMsg = pr2_controllers_msgs.msg.JointTrajectoryGoal() 00166 00167 trajPoint = trajectory_msgs.msg.JointTrajectoryPoint() 00168 trajPoint.positions= angles 00169 trajPoint.velocities = [0 for i in range(0, len(self.joint_names))] 00170 trajPoint.time_from_start = rospy.Duration(time) 00171 00172 trajMsg.trajectory.joint_names = self.joint_names 00173 trajMsg.trajectory.points.extend([trajPoint]) 00174 self.sendTraj(trajMsg, wait) 00175 00176 def goToPoint(self, point, frame_id = "torso_lift_link", wait = True): 00177 pose = makePose(point, [0,0,0,1], frame_id) 00178 ik = self.getIK(pose, "r_wrist_roll_link", self.getArmSeedAngles()) 00179 self.goToAngle(ik.solution.joint_state.position, wait) 00180 00181 00182 def goToPose(self, pos, orientation, frame_id, time, wait = True): 00183 """ This method uses getIK to move the x_wrist_roll_link to the 00184 specific point and orientation specified in frame_id's coordinates. 00185 Time specifies the duration of the planned trajectory, 00186 wait whether to block or not. 00187 """ 00188 pose = makePose(pos, orientation, frame_id) 00189 ik = self.getIK(pose, "r_wrist_roll_link", self.getArmSeedAngles()) 00190 self.goToAngle(ik.solution.joint_state.position, time, wait) 00191 00192 def makePose(xyz, orientation, frameID): 00193 """This is a shortcut method for making pose stamped messages 00194 """ 00195 pose = geometry_msgs.msg.PoseStamped() 00196 pose.pose.position.x, pose.pose.position.y, pose.pose.position.z = xyz[0], xyz[1], xyz[2] 00197 pose.pose.orientation.x = orientation[0] 00198 pose.pose.orientation.y = orientation[1] 00199 pose.pose.orientation.z = orientation[2] 00200 pose.pose.orientation.w = orientation[3] 00201 pose.header.frame_id = frameID 00202 pose.header.stamp = rospy.Time.now() 00203 return pose 00204 00205