Home >

Modern Embedded Programming with Hierarchical State Machines and Active Objects

Miro Samek - Watch Now - Duration: 41:16

Perhaps you've heard about hierarchical state machines (UML statecharts), modeling tools and automatic code generation, but have never tried them or seen them used in practice.

This presentation will show you, step-by-step, the process of designing and implementing a fun "Fly 'n' Shoot" game to run on an embedded ARM Cortex-M board as well as on your PC.

Specifically, you will see how to partition a problem into loosely coupled, event-driven components called active objects and then how to design interactions among them using sequence diagrams.

Next, you will see how to elaborate the internal behavior of identified active objects with modern hierarchical state machines.

And finally, you will see how the state machines are implemented in C and how this code can be generated automatically.

The session will utilize hands-on demonstrations using EFM32 Pearl-Gecko ARM Cortex-M4 board, the QP/C real-time embedded framework and the QM modeling and code-generation tool.
M↓ MARKDOWN HELP
italicssurround text with
*asterisks*
boldsurround text with
**two asterisks**
hyperlink
[hyperlink](https://example.com)
or just a bare URL
code
surround text with
`backticks`
strikethroughsurround text with
~~two tilde characters~~
quote
prefix with
>

MiroSpeaker
Score: 0 | 5 months ago | no reply

I have just added several answers to questions asked during the live Q&A session. Please check out that discussion.

MarkW
Score: 1 | 5 months ago | 1 reply

Great implementation and really awesome design paradigm that I would like to try get started at the company I work for (which is "unfortunately" very large with very 'set' ways about current designs. But perhaps a new project will come along where it can be introduced.
I am thinking about how this can be applied where there is a object that reads/writes data to NvM. Other tasks might need to read/write, so eventually you will have a kind of 'blocking'. I can think of a task that needs to read data and then perform an action, so it posts a 'read event' to an NvM object, and then subscribes to a "READ_DONE" event. Is this the right way of thinking? And then the NvM object would have a state machine that is serviced on a tick (since it might need to wait for hardware)?

MiroSpeaker
Score: 0 | 5 months ago | 1 reply

If you encapsulate the NvM inside a dedicated AO, you won't have blocking of other AOs, because you have queuing of events. But you must be careful to adequately size the event queue of the NvM-AO and to also adequately size the event pool from which you'll allocate the events.

You typically don't need to have "READ_DONE" event, just as empty acknowledgements. Instead, you will have NVM_DATA event, which will carry the requested data inside.

Regarding the internal structure of the NvM-AO, yes, it will have a state machine, or maybe two state machines one for reading and an "orthogonal" component state machine for writing (similar to the Mine components of the Tunnel-AO in the game). If the NvM-AO needs to wait for some timeouts, it can use Time-Events (please watch my last year's presentation and the little FreeACT framework).

In the end, having a dedicated NvM-AO has numerous benefits compared to the traditional sharing of the NvM and protecting it with a mutex.

  • NvM-AO will resolve any potential conflicts. For example, two AOs trying to write to the same location at the same time will be naturally sequenced by the event queue of the NvM-AO and RTC (run-to-completion) event processing.

  • NvM-AO can use the "Deferred Event" state pattern to appear always responsive to events.

  • NvM-AO can also much easier implement any wear-leveling algorithms than an alternative when access to NvM is "distributed" among many threads.

  • NvM-AO with clean event-driven interface is more portable if the underlying NvM interface or technology were to change.

  • Finally, note that a dedicated NvM-AO will not slow down the access to the NvM, because the bottleneck is not the CPU. Rather, the bottleneck is the NvM interface and potentially the erase operations when writing.

Of course, this is just your example of NvM management. But similar arguments can be made about most other resources--it usually is better and cleaner to have a "manager" of "broker" to resolve conflicts, streamline access, etc.

MarkW
Score: 0 | 5 months ago | no reply

Thanks for the detailed response!
I can definitely see the advantages of this event driven AO paradigm, and will definitely try it when the opportunity arises in my professional career (also in a personal project if I get time for that)

malreverse
Score: 1 | 5 months ago | 1 reply

Are there good SDD's (software design documents) out there that are available for download? Thank you.

MiroSpeaker
Score: 1 | 5 months ago | no reply

The design documentation for customer's projects done with QP is typically proprietary, so I can't point you there. But it's a good idea to provide an example of an "open source" SDD even for a small application like the "Fly 'n' Shoot" game. I will put it on my TODO list and try to deliver this in the upcoming QP releases.

Harri
Score: 1 | 5 months ago | no reply

Thank you for the presentation!
I definitely need to try this pattern both in embedded and Windows ports. Also glad to see Gecko boards represented :)

Dan6077
Score: 2 | 5 months ago | 1 reply

I really like the concept of Active Actors
It looks a little like the code that one would create in Visual Basic and attach to a Button Click

MiroSpeaker
Score: 1 | 5 months ago | no reply

Visual Basic is an example of an event-driven architecture, but active objects, at least in the QP framework, have state machines. This is a much more powerful mechanism than simple "event-handlers" from Visual Basic, because a state machine remembers the context (as the current state) from one event to the next. In my last year's talk I exactly used the Visual Basic calculator example to illustrate the problems with simple "event-handlers". I'd highly recommend that you watch that presentation, because it also introduced state machines.

Javi
Score: 0 | 5 months ago | 1 reply

Thank Miro, great talk, amazing job!
just a quick question about event passing. How are they sent/receive? Do you use a queue? a mailbox?
This is a new paradigm for me and I am impressed how nice and clean it is! looking forward to try it out!
Thanks again for sharing
javi

MiroSpeaker
Score: 2 | 5 months ago | no reply

Yes, there is an event queue in each active object. If you are interested in the details, please watch my last year's presentation. Among others, I presented there a minimal, but fully functional implementation of the "Active Object" design pattern on top of FreeRTOS. That little framework was called "FreeACT" (I mentioned it in this talk) with the code available on GitHub (see https://github.com/QuantumLeaps/ ).

mgaron
Score: 2 | 5 months ago | 1 reply

I'm already sold and using the QP framework, yet attending these sessions year after year, still amazed by the simplicity and elegance of it. Whenever possible, I avoid going back to traditional RTOSes.
I still learned a 2-3 things from this presentation. Thanks a lot and keep up the good work.

MiroSpeaker
Score: 1 | 5 months ago | no reply

You seem to be well informed about the method, but I'm glad that you were still able to find something new in this presentation. The concepts of active objects and state machines can be implemented in many different ways. But as usual, the devil is in the detail, so to be really useful for deeply embedded MCUs the abstractions have to be implemented efficiently and the supported features need to be chosen very carefully.

KushP
Score: 2 | 5 months ago | 1 reply

Is there any advantage of using a sequential code flow (like RTOS or foreground/background) over event-driven code flow (RTEF)?

MiroSpeaker
Score: 1 | 5 months ago | no reply

You might want to watch my last year's presentation, were exactly I explain the advantages and disadvantages of sequential and event-driven paradigm.

MiroSpeaker
Score: 3 | 5 months ago | no reply

I hope you enjoy the presentation. Please join me for live Q&A session. I'm really looking forward to interesting questions and discussion!

OUR SPONSORS