Skip to content

Commit 5e9dbcb

Browse files
committed
adding cleanup to python demos
1 parent 200fda0 commit 5e9dbcb

File tree

12 files changed

+177
-99
lines changed

12 files changed

+177
-99
lines changed

README.md

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def main() -> None:
257257
)
258258

259259
# Publish FSM information for visualization
260-
YasminViewerPub(sm, "YASMIN_DEMO")
260+
viewer = YasminViewerPub(sm, "YASMIN_DEMO")
261261

262262
# Execute the FSM
263263
try:
@@ -266,10 +266,13 @@ def main() -> None:
266266
except KeyboardInterrupt:
267267
if sm.is_running():
268268
sm.cancel_state()
269+
finally:
270+
viewer.cleanup()
271+
del sm
269272

270-
# Shutdown ROS 2 if it's running
271-
if rclpy.ok():
272-
rclpy.shutdown()
273+
# Shutdown ROS 2 if it's running
274+
if rclpy.ok():
275+
rclpy.shutdown()
273276

274277

275278
if __name__ == "__main__":
@@ -288,9 +291,13 @@ ros2 run yasmin_demos remap_demo.py
288291
```
289292

290293
```python
294+
import rclpy
295+
291296
import yasmin
292297
from yasmin import State, Blackboard, StateMachine
298+
from yasmin_ros import set_ros_loggers
293299
from yasmin_ros.basic_outcomes import SUCCEED
300+
from yasmin_viewer import YasminViewerPub
294301

295302

296303
class Foo(State):
@@ -359,16 +366,11 @@ class BarState(State):
359366
return SUCCEED
360367

361368

362-
if __name__ == "__main__":
363-
"""
364-
The main entry point of the application.
365-
366-
Initializes the ROS 2 environment, sets up the state machine,
367-
and handles execution and termination.
369+
def main() -> None:
370+
yasmin.YASMIN_LOG_INFO("yasmin_remapping_demo")
371+
rclpy.init()
372+
set_ros_loggers()
368373

369-
Raises:
370-
KeyboardInterrupt: If the execution is interrupted by the user.
371-
"""
372374
bb = Blackboard()
373375
bb["msg1"] = "test1"
374376
bb["msg2"] = "test2"
@@ -393,7 +395,27 @@ if __name__ == "__main__":
393395
remappings={"bar_data": "foo_out_data"},
394396
)
395397

