자이로와 가속도계
자이로스코프
섹션 제목: “자이로스코프”자이로스코프는 각 축을 중심으로 한 로봇의 회전 속도를 초당 라디안 단위로 반환해요. 스타터 로봇에서는 이름이 gyro예요.
getValues()는 [x, y, z]를 반환해요. Y축이 수직이므로 getValues()[1]이 로봇의 회전 속도예요. 왼쪽으로 돌 때 양수가 돼요.
예제: 90도 회전하기
섹션 제목: “예제: 90도 회전하기”회전 속도에 타임 스텝을 곱해서 더해 나가면 회전한 각도를 구할 수 있어요. 속도에 의존하지 않기 때문에 회전을 시간으로 제어하는 것보다 더 믿을 만해요.
from controller import Robotimport math
robot = Robot()timestep = int(robot.getBasicTimeStep())
left = robot.getDevice("left wheel motor")right = robot.getDevice("right wheel motor")left.setPosition(float("inf"))right.setPosition(float("inf"))
gyro = robot.getDevice("gyro")gyro.enable(timestep)
def turnLeft(angle): turned = 0 left.setVelocity(-2) right.setVelocity(2) while abs(turned) < angle and robot.step(timestep) != -1: turned += gyro.getValues()[1] * timestep / 1000 # Speed multiplied by the time, in seconds left.setVelocity(0) right.setVelocity(0)
turnLeft(math.pi / 2)print("Turned 90 degrees")#include <cmath>#include <iostream>#include <webots/Robot.hpp>#include <webots/Motor.hpp>#include <webots/Gyro.hpp>
using namespace webots;
Robot *robot;Motor *leftMotor, *rightMotor;Gyro *gyro;int timeStep;
void turnLeft(double angle) { double turned = 0; leftMotor->setVelocity(-2); rightMotor->setVelocity(2); while (fabs(turned) < angle && robot->step(timeStep) != -1) turned += gyro->getValues()[1] * timeStep / 1000.0; // Speed multiplied by the time, in seconds leftMotor->setVelocity(0); rightMotor->setVelocity(0);}
int main() { robot = new Robot(); timeStep = (int)robot->getBasicTimeStep();
leftMotor = robot->getMotor("left wheel motor"); rightMotor = robot->getMotor("right wheel motor"); leftMotor->setPosition(INFINITY); rightMotor->setPosition(INFINITY);
gyro = robot->getGyro("gyro"); gyro->enable(timeStep);
turnLeft(M_PI / 2); std::cout << "Turned 90 degrees" << std::endl;
delete robot; return 0;}스타터 로봇으로 테스트했을 때, 이렇게 계산한 각도는 실제 회전과 0.01 라디안 이내로 일치했어요. 작은 오차도 게임 동안 쌓이므로, 기회가 있을 때마다 방향을 보정하세요. 예를 들어 벽에 맞춰 정렬할 수 있어요.
가속도계
섹션 제목: “가속도계”가속도계는 각 축 방향의 가속도를 m/s² 단위로 반환해요. 스타터 로봇에서는 이름이 accelerometer예요. 정지해 있을 때 getValues()[1]은 약 9.8인데, 이것이 중력이에요.
관성 측정 장치
섹션 제목: “관성 측정 장치”관성 측정 장치(inertial unit)는 스타터 로봇에 없어요. Robot Customizer로 추가할 수 있으며, 기본 이름은 inertial_unit이에요. 회전 속도를 더해 나갈 필요 없이 getRollPitchYaw()로 방향을 바로 알려 줘요. Webots 레퍼런스를 참고하세요.
