YAKINDU, Statecharts, Embedded, State Machine, IoT, YAKINDU Labs

Embedded system development with YAKINDU Statechart Tools and RKH

Developing complex, event-driven systems for embedded devices can be a very hard and painful challenge. State machines can help to describe the dynamic behavior of reactive systems on a higher abstraction level. In addition, there is a framework available called RKH (Reactive frameworK for Hierarchical state machines) that makes it easy to implement state machines for embedded devices. In this blog post, we are taking the next step and bring YAKINDU Statechart Tools (YSCT) and RKH together.

Bringing together the best parts of two different worlds

You may have read in our blog about the RKH state machine framework, developed by Vortexmakes. The article has been contributed by Leandro Francucci, one of the main RKH developers. This framework is a very powerful tool to develop state machines in the C programming language.

C programmers don’t need to know how the RKH handles the execution of a state machine. States, transitions, actions, entries, and so on are defined in C macros and are handled via function pointers. This is an effective approach to avoid writing endless spaghetti code describing your state machine behavior. But still, there is one main issue: The state machine must be developed in C and there is no graphical editor!

That’s why Leandro approached us with the idea to collaborate and bring both together, the RKH state machine framework and YAKINDU Statechart Tools with its graphical statechart editor. As both tools are built upon the same notion of statecharts, this shouldn’t be a big deal. And so I started to develop a code generator for the RKH framework.

 

 

The combination of YAKINDU Statechart Tools and RKH now allows you to easily create your statechart as a graphical model, while automatically generating RKH-compliant C source code in the background. This code can be compiled and flashed directly to your device.

Using the YSCT editor, you can easily build complex systems using hierarchies and subdiagrams. For the textual parts in the graphical model, an expression language is used with corresponding tool support for validation, content assist, hyperlinking, and so on.

Another benefit of using YAKINDU Statechart Tools is the possibility to simulate and test the state machine independently of the generated code. This speeds up your code-compile-test cycles.

To sum up the benefits of YAKINDU Statechart Tools:

  • Easy creation of state machines
  • Syntactic and semantic validation while modeling
  • Simulation
  • Testing
  • Test coverage reports

Currently, we are developing the RKH code generator together. We are sharing a Git repository and our knowledge in order to combine the advantages of YSCT with RKH.

Having a look at the generated code

As our statechart models are completely target language-independent, each existing model can be used to generate C source code that uses the RKH framework.

To do so, we have added the RKH code generator to our YAKINDU Labs repository. You can install it by selecting “Install more…” in the code generator selection dialog.

By using the example in the following figure, we will have a closer look at the generated C code. The statechart contains the three simple states StateA, StateB, and StateD, as well as the composite state StateC:

In the definition section, two variables are defined, an integer and a boolean one. Additionally, an incoming event is used as a trigger. The part declared at the end is an operation that receives an integer parameter and returns a boolean.

YAKINDU-Statechart-Tools-RKH-code-generator-definition-section

Each component of the graphical model will be transformed to a corresponding element of the RKH framework. Therefore, mainly the declaration of the model and the execution will be separated.

In detail, the execution of the state machine transition StateAStateB and the execution of the action associated with that transition, i.e, myInt++, will be handled in different parts in the generated code. I will describe below how YAKINDU Statechart Tools transforms the model into RKH-compliant C code, using StateB as an example.

In the terminology of the RKH Framework, StateB is a so-called BasicState. This is how RKH defines a basic stat:

RKH_CREATE_BASIC_STATE(stateName, entryAction, exitAction, parent, evtPrepro)

  • stateName: state name
  • entryAction: pointer to state entry action (function). This argument is optional, thus it could be declared as NULL
  • exitAction: pointer to state exit action (function). This argument is optional, thus it could be declared as NULL
  • parent: pointer to parent state
  • evtPrepro: it is not used

The YSCT code generator generates two header and two source files. As a first step, all basic states are declared in a header file:

RKH_DCLR_BASIC_STATE StateA, StateB, StateD;

In the corresponding C file, the basic state macro is called to define the state. The parameters are generated by the name of the state.

RKH_CREATE_BASIC_STATE(StateB, Model_enStateB, Model_exStateB, RKH_ROOT, NULL);

The state name is StateB, which got one entry and one exit action. For these actions, corresponding entry and exit functions will be generated and referenced in the macro call. Since StateB is on the statecharts root level and not placed in a composite state, it’s parent state must by convention be RKH_ROOT. RKH does not use the evtPrepro parameter; it is set to NULL.

The code generator implements the entry and exit functions in a different source file, which holds all actions:

void enStateB(Default *const me) { me->myBool = myOp(me->myInt); }

According to this scheme, the whole YAKINDU statechart model will be transformed to RKH-compliant code, without having to program a single line of code in C!

Summary

While YAKINDU Statechart Tools is (not only) a tool for graphical modeling, RKH serves to simplify your C code. YAKINDU Statechart Tools and RKH are using the same concept of statecharts to easily develop reactive event-driven systems. This allows a combination of both worlds without much effort.

This use case shows that YAKINDU Statechart Tools can easily be adapted to different target frameworks. As the modeling part of the tool is platform-independent, only the code generator has to be implemented. Moreover, having developed a good handful of code generators, our experience and our existing code base helps us to develop new code generators quickly and robustly.

Download-YAKINDU-Statechart-Tools

   
About Robin Herrmann

Robin Herrmann is working as a software engineer at itemis in Lünen since 2018. He is holding a master’s degree in “Informations- und Elektrotechnik”. He is especially interested in the development of embedded systems. Thus he extends YAKINDU Statechart Tools to support embedded development even better.