Table of Contents
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.
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 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).
distance = 100;
if (distance < 10)
echo ("Obstacle detected")
else
echo ("No obstacle");
[00042854] *** 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.
The while construct is similar to what is available in C:
i = 0;
while (i <= 2) {
i << echo (i);
i++;
};
[00043477:i] *** 0.000000
[00043516:i] *** 1.000000
[00043536:i] *** 2.000000
The for construct is similar to what is available in C:
for (i = 0; i <= 2; i++) i << echo (i); [00022967:i] *** 0.000000 [00022991:i] *** 1.000000 [00023011:i] *** 2.000000
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.
There is also a for in construct to iterate lists:
for i in [0, 1, 2]
{
i << echo (i);
};
[00117921:i] *** 0.000000
[00117921:i] *** 1.000000
[00117921:i] *** 2.000000
You can also iterate a sequence of numbers:
for i in seq(3)
{
i << echo (i);
};
[00176286:i] *** 0.000000
[00176286:i] *** 1.000000
[00176286:i] *** 2.000000
for in is an exception: even when the iterated command is a single
command, like in the above example, you must enclose it between brackets.