geometric_based_probing_garnics_demo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; 
00004 roslib.load_manifest('zyonz_apps')
00005 import rospy
00006 import smach
00007 import smach_ros
00008 import tf
00009 from pprint import pprint
00010 import copy
00011 
00012 from smach import StateMachine, CBState, Iterator
00013 from smach_ros import ServiceState
00014 from actionlib import *
00015 from actionlib.msg import *
00016 from std_msgs.msg import String, Int8, Int32
00017 from sensor_msgs.msg import PointCloud2, JointState
00018 from geometry_msgs.msg import Transform, PoseStamped, Pose
00019 
00020 from iri_common_smach.st_get_pcl import GetPCL
00021 from zyonz_msgs.msg              import chlorophyll_sampleAction, chlorophyll_sampleGoal
00022 from zyonz_msgs.srv              import GetProbingSteps
00023 from zyonz_apps_base.probing     import ZyonzProbingSMFactory
00024 from zyonz_apps_base.move        import SM_ZYONZ_GoHome
00025 
00026 class FindProbingPoint(smach.State):
00027     def __init__(self):
00028         smach.State.__init__(self, outcomes    = ['success','fail'],
00029                                    input_keys  = ['pcl'],
00030                                    output_keys = ['probing_steps'])
00031     def execute(self, userdata):
00032         handler = rospy.ServiceProxy('/zyonz/skills/geometric_based_leaf_probing/leaf_probing_srv', GetProbingSteps)
00033 
00034         try:
00035             response = handler(userdata.pcl)
00036         except rospy.ServiceException, e:
00037             return 'fail'
00038 
00039         if (not response):
00040             return 'fail'
00041 
00042         pub = rospy.Publisher('/debug/probing/grasp', PoseStamped, None, False, True)
00043         pub.publish(response.steps[0].probing)
00044         pprint(response.steps[0].probing)
00045 
00046         pre_pose_st = PoseStamped()
00047         pre_pose_st.header = response.steps[0].pre_probing.header
00048         pre_pose_st.pose   = response.steps[0].pre_probing.poses[0]
00049         pub = rospy.Publisher('/debug/probing/pre_grasp', PoseStamped, None, False, True)
00050         pub.publish(pre_pose_st)
00051         pprint(pre_pose_st)
00052 
00053         userdata.probing_steps = response
00054 
00055         return 'success'
00056 
00057 class TransformProbingPose(smach.State):
00058     def __init__(self):
00059         smach.State.__init__(self, outcomes    = ['success','fail'],
00060                                    input_keys  = ['pose_st','pre_pose_st'],
00061                                    output_keys = ['joints_to_position','pre_joints'])
00062 
00063     def execute(self, userdata):
00064         try: 
00065           handler = rospy.ServiceProxy('/zyonz/iri_wam_spad_ik/get_wam_ik', 
00066                                      wamInverseKinematics)
00067           response = handler(userdata.pose_st)
00068 
00069           if (response):
00070               userdata.joints_to_position = response.joints
00071 
00072           response = handler(userdata.pre_pose_st)
00073 
00074           if (response):
00075               userdata.pre_joints = response.joints
00076 
00077               return 'success'
00078         except rospy.ServiceException, e:
00079               return 'fail'
00080 
00081         return 'fail'
00082 
00083 
00084 class MoveApproachingPose(smach.State):
00085     def __init__(self):
00086         smach.State.__init__(self, outcomes    = ['success','fail'],
00087                                    input_keys  = ['pre_joints'])
00088 
00089     def execute(self, userdata):
00090         return wam_joints_move(userdata.pre_joints)
00091 
00092 
00093 class MovePosition(smach.State):
00094     def __init__(self):
00095         smach.State.__init__(self, outcomes    = ['success','fail'],
00096                                    input_keys  = ['joints_to_position'])
00097 
00098     def execute(self, userdata):
00099         return wam_joints_move(userdata.joints_to_position)
00100 
00101 def construct_main_sm():
00102     sm = StateMachine(outcomes = ['succeeded','aborted'])
00103     f_probing  = ZyonzProbingSMFactory()
00104     sm_probing = f_probing.build_sm()
00105     f_move     = SM_ZYONZ_GoHome()
00106     sm_home    = f_move.build_sm()
00107 
00108     with sm:
00109         smach.StateMachine.add('GO_HOME', sm_home,
00110                           transitions={'success' : 'CAPTURE_PCL',
00111                                        'aborted' : 'aborted'})
00112 
00113         smach.StateMachine.add('CAPTURE_PCL',
00114                          GetPCL("/zyonz/euclidean_cluster/output"),
00115                             transitions = {'success' : 'FIND_PROBING_POINT',
00116                                            'fail'    : 'aborted'},
00117                             remapping   = {'pcl_RGB'  : 'sm_pcl'})
00118 
00119         smach.StateMachine.add('FIND_PROBING_POINT', FindProbingPoint(),
00120                 transitions = {'success' : 'PROBING_ACTION',
00121                                'fail'    : 'FIND_PROBING_POINT'},
00122                 remapping   = { 'pcl'           : 'sm_pcl' ,
00123                                 'probing_steps' : 'sm_probing_msg' })
00124 
00125         smach.StateMachine.add('PROBING_ACTION', sm_probing,
00126                           transitions  = {'succeeded'             : 'GO_END',
00127                                           'no_kinematic_solution' : 'aborted',
00128                                           'aborted'               : 'aborted'},
00129                           remapping    = {'probing_steps' : 'sm_probing_msg'})
00130 
00131         smach.StateMachine.add('GO_END', sm_home,
00132                           transitions={'success'   : 'succeeded',
00133                                        'aborted'   : 'aborted'})
00134 
00135     return sm
00136 
00137 def main():
00138     rospy.init_node("base_probing_sm")
00139     sm_main = construct_main_sm()
00140 
00141     # Run state machine introspection server for smach viewer
00142     sis = smach_ros.IntrospectionServer('zyonz_smach_base_probing', sm_main,
00143                                         '/zyonz_geometric_based_leaf_probing')
00144     sis.start()
00145 
00146     outcome = sm_main.execute()
00147 
00148     rospy.spin()
00149     sis.stop()
00150 
00151 if __name__ == "__main__":
00152     main()


zyonz_apps
Author(s): sfoix
autogenerated on Fri Dec 6 2013 23:27:41