396-
sm.execute(bb)
398+
# Launch YASMIN Viewer publisher for state visualization
399+
viewer = YasminViewerPub(sm, "YASMIN_REMAPPING_DEMO")
400+
401+
# Execute the FSM
402+
try:
403+
outcome = sm(bb)
404+
yasmin.YASMIN_LOG_INFO(outcome)
405+
except KeyboardInterrupt:
406+
if sm.is_running():
407+
sm.cancel_state()
408+
finally:
409+
viewer.cleanup()
410+
del sm
411+
412+
# Shutdown ROS 2 if it's running
413+
if rclpy.ok():
414+
rclpy.shutdown()
415+
416+
417+
if __name__ == "__main__":
418+
main()
397419
```
398420

399421
</details>
@@ -558,7 +580,7 @@ def main() -> None:
558580
)
559581

560582
# Publish FSM information for visualization
561-
YasminViewerPub(sm, "YASMIN_CONCURRENCE_DEMO")
583+
viewer = YasminViewerPub(sm, "YASMIN_CONCURRENCE_DEMO")
562584

563585
# Execute the FSM
564586
try:
@@ -567,10 +589,13 @@ def main() -> None:
567589
except KeyboardInterrupt:
568590
if sm.is_running():
569591
sm.cancel_state()
592+
finally:
593+
viewer.cleanup()
594+
del sm
570595

571-
# Shutdown ROS 2 if it's running
572-
if rclpy.ok():
573-
rclpy.shutdown()
596+
# Shutdown ROS 2 if it's running
597+
if rclpy.ok():
598+
rclpy.shutdown()
574599

575600

576601
if __name__ == "__main__":
@@ -736,7 +761,7 @@ def main() -> None:
736761
)
737762

738763
# Publish FSM info
739-
YasminViewerPub(sm, "YASMIN_SERVICE_CLIENT_DEMO")
764+
viewer = YasminViewerPub(sm, "YASMIN_SERVICE_CLIENT_DEMO")
740765

741766
# Execute FSM
742767
try:
@@ -745,10 +770,13 @@ def main() -> None:
745770
except KeyboardInterrupt:
746771
if sm.is_running():
747772
sm.cancel_state()
773+
finally:
774+
viewer.cleanup()
775+
del sm
748776

749-
# Shutdown ROS 2
750-
if rclpy.ok():
751-
rclpy.shutdown()
777+
# Shutdown ROS 2 if it's running
778+
if rclpy.ok():
779+
rclpy.shutdown()
752780

753781

754782
if __name__ == "__main__":
@@ -930,7 +958,7 @@ def main() -> None:
930958
)
931959

932960
# Publish FSM information
933-
YasminViewerPub(sm, "YASMIN_ACTION_CLIENT_DEMO")
961+
viewer = YasminViewerPub(sm, "YASMIN_ACTION_CLIENT_DEMO")
934962

935963
# Create an initial blackboard with the input value
936964
blackboard = Blackboard()
@@ -943,10 +971,13 @@ def main() -> None:
943971
except KeyboardInterrupt:
944972
if sm.is_running():
945973
sm.cancel_state() # Cancel the state if interrupted
974+
finally:
975+
viewer.cleanup()
976+
del sm
946977

947-
# Shutdown ROS 2
948-
if rclpy.ok():
949-
rclpy.shutdown()
978+
# Shutdown ROS 2 if it's running
979+
if rclpy.ok():
980+
rclpy.shutdown()
950981

951982

952983
if __name__ == "__main__":
@@ -1071,7 +1102,7 @@ def main() -> None:
10711102
)
10721103

10731104
# Publish FSM information
1074-
YasminViewerPub(sm, "YASMIN_MONITOR_DEMO")
1105+
viewer = YasminViewerPub(sm, "YASMIN_MONITOR_DEMO")
10751106

10761107
# Execute FSM
10771108
try:
@@ -1080,10 +1111,13 @@ def main() -> None:
10801111
except KeyboardInterrupt:
10811112
if sm.is_running():
10821113
sm.cancel_state()
1114+
finally:
1115+
viewer.cleanup()
1116+
del sm
10831117

1084-
# Shutdown ROS 2
1085-
if rclpy.ok():
1086-
rclpy.shutdown()
1118+
# Shutdown ROS 2 if it's running
1119+
if rclpy.ok():
1120+
rclpy.shutdown()
10871121

10881122

10891123
if __name__ == "__main__":
@@ -1214,7 +1248,7 @@ def main() -> None:
12141248
)
12151249

12161250
# Launch YASMIN Viewer publisher for state visualization
1217-
YasminViewerPub(sm, "YASMIN_PUBLISHER_DEMO")
1251+
viewer = YasminViewerPub(sm, "YASMIN_PUBLISHER_DEMO")
12181252

12191253
# Initialize blackboard with counter values
12201254
blackboard = Blackboard()
@@ -1227,9 +1261,13 @@ def main() -> None:
12271261
yasmin.YASMIN_LOG_INFO(outcome)
12281262
except Exception as e:
12291263
yasmin.YASMIN_LOG_INFO(str(e))
1264+
finally:
1265+
viewer.cleanup()
1266+
del sm
12301267

1231-
# Shutdown ROS
1232-
rclpy.shutdown()
1268+
# Shutdown ROS 2 if it's running
1269+
if rclpy.ok():
1270+
rclpy.shutdown()
12331271

12341272

12351273
if __name__ == "__main__":
@@ -1382,7 +1420,7 @@ def main() -> None:
13821420
)
13831421

13841422
# Publish FSM information for visualization
1385-
YasminViewerPub(sm, "YASMIN_PARAMETERS_DEMO")
1423+
viewer = YasminViewerPub(sm, "YASMIN_PARAMETERS_DEMO")
13861424

13871425
# Execute the FSM
13881426
try:
@@ -1391,10 +1429,13 @@ def main() -> None:
13911429
except KeyboardInterrupt:
13921430
if sm.is_running():
13931431
sm.cancel_state()
1432+
finally:
1433+
viewer.cleanup()
1434+
del sm
13941435

1395-
# Shutdown ROS 2 if it's running
1396-
if rclpy.ok():
1397-
rclpy.shutdown()
1436+
# Shutdown ROS 2 if it's running
1437+
if rclpy.ok():
1438+
rclpy.shutdown()
13981439

13991440

14001441
if __name__ == "__main__":
@@ -1597,7 +1638,7 @@ def main() -> None:
15971638
)
15981639

15991640
# Publish FSM information for visualization
1600-
YasminViewerPub(sm, "YASMIN_NAV2_DEMO")
1641+
viewer = YasminViewerPub(sm, "YASMIN_NAV2_DEMO")
16011642

16021643
# Execute the state machine
16031644
blackboard = Blackboard()
@@ -1608,13 +1649,14 @@ def main() -> None:
16081649
yasmin.YASMIN_LOG_INFO(outcome)
16091650
except KeyboardInterrupt:
16101651
sm.cancel_state() # Handle manual interruption
1652+
finally:
1653+
viewer.cleanup()
1654+
del sm
16111655

1612-
# Shutdown ROS 2
1613-
if rclpy.ok():
1614-
if sm.is_running():
1656+
# Shutdown ROS 2 if it's running
1657+
if rclpy.ok():
16151658
rclpy.shutdown()
16161659

1617-
16181660
if __name__ == "__main__":
16191661
main()
16201662
```
@@ -1670,7 +1712,7 @@ def main() -> None:
16701712
)
16711713

