이동
로봇에는 바퀴가 두 개 있고, 각 바퀴에 모터가 하나씩 달려 있어요. 스타터 로봇에서는 left wheel motor와 right wheel motor예요. 각 바퀴의 속도를 설정해서 로봇을 움직여요.
| 왼쪽 바퀴 | 오른쪽 바퀴 | 로봇은… |
|---|---|---|
| 앞으로 | 앞으로 | 앞으로 이동 |
| 뒤로 | 뒤로 | 뒤로 이동 |
| 뒤로 | 앞으로 | 제자리에서 왼쪽으로 회전 |
| 앞으로 | 뒤로 | 제자리에서 오른쪽으로 회전 |
| 빠르게 | 느리게 | 오른쪽으로 곡선 주행 |
속도의 단위는 초당 라디안이고, 최대 6.28(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;}바퀴가 얼마나 돌았는지 측정하기
섹션 제목: “바퀴가 얼마나 돌았는지 측정하기”각 모터에는 인코더라고도 하는 위치 센서가 있어서, 바퀴가 얼마나 돌았는지 라디안 단위로 측정해요. 정해진 거리를 이동할 때 유용해요. 뒤로 돌면 값이 줄어들어요.
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;}시간으로 동작을 제어하는 방법은 간단하지만, 결과가 속도에 따라 달라져요. 인코더는 동작 자체를 측정하므로 더 믿을 만해요. 정확하게 회전하려면 자이로가 더 좋아요.
다음으로 센서 읽는 법을 배워 보세요.
