Hi, my team is looking to migrate to a state machine based codebase. We’ve done some workarounds with passing a lot of enums around but a dedicated framework would be best. Has anyone used things like this and if so, what would you recommend?
Have you looked into the WPILib command-based framework? Commands themselves are essentially state machines, and as a bonus you get an event bus for button and sensor bindings.
Some of my team’s old robots had code that was structured in basically hierarchical state machines. And then the IO and state estimation code was separated from that in a clean way. So at the top level you’d end up with something like this:
Is that generally the direction that you’re looking for?
We had FSM training in Java and I think it’s pretty close to C++ so examples should make sense to you. We looked at and tried a few minimal “frameworks” on the Internet such as jeasy and decided to code simply in Java enums. You might have to use C++ class or struct instead of enum, I’ve forgotten a lot of C++. The student exercises and examples are at https://github.com/SirLanceABot/Summer2019FSM .
FSMtemplate and FanFSM are the closest to what I think you want. Oblarg pointed out the need for event handling and we haven’t developed much along those lines but tried to start something not too much like spaghetti code. You can see our 2022 robot code at https://github.com/SirLanceABot/2022-Robot-Development .
I, too, encourage a good look at WPILib command-based. We’ve never used it and that’s going to be my summer project. It handles a lot that the students otherwise struggle with. We might be using it soon.
C++ has enums and enum classes. The latter has stronger type safety by preventing implicit conversion to int.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.