State Machine

Overview

A state machine enables the user to specify a set of modes, that each can take different mode values.

  1. For example We could have a "day" mode that can take the values "weekday" and "weekend", and a "time" mode that could take the "daytime" and "nighttime" values.
    • Also, a set of states is chosen, each state taking different values.
  2. For example: We could have a "heating" state that could take one of the "normal" and "economical" values.
    • As the modes and states are represented by blocks, they can be linked. This way the user will specify, that when the "day" is "weekend" the "heating" to be "economical". 

Steps to create a state machine

Create a new program (temporary way)

1. Navigate to the correct context that you'd like the program to reside on and click "new program" in the bLine menu.

2. Once the form appears you can select the points that you'd like to use in the program.

3. To trigger the program to act as a "state machine" you will need to add the following marker tag to your program isStateMachine (see steps below)

  • In the Folio app run a query for all of your existing programs by typing in 'program'


  • add isStateMachine as a marker tag

Add the mode and state blocks

The state machine can have one or multiple modes and one or multiple states.

Each mode and state has an associated string variable that represents it.

Each mode and state can take one of a fixed set of values.


1. Click edit the program.

2. Drag a mode block out of the block library. 

3. Write the mode name in the input box found on the top of the block.

4. For each mode value that the current mode can take, add it on the bottom part of the block.

In this example we have a fist mode "day" that has the mode values "weekday" and "weekend" and a second mode "time" that has the mode values "daytime" and "nighttime"

5. In a similar way, drag finds the state block in the block library and drag it into the stage.

6. Set the state name in the top input box.

7. Add the state values.

In this example, we have one state named "heating" with two values "normal" and "economical"

Link the mode and state blocks

In this example, we link the daytime and weekend values to economical, and the daytime to normal. We want to have "normal" heating in the weekdays and daytime, and "economical" heating in weekends or nighttime, as it's an office building.

Because the "day" mode block is linked closer to the "start" block than the "time" block, the "day" block will have greater priority. So the value "weekend" will override any value set by the "time" block.


Edit the mode block(s)

Click on the pencil on top of the mode block.

In this tab add the logic that sets the current mode value.

Drag the mode setter from the right side of the stage, the one that has the (mode) written next to it.

You can select the value you set to the mode from the drop-down list.


Edit the state block(s)

To edit a state block, first click on the pencil on the top of the block.

This makes the tabs associated with the state values to become visible. In this case "heating = normal" and "heating = economical" will show up.

Then click on "heating = normal".

Here add the logic for what happens when the "heating" state is "normal".

Next, click on the "heating = economical".

Here add the logic for what happens when the "heating" state is "economical".


Generated code

The generated code is based on the following three parts:

Mode setup

In this part, the mode values are set.

After editing the mode blocks, for each mode block a setup while be generated. In our current example the equivalent of this pseudo-code will appear:

macro setup

//day block

dayNumber = (dayNumber + 0.5) % 7;

if (dayNumber > 4) {

    day = "weekend";

}

else {

    day = "weekday";

}

//time block

hour = (hour + 0.5) % 24;

if (inRange(hour, 6, 18)) {

     time = "daytime";

}

else {

    time = "nightime"

}


Setting the state values

In this part the state values are set based on the linking done between the mode and state blocks.

In our example this code is generated: 

setting the state values

if(time = "daytime") {

    heating = "normal";

}

if(time = "nighttime") {

    eating = "economical";

}

//because the day mode is linked first in the diagram, it has greater priority. So its code is generated after the time mode code.

if (day == "weekend") {

    heating = "economical";

}


Taking actions based on the state values

In this part the actions are done based on the current state values

actions

if (heating == "normal") {

    temp = 70;

}

else {

    temp = 50;

}


Associated string variables

When saving the program, for each mode and state block an associated string variable will be created.

Example Results

As the hour variable changes its value, the time will toggle between "daytime" and "nighttime".

As the dayNumber variable changes its value, the day will toggle between "weekend" and "weekday".

When time is "daytime" and day is "weekday" the heating is "normal". This sets temp to 70.

Otherwise, when time is "nighttime" or day is "weekend", the heating is "economical". This sets temp to 50.