7.2 Tutorial 1: Your first Simulation in Webots (20 minutes)
In this first tutorial, you will create your first simulation.
This simulation will contain a simple environment (a floor and a light), a predefined robot (e-puck) and
a controller program that will make the robot move (see figure 7.2).
The objective of this tutorial is to familiarize yourself with the user interface
and with the basic concepts of Webots.

Figure 7.2:
What you should see at the end of the tutorial.
7.2.1 Create a new World
In this subsection, we will create a new simulation.
The content of a simulation is stored in a world file.
This world file contains all the information related
to your simulation, i.e. where are the objects, how do they look
like, how do they interact with each other, what is the color of the sky,
where is the gravity vector, etc.
|
|
A world is defined by a tree of nodes.
Each node has some customizable properties called fields.
A world is stored in a file having the .wbt suffix.
The format of this file is derived from the VRML
language, and is human readable.
The world files must be stored directly in the project subdirectory called worlds.
|
|
Webots is currently open and runs an arbitrary simulation.
|
|
Stop the current simulation by clicking on the Stop button of the 3D view.
The simulation is stopped if the virtual time counter on the 3D view toolbar is stable.
|
|
|
|
Create a new world by selecting the File > New World menu item.
|
|
A new world is now open.
It contains a checkerboard floor with a point light above it.
Your environment should look like the one depicted in the figure 7.1.
|
|
Save the new world into your project by selecting the File > Save World As... menu item.
Using the dialog box save the world into the my_webots_projects/tutorials/worlds/my_first_simulation.wbt
file location.
|
|
|
|
Revert the simulation by selecting the File > Revert World menu item.
|
|
|
|
You can change the viewpoint of the 3D view by using the mouse buttons (left button, right button and the wheel).
|
|
|
|
Webots nodes stored in world files are organized in a tree structure called the scene tree.
The scene tree can be viewed in two subwindows of the main window:
the 3D view (at the center of the main window) is the 3D representation of the scene tree
and the scene tree view (on the left) is the hierarchical representation of the scene tree.
The scene tree view is where the nodes and the fields can be modified.
|
|
|
|
In the 3D view, click on the floor to selected it.
When it is selected the floor is surrounded by white lines
and the corresponding node is selected in the scene tree view.
Now click on the blue sky to unselect the floor.
|
|
7.2.2 Add an e-puck Robot
The e-puck is a small robot having
differential wheels, 10 LEDs, and
several sensors including 8 distance sensors and a camera.
In this tutorial we are only interested in using its wheels.
We will learn how to use some other e-puck features in the other tutorials.
Now we are going to add an e-puck model to the world.
Make sure that the simulation is stopped and that the virtual time elapsed is 0.
|
|
When a Webots world is modified with the intention of being saved,
it is fundamental that the simulation is first stopped and reverted to its initial state,
i.e. the virtual time counter on the 3D view toolbar should show 0:00:00:000.
Otherwise at each save, the position of each 3D objects can accumulate errors.
Therefore, any modification of the world should be performed in that order:
stop, revert, modify and save the simulation.
|
|
As we don't need to create the e-puck robot from scratch,
we will just have to import a special EPuck node (in fact: a prototype).
A prototype is an abstract assamblage of several nodes.
Prototypes are defined in separate .proto, but this will be explained in more details later.
For now consider the EPuck node as a black box that contains all the necessary nodes to define a e-puck robot.
|
|
Select the last node of the scene tree view (called FLOOR).
In order to add the EPuck node, click on the Add New button at the top of the scene tree view.
In the open dialog box, and choose PROTO (Webots) > robots > e-puck > EPuck (DifferentialWheels).
Then save the simulation.
|
|
|
|
Now if you run the simulation, the robot moves: that's because the robot uses a default controller with that behavior.
Please stop and revert the simulation before going on.
|
|
|
|
Using the mouse, it is possible to change the robot's position in the 3D view.
The robot can be moved parallel to the floor using: SHIFT + left-clicking + drag.
The robot can be moved up or down using: SHIFT + mouse-wheel.
The robot can be rotated: SHIFT + right-clicking + drag.
The rotation axis can be set by hitting the SHIFT key twice.
Finally, you can add a force to the robot: CTRL + ALT + left-clicking + drag.
|
|
|
|
Starting the simulation by pressing the Run button
will make Webots running the simulation as fast as possible.
In order to obtain a real-time simulation speed, the Real-Time
button has to be pressed.
|
|
Now we are going to modify the world and decrease the step of the physics simulation:
this will increase the accuracy of the simulation.
|
|
In the scene tree view, expand the WorldInfo node (the first node).
Set its basicTimeStep field to 16.
Then save the simulation.
|
|
Just after you added the EPuck node, two black superimposed windows appeared in the upper left corner of the 3D view.
They show the content of Camera and Display nodes, but they will stay black until
not explicitly used during a simulation. Their type is specified by the border color:
magenta for a Camera window and cyan for a Display window. In order to hide them, you simply have to set the
pixelSize equal to 0. Then, if you want to re-enable
them, you have to set this field value to a positive number. Detailed definitions can be found in chapter 3 of the
Reference Manual.
|
|
In this tutorial we will not use the Camera and the Display devices of the EPuck. So we can hide the two
windows by expanding the EPuck node and setting the fields camera_pixelSize
and display_pixelSize to 0. Don't forget to revert the simulation before
changing the values and to save it after the modifications.
|
|
7.2.3 Create a new Controller
We will now program a simple controller that will just make the robot move forwards.
As there is no obstacle, the robot will go forwards for ever.
Firstly we will create and edit the C controller, then we will link it to the robot.
|
|
A controller is a program that defines the behavior of a robot.
Webots controllers can be written in the following programming languages: C, C++, Java, Python, Matlab, etc.
Note that C, C++ and Java controllers need to be compiled before they can be run as robot controllers.
Python and Matlab controllers are interpreted languages so they will run without being compiled.
The controller field of a robot specifies which controller is currently linked with to it.
Plase take notice that a controller can be used by several robots, but a robot cans use only one controller at a time.
|
|
|
|
Each robot controller is executed in a separate child process spawned by Webots.
Controllers don't share the same address space, and they can run in different processor cores.
|
|
|
|
Other languages than C are available but may require a setup.
Please refer to the language chapter to setup the other languages (see chapter 4).
|
|
|
|
Create a new C controller called epuck_go_forwards using the Wizards > New Robot Controller... menu.
This will create a new epuck_go_forwards directory in my_webots_projects/tutorials/controllers.
Select the option asking you to open the source file in the text editor.
|
|
The new C source file is displayed in Webots text editor window.
This C file can be compiled without any modification, however the code has no real effect.
We will now link the EPuck node with the new controller before modifying it.
|
|
Link the EPuck node with the epuck_go_forwards controller.
This can be done in the scene tree view by selecting the controller field of the EPuck node,
then use the field editor at the bottom of the scene tree view: push the Select... button and
then select epuck_go_forwards in the list.
Once the controller is linked, save the world.
|
|
|
Modify the program by inserting an include statement (#include <webots/differential_wheels.h>), and
by applying a differential wheels command (wb_differential_wheels_set_speed(100, 100)) :
#include <webots/robot.h>
// Added a new include file
#include <webots/differential_wheels.h>
#define TIME_STEP 64
int main(int argc, char **argv)
{
wb_robot_init();
// set up the speeds
wb_differential_wheels_set_speed(100, 100);
do {
} while (wb_robot_step(TIME_STEP) != -1);
wb_robot_cleanup();
return 0;
} |
|
|
|
|
Save the modified source code (File > Save Text File),
and compile it (Build > Build).
Fix any compilation error if necessary.
When Webots proposes to revert the simulation, choose Yes.
|
|
If everything is ok, your robot should go forwards.
|
|
In the controllers directory of your project,
a directory containing the epuck_go_forwards controller
has been created.
The epuck_go_forwards directory contains an epuck_go_forwards
binary file generated after the compilation of the controller.
Note that the controller directory name should match with the binary name.
|
|
7.2.4 Conclusion
We hope you enjoyed creating your first simulation.
You have been able to set up your environment, to add a robot and to program it.
The important thing is that you learnt the fundamental concepts summarized below:
A Webots world is made of nodes organized in a VRML-like tree structure.
A world is saved in a .wbt file stored in a Webots project.
The project also contains the robot controllers which are
the programs that define the robots behavior.
Robot controllers can be written in C (or other languages).
C controllers have to be compiled before they can be executed.
Controllers are linked to robots via the controller fields of the
robot nodes.