Chapter 4. More advanced features

Table of Contents

Branching and looping
if
while
for, foreach
loop, loopn
Event catching mechanisms
at
whenever
wait, waituntil
timeout, stopif, freezeif
Soft tests
Emit events
Simple events
Events with parameters
Event duration
Pulsing events: the every command
Command tags, flags and command control
Objects grouping
Function definition
Error messages and system messages

At this point, you are already capable of reading and setting sensors and motors in your robot, execute complex scripts or actions and superimpose motion patterns. This could be already enough for most users, but there is more in URBI and the URBI language gives you access to all the programming constructs found in modern languages plus other new constructs useful for robotics.

Branching and looping

The branching and looping constructs of C/C++ are also available in URBI: if else, for, while. The following examples illustrate these constructs (the echo command that you will see simply displays the expression as a system message).

if

if performs a single test and executes the associated command if the test is true:

if (backSensorM > 0) {
       pressed = 1;
       echo "Back sensor pressed";
};

Note that the last command between brackets doesn't need to be ended by a semicolon like in the above example. This is because semicolons are command separators and not command terminators. You can put a semicolon at the end like in C, but it is not required and it has not effect (it adds an empty command).

if (distance < 10)
  echo "Obstacle detected"
else
  echo "No obstacle";
[167322:notag] *** No obstacle

Note that, unlike in C, there is no semicolon before else, but there is a semicolon (or any other command separator) after the concluding }.

distance and backSensorM are two Aibo devices related to the head infrared distance sensor and to the middle (M) back sensor.

while

The while construct is similar to what is available in C:

i=0;
while (i<=2) {
  i:echo i;
  i++;
};
[151228:i] *** 0
[151228:i] *** 1
[151228:i] *** 2

for, foreach

The for construct is similar to what is available in C:

for (i=0;i<=2;i++)
  i:echo i;
[151228:i] *** 0
[151228:i] *** 1
[151228:i] *** 2

Unlike in C, URBI has specific constructs to handle parallel and serial loops: for&, for| and while|. These constructs will start every iteration in parallel (with &) or in series (with |) with a guaranteed time constraint. More details are available in the URBI Language Specification.

As we already mentioned before, there is also a foreach and foreach& construct to iterate lists:

foreach i in [0,1,2] {
  i:echo i;
};
[151228:i] *** 0
[151228:i] *** 1
[151228:i] *** 2

foreach is an exception: even when the iterated command is a single command, like in the above example, you must enclose it between brackets.

loop, loopn

For practical reasons, URBI has added two more constructs, loop and loopn to create infinite loops in the first case and loops iterating n times in the second case. The syntax is:

loop { ... }

and

loopn (n) { ... }