Controlling a NAO robot using states

From our partners in robotics we often hear that TiViPE is easy to use, but for starters the first steps appear to be the biggest hurdle. Here is a step by step approach how to use TiViPE as an end-user programming environment without any textual programming.

In this case we construct a robot control example by using states.

Figure 1: Flow chart of toy scenario.

A toy robot scenario

The scenario is designed by the flow chart illustrated in Figure 1. This flow chart provides the activity of a program, and can be interpreted as moving from one block to another. For instance, when the program is in block number 1, it waits until it gets keyboard input, and only when a ‘y’ is given it activates blocks 2 and 10. In this respect one could also say that the program has active state 1, and one could interpret Figure 1 also as a state chart.
The toy example involves 10 states from the start:
  1. From the start we will wait until the user presses the ‘y’ button. As soon as this button is pressed 2 scenarios will start in parallel (items 2 and 3)
  2. The first scenario “Say: It is a nice day”. Within TiViPE this means that a string is constructed with the following text: “say (It_is_a_nice_day)”, but of course any textual command can be constructed to provide full control over the robot. Since this state activates itself, the command is repeated over and over again. Hence, this scenario might be used as some monitoring scenario that becomes active as soon as the robot needs to stopped to protect users or to protect the robot from being damaged.
  3. The second scenario running in parallel to the first scenario is an interactive scenario that starts with “Hello”.
  4. As soon as Hello has been performed “Bye” is activated.
  5. Next the user can select what action needs to be taken. This state is called “KeySelection” like the first one, but here 2 different keys perform different actions.
  6. “Yes” is activated when the ‘y’ button is pressed or
  7. “No” is activated when the ‘n’ button is pressed.
  8. Both “Yes” and “No” when completed activate another user selection mechanism called “KeySelectionOrTimeOut” this state behaves the same as a “KeySelection” state, but unlike the latter it can also time out. When no appropriate key is pressed, it automatically activates “Hello” after 10 seconds.
  9. In case ‘y’ was pressed “Positive” is activated and this state in turn activates “Hello”.
  10. In case ‘n’ was pressed “Negative” is activated, also this state activates “Hello” after it has been completed.

It is clear that the program sketched above is not a meaningful robotics example, but is aimed to make the user familiar with a sequence of robot actions that can be influenced by the user, and this makes a robot partly autonomous, and partly remote controlled.

The detailed textual description is as follows:

Author Tino Lourens 
Date   17 Oct 2012
Update 24 Mar 2017
Example how to use key interrupts
  xx UpdateStatesNscis (first in the row)
     This module is used for state chart control.
     It starts and updates modules.
     Start 1

   1 CommandStateSelectByKey
     Activate [][y][2&10]   (when y is pressed activate both 2 and 10;
                             otherwise activate 1)
     Info     * Press 'y' to indicate that you are ready.

   2 CommandState2
     Command  say (It_is_a_nice_day)
     Activate 2

  10 CommandState2
     Command  say (Hello)
     Activate 20

  20 CommandState2
     Command  say (Bye)
     Activate 30

  30 CommandStateSelectByKey
     Activate [][y,n][40,50] (when y is pressed activate 40; 
                              when n is pressed activate 50;
                              otherwise activate 30)
     Info     * Press 'y' to say yes; activate state 40.<br>
              * Press 'n' to say no; activate state 50.
                             (Note that <br> is used for a new line in the
                              showInfo module)

  40 CommandState2
     Command  say (Yes)
     Activate 60

  50 CommandState2
     Command  say (No)
     Activate 60

  60 CommandStateSelectByKeyOrTimeOut
     Activate [][y,n][70,80] (when y is pressed activate 70; 
                              when n is pressed activate 80;
                              otherwise activate 60)
     TimeOut  10,000 (10 seconds) TimeOutActivate 10
     Info     * Press 'y' to say positive; activate state 70.<br>
              * Press 'n' to say negative; activate state 80.
     (Note that <br> is used for a new line in the showInfo module)

  70 CommandState2
     Command  say (Positive)
     Activate 10

  80 CommandState2
     Command  say (Negative)
     Activate 10

  xx CommandStateInterrupActiveStatesSCIS (last in the row)
     This module is used for state chart control.
     It sends commands to the robot
     It might interrupt all commands to the robot. 

Toy robot implementation in TiViPE

In the example given above the robot commands are kept short and simple, and thus every robot action state can be implemented by “CommandState2” or “CommandStateTextFile”, latter uses a file where one or more textual commands are given. The 3 keyboard selection mechanisms require the “CommandStateSelectByKey” or  “CommandStateSelectByKeyOrTimeOut”. All these blocks are found in the TiViPE environment in Module->TVP->Robotics->SCIS->States, as illustrated in Figure 2.

Figure  2: Getting modules in TiViPE.

