move_arm_states.py
Go to the documentation of this file.
00001 # ROS imports
00002 import roslib
00003 roslib.load_manifest('srs_states')
00004 import rospy
00005 import smach
00006 
00007 from simple_script_server import *
00008 sss = simple_script_server()
00009 
00010 import tf
00011 from tf.transformations import *
00012 from kinematics_msgs.srv import *
00013 from cob_kinematics.srv import *
00014 from arm_navigation_msgs.srv import *
00015 
00016 TIP_LINK='sdh_palm_link'
00017 
00018 def parse_cartesian_param(param, now = None):
00019     if now is None:
00020         now = rospy.Time.now()
00021     ps = PoseStamped()
00022     ps.pose.orientation.w = 1.0
00023     ps.header.stamp = now
00024     if type(param) is not PoseStamped and param is not None:
00025         ps.header.frame_id = param[0]
00026         if len(param) > 1:
00027             ps.pose.position.x,ps.pose.position.y,ps.pose.position.z = param[1]
00028         if len(param) > 2:
00029             ps.pose.orientation.x,ps.pose.orientation.y,ps.pose.orientation.z,ps.pose.orientation.w = quaternion_from_euler(*param[2])
00030     else:
00031         ps = param
00032     return ps
00033     
00034 def parse_cartesian_parameters(arm_name, parameters):
00035     now = rospy.Time.now()
00036     
00037     # parse pose_target
00038     param = parameters
00039     second_param = None
00040     if type(parameters) is list and len(parameters) > 0:
00041         if type(parameters[0]) is not str:
00042             param = parameters[0]
00043             if len(parameters) > 1:
00044                 second_param = parameters[1]
00045 
00046     pose_target = parse_cartesian_param(param, now)
00047 
00048     # parse pose_origin
00049     param = second_param
00050     ps = PoseStamped()
00051     ps.pose.orientation.w = 1.0
00052     ps.header.stamp = pose_target.header.stamp
00053     ps.header.frame_id = rospy.get_param("/cob_arm_kinematics/"+arm_name+"/tip_name")
00054     if type(param) is not PoseStamped:
00055          if param is not None and len(param) >=1:
00056             ps.header.frame_id = param[0]
00057             if len(param) > 1:
00058                 ps.pose.position.x,ps.pose.position.y,ps.pose.position.z = param[1]
00059             if len(param) > 2:
00060                 ps.pose.orientation.x,ps.pose.orientation.y,ps.pose.orientation.z,ps.pose.orientation.w = quaternion_from_euler(*param[2])
00061     else:
00062         ps = param
00063     return pose_target,ps
00064     
00065 def get_joint_goal(arm_name, target):
00066     SetPlanningSceneDiffService = rospy.ServiceProxy('/environment_server/set_planning_scene_diff', SetPlanningSceneDiff)
00067     planning_scene_request = SetPlanningSceneDiffRequest()
00068     planning_scene_response = SetPlanningSceneDiffService(planning_scene_request)
00069     ps_target, ps_origin = parse_cartesian_parameters(arm_name, target)
00070     iks = rospy.ServiceProxy("/cob_ik_wrapper/arm/get_ik_extended", GetPositionIKExtended)
00071     req = GetPositionIKExtendedRequest()
00072     req.ik_pose = ps_origin.pose
00073     req.constraint_aware = True
00074     req.timeout = rospy.Duration(5.0)
00075     req.ik_request.ik_link_name = ps_origin.header.frame_id
00076     req.ik_request.pose_stamped = ps_target
00077     req.ik_request.ik_seed_state.joint_state.name = ["arm_%d_joint" % (d+1) for d in range(7)]
00078     req.ik_request.ik_seed_state.joint_state.position = [0.0]*7
00079     res = iks(req)
00080     return res.solution.joint_state, res.error_code
00081 
00082 # base class for all move arm states
00083 class _move_arm_base(smach.State):
00084     def __init__(self, additional_input_keys = []):
00085         smach.State.__init__(
00086             self,
00087             outcomes=['succeeded', 'not_completed','failed', 'preempted'],
00088             output_keys=['pose_id'],
00089             input_keys=['poses'] + additional_input_keys)
00090         
00091     def moveArm(self,userdata,poses,mode):
00092         ret  = 'not_completed'
00093         userdata.pose_id = None
00094         
00095         for pose_id in range(len(poses)): # for each pose
00096         
00097             if self.preempt_requested(): # return if preempt requested
00098                 return 'preempted'
00099 
00100             for i in range(5):
00101                 js, err = get_joint_goal('arm',[poses[pose_id],[TIP_LINK]])
00102                 if err.val == err.SUCCESS:
00103                     break
00104 
00105             if err.val != err.SUCCESS:
00106                 print err.val
00107                 continue            
00108 
00109             # start movement
00110             handle_arm= sss.move('arm',[list(js.position)],False, mode=mode)
00111 
00112             if handle_arm is not None:
00113                 # wait while movement
00114                 r = rospy.Rate(10)
00115                 preempted = False
00116                 arm_state = -1
00117                 while True:
00118                         preempted = self.preempt_requested()
00119                         arm_state = handle_arm.get_state()
00120                         if preempted or ( arm_state == 3) or (arm_state == 4):
00121                             break # stop waiting  
00122                         r.sleep()
00123                         
00124                 # stop arm in any case
00125                 sss.stop("arm")
00126                 if preempted:
00127                     handle_arm.cancel()
00128                     ret = 'preempted'
00129                 elif arm_state == 3:
00130                     ret = 'succeeded'
00131                     userdata.pose_id = pose_id # keep pose_id for subsequent state
00132                 else:
00133                     ret = 'not_completed'
00134                 
00135                 break
00136             else:
00137                 ret = 'failed'
00138                 
00139         return ret
00140 
00141 
00142 # move arm state with flag for planning
00143 class move_arm(_move_arm_base):
00144     def __init__(self):
00145         _move_arm_base.__init__(self,['mode'])
00146     def execute(self, userdata):
00147         return self.moveArm(userdata,userdata.poses,userdata.mode)
00148         
00149 # move arm state with planning
00150 class move_arm_planned(_move_arm_base):
00151     def __init__(self):
00152         _move_arm_base.__init__(self)
00153     def execute(self, userdata):
00154         return self.moveArm(userdata, userdata.poses,'planned')
00155 # move arm stat without planning
00156 class move_arm_unplanned(_move_arm_base):
00157     def __init__(self):
00158         _move_arm_base.__init__(self)
00159     def execute(self, userdata):
00160         return self.moveArm(userdata, userdata.poses,'unplanned')


srs_states
Author(s): Georg Arbeiter
autogenerated on Mon Oct 6 2014 08:34:06