📜 ⬆️ ⬇️

State machines in the SimInTech dynamic simulation environment

Introduction


SimInTech is a medium for creating mathematical models of any systems, whose dynamics equation can be represented as input-output relations (DataFlow representation). To implement the approach of finite automata in the SimInTech environment, there was a conditional execution unit of the submodel that provided stopping and starting the simulation according to the condition coming from outside. This approach provides an opportunity to divide the general model into states and at each moment of time only those submodels are calculated, whose execution conditions are set to true. However, such an implementation of the automaton approach is not very convenient from the point of view of clarity, and required additional adjustment of the system parameters.

Currently, all the necessary settings are implemented in a special library of blocks, which provides for the creation of system models in the form of finite automata (State Flow view) and can be used to create control models.

This article shows an example of using elements of the State Machine Library to create a control system.

Formulation of the problem


To demonstrate the simulation with the use of finite automata, the model of control of a hot-water boiler is used. If the temperature is lower than the set temperature, the controller turns on the heater for a period of not more than 20 seconds, with a delay between 40 turns on, as well as an indication of its state by turning the indication lamp on and off.
')
When the heater is on, the heating power is constant and heats up 1 liter of water by 1 degree per second.
When the heater is turned off, the losses are constant and such that they cool 1 liter of water by 0.1 degrees per second.
The amount of water is 25 liters.

The operation algorithm of the temperature maintenance automatic works as follows:

As input effects in the regulator are given:
1) The set point (setpoint) for the temperature to be maintained.
2) The current temperature value received from the temperature sensor.

Model description in terms of finite automata


The operation of the state machines of the heater controller can be described as follows:
The machine has two states on and off.
At the initial moment of time, the active state is off.

The state is off.
No heating.
The indicator flashes green with a frequency of 5 seconds.
Transition between states: from the off state to the on state is performed if two conditions are met: the controller is in the off state for more than 40 seconds and the temperature from the sensor is below the specified setpoint.

Status enabled
Heats up.
The heating indicator blinks red with a frequency of 1 second.
The transition from the on state to the off state is performed in two cases:
1) Heating was carried out for 20 seconds.
2) The temperature from the sensor has reached the setpoint.

A schematic of finite automata that implements this algorithm is shown in Figure 1:

image
Figure 1. The scheme of the state machine

The implementation of the state machine in SimInTech


Creating an object model

The model of the heater is created using standard SimInTech modeling tools and is a submodel, to the input of which a heater switch is turned on (0 is turned off, 1 is turned on), the water temperature is calculated at the output.

Place the “Sub-model” block from the “Substructures” tab on the diagram and assemble the model as shown in Figure 2.

image
Figure 2. Heater model

A logical variable is supplied to the input of the unit - a sign of the heater operation, 1 - the heater is turned on, 0 - the heater is turned off. This variable is inverted and fed to the key "A3". Depending on this variable, the key transmits to the output the values ​​obtained from blocks of the type “Constant”: 1 - heating, or -0.1 - cooling. The output of the key block "A3" is integrated by a standard integrator. Thus, temperature values ​​are formed. The parameters of the integrator block are shown in Figure 3.

image
Figure 3. Integrator parameters

The initial temperature is 15, the gain is 1/25 (25 liters heat up at a given speed).
To test the operation of the heater model, we feed the value from the “Meander” block, output the output to the graph along with the input. Scheme of the model is shown in Figure 4.

image
Figure 4. General scheme for testing the heater model

The simulation result is shown in Figure 5. During periods of time when the meander value is 1 (imitation of the heater on), the model at the expense of the integrator accumulates temperature at the heating rate. During periods when the meander values ​​are 0 (imitation of the heater turned off), the temperature decreases with the cooling rate.

image
Figure 5. Heater model operation schedule

Thus, we made sure that the created model can be used to test the operation of the heater controller.

Creating a heater control unit based on state machines

To create a controller, we use the “State machine state map” block on the “State machines” tab. See Figure 6:

image
Figure 6. The tab “State machines”, the block “Carats of states of a finite state machine”

Place the block on the circuit together with the “Heater model” block created earlier. Since this block is based on the standard SimInTech submodel, you can work with it in the same way as the submodel: add inputs and outputs, add signals, add properties, declare and use internal variables, link it to the signal database.
Enter inside the state machine of the state machine with a double click on it. The only difference from the standard submodel is the presence of an additional block on the internal circuit, the “State selection block” in the left corner of the circuit (see Fig. 7)

Before proceeding to the creation of a state map, we will prepare in this block the inputs and outputs necessary for the connection of the controller with the heater model.

The setpoint and actual temperature will be used as the input, the on / off state of the heater will be calculated as the output and the light will be displayed.

Place two blocks “Input port” and two “Output port” blocks from the “Substructure” tab on the diagram and change their names as shown in Figure 7:

