@@ -64,12 +64,6 @@ def __init__(self):
64
64
PreemptableStateMachine .initialize_ros (self )
65
65
LockableStateMachine .initialize_ros (self )
66
66
67
- # set up proxys for sm <--> GUI communication
68
- # publish topics
69
- self ._pub = ProxyPublisher ({'flexbe/behavior_update' : String ,
70
- 'flexbe/request_mirror_structure' : Int32 })
71
- self ._heartbeat_pub = self .create_publisher (Int32 , 'flexbe/mirror/heartbeat' , 2 )
72
-
73
67
self ._timing_event = threading .Event () # Used for wait timer
74
68
self ._running = False
75
69
self ._stopping = False
@@ -80,20 +74,30 @@ def __init__(self):
80
74
self ._sync_lock = threading .Lock ()
81
75
self ._state_checksums = {}
82
76
77
+ # set up pub/sub sm <--> GUI communication of mirror
78
+ # publish topics
79
+ self ._heartbeat_pub = self .create_publisher (Int32 , 'flexbe/mirror/heartbeat' , 2 )
80
+ self ._request_struct_pub = self .create_publisher (Int32 , 'flexbe/request_mirror_structure' , 2 )
81
+
82
+ # listen for mirror control messages using standard subscriptions
83
+ self ._status_sub = self .create_subscription (BEStatus , 'flexbe/status' , self ._status_callback , 10 )
84
+ self ._struct_sub = self .create_subscription (ContainerStructure , 'flexbe/mirror/structure' ,
85
+ self ._mirror_structure_callback , 10 )
86
+ self ._sync_sub = self .create_subscription (BehaviorSync , 'flexbe/mirror/sync' , self ._sync_callback , 10 )
87
+ self ._preempt_sub = self .create_subscription (Empty , 'flexbe/mirror/preempt' , self ._preempt_callback , 10 )
88
+ self ._onboard_heartbeat_sub = self .create_subscription (BehaviorSync , 'flexbe/heartbeat' , self ._heartbeat_callback , 10 )
89
+ self ._sync_heartbeat_mismatch_counter = 0
90
+
83
91
self ._outcome_topic = 'flexbe/mirror/outcome'
92
+ self ._update_topic = 'flexbe/behavior_update'
93
+ # Use proxy publisher/subscriber for access in states
94
+ # but just initialize here once for all
95
+ self ._beh_update_pub = ProxyPublisher ({self ._update_topic : String })
84
96
85
- # listen for mirror message
86
97
self ._sub = ProxySubscriberCached ()
87
98
self ._sub .subscribe (self ._outcome_topic , UInt8 , inst_id = id (self ))
88
99
self ._sub .enable_buffer (self ._outcome_topic )
89
100
90
- self ._sub .subscribe ('flexbe/status' , BEStatus , self ._status_callback , inst_id = id (self ))
91
- self ._sub .subscribe ('flexbe/mirror/structure' , ContainerStructure , self ._mirror_structure_callback , inst_id = id (self ))
92
- self ._sub .subscribe ('flexbe/mirror/sync' , BehaviorSync , self ._sync_callback , inst_id = id (self ))
93
- self ._sub .subscribe ('flexbe/mirror/preempt' , Empty , self ._preempt_callback , inst_id = id (self ))
94
- self ._sub .subscribe ('flexbe/heartbeat' , BehaviorSync , self ._heartbeat_callback , inst_id = id (self ))
95
- self ._sync_heartbeat_mismatch_counter = 0
96
-
97
101
# no clean way to wait for publisher to be ready...
98
102
Logger .loginfo ('--> Mirror - setting up publishers and subscribers ...' )
99
103
self ._timing_event .wait (1.0 ) # Give publishers time to initialize
@@ -184,7 +188,7 @@ def _activate_mirror(self, struct_msg):
184
188
else :
185
189
Logger .localwarn (f'Error processing mirror structure for behavior checksum id = { struct_msg .behavior_id } ' )
186
190
Logger .logwarn ('Requesting a new mirror structure from onboard ...' )
187
- self ._pub .publish ('flexbe/request_mirror_structure' , Int32 (data = struct_msg .behavior_id ))
191
+ self ._request_struct_pub .publish (Int32 (data = struct_msg .behavior_id ))
188
192
self ._active_id = struct_msg .behavior_id
189
193
return
190
194
@@ -249,7 +253,7 @@ def _start_mirror(self, msg):
249
253
if self ._sm is None :
250
254
Logger .localwarn (f'Missing correct mirror structure for starting behavior checksum id ={ msg .behavior_id } ' )
251
255
Logger .logwarn ('Requesting mirror structure from onboard ...' )
252
- self ._pub .publish ('flexbe/request_mirror_structure' , Int32 (data = msg .behavior_id ))
256
+ self ._request_struct_pub .publish (Int32 (data = msg .behavior_id ))
253
257
self ._active_id = msg .behavior_id
254
258
return
255
259
@@ -271,19 +275,21 @@ def _stop_mirror(self, msg):
271
275
if self ._sm is not None and self ._running :
272
276
if msg is not None and msg .code == BEStatus .FINISHED :
273
277
Logger .loginfo ('Onboard behavior finished successfully.' )
274
- self ._pub .publish ('flexbe/behavior_update' , String ())
278
+ self ._beh_update_pub .publish (self . _update_topic , String ())
275
279
elif msg is not None and msg .code == BEStatus .SWITCHING :
276
280
self ._starting_path = None
277
281
Logger .loginfo ('Onboard performing behavior switch.' )
278
282
elif msg is not None and msg .code == BEStatus .READY :
279
283
Logger .loginfo ('Onboard engine just started, stopping currently running mirror.' )
280
- self ._pub .publish ('flexbe/behavior_update' , String ())
284
+ self ._beh_update_pub .publish (self . _update_topic , String ())
281
285
elif msg is not None :
282
286
Logger .logwarn ('Onboard behavior failed!' )
283
- self ._pub .publish ('flexbe/behavior_update' , String ())
287
+ self ._beh_update_pub .publish (self . _update_topic , String ())
284
288
285
289
self ._wait_stop_running ()
286
290
291
+ self ._sm .destroy ()
292
+
287
293
else :
288
294
Logger .localinfo ('No onboard behavior is active.' )
289
295
@@ -398,7 +404,7 @@ def _restart_mirror(self, msg):
398
404
else :
399
405
Logger .localwarn (f'Missing correct mirror structure for restarting behavior checksum id ={ msg .behavior_id } ' )
400
406
Logger .logwarn ('Requesting mirror structure from onboard ...' )
401
- self ._pub .publish ('flexbe/request_mirror_structure' , Int32 (data = msg .behavior_id ))
407
+ self ._request_struct_pub .publish (Int32 (data = msg .behavior_id ))
402
408
self ._active_id = msg .behavior_id
403
409
return
404
410
@@ -426,6 +432,7 @@ def _execute_mirror(self):
426
432
try :
427
433
result = self ._sm .spin ()
428
434
Logger .loginfo (f"Mirror for active id = { self ._active_id } finished with result '{ result } '" )
435
+ self ._sm .destroy ()
429
436
except Exception as exc :
430
437
try :
431
438
Logger .logerr ('\n (_execute_mirror Traceback): Caught exception on preempt:\n %s' % str (exc ))
0 commit comments