Create and Play Your First Robot Program Easily using Webots Robot Simulator
“Do you want to learn robot, but you don’t have money to buy the robot? Or you don’t know where to start?”
Webots simulator is the solution! You can learn with any kind of robots, any kind of environments. Also you can create your custom robots and environment. The most interesting is Webots is free and open source.
Here we will discuss about Webots, and create our first robot program to make it move. The robot program is implemented to e-puck robot.
This story is the first part of Webots tutorial series. Below is the link of the series:
- Current story
- Simple Avoidance Algorithm, Implemented on e-puck Robot and Simulated on Webots Robot Simulator
- Move Your Robot to Specific Coordinates
- Adaptive Movement
Stay tuned for more series. Later we will discuss about how to implement Particle Swarm Optimization (PSO) in Webots. PSO is used in real life for a scenario like Search and Rescue Operations.
My Experience using Webots
I have used Webots to help my friend in her thesis to develop obstacle avoidance algorithms in Niching Particle Swarm Optimization algorithms for multi-target search multi-robot system. Robots is locate in a search area. Then the robots move to search for a targets. With Niching Particle Swarm Optimization, robots is sharing their best location related to possible target location, so robots can find the best possible target location. In this simulation, we used 40 robots, and 4 targets.
My experience in developing this project using Webots is it’s easy to learn and use, complete tools, and realistic. I will discuss about this project soon.
Webots: Robot Simulator
Official website: https://cyberbotics.com
Webots is an open source robot simulator that provides a complete development environment to model, program and simulate robots. Thousands of institutions worldwide use it for R&D and teaching. Webots has been codeveloped by the Swiss Federal Institute of Technology in Lausanne, thoroughly tested, well documented and continuously maintained since 1996. It is the most efficient solution to quickly get professional results. [Reference]
What Can I Do with Webots?
- Develop custom robots or using available robot
- Develop custom 3D environment (ground, obstacle, object, sky, physics behavior)
- Create your robots logic
- Simulate and testing
- Record or Screenshot your simulation
Webots Research Area
Many mobile robotics projects have relied on Webots for years in the following areas:
- Mobile robot prototyping (academic research, the automotive industry, aeronautics, the vacuum cleaner industry, the toy industry, hobbyists, etc.)
- Robot locomotion research (legged, humanoids, quadrupeds robots, etc.)
- Multi-agent research (swarm intelligence, collaborative mobile robots groups, etc.)
- Adaptive behavior research (genetic algorithm, neural networks, AI, etc.).
- Teaching robotics (robotics lectures, C/C++/Java/Python programming lectures, etc.)
- Robot contests (e.g. Robotstadium or Rat’s Life)
Webots Simulation
A Webots simulation is composed of following items:
- A Webots world file (.wbt) that defines one or several robots and their environment. The .wbt file does sometimes depend on external PROTO files (.proto) and textures.
- One or several controller programs for the above robots (in C/C++/Java/Python/MATLAB).
- An optional physics plugin that can be used to modify Webots regular physics behavior (in C/C++)
Available robots
Webots have many kinds of available robot, for example:
- e-puck (two-wheeled robot)
- Parallax Boe-Bot (three-wheeled robot)
- Four-wheeled robot
- Hexapod robot
- Robotics arm
- Small size car-like robot
- Humanoid robot
- Dog-like robot
Programming Language
You can write the webots robot controller using C++, Java, Python or MATLAB. Choose any kind of your favourite programming language.
Webots User Interface
Webots GUI is composed of four principal windows:
- 3D window that displays and allows you to interact with the 3D simulation
- Scene tree which is a hierarchical representation of the current world
- Text editor that allows you to edit source code, and finally
- Console that displays both compilation and controller outputs.
User Guide
Webots has provide a great and clean user guide. Also there is a tutorial that you can learn.
e-puck Robot
Official website: https://e-puck.org
Wiki: https://www.gctronic.com/doc/index.php/e-puck2
The e-puck robot is a mini mobile robot developed by GCtronic and EPFL. The e-puck robot is designed and has been widely used in research and educational purposes and is easy to use [Reference].
e-puck has been a very successful robot since 2005. About 3000 units used both in education and research. From January 2018 version 2 is available [Reference].
The e-puck robot is a non-holonomic wheeled robot. Non-holonomic wheeled robot is a robot that depends on the path it can reach to move and cannot move in all directions [Reference], different from the holonomic wheeled robot that can move in all directions [Reference] without changing its orientation. Therefore, if the e-puck robot wants to move to a different direction, the e-puck robot must rotate its body first to face that location.
Simple Tutorial
Here I create a simple tutorial to create the environment, add obstacle, add e-puck robot, and create the e-puck robot program to make the robot move forward.
Before, install webots on your computer from this tutorial
1. Create Project Directory
- Click “Wizards” menu then click “New Project Directory” submenu
- Click “Next”
- Choose directory for your new project, then click “Next”
- Choose a name for the new world (here I name it “main.wbt”), and check the features you want (here I check the “Center view point”, “Add a textured background”, “Add a directional light”, and “Add a rectangle arena”. Then click “Next”
- Finally click “Finish”
Below is the created environment look:
2. Add e-puck Robot
Select the last node “RectangleArena” of the scene tree view. Click on the Add button (plus sign) at the top of the scene tree view. In the dialog box, choose “PROTO nodes (Webots) / robots / gctronic / e-puck / E-puck (Robot)”, then click “Add”. An e-puck robot should appear in the middle of the arena. Save the simulation.
3. Centering the Arena
We want to look the arena from above to make it clearly visible. You can rotate the arena in X, Y, or Z axis by “hold left click” and “move the cursor”. You can move the arena by “hold right click” and “move the cursor”. You can zoom-in or zoom-out by “scrolling your mouse up or down”.
Here I give you my setting to make the arena looked from above. Double-click on the “Viewpoint” node in the scene tree. This should open the node and display its fields. Select the “orientation” field and set its “angle” value to 1.58. Select the “position” field and set its “x” value to -0.034, “y” value to 2.2365, and “z” value to -0.023.
Below is the environment look:
4. Add Obstacle
- Add rectangle obstacle. Select the last node “E-puck” of the scene tree view. Click on the Add button (plus sign) at the top of the scene tree view. In the dialog box, choose “PROTO nodes (Webots) / objects / factory / containers / WoodenBox (solid)”, then click “Add”. Then Double-click on the “WoodenBox” node in the scene tree. This should open the node and display its fields. Select the “size” field and set its “x” value to -0.2, “y” value to 0.1, and “z” value to -0.1. Select the “translation” field and set its “x” value to 0, “y” value to 0.05, and “z” value to -0.2.
- Add tube obstacle. Select the last node “E-puck” of the scene tree view. Click on the Add button (plus sign) at the top of the scene tree view. In the dialog box, choose “PROTO nodes (Webots) / objects / obstacles / OilBarrel (Solid)”, then click “Add”. Then Double-click on the “OilBarrel” node in the scene tree. This should open the node and display its fields. Select the “radius” field and set it value to -0.05. Select the “height” field and set it value to 0.1 Select the “translation” field and set its “x” value to 0.2, “y” value to 0.05, and “z” value to -0.2.
Below is the environment look:
5. Create e-puck robot controller
- Click “Wizards” menu then click “New Robot Controller” submenu
- Click “Next”
- Choose the language for your controller program. Here I use “C” language. Then click “Next”
- Specify a name for the controller name. Here I use “e-puck-move_forward”. Then click next.
- Finally click “Finish”
Below is your controller source code look:
6. Edit your controller program
You can edit the controller from webots built in text editor. Or you can edit with your favourite IDE. The controller directory is placed in “[your project directory]/controllers”. The program is placed inside this controller directory.
Here is the move forward source code, or you can view it on my from GitHub Repository. Copy and paste the source code to your controller program.
Then click “Build” menu, then click “Build” submenu. Make sure that your controller program is open in webots text editor before build.
The important things about this program:
- wb_robot_get_basic_time_step()
Webots have simulator time step. The basic time step is the time step increment used by Webots to advance the virtual time and perform physics simulation [reference]. - wb_robot_step(time_step)
This command is to perform simulation steps. This needed for the controller time step. The controller time step is the time increment of time executed at each iteration of the control loop of a controller. We must call this to synchronize our program and the simulator condition. It will return -1 if the simulation is stopped. If we not call this command, the robot will do nothing. For example the wb_motor_set_velocity(left_motor, MAX_SPEED) only set the motor speed value. So we need to call and looping the wb_robot_step(time_step) command to make the robot move.
Program Flow:
- First we initialize the robot, and the necessary stuff like get handler to the motors.
- Then we make the robot move forward using the while loop.
- When the while loop exit, we cleanup the webots resources.
Note: in this program we run an infinite while loop, so the wb_robot_cleanup is never reached.
7. Setting e-puck robot controller
Double-click on the “E-puck” node in the scene tree. This should open the node and display its fields. Select the “controller” field, then click select, then choose your controller name (here my controller name is “e-puck-move_forward”).
8. Play it
Above the 3D window, click play button to run the simulation in real-time). If the simulation is run, then this button is turn into pause button. Click this pause button if you want to pause the simulation.
The simulation is look like below:
9. Lastly
Don’t forget to save your world by click the save button or by menu “File -> Save World”.
When a Webots world is modified with the intention of being saved, it is fundamental that the simulation is first paused and reloaded to its initial state, i.e. the virtual time counter on the main toolbar should show 0:00:00:000. Otherwise at each save, the position of each 3D object can accumulate errors. Therefore, any modification of the world should be performed in that order: pause, reset, modify and save the simulation
Download this Project
You can download this project from my Gihub. Simply download it, and run it in your webots.
Going Further
Please read this tutorial to advance your knowledge.
Next Tutorial
Stay tuned for more tutorial. You can follow my Medium or my Github.
Give claps if you like this stories.
Give response if you like or if you have any advice.
Contact me if you have any question at my LinkedIn
Thank you!