image
Figure 7. Prepared for data exchange block "Map of States of the state machine"

At this stage, you can exit the block by double-clicking on the empty schema meta and at the top level, the block “State machine of the state machine” will have blocks - input and output ports - for connecting to the general scheme. Connect the circuit and sign the blocks, as shown in Figure 8:

image
Figure 8. The block "State of the machine"

Put two blocks “State of the machine” on the circuit inside the block “Heater Controller”. One of the blocks will simulate the state off, the other - on. Sign the blocks accordingly, as shown in Figure 9.

image
Figure 9. The block "State of the machine"

Put two blocks “State of the machine” on the circuit inside the block “Heater Controller”. One of the blocks will simulate the state off, the other - on. Sign the blocks accordingly, as shown in Figure 10.

image
Figure 10. The controller circuit with two added states

For the state map to work correctly, you must specify the initial state of the system. In our case, the initial state of the machine will be off. Select a block by clicking and right-click. In the drop-down menu, select "Properties". A window for editing properties will appear, in which you need to select yes in the only “Default” property (see Fig. 11):

image
Figure 11. Setting properties of the first active state in the map

Before proceeding to the creation of the logic of operation of automata and transitions between states, we will add the “More” logic block to the circuit and compare the set temperature with the temperature obtained from the model. Thus, we get a new logical variable that takes the value 1 when the temperature is less than the setpoint, and 0 when the temperature is greater than or equal to the setpoint.

Add the “More” block and connect the circuit as shown in Figure 12:
image
Figure 12: Comparison of the set and measured temperature

Please note that SimInTech does not allow connecting the output of the “More” block to the input of the “Automaton state” block, because in the first case the line provides the transfer of value between the blocks, and in the second - the transition from state to state.

Creating state logic


Double-click on the state block is off to make the transition to the internal structure of the block. Diagram of the internal initial structure of the block is shown in Figure 13:

image
Figure 13. The internal structure of the block "State of the machine"

The internal structure by default contains the “Execution of execution condition” block at the top of the diagram. This block receives a command to activate the state from the top-level structure (“State Selection Block”); the user does not need to change this block.
In addition, the block contains two special “input-output” blocks:
1) "FROM_STATE" - entry into the state;
2) "TO_STATE" - exit from the state;
These ports correspond to the input and output in the block on the top-level diagram. These blocks are located on the “State machines” tabs: the “Status Input” and “Status Output” blocks, respectively.
The operation scheme of the automata provides for a state that one output is turned on (if two conditions coincide: it is turned off for 40 seconds and the temperature is lower than the set one), but it can be returned to the state by two conditions: either the heater is operated for 20 seconds or the set temperature is reached. Add a block “Status input” to the scheme from the tab “State machines” (see fig. 14):

image
Figure 14. “State machines” tab “Status input” block

Since to calculate the condition for exiting the state, we need the value of external comparison of the set temperature with the measured one, add the “Input port” block from the “Substruktra” tab to the circuit (see fig. 15)

image
Figure 15. “Substructures” tab “Input port” block

To get the results of calculations performed in this state, add the “Output port” block from the “Substructures” tab to the scheme (see Fig. 16):

image
Figure 16. “Substructures” tab “Output port” block

Change the name of the port “Input” to “Cold” - here there will be a variable comparing the measured temperature with the set one, and the name of the port “Output” to “On / Off” - here we will read the sign of switching the heater on and off.
The general scheme prepared for creating state logic is presented in Figure 17.

image
Figure 17. Block “State of the machine” prepared for the creation of logic

In the off state, the machine should be within 40 seconds. If after a given period of time the water temperature is less than the specified one (input Cold = 1), it is necessary to switch to the on state. For the formation of the delay, use the block "block of state exposure". (see figure 18):

image
Figure 18. “State machines” tab “Status shutdown” block

This block, connected to the output of the block “Run condition”, switches on the timer and generates a signal for the end of time at a specified interval. Place the block on the field and set the shutter speed to 40.

The end-of-exposure signal is combined with an external signal (old) through a logical block "and". If they coincide (the off time is over and the temperature is below the setpoint), then we form a command to exit the state and at the same time send an output signal to turn on the heater. The general scheme of the logic of work is presented in Figure 19.

image
Figure 19 The logic of working in the "off" state

When forming the output from the state block, it is necessary to take into account that the block in the active state constantly calculates and generates the value of the output signals (in our case, this is output On / Off). At the completion of the work status and exit from it on the line remains the last calculated value. In our case, the value 1 will be formed at the exit from the state and it will remain until the state is active again.

After completing the creation of a model of the work in the off state, we will rise to a higher level by double clicking on an empty part of the scheme (see Fig. 20).

image
Figure 20. Diagram of the state map after editing the “off” block

