Skip to content

Commit

Permalink
fix typo in flexbe_mirror; ignore second behavior start request while…
Browse files Browse the repository at this point in the history
… first is processing (in case of zombie launcher)
  • Loading branch information
David Conner committed Jul 2, 2024
1 parent 19591e1 commit f66e0a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion flexbe_mirror/flexbe_mirror/flexbe_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def _start_mirror(self, msg, start_time):
if len(self._struct_buffer) > 0:
while self._sm is None and len(self._struct_buffer) > 0:
# Search buffer looking for desired structure
struct = self._struct_buffer.popLeft()
struct = self._struct_buffer.popleft()
if struct.behavior_id == msg.behavior_id:
self._mirror_state_machine(struct)
Logger.localinfo(f"Mirror built for checksum '{msg.behavior_id}'")
Expand Down
17 changes: 14 additions & 3 deletions flexbe_onboard/flexbe_onboard/flexbe_onboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self):
self._trigger_ready = True
self._running = False
self._run_lock = threading.Lock()
self._starting = False
self._switching = False
self._switch_lock = threading.Lock()
self._behavior_id = -1
Expand Down Expand Up @@ -152,6 +153,12 @@ def _parse_version(v):
return result

def _behavior_callback(self, beh_sel_msg):
if self._starting:
# Prevent multiple request messages from triggering too soon
Logger.logwarn(f"Received behavior start request for {beh_sel_msg.behavior_key} "
f"({beh_sel_msg.behavior_id}) while prior request was starting.\n Ignore second request!")
return
self._starting = True # Prevent two start requests from ocurring back to back
self._trigger_ready = False # We have received the behavior selection request
self._ready_counter = 0
thread = threading.Thread(target=self._behavior_execution, args=[beh_sel_msg])
Expand Down Expand Up @@ -222,6 +229,7 @@ def _behavior_execution(self, beh_sel_msg):
# self._status_pub.publish(BEStatus(stamp=self.get_clock().now().to_msg(), code=BEStatus.READY))
Logger.localinfo('\033[92m--- Behavior Engine ready to try again! ---\033[0m')
self._ready_counter = 8 # Trigger heartbeat to republish READY within 2 seconds
self._starting = False # Now allow a new start requests
return

# perform the behavior switch if required
Expand All @@ -244,6 +252,7 @@ def _behavior_execution(self, beh_sel_msg):
' because switching is not possible.')
self._proxy_pub.publish(Topics._CMD_FEEDBACK_TOPIC,
CommandFeedback(command='switch', args=['not_switchable']))
self._starting = False # Now allow a new start requests
return

self._status_pub.publish(BEStatus(stamp=self.get_clock().now().to_msg(),
Expand All @@ -261,11 +270,11 @@ def _behavior_execution(self, beh_sel_msg):

# extract the active state if any
if active_states is not None:
Logger.localinfo(f"Behavior Engine - '{self.be.name}': {self.be.beh_id} "
Logger.localinfo(f"Behavior Engine - '{be.name}': {be.beh_id} "
f'switching behaviors from active state {[acst.name for acst in active_states]} ...')
try:
if len(active_states) > 1:
Logger.localinfo(f"\n\nBehavior Engine - '{self.be.name}': {self.be.beh_id} "
Logger.localinfo(f"\n\nBehavior Engine - '{be.name}': {be.beh_id} "
f'- switching only top level state!\n')
Logger.logwarn('Switch multiple active states?')

Expand All @@ -280,6 +289,7 @@ def _behavior_execution(self, beh_sel_msg):
self._status_pub.publish(BEStatus(stamp=self.get_clock().now().to_msg(),
behavior_id=self.be.beh_id,
code=BEStatus.RUNNING))
self._starting = False # Now allow a new start requests
return
# stop the rest
Logger.localinfo(f"Behavior Engine - '{self.be.name}': {self.be.beh_id} - "
Expand All @@ -296,6 +306,7 @@ def _behavior_execution(self, beh_sel_msg):
f'key={beh_sel_msg.behavior_key}={be.beh_id} ({beh_sel_msg.behavior_id}) ...')
assert self.be is None, 'Run lock with old behavior active?'
self._running = True
self._starting = False # Now allow a new start requests
self.be = be

result = None
Expand Down Expand Up @@ -346,7 +357,7 @@ def _behavior_execution(self, beh_sel_msg):
self._status_pub.publish(BEStatus(stamp=self.get_clock().now().to_msg(), code=BEStatus.READY))
Logger.localinfo('\033[92m--- Behavior Engine finished - ready for more! ---\033[0m')

# Logger.localinfo(f"Behavior execution finished for id={self.be.beh_id}, exit thread!")
Logger.localinfo(f"Behavior execution finished for id={self.be.beh_id}, exit thread!")
self._running = False
self._switching = False
self.be = None
Expand Down

0 comments on commit f66e0a9

Please sign in to comment.