Engee documentation
Notebook

Building and running a state flow diagram

State Flow Diagram is a graphical representation of a finite automaton consisting of states, transitions, and data. You can create a diagram to determine how the system responds to external input signals, events, and time conditions.

Let's look at how to create logic for a half-wave rectifier.

Constructing a state flow diagram

The diagram contains two states labelled On and Off. In the On state, the output signal of the diagram y is equal to the input x. In the Off state, the output signal is zero. When the input signal crosses some threshold t0, the diagram transitions between these states. Actions in each state update the value of y at each time step of the simulation.

To create and simulate this diagram follow the steps below.

Open the editor

  1. Create a new model and add a Chart block to the modelling field.
In [ ]:
engee.create("rectifier_start1", sync_gui=true)
Out[0]:
Model(
	name: rectifier_start1
	id: 1796d302-d597-4d00-9e82-cb975c02cb75
)
In [ ]:
engee.add_block("/StateMachines/Chart", "rectifier_start1/", sync_gui=true)
Out[0]:
"rectifier_start1/Chart"
  1. Double-click the Chart box to open the editor.

The main components of the Chart block editor are the chart canvas, the object panel and the settings panel where you can set signals.

  • The Chart Canvas is the area where you create a chart by combining states and transitions.
  • The left side of the canvas displays a panel with objects from which you can make a diagram.
  • In the top right corner is a gear button. If you click on it inside the editor, it opens the "Signals" section. In it you can set input and output signals, as well as set a local variable.

sc_chart.png

Adding states and transitions

  1. In the Object panel, select the State object and move it to the diagram canvas. A state with a default transition appears. In the text box, enter the state name On and the state action in the highlighted curly brackets y=x.

state_1_out_1_in_1732104094349.png

  1. Add another state. Right-click and drag the On state. Change the name of the new state to Off. Double-click the state and change the state action to y = 0.

state_1_out_1_in_1732104298320.png

  1. Place two state objects next to each other and connect them with a transition. To draw a transition, click and drag the cursor from the boundary of one state to the boundary of the other state. When the transition is highlighted in blue, a menu appears next to it to add a condition ([]) for the transition or an action ({}) that will occur during the transition. Add two transitions.

state_1_out_1_in_1732104359102.png

  1. To add conditions for the transitions of our diagram, click on the transition and enter the condition in square brackets. Set the condition $x<t0$ to transition from On to Off, and $x>=t0$ to transition back.

state_1_out_1_in_1732104455350.png

  1. Additional functions for working with the state diagram.

    • Move each transition marker to a convenient location above or below the corresponding transition for greater clarity.
    • Press the spacebar to resize the diagram to fit the canvas size.

Set the signals

Before you can execute a diagram, you must define each symbol you use in the diagram and specify its scope (for example, input data, output data, or local data).

  1. Open the settings tab by clicking the gear in the upper-right corner.
  2. Click on the + sign and add the name of the signals you are using and the type. The Type column shows the intended scope for each undefined symbol depending on its use in the diagram.
  3. Define the variables to be used as shown in the figure below.

image.png

Simulate the model

To simulate a model, connect the Chart block to other model blocks via the input and output ports.

  1. To return to the Engee modelling environment, click on the model name in the navigation bar.
  1. Add the source to the model:

    • From the Sources section, add a sine wave block - Sine Wave.
    • Double-click the Sine Wave block and set the sampling time to 0.2.
    • Connect the output of the Sine Wave block to the input of the state diagram.
    • Designate the signal as x.
  1. Add the receiver to the model:

    • From the Receivers section, add a Terminator block.
    • Connect the output of the Chart block to the input of the Terminator block.
    • Designate the signal as y.
  1. Write down the signals

    • Click on the signal line. Next, point to the three dots and click on image.png.
    • Record the x and y signals.
  1. To start the simulation, press "Execute" !"(attachment:image_3.png). During the simulation, active states and transitions are highlighted.

Displaying results

  1. After modelling, click on the Graphs tab in the top right corner.
  2. Select the x and y signals from the signals menu.

image.png

  1. you can reflect the signals on one chart or on two different charts.

image.png

Conclusion

The simulation results show that the rectifier filters out negative input values.