Home > On-Demand Archives > Talks >

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.
italicssurround text with
boldsurround text with
**two asterisks**
or just a bare URL
surround text with
strikethroughsurround text with
~~two tilde characters~~
prefix with

Score: 0 | 2 years ago | no reply

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

Score: 1 | 2 years 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)?

Score: 0 | 2 years 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.

Score: 0 | 2 years 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)

Score: 1 | 2 years ago | 1 reply

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

Score: 1 | 2 years 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.

Score: 1 | 2 years 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 :)

Score: 2 | 2 years 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

Score: 1 | 2 years 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.

Score: 0 | 2 years 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

Score: 2 | 2 years 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/ ).

Score: 2 | 2 years 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.

Score: 1 | 2 years 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.

Score: 2 | 2 years ago | 1 reply

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

Score: 1 | 2 years 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.

Score: 3 | 2 years 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!