Tu primer programa
Tu primer programa hace avanzar el robot y lo detiene delante de una pared. Es corto, pero tiene todo lo que necesita cualquier programa.
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;}Cómo funciona
Sección titulada «Cómo funciona»- Crea el robot.
Robot()conecta tu programa con el robot del simulador. - Obtén el paso de tiempo. La simulación avanza en pasos de unos pocos milisegundos.
getBasicTimeStep()da el paso del mundo. - Obtén los dispositivos. Cada motor y cada sensor se encuentran por su nombre. Son los nombres que les diste cuando construiste tu robot.
- Activa los sensores. Un sensor no da ningún valor hasta que se activa. El paso de tiempo le indica cada cuánto actualizarse.
- Bucle.
robot.step(timeStep)hace avanzar la simulación un paso y devuelve -1 cuando la simulación termina. Todo lo que hace tu robot ocurre dentro de este bucle.
Ejecútalo
Sección titulada «Ejecútalo»- Guarda el programa como
hello.py. - Abre
game/worlds/mapping_example_1.wbty carga el robot inicial con el botón LOAD de la derecha. - Carga
hello.pycon el botón LOAD de la izquierda y pulsa el botón de inicio.
El robot avanza, y la consola de Webots muestra la distancia a la pared que tiene delante hasta que el robot se detiene. Si en su lugar ves un error, consulta Solución de problemas.
Pruébalo
Sección titulada «Pruébalo»- Cambia la velocidad de
3.0a6.0. ¿Qué pasa cerca de la pared? - Haz que el robot se detenga a 10 cm en lugar de a 5 cm.
- Imprime también el tiempo con
robot.getTime().
A continuación, aprende a avanzar y girar en Movimiento.