16721714
# Publish FSM information for visualization
1673-
YasminViewerPub(sm, "YASMIN_FACTORY_DEMO")
1715+
viewer = YasminViewerPub(sm, "plugin_demo")
16741716

16751717
# Execute the FSM
16761718
try:
@@ -1679,10 +1721,13 @@ def main() -> None:
16791721
except KeyboardInterrupt:
16801722
if sm.is_running():
16811723
sm.cancel_state()
1724+
finally:
1725+
viewer.cleanup()
1726+
del sm
16821727

1683-
# Shutdown ROS 2 if it's running
1684-
if rclpy.ok():
1685-
rclpy.shutdown()
1728+
# Shutdown ROS 2 if it's running
1729+
if rclpy.ok():
1730+
rclpy.shutdown()
16861731

16871732

16881733
if __name__ == "__main__":

yasmin_demos/yasmin_demos/action_client_demo.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def main() -> None:
174174
)
175175

176176
# Publish FSM information
177-
YasminViewerPub(sm, "YASMIN_ACTION_CLIENT_DEMO")
177+
viewer = YasminViewerPub(sm, "YASMIN_ACTION_CLIENT_DEMO")
178178

179179
# Create an initial blackboard with the input value
180180
blackboard = Blackboard()
@@ -187,10 +187,13 @@ def main() -> None:
187187
except KeyboardInterrupt:
188188
if sm.is_running():
189189
sm.cancel_state() # Cancel the state if interrupted
190+
finally:
191+
viewer.cleanup()
192+
del sm
190193

191-
# Shutdown ROS 2
192-
if rclpy.ok():
193-
rclpy.shutdown()
194+
# Shutdown ROS 2 if it's running
195+
if rclpy.ok():
196+
rclpy.shutdown()
194197

195198

196199
if __name__ == "__main__":

yasmin_demos/yasmin_demos/concurrence_demo.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def main() -> None:
166166
)
167167

168168
# Publish FSM information for visualization
169-
YasminViewerPub(sm, "YASMIN_CONCURRENCE_DEMO")
169+
viewer = YasminViewerPub(sm, "YASMIN_CONCURRENCE_DEMO")
170170

171171
# Execute the FSM
172172
try:
@@ -175,10 +175,13 @@ def main() -> None:
175175
except KeyboardInterrupt:
176176
if sm.is_running():
177177
sm.cancel_state()
178+
finally:
179+
viewer.cleanup()
180+
del sm
178181

179-
# Shutdown ROS 2 if it's running
180-
if rclpy.ok():
181-
rclpy.shutdown()
182+
# Shutdown ROS 2 if it's running
183+
if rclpy.ok():
184+
rclpy.shutdown()
182185

183186

184187
if __name__ == "__main__":

yasmin_demos/yasmin_demos/factory_demo.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main() -> None:
4242
)
4343

4444
# Publish FSM information for visualization
45-
YasminViewerPub(sm, "plugin_demo")
45+
viewer = YasminViewerPub(sm, "plugin_demo")
4646

4747
# Execute the FSM
4848
try:
@@ -51,10 +51,13 @@ def main() -> None:
5151
except KeyboardInterrupt:
5252
if sm.is_running():
5353
sm.cancel_state()
54+
finally:
55+
viewer.cleanup()
56+
del sm
5457

55-
# Shutdown ROS 2 if it's running
56-
if rclpy.ok():
57-
rclpy.shutdown()
58+
# Shutdown ROS 2 if it's running
59+
if rclpy.ok():
60+
rclpy.shutdown()
5861

5962

6063
if __name__ == "__main__":

0 commit comments

Comments
 (0)