Seu primeiro programa
Seu primeiro programa faz o robô andar para a frente e parar diante de uma parede. Ele é curto, mas tem tudo o que todo programa precisa.
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;}Como funciona
Seção intitulada “Como funciona”- Crie o robô.
Robot()conecta o seu programa ao robô no simulador. - Obtenha o passo de tempo. A simulação avança em passos de alguns milissegundos.
getBasicTimeStep()fornece o passo do mundo. - Obtenha os dispositivos. Cada motor e sensor é encontrado pelo nome. Esses são os nomes que você deu a eles quando montou seu robô.
- Ative os sensores. Um sensor não fornece valores até ser ativado. O passo de tempo diz a ele com que frequência atualizar.
- Laço.
robot.step(timeStep)avança a simulação em um passo e retorna -1 quando a simulação termina. Tudo o que o seu robô faz acontece dentro desse laço.
Execute
Seção intitulada “Execute”- Salve o programa como
hello.py. - Abra
game/worlds/mapping_example_1.wbte carregue o robô inicial com o botão LOAD da direita. - Carregue
hello.pycom o botão LOAD da esquerda e pressione o botão de início.
O robô anda para a frente, e o console do Webots mostra a distância até a parede à sua frente até o robô parar. Se em vez disso aparecer um erro, veja Solução de problemas.
Experimente
Seção intitulada “Experimente”- Mude a velocidade de
3.0para6.0. O que acontece perto da parede? - Faça o robô parar a 10 cm em vez de 5 cm.
- Mostre também o tempo com
robot.getTime().
Em seguida, aprenda a andar e girar em Movimento.
