Skip to content

Commit

Permalink
New State Machine Visualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrhm committed Jul 6, 2024
1 parent 11ae92c commit 0272071
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions launch/autonomy.launch
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<launch>
<rosparam command="load" file="$(find mrover)/config/esw.yaml"/>

<arg name="tag_detector" default="true"/>
<arg name="object_detector" default="false"/>
<arg name="cost_map" default="true"/>
Expand Down
31 changes: 16 additions & 15 deletions scripts/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

import rospy
import sys
from smach_msgs.msg import SmachContainerStatus, SmachContainerStructure
from mrover.msg import StateMachineStructure, StateMachineStateUpdate
from threading import Lock
from dataclasses import dataclass
from typing import Optional, List, Dict


STRUCTURE_TOPIC = "/smach/container_structure"
STATUS_TOPIC = "/smach/container_status"
STRUCTURE_TOPIC = "nav_structure"
STATUS_TOPIC = "nav_state"


@dataclass
Expand All @@ -30,7 +30,7 @@ class State:
class StateMachine:
def __init__(self):
self.states: Dict[str, State] = {}
self.structure: Optional[SmachContainerStructure] = None
self.structure: Optional[StateMachineStructure] = None
self.mutex: Lock = Lock()
self.cur_active: str = ""
self.previous_state: str = ""
Expand All @@ -50,17 +50,18 @@ def set_active_state(self, active_state):
f"Current time: {now} Previous state: {self.previous_state} Current State: { self.cur_active}"
)

def _rebuild(self, structure: SmachContainerStructure):
def _rebuild(self, structure: StateMachineStructure):
"""
rebuilds the state dictionary with a new structure message
"""
self.states = {child: State(child, []) for child in structure.children}
for start, end in zip(structure.outcomes_from, structure.outcomes_to):
if end != "None":
self.states[start].children.append(self.states[end])
self.states = {child.origin: State(child.origin, []) for child in structure.transitions}
for transition in structure.transitions:
origin = transition.origin
for to in transition.destinations:
self.states[origin].children.append(self.states[to])
self.needs_redraw = True

def check_rebuild(self, structure: SmachContainerStructure):
def check_rebuild(self, structure: StateMachineStructure):
"""
checks if the structure passed as input matches the structure already represented (thread safe)
"""
Expand All @@ -71,10 +72,10 @@ def check_rebuild(self, structure: SmachContainerStructure):
self._rebuild(structure)
self.structure = structure

def container_status_callback(self, status: SmachContainerStatus):
self.set_active_state(status.active_states[0])
def container_status_callback(self, status: StateMachineStateUpdate):
self.set_active_state(status.state)

def container_structure_callback(self, structure: SmachContainerStructure):
def container_structure_callback(self, structure: StateMachineStructure):
self.check_rebuild(structure)


Expand Down Expand Up @@ -121,12 +122,12 @@ def update(self):
rospy.init_node("smach_visualizer", anonymous=False, disable_signals=True, log_level=rospy.INFO)
rospy.Subscriber(
STRUCTURE_TOPIC,
SmachContainerStructure,
StateMachineStructure,
state_machine.container_structure_callback,
)
rospy.Subscriber(
STATUS_TOPIC,
SmachContainerStatus,
StateMachineStateUpdate,
state_machine.container_status_callback,
)
app = QApplication([]) # type: ignore
Expand Down
4 changes: 3 additions & 1 deletion src/navigation/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def __init__(self, context: Context):
)
self.state_machine.add_transitions(
SearchState(),
[ApproachTargetState(), LongRangeState(), WaypointState(), RecoveryState()],
[
ApproachTargetState(), LongRangeState(), WaypointState(), RecoveryState()
],
)
self.state_machine.add_transitions(DoneState(), [WaypointState()])
self.state_machine.add_transitions(
Expand Down

0 comments on commit 0272071

Please sign in to comment.