Movimento
O robô tem duas rodas, cada uma com seu próprio motor: left wheel motor e right wheel motor no robô inicial. Você o controla ajustando a velocidade de cada roda:
| Roda esquerda | Roda direita | O robô… |
|---|---|---|
| Para a frente | Para a frente | Anda para a frente |
| Para trás | Para trás | Anda para trás |
| Para trás | Para a frente | Gira no lugar para a esquerda |
| Para a frente | Para trás | Gira no lugar para a direita |
| Rápida | Lenta | Faz uma curva para a direita |
As velocidades são em radianos por segundo, até 6,28 (uma volta da roda por segundo). Velocidades negativas giram a roda para trás.
Andando
Seção intitulada “Andando”- Obtenha cada motor pelo nome.
- Defina a posição dele como infinito, para que ele gire sem limite.
- Defina a velocidade dele.
Os motores mantêm a última velocidade que você definiu, então o robô continua se movendo enquanto o seu programa faz outras coisas. Este programa anda para a frente, para trás e gira no lugar para a esquerda, por um segundo cada:
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())maxSpeed = 6.28 # The maximum speed of the wheels, in radians per second
leftWheel = robot.getDevice("left wheel motor")rightWheel = robot.getDevice("right wheel motor")leftWheel.setPosition(float("inf"))rightWheel.setPosition(float("inf"))
def drive(left, right, seconds): # Set the speed of each wheel, and keep it for some time leftWheel.setVelocity(left) rightWheel.setVelocity(right) start = robot.getTime() while robot.step(timeStep) != -1: if robot.getTime() - start >= seconds: break
drive(maxSpeed, maxSpeed, 1.0) # Forwarddrive(-maxSpeed, -maxSpeed, 1.0) # Backwarddrive(-2.0, 2.0, 1.0) # Spin to the leftdrive(0, 0, 0.1) # Stopprint("Done")#include <iostream>#include <cmath>#include <webots/Robot.hpp>#include <webots/Motor.hpp>
using namespace webots;
Robot *robot;Motor *leftWheel, *rightWheel;int timeStep;const double maxSpeed = 6.28; // The maximum speed of the wheels, in radians per second
// Set the speed of each wheel, and keep it for some timevoid drive(double left, double right, double seconds) { leftWheel->setVelocity(left); rightWheel->setVelocity(right); double start = robot->getTime(); while (robot->step(timeStep) != -1) { if (robot->getTime() - start >= seconds) break; }}
int main() { robot = new Robot(); timeStep = (int)robot->getBasicTimeStep();
leftWheel = robot->getMotor("left wheel motor"); rightWheel = robot->getMotor("right wheel motor"); leftWheel->setPosition(INFINITY); rightWheel->setPosition(INFINITY);
drive(maxSpeed, maxSpeed, 1.0); // Forward drive(-maxSpeed, -maxSpeed, 1.0); // Backward drive(-2.0, 2.0, 1.0); // Spin to the left drive(0, 0, 0.1); // Stop std::cout << "Done" << std::endl;
delete robot; return 0;}Medindo quanto uma roda girou
Seção intitulada “Medindo quanto uma roda girou”Cada motor tem um sensor de posição, também chamado de encoder, que conta quanto a roda girou, em radianos. Ele é útil para andar uma distância fixa. Girar para trás faz a contagem diminuir.
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
leftWheel = robot.getDevice("left wheel motor")rightWheel = robot.getDevice("right wheel motor")leftWheel.setPosition(float("inf"))rightWheel.setPosition(float("inf"))
# Each wheel has a position sensor, which counts how far it has turnedleftEncoder = leftWheel.getPositionSensor()rightEncoder = rightWheel.getPositionSensor()leftEncoder.enable(timeStep)rightEncoder.enable(timeStep)
robot.step(timeStep) # Step once, so that the sensors have a valuestart = leftEncoder.getValue()
leftWheel.setVelocity(5.0)rightWheel.setVelocity(5.0)
while robot.step(timeStep) != -1: turned = leftEncoder.getValue() - start if turned > 10.0: # Stop after 10 radians break
leftWheel.setVelocity(0)rightWheel.setVelocity(0)print("Left wheel:", round(leftEncoder.getValue() - start, 2), "rad")#include <iostream>#include <cmath>#include <webots/Robot.hpp>#include <webots/Motor.hpp>#include <webots/PositionSensor.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
Motor *leftWheel = robot->getMotor("left wheel motor"); Motor *rightWheel = robot->getMotor("right wheel motor"); leftWheel->setPosition(INFINITY); rightWheel->setPosition(INFINITY);
// Each wheel has a position sensor, which counts how far it has turned PositionSensor *leftEncoder = leftWheel->getPositionSensor(); PositionSensor *rightEncoder = rightWheel->getPositionSensor(); leftEncoder->enable(timeStep); rightEncoder->enable(timeStep);
robot->step(timeStep); // Step once, so that the sensors have a value double start = leftEncoder->getValue();
leftWheel->setVelocity(5.0); rightWheel->setVelocity(5.0);
while (robot->step(timeStep) != -1) { double turned = leftEncoder->getValue() - start; if (turned > 10.0) // Stop after 10 radians break; }
leftWheel->setVelocity(0); rightWheel->setVelocity(0); cout << "Left wheel: " << leftEncoder->getValue() - start << " rad" << endl;
delete robot; return 0;}Controlar um movimento pelo tempo é simples, mas o resultado muda com a velocidade. Os encoders medem o próprio movimento, então são mais confiáveis. Para giros precisos, o giroscópio é ainda melhor.
Em seguida, aprenda a ler os sensores.
