6.2 Supervisor Programming
The programming examples provided here are in C, but same concepts apply to C++/Java/Python/Matlab.
6.2.1 Introduction
The Supervisor is a special kind of Robot. In object-oriented jargon we would say that the Supervisor class inherits from the Robot class or that the Supervisor class extends the Robot class. The important point is that the Supervisor node offers the wb_supervisor_*() functions in addition to the regular wb_robot_*() functions. These extra functions can only be invoked from a controller program associated with a Supervisor node, not with a Robot or a DifferentialWheels node. Note that Webots PRO is required to create Supervisor nodes or use the wb_supervisor_*() functions.
In the Scene Tree, a Supervisor node can be used in the same context where a Robot node is used, hence it can be used as a basis node to model a robot. But in addition, the wb_supervisor_*() functions can also be used to control the simulation process and modify the Scene Tree. For example the Supervisor can replace human actions such as measuring the distance travelled by a robot or moving it back to its initial position, etc. The Supervisor can also take a screen shot or a video of the simulation, restart or terminate the simulation, etc. It can read or modify the value of every fields in the Scene Tree, e.g. read or change the position of robots, the color of objects, or switch on or off the light sources, and do many other useful things.
One important thing to keep in mind is that the Supervisor functions correspond to functionalities that are usually not available on real robots; they rather correspond to a human intervention on the experimental setup. Hence, the Robot vs. Supervisor distinction is intentional and aims at reminding the user that Supervisor code may not be easily transposed to real robots.
Now let's examine a few examples of Supervisor code.
6.2.2 Tracking the Position of Robots
The Supervisor is frequently used to record robots trajectories. Of course, a robot can find its position using a GPS, but when it is necessary to keep track of several robots simultaneously and in a centralized way, it is much simpler to use a Supervisor.
The following Supervisor code shows how to keep track of a single robot, but this can easily be transposed to an arbitrary number of robots. This example code finds a WbNodeRef that corresponds to the robot node and then a WbFieldRef that corresponds to the robot's translation field. At each iteration it reads and prints the field's values.
#include <webots/robot.h> |
As illustrated by the example, it is better to get the WbNodeRefs and WbFieldRefs only once, at the beginning of the simulation (keeping the invariants out of the loop). The call to wb_supervisor_node_get_from_def() searches for an object named "MY_ROBOT" in the Scene Tree. Note that the name in question is the DEF name of the object, not the name field which is used to identify devices. The function returns a WbNodeRef which is an opaque and unique reference to the corresponding Scene Tree node. Then the call to wb_supervisor_node_get_field() finds a WbFieldRef in the specified node. The "translation" field represents the robot's position in the global (world) coordinate system.
In the while loop, the call to wb_supervisor_field_get_sf_vec3f() is used to read the latest values of the specified field. Note that, unlike sensor or actuator functions, the wb_supervisor_field_*() functions are executed immediately: their execution is not postponed to the next wb_robot_step() call.
6.2.3 Setting the Position of Robots
Now let's examine a more sophisticated Supervisor example. In this example we seek to optimize the locomotion of a robot: it should walk as far as possible. Suppose that the robot's locomotion depends on two parameters (a and b), hence we have a two-dimensional search space.
In the code, the evaluation of the a and b parameters is carried out in the the while loop. The actuateServos() function here is assumed to call wb_servo_set_postion() for each Servo involved in the locomotion. After each evaluation the distance travelled by the robot is measured and logged. Then the robot is moved (translation) back to its initial position (0, 0.5, 0) for the next evaluation. To move the robot we need the wb_supervisor_*() functions and hence the base node of this robot in the Scene Tree must be a Supervisor and not a Robot.
#include <webots/robot.h> |
Please note that the program structure is composed of three nested for loops. The two outer loops change the values of the a and b parameters. The innermost loop makes the robot walk during 60 seconds. One important point here is that the call to wb_robot_step() is placed in the innermost loop. This allows the servo positions to be updated at each iteration of the loop. If wb_robot_step() was placed anywhere else, this would not work.