I've been casually looking for a good solution to handle state machines throughout the system.
Requirements
FYI @simonegu @julianoes
FWIW, Yakindu statechart tools can be run headless (without running Eclipse UI), and moving charts between Qt and Yakindu somewhat works.
https://github.com/Yakindu/statecharts
@dagar I made a dumb little example in PX4 that uses this library https://github.com/amaiorano/hsm
See https://github.com/PX4/Firmware/tree/hsm_example
It's essentially the state_args.cpp with a few modifications. I wouldn't even know where to begin with trying to implement this in something like Commander... Hopefully this is somewhat helpful.
make px4_sitl_default jmavsim
pxh > hsm_example
Right now the plotHsm.py doesn't work, but I was able to manually generate the chart:
python $PX4_DIR/src/lib/hsm/tools/hsmToDot.py $PX4_DIR/src/modules/hsm_example/hsm_example.h > hsm_example.dot
dot hsm_example.dot -Tpng -o hsm_example.png

Output
pxh> hsm_example
HSM_1_TestHsm: Init : Alive
HSM_1_TestHsm: Entry : Locomotion
HSM_1_TestHsm: Entry : Stand
HSM_2_TestHsm: Pop : Stand
HSM_1_TestHsm: Sibling : Move
HSM_2_TestHsm: Pop : Move
HSM_2_TestHsm: Pop : Locomotion
HSM_1_TestHsm: Sibling : Attack
>>> Attacking: 0
>>> Attacking: 0
>>> Attacking: 0
HSM_2_TestHsm: Pop : Attack
HSM_1_TestHsm: Sibling : Attack
>>> Attacking: 1
>>> Attacking: 1
>>> Attacking: 1
HSM_2_TestHsm: Pop : Attack
HSM_1_TestHsm: Sibling : Attack
>>> Attacking: 2
>>> Attacking: 2
>>> Attacking: 2
>>> Attacking: 2
I have the debug level set to diagnostic, so you can see all state transitions and their relationships.
Julian's state machine in PX4/avoidance. https://github.com/PX4/avoidance/pull/418
Thanks @dakejahl and @jkflying. That's helpful, however, I still believe the hard part is capturing the intended behaviour and put it into a diagram than to actually implement the state machine. I have implemented a state machine like this in the past as well but was struggling with formulating the actual behaviour without having to do it using hierarchical state machines.
Maybe a first step would be drawing a high level state transition diagram and refactoring into that, even if inside the different states there is still a lot of ad-hoc implicit state code?
➤ David Sidrane commented:
Have a look at https://www.visual-paradigm.com/
It is a great tool and there is a comunity edition
I have implemented a state machine like this in the past as well but was struggling with formulating the actual behaviour without having to do it using hierarchical state machines.
Why without hierarchical state machines?
Right so for hierarchical state machines you either need to extend the implementations or combine them manually.
Added https://github.com/andrew-gresyk/HFSM2 to the list to investigate.
You might consider a language @fsantanna created called ceu. http://ceu-lang.org/index.html
It generates C code that you would have to integrate into an event loop.
It's not a state machine generator, but I find the semantics easier on my brain than state machines, even hierarchical.
Option used by NASA Astrobee: https://github.com/nasa/astrobee/blob/bb0fc3e4110a14929bd4cf35c12b3c169bc6c756/shared/ff_util/include/ff_util/ff_fsm.h
(not generated automatically)
@dagar
Anybotic uses something like that to switch the robot to and from different modes with safety fallback.
https://docs.leggedrobotics.com/rocoma_doc/page_features.html
Most helpful comment
@dagar I made a dumb little example in PX4 that uses this library https://github.com/amaiorano/hsm
See https://github.com/PX4/Firmware/tree/hsm_example
It's essentially the
state_args.cppwith a few modifications. I wouldn't even know where to begin with trying to implement this in something likeCommander... Hopefully this is somewhat helpful.make px4_sitl_default jmavsimpxh > hsm_exampleRight now the
plotHsm.pydoesn't work, but I was able to manually generate the chart:Output
I have the debug level set to diagnostic, so you can see all state transitions and their relationships.