Wednesday, October 14, 2020

[Example] Creating a counter in Simulink (State chart approach)

In my previous post, I've covered the topic of creating a counter in Simulink, using a block diagram approach. While not complicated, using a block diagram for this functionality may not be ideal in terms of legibility and size of your model. I am going to write a detailed tutorial on the topic, but generally, keep in mind that for event-based logic, time-related operations (counters/countdowns) and complex logic where switching from a state to another does not necessarily take place in sequential order, Stateflow is a much better suited tool. Let's get to it. 

1) Objective
 Create a counter, either based on the simulation time or an external event, using Stateflow.

2) Model configuration

Solver -> discrete, fixed simulation step. Step-size -> 0.01s.

3) Principle of operation:

Just as before, we will be adding a constant value equal to the simulation step every time the computation is reiterated. We will also keep in mind that we must not perform the addition at 0s.

4) Solutions:



This is a very simple, yet effective solution. The first simulation step will be dedicated to the initialization of our counter: {counter = 0;}. Keep in mind that instructions should end with a semicolon, or else the output will be printed in the MATLAB Command Window every time the instruction is executed. The during keyword inside our Comp state indicated that the instruction written after it will be executed every simulation step. Therefore, our counter will be incremented by 0.01 every time 0.01 seconds pass, which is what we want.

I should mention, though, that in automotive, Simulink models are used to generate C code that is embedded in the various control units of the vehicles. I avoid using built-in functionalities like action types, in order to avoid getting undefined behavior when generating code. If I were to implement this functionality, I would rather use the solution below.


After performing the initialization using the default transition, we will remain in the Comp state throughout the simulation. Both ends of the inner transition are connected to the same state (I've used opposite edges to improve legibility, but you can connect them as you wish, even on the same side of the state, although it is bad practice). This transition will take place every simulation step, while we are in the Comp state. The instruction will increment our counter by the simulation step. Therefore, we will have the same functionality as in the previous case, but without a state action keyword.

NOTE 1: We can easily observe how quickly this temporal functionality has been implemented when compared to the block diagram approach. Both methods are, although, equally valid. 

NOTE 2: I wanted to specify this in the previous post, but it was already getting quite big. These examples are not meant to teach the meaning of each block, but rather how to construct a specific functionality. I will create special [Tutorial] type posts for those who have no prior knowledge of how to use the blocks to build models. The main purpose of this blog will remain, however, the creation of specific behavior models, as I have seen throughout the years that there aren't many tutorials on this particular subject online.

No comments:

Post a Comment