@@ -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
275278if __name__ == " __main__" :
@@ -288,9 +291,13 @@ ros2 run yasmin_demos remap_demo.py
288291```
289292
290293``` python
294+ import rclpy
295+
291296import yasmin
292297from yasmin import State, Blackboard, StateMachine
298+ from yasmin_ros import set_ros_loggers
293299from yasmin_ros.basic_outcomes import SUCCEED
300+ from yasmin_viewer import YasminViewerPub
294301
295302
296303class 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
576601if __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
754782if __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
952983if __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
10891123if __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
12351273if __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
14001441if __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-
16181660if __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
16881733if __name__ == " __main__" :
0 commit comments