移動
ロボットには 2 つの車輪があり、それぞれに専用のモーターが付いています。スターターロボットでは left wheel motor と right wheel motor です。各車輪の速度を設定してロボットを動かします。
| 左の車輪 | 右の車輪 | ロボットの動き |
|---|---|---|
| 前進 | 前進 | 前進する |
| 後退 | 後退 | 後退する |
| 後退 | 前進 | その場で左に回転する |
| 前進 | 後退 | その場で右に回転する |
| 速い | 遅い | 右にカーブする |
速度の単位は ラジアン毎秒 で、最大 6.28(1 秒に車輪 1 回転)です。負の速度にすると車輪は逆回転します。
- 名前を指定して各モーターを取得します。
- 位置を無限大に設定して、制限なく回るようにします。
- 速度を設定します。
モーターは最後に設定した速度を保つので、プログラムがほかの処理をしている間もロボットは動き続けます。このプログラムは、前進、後退、左回転をそれぞれ 1 秒ずつ行います。
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;}車輪がどれだけ回ったかを測る
Section titled “車輪がどれだけ回ったかを測る”各モーターには位置センサー(エンコーダーとも呼びます)があり、車輪が回った量をラジアンで数えます。決まった距離を走らせるのに便利です。逆回転すると値は減っていきます。
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;}時間で動きを制御するのは簡単ですが、結果は速度によって変わります。エンコーダーは動きそのものを測るので、より確実です。正確に旋回したいなら、ジャイロがさらに適しています。
次は、センサーの読み取り方を学びましょう。
