cancel
Showing results for 
Search instead for 
Did you mean: 

Statemachine or Statechart implentation in C

LMorr.3
Senior II

Does anyone know the best way to implement a statechart or statemachine in C? Are there specific libraries required or should I consider just using simple statemachine tables with switch statements?

Maybe statechart is overkill for my app and using a simple statemachine will suffice, but looking for design options to choose from.

Thanks!

7 REPLIES 7

> best way

There is no "best way" in programming, and even least so in microcontrollers programming.

Is your application that complex that it grants the time/effort to learn and use a library? Go ahead. Is it not? Just use a switch and enums with appropriately named states - that's simple to understand, portable and always available.

JW

AScha.3
Chief III

...so switch(state) is your friend . :)

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III
LMorr.3
Senior II

Thanks for the suggestions. I had started by using switch/enums and then did not want to re-invent the wheel so started looking at how other have implemented state machines in c for uCs. I may end up just sticking with switch/enums but I'm still looking at solutions to gain a more complete understanding.

I could implement something a bit more functional that switch/enums like this one which uses a pointer to state functions:

http://www.heptapod.com/lib/statemachines/

I started watching the QL tutorial which seems to be a great solution for my project, however I now see I would need to purchase a license to use the generated C code in my final product? I hope I have misunderstood their license agreement, since I thought the final genrated C code was not dependant on a QL framework library to run. It would be great if I could visually create my model/state machine and then generate standard c code with no 3rd party/QL library dependencies.

Update: I'm considering using Super Simple Tasker together with it's queued multiple 'activation' feature to implement a FSM. Each 'task' will represent a state ( Active Object? ) which runs to completion.

LCE
Principal

KISS! ;)

=> so one more vote for switch/case with enums

==> other people and you in the future might still understand what you have done.

LMorr.3
Senior II

Thanks for all the info. I was not able to 'easily' setup the 'SuperSimpleTasker' on the STM32F407G disco board. I'll try just using simple switch/enums as suggested to KISS to see how far I get or if a scheduler is needed. The SST hooks into hardware interrupts making things more complex which goes against KISS. The scheduler will 'run tasks until complete' (RTC) anyways so the only benefit of a tasker in my case would be to preempt lower priority tasks which can be achieved using hardware interrupts.

LMorr.3
Senior II

Thanks to Pavel for suggesting the state-machine.com as the videos there have been informative.

I'm discovering some limitations to using case/enum, as well as table based state machine implementations. For example adding more states/events/actions, handling hierarchical states, and triggering entry/exit actions.

I'm now considering an event based state machine, using the Active Object pattern together with uC/OS RTOS? The following video shows an example of an 'optimal' c implementation:

https://www.youtube.com/watch?v=FCymm6PBtOs&list=PLPW8O6W-1chwyTzI3BHwBLbGQoPFxPAPM&index=40

I'm curious to find out how others are approaching this 'modern' state machine design pattern.