object_manipulation_demo.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import smach
5 import smach_ros
6 
7 import std_msgs.msg as std_msg
8 import std_srvs.srv as std_srv
9 import geometry_msgs.msg as geometry_msg
10 
11 from actionlib import *
12 from actionlib_msgs.msg import *
14 
15 
16 class ObjDetectedCondition(smach.State):
17  '''Check for the object detection result to retry if no objects where detected'''
18  def __init__(self):
19  ''' '''
20  smach.State.__init__(self, outcomes=['preempted', 'satisfied', 'fold_arm', 'retry'],
21  input_keys=['od_attempt', 'obj_names'],
22  output_keys=['od_attempt'])
23 
24  def execute(self, userdata):
25  rospy.sleep(2.0)
26  if self.preempt_requested():
27  self.service_preempt()
28  return 'preempted'
29  if len(userdata.obj_names) > 0:
30  userdata.od_attempt = 0
31  return 'satisfied'
32  userdata.od_attempt += 1
33  if userdata.od_attempt == 1:
34  return 'fold_arm'
35  return 'retry'
36 
37 class ExecuteUserCommand(smach.State):
38  '''Different starts of the SM depending on the command provided when calling
39  the actionlib wrapper. TODO: I think this can be done w/o creating a class...'''
40  def __init__(self):
41  smach.State.__init__(self, outcomes=['start', 'reset', 'fold', 'quit'],
42  input_keys=['user_command'])
43 
44  def execute(self, ud):
45  rospy.loginfo("Executing User Command '%s'", ud['user_command'].command)
46  return ud['user_command'].command
47 
48 
49 rospy.init_node('object_manipulation_smach')
50 
51 # Object manipulation level sm; must be global so the concurrent USER_CMDS sm can signal to it the
52 # commands received by keyboard. TODO: there must be a better way to do this!!!
53 sm = smach.StateMachine(outcomes=['quit', 'error', 'aborted', 'preempted'],
54  input_keys = ['user_command'], output_keys = ['ucmd_outcome'])
55 with sm:
56  ''' User data '''
57  sm.userdata.user_command = UserCommandGoal()
58  sm.userdata.ucmd_outcome = UserCommandResult()
59  sm.userdata.od_attempt = 0
60  sm.userdata.frame = rospy.get_param('~arm_link', '/arm_link')
61  sm.userdata.obj_names = []
62  sm.userdata.obj_name = std_msg.String()
63  sm.userdata.header = std_msg.Header()
64  sm.userdata.pick_pose = geometry_msg.Pose()
65  sm.userdata.place_pose = geometry_msg.Pose()
66  sm.userdata.named_pose_target_type = MoveToTargetGoal.NAMED_TARGET
67  sm.userdata.arm_folded_named_pose = 'resting'
68 
69  smach.StateMachine.add('ExecuteUserCommand',
71  transitions={'start':'ObjectDetection',
72  'reset':'ObjectDetection',
73  'fold':'FoldArm',
74  'quit':'FoldArmAndQuit'})
75 
76  # Object detection sub state machine; iterates over object_detection action state and recovery
77  # mechanism until an object is detected, it's preempted or there's an error (aborted outcome)
78  od_sm = smach.StateMachine(outcomes = ['succeeded','preempted','aborted'],
79  input_keys = ['od_attempt', 'frame', 'obj_names',
80  'named_pose_target_type', 'arm_folded_named_pose'],
81  output_keys = ['obj_names'])
82  with od_sm:
83  smach.StateMachine.add('ObjectDetectionOnce',
84  smach_ros.SimpleActionState('object_detection',
85  ObjectDetectionAction,
86  goal_slots=['frame'],
87  result_slots=['obj_names']),
88  remapping={'frame':'frame',
89  'obj_names':'obj_names'},
90  transitions={'succeeded':'ObjDetectedCondition',
91  'preempted':'preempted',
92  'aborted':'aborted'})
93 
94  smach.StateMachine.add('ObjDetectedCondition',
96  remapping={'obj_names':'obj_names'},
97  transitions={'satisfied':'succeeded',
98  'preempted':'preempted',
99  'fold_arm':'ObjDetectionFoldArm',
100  'retry':'ObjectDetectionOnce'})
101 
102  smach.StateMachine.add('ObjDetectionFoldArm',
103  smach_ros.SimpleActionState('move_to_target',
104  MoveToTargetAction,
105  goal_slots=['target_type', 'named_target']),
106  remapping={'target_type':'named_pose_target_type',
107  'named_target':'arm_folded_named_pose'},
108  transitions={'succeeded':'ObjectDetectionOnce',
109  'preempted':'preempted',
110  'aborted':'ObjectDetectionOnce'})
111 
112  smach.StateMachine.add('ObjectDetection', od_sm,
113  remapping={'frame':'frame'},
114  transitions={'succeeded':'InteractiveManip',
115  'preempted':'preempted',
116  'aborted':'error'})
117 
118  smach.StateMachine.add('InteractiveManip',
119  smach_ros.SimpleActionState('interactive_manipulation',
120  InteractiveManipAction,
121  goal_slots=['frame'],
122  result_slots=['obj_name', 'header', 'pick_pose', 'place_pose']),
123  remapping={'frame':'frame',
124  'obj_name':'obj_name',
125  'header':'header',
126  'pick_pose':'pick_pose',
127  'place_pose':'place_pose'},
128  transitions={'succeeded':'PickAndPlace',
129  'preempted':'preempted',
130  'aborted':'ObjectDetection'})
131 
132  smach.StateMachine.add('PickAndPlace',
133  smach_ros.SimpleActionState('pick_and_place',
134  PickAndPlaceAction,
135  goal_slots=['frame', 'obj_name', 'header', 'pick_pose', 'place_pose'],
136  result_slots=[]),
137  remapping={'frame':'frame',
138  'header':'header',
139  'pick_pose':'pick_pose',
140  'place_pose':'place_pose'},
141  transitions={'succeeded':'ObjectDetection',
142  'preempted':'preempted',
143  'aborted':'ObjectDetection'}) # back to the beginning... we should open the gripper, in case we have picked an object (TODO)
144 
145  smach.StateMachine.add('FoldArm',
146  smach_ros.SimpleActionState('move_to_target',
147  MoveToTargetAction,
148  goal_slots=['target_type', 'named_target']),
149  remapping={'target_type':'named_pose_target_type',
150  'named_target':'arm_folded_named_pose'},
151  transitions={'succeeded':'ObjectDetection',
152  'preempted':'preempted',
153  'aborted':'ObjectDetection'})
154 
155  smach.StateMachine.add('FoldArmAndQuit',
156  smach_ros.SimpleActionState('move_to_target',
157  MoveToTargetAction,
158  goal_slots=['target_type', 'named_target']),
159  remapping={'target_type':'named_pose_target_type',
160  'named_target':'arm_folded_named_pose'},
161  transitions={'succeeded':'quit',
162  'preempted':'quit',
163  'aborted':'error'})
164 
165 
166  # Construct action server wrapper
167  asw = smach_ros.ActionServerWrapper(
168  'user_commands_action_server', UserCommandAction,
169  wrapped_container = sm,
170  succeeded_outcomes = ['quit'],
171  aborted_outcomes = ['aborted'],
172  preempted_outcomes = ['error'],
173  goal_key = 'user_command',
174  result_key = 'ucmd_outcome')
175 
176  # Run the server in a background thread
177  asw.run_server()
178 
179  # Create and start the introspection server
180  sis = smach_ros.IntrospectionServer('object_manipulation', sm, '/SM_ROOT')
181  sis.start()
182 
183  # Wait for control-c
184  rospy.spin()


turtlebot_arm_object_manipulation
Author(s): Jorge Santos
autogenerated on Fri Feb 7 2020 03:56:40