 Home Computer Science Real-Time and Distributed Real-Time Systems: Theory and Applications

# MATLAB Stateflow

MATLAB  Stateflow charts can be used to represent event-driven systems using sequential logic based on FSMs. A Stateflow chart is a graphical representation of an FSM. The states and transitions form the basic building blocks of the sequential logic system used by Stateflow.

The basic module for building a model using Stateflow is a chart, which can be included in a Simulink® model like any other block from the Simulink Library. A chart allows a developer to define an FSM using graphical blocks like states, transitions, conditions, actions, input-output data, and events. Details of MATLAB Stateflow are available in Reference 5. In this section the Stateflow representation of an FSM is illustrated with a simple example: the vending machine represented in Figure 4.2 as a Moore machine.

With the FSM defined by Figure 4.2, the corresponding Stateflow implementation is represented by the chart shown in Figure 4.7.

In Figure 4.7, the states are represented by the rectangular boxes and the transitions by the directed lines connecting two states. The condition for each transition is enclosed between two square brackets [], and the corresponding transition equation, which is executed once the condition is evaluated to be true, is enclosed between two curly brackets {}. The variables coin, op, and w are inputs to the chart representing, respectively, the denomination of the coin (i.e., 1€ or 0.5€); the selected option, that is, 1 for coke and 2 for water; and a flag indicating that the user is collecting the bottle. Similarly the variables FIGURE 4.7

Representation of a vending machine using MATLAB Stateflow.

disp and drink denote the pending amount and the drink dispensed, which is again 1 for coke and 2 for water. The directives entry and exit denote the actions taken by the chart when a particular state is entered and when an exit occurs, respectively. Similarly, the directive during can be used in states s4 and s7 to activate a function dispense_bottle() to actually dispense the bottle containing the selected drink. This is not shown in the Stateflow chart and is left as an exercise for the reader. Another implicit assumption is this: the machine does not accept a fresh input until the present user collects the bottle when the flag w is reset.

The functioning of this chart can be understood by analyzing the corresponding Simulink model represented in Figure 4.8.

The model shows a case where the user selects the option op = 2, that is, water, and inserts a 1€ coin. With this, the activated state in Figure 4.7 becomes s4, as shown in Figure 4.9.

The state s4 shall remain activated until the user withdraws the bottle, which is accomplished by clicking on the manual switch, shown in Figure 4.8, to move it from 0 to 1, thus changing the variable w accordingly. This causes a transition from state s4 to s0, as shown in Figure 4.10.

It is clearly seen from an examination of Figures 4.7 through 4.10 that transitions from state s0 remain blocked until the flag is reset by switching back the manual switch to position 0, which indicates that the user has completed the transaction.

The chart shown in Figure 4.7 is very basic and can be implemented in a variety of ways using more options detailed in Reference 5. FIGURE 4.8

Simulink model for a vending machine. FIGURE 4.9

(See color insert.) State activation in the chart of Figure 4.7 corresponding to the model in Figure 4.8.

NUMERICAL AND ANALYTICAL PROBLEMS

4.1 A full-adder circuit accepts two bits Xu X2 as inputs and produces an output bit Y and a carry bit C. Represent this as (a) a Mealy machine and (b) a Moore machine. Which representation requires fewer states?

86

Real-Time and Distributed Real-Time Systems FIGURE 4.10

(See color insert.) State activation in the chart of Figure 4.7 corresponding to the model in Figure 4.8 during retraction of bottle. FIGURE 4.11

• 4.2 Represent the traffic light controller of Figure 4.1 as a Stateflow chart using a suitable representation of the FSM, that is, a Mealy or a Moore machine.
• 4.3 Augment the model of Figure 4.8 by adding another Stateflow block that uses another chart to represent the dispenser using the variable drink as the input.
• 4.4 Consider a toy comprising two cars that run on electric tracks, as shown in the Figure 4.11a . Two toy cars ply on the two tracks X and Y, and the speed is controlled by varying the current in the tracks using a microcontroller. Each track has one pair of sensors to monitor approach and recession of the cars relative to the crossroad (Figure 4.11b). Model the system as (a) an event-triggered system and (b) a time-triggered system and represent the control task using MATLAB Stateflow so that car on the X track stops when both cars approach the crossroad simultaneously.

# References

• 1. Kopetz, H. Real-Time Systems: Design Principles for Distributed Embedded Applications. Springer, 2011, 978-144, 198, 236.
• 2. Alur, R. and Dill, D. L. A theory of timed automata. Theoretical Computer Science, 126(2), 183-235, 1994.
• 3. Henzinger, T. A. The theory of hybrid automata, in Verification of Digital and Hybrid Systems, eds. M. K. Inan and R. P. Kurshan. Springer (NATO ASI series), 2000, 265-292.
• 4. MATLAB. www.mathworks.com.
• 5. MathWorks. Stateflow User's Guide. http://nl.mathworks.com/help/pdf_doc/ stateflow/ sf_ug.pdf.
• 6. Mandal, T. Development of a microcontroller based Automatic traffic control system using slot cars. Master's dissertation, Jadavpur University, 2013. http:// dspace.jdvu.ac.in/handle/123,456,789/28,411?mode = full.

 Related topics