The appearance of the status block is turned off in the diagram has changed. After adding the input and output ports inside the status block, three input ports and two output ports appeared on the outside of the block. The color of standard ports for transmitting signals is black, the color of ports for connecting state transition lines is red. The user cannot connect data lines to the status ports and, conversely, the state transition lines to the data ports.

Let us turn to the formation of the logic of work in the enabled state.

Go inside the block by double clicking on the image. For this block there will be two conditions for exit from the state. By default, there is one “Status Output” in the block. Place the second block “Status output” on the state diagram (see fig. 21):

image
Figure 21. “State machines” tab “Status output” block

To form the exit condition, it is necessary to transfer the temperature comparison value to the block. To do this, we will add to the scheme the “Output port” block from the “Substructures” tab (see Fig. 15).

To control work for 20 seconds, we put on the diagram the “State shutdown” block (see Fig. 17), and set it as the “Time to transition to another state” parameter - 20.

The general structure of the logic of the block will look as shown in Figure 22:

image
Figure 22. Work structure in the “on” state

When switching to the on state, the holding timer is activated; after the time has elapsed, a transition is made to the first output.

At the same time, the value obtained from data entry is analyzed. At this port, we will submit the result of comparing the set temperature with the measured one. If the result is 1 (true), the temperature in the boiler is lower than the set one and it is necessary to continue heating. If the value is 0 (false), then the heating must be completed. The “operator NOT” block provides the inversion of the input and the generation of a command for the transition from the state enabled by the second output (see Fig. 22).

Let's move to the top level of this scheme. Two additional ports appeared in the automaton status block. And now the block contains:
• one input port to go to the state (red);
• one input port for data (black);
• two output ports for transition from the state (red color).
Connect the ports as shown in Figure 23:

image
Figure 23. The logic of the state machine in the assembly

Thus, we have prepared a state machine-based control model for testing. At this stage, we connected the output to the indicator to the power-on port to eliminate the error associated with the unconnected input port.
The indicator port at this stage displays the status (on / off). We will do the modeling of the intervals and colors of the indicator later.

Call the settings, calculation by clicking on the button "Calculation parameters" (see. Fig. 24):

image
Figure 24. “Calculation Parameters” button

On the tab "Calculation Parameters" set the "Final calculation time" 700 seconds. (see fig. 25)

image
Figure 25. Calculation parameters

On the “Synchronization” tab, check the box labeled “Synchronize with real time” and “Acceleration rate” - 100 (see. Fig. 26).

image
Figure 26. Calculation parameters. Synchronization

By setting the synchronization mode, we will be able to observe the switching between the states of the model directly in the diagram. Otherwise, the calculation takes place too quickly and the switching of the state can be monitored only by graphs.
Run the model on the calculation by clicking on the start button (see. Fig. 27).

image
Figure 27. Running on model calculation

If you assembled everything according to the instructions, then you received a water heater model controlled by the system based on the finite state machine logic. A switch from state to state can be observed on the controller circuit during simulation, the state active at the moment of simulation is displayed in green (see Fig. 28).

image
Figure 28. Scheme of the finite automata in the simulation mode

The simulation results are presented in Figures 29 (heater operation mode) and 30 (boiler temperature). On the graph of the operation mode it can be seen that at the initial moment of time the machine is in the off state (see fig. 29).

image
Figure 29. Heater Operation Mode

image
Figure 30. Heater Temperature

The temperature graph (Fig. 30) shows that at the initial moment the temperature is 15 degrees and decreases with the cooling rate. After being in the off state for 40 seconds, the transition to the on state occurs (see fig. 29). In this state, heating occurs at a rate specified in the heater model (see Fig. 30). After working off for 10 seconds, the transition to the off state occurs (see fig.29). These cycles are repeated until the temperature reaches a setpoint of 20 degrees. After that, the on-cycle is shortened, since the transition from the on state to the off state takes place when the setpoint for temperature is reached. This can be seen on the “Mode of operation” graph after 500 seconds of calculation (see fig. 29).

Thus, we were convinced that the control model based on the finite state machine logic works and maintains the set temperature in the heater.

Please note that the on / off signal (on / off) we form in the unit is turned off, since this state is active by default and its outputs are defined at the start. When exiting from this state, we change the signal value by 1 (see Figure 18) and it remains in the enabled state (1) as long as the state is not active. Since there are only two states in this state machine, we can send this signal to the output from the state map. In any active state, the signal value will be determined and true. If we have more than two states, then the output from the “State Map” submodel should be determined or calculated in each state block, otherwise a situation is possible using variables that are not defined due to an inactive state.

The next part of the article will be:
- Demonstration of other data exchange capabilities in the state machine schema.
- The process of creating a nested state machine for modeling the operation of the indicator. Part 2 is here ....
- The process of obtaining a C code from the created model of the control system.

A trial version of SimInTech can be downloaded from the developer's site. Here ...

Source: https://habr.com/ru/post/307090/


All Articles