İçeriğe geç
Türkçe

Konum sensörü

Konum sensörü ya da GPS, robotun konumunu metre cinsinden verir. Başlangıç robotunda adı gps’tir. Duvar işaretlerini bildirmek için ona ihtiyacın var ve sistemli keşif yapmana da çok yardımcı olur.

getValues(), [x, y, z] döndürür. X ve Z yataydır, Y ise dikeydir. Bu yüzden labirentteki konum için X ve Z’yi kullan:

Yatay düzlemin X ve Z eksenleri, Y dikey

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’in birkaç milimetrelik gürültüsü vardır; bu yüzden robot dururken bile değerler biraz değişir. Konumların tam olarak eşit olup olmadığını asla kontrol etme.

Bu program, robot başladığı yerden yarım kare uzaklaşana kadar ileri gider:

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