

Wb_distance_sensor_enable( sensor, TIME_STEP) Sensor = wb_robot_get_device( 'my_distance_sensor ')
#Webots controller update#
The next example does continuously update and print the value returned by a DistanceSensor:
#Webots controller how to#
Now that we have seen how to print a message to the console, we shall see how to read the sensors of a robot. When the loop exists, no further communication with Webots is possible and the only option is to confirm to Webots to close the communication by calling the wb_robot_cleanup function. Therefore, in this example, the control loop will run as long as the simulation runs. This function will indeed return -1 when Webots terminates the controller (see Controller Termination). Note that in this "Hello World!" example, the exit condition of the while loop is the return value of the wb_robot_step function. This duration specifies an amount of simulated time, not real (wall clock) time, so it may actually take 1 millisecond or one minute of real time, depending on the complexity of the simulated world. The value 32 specifies the duration of the control steps, i.e., the wb_robot_step function shall compute 32 milliseconds of simulation and then return. The wb_robot_step function needs to be present in every controller and it must be called at regular intervals, therefore it is usually placed in the main loop as in the above example. This function synchronizes the controller's data with the simulator. Within that loop there is a call to the wb_robot_step function. Usually the highest level control code is placed inside a for or a while loop. Note that the wb_robot_init and wb_robot_cleanup functions exist only in the C API, they do not have any equivalent in the other supported programming languages. The wb_robot_cleanup function does the opposite: it closes the communication between the controller and Webots to terminate the controller smoothly. This function initializes the communication between the controller and Webots. Ī call to the initialization wb_robot_init function is required before any other C API function call. Like with any regular C code it is also possible to include the standard C headers, e.g. These header files must be included using statements like #include where xyz represents the name of a Webots node in lowercase. Webots C API (Application Programming Interface) is provided by regular C header files. The standard output and error streams are automatically redirected to Webots console for all Webots supported languages. This code repeatedly prints "Hello World!" to the standard output stream which is redirected to Webots console. Wb_console_print( sprintf( 'Hello World! \n '), WB_STDOUT)
