This tutorial goes over 3 different strategies for controlling which of multiple states is active. For the simplest situations, you can use a Switch node. When there are multiple states that always follow the same order, you can use a Multi Step node. When the state logic is more complex, you can use a Highest Activation node.

View discussion page on forum

This tutorial is about different strategies for controlling states in Embrio. What do I mean by state? A “State” is the internal configuration of a computer program. Consider a project that uses an RGB LED as an indicator. At different times, the LED should act differently. For a simple example, I have a button which when pressed cycles though three different patterns. Each pattern is considered a state. This tutorial talks about how to control which state of many possible states is active in an Embrio program.

In Embrio, everything gets encapsulated by an Agent. In this case, there is one agent that encapsulates the LED, one for the button, and one agent for each state. Make sure to go over and understand the Agents tutorial before getting to far into this one or you might get confused.

Toggling between 2 choices is one of the simplest state control scenarios. To do this, you use a switch node from the flow control menu. A switch has an output activation that is either on or off, and the state of the switch is changed by using the turn on, turn off, or toggle input triggers. First I attach the switches activation to the Green led agent. Now when I toggle the switch, the green light turns on and off. When the switch outputs a 0 the light is off, but I want the LED to be blinking red at this point. I add a flip value node and attach the switch’s activation to it, and connect it to the Blink RED agent. Finally I connect the button agent’s trigger to the switch’s toggle, and now pressing the button changes between the two states.

Now lets say I want to add the other state to the mix. I have another agent that randomly changes the color of the LED, you can see how to do this in the Random Fade RGB LED tutorial. All you need to know about this agent for now is that it has an activation input, which when above .5 turns it on.

Now I have 3 LED control agents on my top level agent. I get rid of the switch node, and add a multi step node from the flow control menu. I have 3 states, so I click the add button 3 times. This node has one active output at a time. Firing the Next input trigger goes to the next step, and the reset trigger goes back to the first step. When the next input is fired while the last step is already active, the done output trigger fires. If I connect this to the reset input I can cycle through the 3 states. I connect one output to each LED state, connect the button to the next trigger, and now clicking the button cycles through the 3 led states. This node is great for controlling a program flow that always follows the same process.

For the last state control example in this tutorial, I’ll use a Highest Activation node. Like the multi step node, I can add however many states I want. I’ll add 3, one for each control agent. Notice that there is one input activation for each output activation. The output activation corresponding to the highest input activation is set to 1, the rest are set to 0. Again I’ll connect one output to each LED control agent.

This node lets you control multiple states when the logic behind which state is on is more complicated than just being on or off, or in a known sequence. Lets say that in the default state the LED should be green, when you press the button it changes to the random color fade mode, and stops when you press it again. I’ll add a distance sensor to the project, and when it detects an obstacle within a set range the LED blinks red.

As I said, the Green light is the default behavior, so I’ll change its input activation to some high value, and 0 out the others, so that it is on by default. Now I want the button to turn on and off the color fading behavior. I add a switch node, hook up the button to the toggle trigger, and attach its activation to the input corresponding with the Color change agent. When the switch is on, it now has the highest activation, so the color fade agent gets turned on.

Now I deal with the distance sensor. I have an agent that outputs an activation of 1 when something is blocking the sensor, and 0 when it is not. I plug this activation into the final input activation. Now notice that both the blink red and random color fade activation can both be 1 at the same time. I want to give precedence to the blink red agent, so I’ll bring down the On value on the switch to something below 1 and above the default value of the green light activation. So when just the random color fade switch is on, it wins the activation pass through over the default value, but when the distance sensor activates, it wins by having the highest input activation. This technique of having a “desire” agent for each state is an easy and powerful way to control complicated state schemes in your program.

View discussion page on forum