Skip to content

Commit aa2afe4

Browse files
committed
improving Python + cleaning Python viewer pub on shutdown
1 parent 414e2ee commit aa2afe4

File tree

8 files changed

+30
-14
lines changed

8 files changed

+30
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class FooState(State):
439439
440440
Outcomes:
441441
outcome1: Indicates the state should continue.
442-
outcome2: Indicates the state should cotninue.
442+
outcome2: Indicates the state should continue.
443443
outcome3: Indicates the state should finish execution and return.
444444
"""
445445
super().__init__(["outcome1", "outcome2", "outcome3"])

docs/tutorials/python/concurrence_demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ <h2>1. Create the FooState</h2>
192192

193193
Outcomes:
194194
outcome1: Indicates the state should continue.
195-
outcome2: Indicates the state should cotninue.
195+
outcome2: Indicates the state should continue.
196196
outcome3: Indicates the state should finish execution and return.
197197
"""
198198
super().__init__(["outcome1", "outcome2", "outcome3"])

yasmin_demos/yasmin_demos/concurrence_demo.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self) -> None:
3737
3838
Outcomes:
3939
outcome1: Indicates the state should continue.
40-
outcome2: Indicates the state should cotninue.
40+
outcome2: Indicates the state should continue.
4141
outcome3: Indicates the state should finish execution and return.
4242
"""
4343
super().__init__(["outcome1", "outcome2", "outcome3"])
@@ -59,8 +59,6 @@ def execute(self, blackboard: Blackboard) -> str:
5959
yasmin.YASMIN_LOG_INFO("Executing state FOO")
6060
time.sleep(2) # Simulate work by sleeping
6161

62-
outcome = ""
63-
6462
blackboard["foo_str"] = f"Counter: {self.counter}"
6563

6664
if self.counter < 3:

yasmin_demos/yasmin_demos/factory_demo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def main() -> None:
5151
yasmin.YASMIN_LOG_INFO(outcome)
5252
except Exception as e:
5353
yasmin.YASMIN_LOG_WARN(e)
54+
finally:
55+
del sm
5456

5557
# Shutdown ROS 2 if it's running
5658
if rclpy.ok():

yasmin_factory/yasmin_factory/yasmin_factory.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self) -> None:
2929
"""
3030

3131
self._cpp_factory = CppStateFactory()
32-
self._xml_path = ""
32+
self._xml_path: str = ""
3333

3434
def create_state(self, state_elem: ET.Element) -> State:
3535
"""
@@ -42,7 +42,6 @@ def create_state(self, state_elem: ET.Element) -> State:
4242
ValueError: If the state type is unknown or if required attributes
4343
are missing.
4444
"""
45-
4645
state_type = state_elem.attrib.get("type", "py")
4746
class_name = state_elem.attrib["class"]
4847

@@ -52,11 +51,10 @@ def create_state(self, state_elem: ET.Element) -> State:
5251
state_class = getattr(module, class_name)
5352
return state_class()
5453

55-
elif state_type == "cpp":
54+
if state_type == "cpp":
5655
return self._cpp_factory.create(class_name)
5756

58-
else:
59-
raise ValueError(f"Unknown state type: {state_type}")
57+
raise ValueError(f"Unknown state type: {state_type}")
6058

6159
def create_concurrence(self, conc_elem: ET.Element) -> Concurrence:
6260
"""
@@ -121,11 +119,11 @@ def create_sm(self, root: ET.Element) -> StateMachine:
121119
try:
122120
package_path = get_package_share_path(package)
123121
file_path = ""
124-
for root, dirs, files in os.walk(package_path):
122+
for dirpath, _, files in os.walk(package_path):
125123
if file_name in files:
126-
file_path = os.path.join(root, file_name)
124+
file_path = os.path.join(dirpath, file_name)
127125
break
128-
except Exception as e:
126+
except Exception:
129127
file_path = ""
130128

131129
if file_path:

yasmin_factory/yasmin_factory/yasmin_factory_node.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def main() -> None:
5151
yasmin.YASMIN_LOG_INFO(outcome)
5252
except Exception as e:
5353
yasmin.YASMIN_LOG_WARN(e)
54+
finally:
55+
del sm
5456

5557
# Shutdown ROS 2 if it's running
5658
if rclpy.ok():

yasmin_ros/yasmin_ros/monitor_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def execute(self, blackboard: Blackboard) -> str:
147147
if self.is_canceled():
148148
return CANCEL
149149

150-
while self._timeout is not None and not timeout_flag:
150+
if self._timeout is not None and not timeout_flag:
151151
yasmin.YASMIN_LOG_WARN(
152152
f"Timeout reached, topic '{self._topic_name}' is not available"
153153
)

yasmin_viewer/yasmin_viewer/yasmin_viewer_pub.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ def __init__(
7373
## A timer to periodically publish the FSM state.
7474
self._timer = self._node.create_timer(1 / rate, self._publish_data)
7575

76+
## Register shutdown callback to clean up the viewer.
77+
self._node.context.on_shutdown(self._on_shutdown)
78+
79+
def _on_shutdown(self) -> None:
80+
"""
81+
Callback executed on shutdown to clean the viewer.
82+
83+
Publishes an empty state machine message to clear the viewer display.
84+
85+
Returns:
86+
None
87+
"""
88+
self._pub = None
89+
self._timer = None
90+
self._fsm = None
91+
7692
def parse_transitions(self, transitions: Dict[str, str]) -> List[TransitionMsg]:
7793
"""
7894
Converts a dictionary of transitions into a list of TransitionMsg.

0 commit comments

Comments
 (0)