Your first program
Your first program drives the robot forward, and stops it in front of a wall. It is short, but it has everything every program needs.
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
# Get the devices of the starter robot by nameleftWheel = robot.getDevice("left wheel motor")rightWheel = robot.getDevice("right wheel motor")frontDist = robot.getDevice("frontDist")
# Sensors must be enabled before usefrontDist.enable(timeStep)
# Let the wheels turn without limit, and start driving forwardleftWheel.setPosition(float("inf"))rightWheel.setPosition(float("inf"))leftWheel.setVelocity(3.0)rightWheel.setVelocity(3.0)
# The main loop: each step, the simulation advances by timeStep millisecondswhile robot.step(timeStep) != -1: distance = frontDist.getValue() print("Distance to the wall in front:", round(distance, 3), "m")
if distance < 0.05: # Closer than 5 cm: stop leftWheel.setVelocity(0) rightWheel.setVelocity(0) print("Wall reached!") break#include <iostream>#include <cmath>#include <webots/Robot.hpp>#include <webots/Motor.hpp>#include <webots/DistanceSensor.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
// Get the devices of the starter robot by name Motor *leftWheel = robot->getMotor("left wheel motor"); Motor *rightWheel = robot->getMotor("right wheel motor"); DistanceSensor *frontDist = robot->getDistanceSensor("frontDist");
// Sensors must be enabled before use frontDist->enable(timeStep);
// Let the wheels turn without limit, and start driving forward leftWheel->setPosition(INFINITY); rightWheel->setPosition(INFINITY); leftWheel->setVelocity(3.0); rightWheel->setVelocity(3.0);
// The main loop: each step, the simulation advances by timeStep milliseconds while (robot->step(timeStep) != -1) { double distance = frontDist->getValue(); cout << "Distance to the wall in front: " << distance << " m" << endl;
if (distance < 0.05) { // Closer than 5 cm: stop leftWheel->setVelocity(0); rightWheel->setVelocity(0); cout << "Wall reached!" << endl; break; } }
delete robot; return 0;}How it works
Section titled “How it works”- Create the robot.
Robot()connects your program to the robot in the simulator. - Get the time step. The simulation advances in steps of a few milliseconds.
getBasicTimeStep()gives the step of the world. - Get the devices. Each motor and sensor is found by its name. These are the names you gave them when you built your robot.
- Enable the sensors. A sensor gives no values until it is enabled. The time step tells it how often to update.
- Loop.
robot.step(timeStep)advances the simulation by one step, and returns -1 when the simulation ends. Everything your robot does happens inside this loop.
Run it
Section titled “Run it”- Save the program as
hello.py. - Open
game/worlds/mapping_example_1.wbt, and load the starter robot with the right LOAD button. - Load
hello.pywith the left LOAD button, and press start.
The robot drives forward, and the Webots console shows the distance to the wall in front of it until the robot stops. If you see an error instead, check Troubleshooting.
Try it
Section titled “Try it”- Change the speed from
3.0to6.0. What happens near the wall? - Make the robot stop at 10 cm instead of 5 cm.
- Print the time with
robot.getTime()as well.
Next, learn to drive and turn in Movement.
