Skip to content
0:00 / 0:00

#hierarchicalstatemachines#uml#qp/c

#42 State Machines Part-8: Semantics of Hierarchical State Machines

Quantum Leaps, LLC

74K subscribers • 20 videos on Hallway

YouTube
  • Recommended by 1 person: 1 in engineering communities.
  • A classic. People kept recommending it from 2021 to 2025.
  • Presented by Miro Samek, founder of Quantum Leaps
  • Linked in the descriptionQuantumLeaps/modern-embedded-programming-course
  • Made by the vendor. The company whose product it covers made or sponsored it.

Intermediate. Assumes working knowledge.

19K views5 years ago1 recommendation

Systems programmingSoftware designEmbedded systemsDesign patternsSoftware architecture

Miro Samek explains the execution semantics and transition behaviors of hierarchical state machines with multiple levels of nesting.

This lesson continues the subject of STATE MACHINES. Today you will dive deeper into the semantics of hierarchical state machines. Specifically, in this lesson, you will examine a hierarchical state machine with multiple levels of state nesting. You will then see how this more advanced state machine handles various transitions and exactly which actions are executed in which order. Contents with Timestamps: ------------------------- The QHsmTst example in the QP/C framework directory Download for the book "Practical UML Statecharts in C/C++" Explanation of the QHsmTst example Concept of "Software Tracing" Code generated from QHsmTst model Personal story and a case for code generation Building the QHsmTst example from the command prompt Explanation of the semantics Semantics of the top-most initial transition Semantics of regular state transitions Least Common Ancestor (LCA) state Semantics of internal transitions Semantics of self transitions Semantics of guard conditions (on regular transitions) Guaranteed cleanup and initialization High-Level Transitions Guard conditions (on internal transitions) Copying and modifying the QHsmTst example Defining the QPC environment variable Adjusting the Makefile The explicit [else] guard condition Cleanly terminating a state machine Summary ------- Resources: Companion web page for this video course: state-machine.com/quickstart GitHub repository for projects for this video course: github.com/QuantumLeaps/modern-embedded-programming-course Transcript of this lesson: state-machine.com/course/lesson42.txt Recommended Reading: "Practical UML Statecharts in C/C++, 2nd Edition: Event-Driven Programming for Embedded Systems" by Miro Samek (state-machine.com/psicc2) Music credits: The background music comes from: bensound.com/royalty-free-music/track/straight

Why it's here

1 person recommends this

What people say. Recommended as an in-depth explanation of hierarchical state machine semantics and transition execution order.

u/active-objectin r/embedded • Recommends

On “Do you use HSM (Hierarcical State Machines)?”

Yes, I've been using and advocating HSMs for the past 20 years. I've found them to be very useful in even the smallest embedded MCUs (e.g., see my article "UML Statecharts at $10.99") Regarding the overhead of ENTRY, EXIT, INIT, etc. it can be small, but you need the right HSM implementation in C or C++. (The boost::sml suggested in the other comment is NOT the right implementation for deeply embedded systems, like ARM Cortex-M MCUs). Here, I would recommend my YouTube video "Optimal State Machine Implementation in C". While you are at it, you might want to also watch the following YouTube videos about Hierarchical State Machines: * What is a Hierarchical State Machine? * [Automatic Co…

u/active-objectin r/embedded • Recommends

On “What is your way to go for state machines in C++?”

All these questions about state machine implementations, specifically for embedded systems are covered in the "State Machines" playlist on YouTube. Among others, you'll find there an overview of implementation techniques, from nested-switch statements, state-tables, and state-machine DSLs with "state machine compilers", to implementations based on function pointers and classes. You will also see a discussion of modern hierarchical state machines, their precise semantics, and their implementation. Speaking of which, for implementing state machines in C++ I would recommend the technique where you *inherit* a state machine base class (as opposed to containing a state machine). That way,…

u/active-objectin r/embedded • Recommends

On “Generic HSM implementation in C”

Hierarchical state machine videos: \- What is a Hierarchical State Machine? \- Semantics of Hierarchical State Machines

u/active-objectin r/embedded • Recommends

On “Your experience with UML for embedded firmware design and debugging”

"Napkin-style UML" used to sketch some informal class, sequence, and state diagrams has certain benefits because it allows some (more visual) people to better "see" the design. But there is much more potential, which is very rarely realized and that is code generation. Here the most constructive for code generation are the UML hierarchical state machines. Typically, I see quite negative (and sometimes even hostile) reactions when UML is mentioned in embedded forums. This is probably the effect of heavyweight tools and high-ceremony methodologies. But parts of the UML can be had with much more lightweight, "low ceremony" tools as well.

u/active-objectin r/embedded • Recommends

On “State Machines in embedded?”

Here is the YouTube playlist that explains state machines in the context of embedded systems: * State Machines Part-1: What is a state machine? * State Machines Part-2: Guard conditions * State Machines Part-3: Input-Driven State Machines * State Machines Part-4: State Tables and Entry/Exit Actions * State Machines Part-5: Optimal Implementation in C * State Machines Part-6: What is a Hierarchical State Machine? * State Machines Part-7:Automatic Code Generation * State Machines Part-8: Semantics of Hierarchical State Machines

u/active-objectin r/embedded • Recommends

On “Is preemptive RTOS costing you too much?”

Yes, RTIC in Rust represents a similar approach to the QK kernel. Both are also related to the RTFM framework (Real-Time for the Masses in Rust) and to the SST (Super-Simple Tasker in C or C++). QP is much more than just a kernel, however. When you start doing event-driven programming seriously, you need extensible events (with parameters/payloads), event delivery mechanisms (posting FIFO/LIFO, publish/subscribe), event memory management (for mutable events), time events (one-shot and periodic), and above all, hierarchical state machines. All of this is known as the Active Object model of computation, and QP provides a lightweight implementation of it for hard real-time embedded systems,…

u/active-objectin r/embedded • Recommends

#42 State Machines Part-8: Semantics of Hierarchical State Machines