Please select these modules, and likewise get “readBooleanTime” and “writeBooleanTime” from
Module->TVP->Shared memory,
“getKeysAndButtons” from
and “NaoRobotN” from
After selection of a module, the module can be moved by positioning the mouse over the module, pushing the left button, and dragging the module. Note that the module has turned green to indicate that it is selected. After the modules have been selected the work space should look as illustrated in Figure 3. Left clicking a connection will turn this connection red, next clicking another connection will create a connection when possible. Right clicking a connection will change its connection type.
Figure 3: Nine different modules are selected.
For the example we need two “CommandStateSlectByKey” modules, one “CommandStateSekectByKeyOrTimeOut” module, and seven “CommandState2” modules. Modules are duplicated by selecting the desired module(s) and by clicking on the “Copy Icon(s).” button as illustrated in Figure 4.
Figure  4: Duplicating modules.
The modules are duplicated, as illustrated in Figure 5.
Figure  5: Duplicated modules.
The modules are moved to a desired position and inputs and outputs connected. In a state chart construction the order is not important, since all modules in the loop are in between the “UpdateStatesNscis” and “CommandStateInterruptActiveStatesSCIS” modules. They are evaluated in a continuous loop. We keep the order the same as given in the flowchart of Figure 1, and its corresponding textual description. The connected result is given in Figure 6. In this figure the second input connection of all three “SelectByKey” modules are connected with the first output of “getKeysAndButtons”. For all “Command” modules the output is connected to the input of the next module to form a chain between the “UpdateStatesNscis” and “CommandStateInterruptActiveStatesSCIS” modules. With this construction a state chart model has been converted in a flow driven model.
Figure  6: Toy example constructed.
The strength of this example is that there is a one-on-one-mapping between the flowchart and the design in TiViPE. In the next steps we fill out the parameters for every module, but before doing so let us first save this graphical program. Here go to “File->Workspace->Save As” to save the network to the desired file, as given in Figure 7. When a network has been loaded, it can be saved by going to “File->Workspace->Save” or by typing Ctrl-S.
Figure  7: Saving a network.
The saved network can be opened using Ctrl-O, so lets continue filling out the parameters. Lets start with “getKeysAndButtons”, click on the right upper rectangle of this module to get the parameter window, see Figure 8.
Figure  8: Editing the parameters of getKeysAndButtons.
Next modify “UpdateStatesNscis”, here change the states to be activated to 1.
Figure  9: Editing the parameters of UpdateStatesNscis.
Next edit the last 4 modules, Figure 10. Please validate that “Key states” has the same name as “Key states” in Figure 9, in this case it is for both “StatesA”. The same holds for the robot key, please validate that the name is the same without the extra “NAOready1” for “CommandStateInterruptActiveStatesSCIS” and “writeBooleanTime”. For the two remote modules (both indicated by a yellow left lower rectangle) “NaoRobotN” and “readBooleanTime” makes sure that the keys are the sam, in this case “NAORready2”. A NAO robot makes use of a unique IP address, please check that the IP address of the robot is the same as given in the left lower area (indicated by a yellow color). Note that the IP address on the robot can be obtained pressing the chest-button on the robot.
Figure  10: Editing the parameters of CommandStateInterruptActiveStateSCIS, writeBooleanTime, NaoRobotN, and readBooleanTime.
Next we edit all three “CommandStateSelectByKey(OrTimeOut)” modules, see Figure 11.
Figure  11: Editing the parameters of CommandStateSelectByKey (2x) and CommandStateSelectByKeyTimeOut (1x). The Information used is given in the extended text description.
Lets save the network by pressing “Ctrl-S”, this is going fast, but it is verified at the bottom of the TiViPE window where the file has been saved. Finally the seven “CommandState” parameters are given in Figure 12.
Figure  12: Editing the parameters of CommandState2 (7 modules).
A state chart program like the one we have constructed needs to be merged. This is done by drawing an area around all “state” modules. This is done by pressing the left button and moving the mouse, see Figure 13. Once the rectangle is around all modules release the mouse, this will select all icons in the rectangle by making them green.
Figure  13: Selecting modules.
Next press the merge button and give an appropriate name, and press “Ok”. We have reached the 10 tabloid module constructor (Figure 14).
Figure  14: Merging a module. Here the position where to place the module (TVP->Test->CommandStateSelectByKeyExample2) is an arbitrary choice.
Just pressing the “Ok” button will compile the module to a new module, this module can be executed by selecting TVP->Test->CommandStateSelectByKeyExample2 once the module has been compiled, as illustrated in Figure 15.
Figure 15: Compilation of a module.
The newly created module can be executed, but before executing the module, right click the module until it turns red. The module can be executed now. However, that the module  has not been documented nor has the pool of parameters been organized, as is illustrated in Figure 16. Also there is still an unused output connection and there are three input connections that are all three connected to the same “getKeysAndButtons” that need to be reduced to a single input.

Figure 16: Unorganized parameters. The result a huge pile of parameters after compounding 13 modules.

The post on merging modules will explain in detail how the module can be optimized.

Leave a Reply

Your email address will not be published. Required fields are marked *