Px4-autopilot: State Machine Options (Library, Framework, Generator, Compiler, etc)

Created on 27 Sep 2018  Â·  13Comments  Â·  Source: PX4/PX4-Autopilot

I've been casually looking for a good solution to handle state machines throughout the system.

Requirements

  • declarative
  • generate documentation (either tables or a graph)

    • SCXML would also be nice

  • if a generator or compiler it needs to be easily accessible, cross platform, and have an acceptable license
  • likely need to support HSM (TBD)
  • entry and exit Actions
  • transition guards
  • error handling/reporting (think about feedback from denied arming or mode change)

Options

enhancement pinned

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.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

hsm_example
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.

All 13 comments

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

hsm_example
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.

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  Â·  4Comments

bosskwei picture bosskwei  Â·  3Comments

kainism picture kainism  Â·  4Comments

JacobCrabill picture JacobCrabill  Â·  4Comments

lgh5054 picture lgh5054  Â·  4Comments