コンテンツにスキップ
日本語

距離センサー

距離センサーは、正面にある最初の物体までの距離を メートル 単位で、最大 0.8 m まで測ります。スターターロボットには leftDist、frontDist、rightDist の 3 つがあり、それぞれ左、前、右を向いています。

上から見た 3 つの距離センサー。左、前、右を向いている

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
frontDist = robot.getDevice("frontDist") # Get the sensor by name
frontDist.enable(timeStep) # Enable it
while robot.step(timeStep) != -1:
print("Front:", round(frontDist.getValue(), 3), "m")

タイルの幅は 12 cm なので、ロボットがタイルの中央にいるとき、横の壁までは約 5 cm です。このプログラムは 3 つのセンサーを確認し、壁が見えているセンサーを表示します。

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
# Keep the sensors in a dictionary, to use them by name
sensors = {}
for name in ["leftDist", "frontDist", "rightDist"]:
sensors[name] = robot.getDevice(name)
sensors[name].enable(timeStep)
WALL = 0.07 # Closer than 7 cm counts as a wall next to the robot
while robot.step(timeStep) != -1:
walls = [name for name, sensor in sensors.items() if sensor.getValue() < WALL]
print("Walls:", walls)

センサーは、ロボットの中心からではなく、ロボット上で取り付けられた位置から距離を測ります。壁の近くで値を表示して、自分でしきい値を決めましょう。