콘텐츠로 이동
한국어

위치 센서

위치 센서, 즉 GPS는 로봇의 위치를 미터 단위로 알려 줘요. 스타터 로봇에서는 이름이 gps예요. 벽 토큰을 보고하려면 GPS가 필요하고, 체계적으로 탐색하는 데에도 큰 도움이 돼요.

getValues()는 [x, y, z]를 반환해요. X와 Z가 수평이고 Y가 수직이므로, 미로 안의 위치에는 X와 Z를 사용하세요.

수평면의 X축과 Z축, 그리고 수직인 Y축

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
gps = robot.getDevice("gps")
gps.enable(timeStep)
while robot.step(timeStep) != -1:
x, y, z = gps.getValues() # In meters. X and Z are horizontal, Y is vertical.
print("x:", round(x, 3), "z:", round(z, 3))

GPS에는 몇 밀리미터의 노이즈가 있어서, 로봇이 멈춰 있어도 값이 조금씩 바뀌어요. 위치가 정확히 같은지 비교하지 마세요.

이 프로그램은 로봇이 출발한 곳에서 반 타일만큼 떨어질 때까지 앞으로 이동해요.

from controller import Robot
import math
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
gps = robot.getDevice("gps")
gps.enable(timeStep)
leftWheel = robot.getDevice("left wheel motor")
rightWheel = robot.getDevice("right wheel motor")
leftWheel.setPosition(float("inf"))
rightWheel.setPosition(float("inf"))
robot.step(timeStep) # Step once, so that the GPS has a value
startX, _, startZ = gps.getValues()
leftWheel.setVelocity(3.0)
rightWheel.setVelocity(3.0)
while robot.step(timeStep) != -1:
x, _, z = gps.getValues()
distance = math.sqrt((x - startX) ** 2 + (z - startZ) ** 2)
print("Distance from the start:", round(distance, 3), "m")
if distance > 0.06: # Half a tile
leftWheel.setVelocity(0)
rightWheel.setVelocity(0)
break