r2_ready_pose.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('r2_control')
00004 import rospy
00005 import actionlib
00006 import math
00007 import random
00008 
00009 from control_msgs.msg import *
00010 from trajectory_msgs.msg import *
00011 from sensor_msgs.msg import JointState
00012 from copy import copy, deepcopy
00013 
00014 TORAD = math.pi/180.0
00015 TODEG = 1.0/TORAD
00016 
00017 class r2ReadyPose :
00018 
00019     def __init__(self, N, wp, arm):
00020 
00021         self.arm = arm
00022         self.currentData = None
00023         self.desiredData = None
00024         self.deadlineData = None
00025 
00026         self.currentState = JointState()
00027         self.currentState.position = [0]*N
00028         self.currentState.velocity = [0]*N
00029         self.currentState.effort = [0]*N
00030         self.numJoints = N
00031         self.waypoints = wp
00032 
00033         self.fingers = [("index",4),("middle",4),("ring",3),("little",3),("thumb",4)]
00034 
00035         rospy.Subscriber("r2/joint_states", JointState, self.jointStateCallback)
00036 
00037         if self.arm=="left" :
00038             self.trajPublisher = rospy.Publisher('/r2/l_arm_controller/command', JointTrajectory)
00039             self.trajClient = actionlib.SimpleActionClient('r2/l_arm_controller/follow_joint_trajectory', FollowJointTrajectoryAction)
00040         elif self.arm=="right" :
00041             self.trajPublisher = rospy.Publisher('/r2/r_arm_controller/command', JointTrajectory)
00042             self.trajClient = actionlib.SimpleActionClient('r2/r_arm_controller/follow_joint_trajectory', FollowJointTrajectoryAction)
00043         elif self.arm=="left_hand" :
00044             self.trajPublisher = rospy.Publisher('/r2/l_hand_controller/command', JointTrajectory)
00045             self.trajClient = actionlib.SimpleActionClient('r2/l_hand_controller/follow_joint_trajectory', FollowJointTrajectoryAction)
00046         elif self.arm=="right_hand" :
00047             self.trajPublisher = rospy.Publisher('/r2/r_hand_controller/command', JointTrajectory)
00048             self.trajClient = actionlib.SimpleActionClient('r2/r_hand_controller/follow_joint_trajectory', FollowJointTrajectoryAction)
00049         elif self.arm=="neck" :
00050             self.trajPublisher = rospy.Publisher('/r2/neck_controller/command', JointTrajectory)
00051             self.trajClient = actionlib.SimpleActionClient('r2/neck_controller/follow_joint_trajectory', FollowJointTrajectoryAction)
00052         else :
00053             rospy.logerr("r2ReadyPose::r2ReadyPose() -- unknown arm")
00054 
00055         self.trajClient.wait_for_server()
00056 
00057         self.actionGoal = FollowJointTrajectoryGoal()
00058 
00059 
00060     def getNumJoints(self) :
00061         return self.numJoints
00062 
00063     def jointStateCallback(self, data):
00064         self.currentState = data
00065 
00066     def computeTrajectory(self, desiredData, deadline):
00067 
00068         jointTraj = JointTrajectory()
00069         currentState = copy(self.currentState)
00070         desiredState = copy(desiredData)
00071 
00072         # create simple lists of both current and desired positions, based on provided desired names
00073         rospy.loginfo("r2ReadyPose::computeTrajectory() -- finding necessary joints")
00074         desiredPositions = []
00075         currentPositions = []
00076         for desIndex in range(len(desiredState.name)) :
00077             for curIndex in range(len(currentState.name)) :
00078                 if ( desiredState.name[desIndex] == currentState.name[curIndex] ) :
00079                     desiredPositions.append(desiredState.position[desIndex])
00080                     currentPositions.append(currentState.position[curIndex])
00081 
00082         rospy.loginfo("r2ReadyPose::computeTrajectory() -- creating trajectory")
00083         jointTraj.joint_names = desiredState.name
00084         jointTraj.points = list()
00085 
00086         for j in range(self.waypoints) :
00087             trajPoint = JointTrajectoryPoint()
00088 
00089             t = (deadline / self.waypoints) * (j + 1)
00090             trajPoint.time_from_start = rospy.Duration(t)
00091 
00092             trajPoint.positions = list()
00093             for i in range(len(desiredPositions)) :
00094                 trajPoint.positions.append( self.minJerk(currentPositions[i], desiredPositions[i], deadline, t) )
00095 
00096             jointTraj.points.append(trajPoint)
00097 
00098         rospy.loginfo("r2ReadyPose::moveToGoal() -- using tolerances")
00099 
00100         return jointTraj
00101 
00102 
00103     def minJerk(self, start, end, duration, t):
00104         tOverD = float(t) / float(duration)
00105         return start + (end - start)*( 10*math.pow(tOverD,3) - 15*math.pow(tOverD,4) + 6*math.pow(tOverD,5) )
00106 
00107     def moveToGoal(self, jointGoal, deadline, useTolerances) :
00108 
00109         self.actionGoal.trajectory = self.computeTrajectory(jointGoal, deadline)
00110 
00111         offset = 0
00112 
00113         if useTolerances :
00114             rospy.loginfo("r2ReadyPose::moveToGoal() -- using tolerances")
00115             self.actionGoal.path_tolerance = []
00116             self.actionGoal.goal_tolerance = []
00117 
00118             if self.arm == "left_hand" :
00119                 for k in range(len(self.fingers)):
00120                     for j in range(self.fingers[k][1]):
00121                         tol.name = "r2/left_arm/hand/" + self.fingers[k][0] + "/joint" + str(j+offset)
00122                         tol.position = 0.2
00123                         tol.velocity = 1
00124                         tol.acceleration = 10
00125                         self.actionGoal.path_tolerance.append(tol)
00126                         self.actionGoal.goal_tolerance.append(tol)
00127             elif self.arm == "right_hand" :
00128                 for k in range(len(self.fingers)):
00129                     for i in range(self.fingers[k][1]):
00130                         tol.name = "r2/right_arm/hand/" + self.fingers[k][0] + "/joint" + str(j+offset)
00131                         print tol.name
00132                         tol.position = 0.2
00133                         tol.velocity = 1
00134                         tol.acceleration = 10
00135                         self.actionGoal.path_tolerance.append(tol)
00136                         self.actionGoal.goal_tolerance.append(tol)
00137             else :
00138                 for i in range(self.numJoints):
00139                     tol = JointTolerance()
00140                     if self.arm == "left" or self.arm == "right" :
00141                         tol.name = "r2/" + self.arm + "_arm/joint" + str(i+offset)
00142                     elif self.arm == "neck" :
00143                         tol.name = "r2/" + self.arm + "/joint" + str(i+offset)
00144                     tol.position = 0.2
00145                     tol.velocity = 1
00146                     tol.acceleration = 10
00147 
00148                     self.actionGoal.path_tolerance.append(tol)
00149                     self.actionGoal.goal_tolerance.append(tol)
00150 
00151         else :
00152             rospy.loginfo("r2ReadyPose::moveToGoal() -- not using tolerances")
00153 
00154         self.actionGoal.goal_time_tolerance = rospy.Duration(10.0)
00155 
00156         # send goal nad monitor response
00157         self.trajClient.send_goal(self.actionGoal)
00158 
00159         rospy.loginfo("r2ReadyPose::moveToGoal() -- returned state: %s", str(self.trajClient.get_state()))
00160         rospy.loginfo("r2ReadyPose::moveToGoal() -- returned result: %s", str(self.trajClient.get_result()))
00161 
00162         return
00163 
00164     def formatJointStateMsg(self, j, offset) :
00165 
00166         if not (len(j) == self.numJoints) :
00167             rospy.logerr("r2ReadyPose::formatJointStateMsg() -- incorrectly sized joint message")
00168             return None
00169 
00170         js = JointState()
00171         js.header.seq = 0
00172         js.header.stamp = rospy.Time.now()
00173         js.header.frame_id = ""
00174         js.name = []
00175         js.position = []
00176 
00177 
00178         if self.arm == "left" or self.arm == "right" :
00179             for i in range(self.numJoints):
00180                 js.name.append("r2/" + self.arm + "_arm/joint" + str(i+offset))
00181                 js.position.append(j[i])
00182         if self.arm == "left_hand" :
00183             for k in range(len(self.fingers)):
00184                 for i in range(self.fingers[k][1]):
00185                     js.name.append("r2/left_arm/hand/" + self.fingers[k][0] + "/joint" + str(i+offset))
00186                     js.position.append(j[i])
00187         if self.arm == "right_hand" :
00188             for k in range(len(self.fingers)):
00189                 for i in range(self.fingers[k][1]):
00190                     js.name.append("r2/right_arm/hand/" + self.fingers[k][0] + "/joint" + str(i+offset))
00191                     js.position.append(j[i])
00192         elif self.arm == "neck" :
00193             for i in range(self.numJoints):
00194                 js.name.append("r2/" + self.arm + "/joint" + str(i+offset))
00195                 js.position.append(j[i])
00196 
00197         return js
00198 
00199 
00200 if __name__ == '__main__':
00201     rospy.init_node('r2_ready_pose')
00202     try:
00203         r2TrajectoryGeneratorLeft = r2ReadyPose(7, 500, "left")
00204         r2TrajectoryGeneratorRight = r2ReadyPose(7, 500, "right")
00205         r2TrajectoryGeneratorNeck = r2ReadyPose(3, 500, "neck")
00206         r2TrajectoryGeneratorLeftHand = r2ReadyPose(15, 10, "left_hand")
00207         r2TrajectoryGeneratorRightHand = r2ReadyPose(15, 10, "right_hand")
00208         rospy.sleep(2)
00209 
00210         lhrp = [0]*15
00211         rhrp = [0]*15
00212 
00213         lrp = [50.0*TORAD, -80.0*TORAD, -105.0*TORAD, -140.0*TORAD, 80.0*TORAD, 0.0*TORAD, 0.0*TORAD]
00214         rrp = [-50.0*TORAD, -80.0*TORAD, 105.0*TORAD, -140.0*TORAD, -80.0*TORAD, 0.0*TORAD, 0.0*TORAD]
00215         nrp = [-20.0*TORAD, 0.0*TORAD, -15.0*TORAD]
00216         print "r2ReadyPose() -- moving to ready pose"
00217 
00218         jointGoalLeft = r2TrajectoryGeneratorLeft.formatJointStateMsg(lrp, 0)
00219         jointGoalRight = r2TrajectoryGeneratorRight.formatJointStateMsg(rrp, 0)
00220         jointGoalNeck = r2TrajectoryGeneratorNeck.formatJointStateMsg(nrp, 0)
00221         jointGoalLeftHand = r2TrajectoryGeneratorLeftHand.formatJointStateMsg(lhrp, 0)
00222         jointGoalRightHand = r2TrajectoryGeneratorRightHand.formatJointStateMsg(rhrp, 0)
00223         r2TrajectoryGeneratorLeft.moveToGoal(jointGoalLeft, 0.5, False)
00224         r2TrajectoryGeneratorRight.moveToGoal(jointGoalRight, 0.5, False)
00225         r2TrajectoryGeneratorLeftHand.moveToGoal(jointGoalLeftHand, 0.1, False)
00226         r2TrajectoryGeneratorRightHand.moveToGoal(jointGoalRightHand, 0.1, False)
00227         r2TrajectoryGeneratorNeck.moveToGoal(jointGoalNeck, 0.5, False)
00228 
00229     except rospy.ROSInterruptException:
00230         pass
00231 
00232 
00233 
00234 


r2_control
Author(s):
autogenerated on Fri Aug 28 2015 11:43:38