This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
mbf_state_machine.py
executable file
·123 lines (106 loc) · 3.42 KB
/
mbf_state_machine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
#from http://wiki.ros.org/move_base_flex/Tutorials/SimpleSmachForMoveBaseFlex
import rospy
import smach
import smach_ros
from geometry_msgs.msg import PoseStamped
from nav_msgs.msg import Path
from mbf_msgs.msg import ExePathAction
from mbf_msgs.msg import GetPathAction
from mbf_msgs.msg import RecoveryAction
def main():
rospy.init_node('mbf_state_machine')
# Create SMACH state machine
sm = smach.StateMachine(outcomes=['succeeded', 'aborted', 'preempted'])
# Define userdata
sm.userdata.goal = None
sm.userdata.path = None
sm.userdata.error = None
sm.userdata.clear_costmap_flag = False
sm.userdata.error_status = None
with sm:
# Goal callback for state WAIT_FOR_GOAL
def goal_cb(userdata, msg):
userdata.goal = msg
return False
# Monitor topic to get MeshGoal from RViz plugin
smach.StateMachine.add(
'WAIT_FOR_GOAL',
smach_ros.MonitorState(
'/move_base_simple/goal',
PoseStamped,
goal_cb,
output_keys=['goal']
),
transitions={
'invalid': 'GET_PATH',
'valid': 'WAIT_FOR_GOAL',
'preempted': 'preempted'
}
)
# Get path
smach.StateMachine.add(
'GET_PATH',
smach_ros.SimpleActionState(
'/move_base_flex/get_path',
GetPathAction,
goal_slots=['target_pose'],
result_slots=['path']
),
transitions={
'succeeded': 'EXE_PATH',
'aborted': 'WAIT_FOR_GOAL',
'preempted': 'preempted'
},
remapping={
'target_pose': 'goal'
}
)
# Execute path
smach.StateMachine.add(
'EXE_PATH',
smach_ros.SimpleActionState(
'/move_base_flex/exe_path',
ExePathAction,
goal_slots=['path']
),
transitions={
'succeeded': 'WAIT_FOR_GOAL',
'aborted': 'RECOVERY',
'preempted': 'preempted'
}
)
# Goal callback for state RECOVERY
def recovery_path_goal_cb(userdata, goal):
if userdata.clear_costmap_flag == False:
goal.behavior = 'clear_costmap'
userdata.clear_costmap_flag = True
else:
goal.behavior = 'straf_recovery'
userdata.clear_costmap_flag = False
# Recovery
smach.StateMachine.add(
'RECOVERY',
smach_ros.SimpleActionState(
'move_base_flex/recovery',
RecoveryAction,
goal_cb=recovery_path_goal_cb,
input_keys=["error", "clear_costmap_flag"],
output_keys = ["error_status", 'clear_costmap_flag']
),
transitions={
'succeeded': 'GET_PATH',
'aborted': 'aborted',
'preempted': 'preempted'
}
)
# Create and start introspection server
sis = smach_ros.IntrospectionServer('smach_server', sm, '/SM_ROOT')
sis.start()
# Execute SMACH plan
sm.execute()
# Wait for interrupt and stop introspection server
rospy.spin()
sis.stop()
if __name__=="__main__":
main()