콘텐츠로 이동
한국어

컬러 센서

컬러 센서는 바닥을 보기 때문에 로봇에게 구멍, 늪, 통로에 대해 알려 줘요. 아래를 향하는 1픽셀짜리 카메라예요. 스타터 로봇에서는 이름이 color이고, 바퀴가 닿기 전에 구멍을 볼 수 있도록 로봇 앞쪽에 달려 있어요.

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
colorSensor = robot.getDevice("color")
colorSensor.enable(timeStep)
while robot.step(timeStep) != -1:
image = colorSensor.getImage() # A single pixel
r = colorSensor.imageGetRed(image, 1, 0, 0)
g = colorSensor.imageGetGreen(image, 1, 0, 0)
b = colorSensor.imageGetBlue(image, 1, 0, 0)
print("r:", r, "g:", g, "b:", b)

다음은 world2.wbt에서 스타터 로봇으로 측정한 값이에요.

바닥 빨강 초록 파랑
일반 252 252 252
늪 245 222 142
구역 1과 2 사이의 통로 (파랑) 91 91 255
구역 2와 3 사이의 통로 (보라) 193 91 251
구역 1과 4 사이의 통로 (초록) 48 255 48
구역 3과 4 사이의 통로 (빨강) 255 91 91

구멍에는 검은 테두리가 있어서 구멍 바로 앞에서 바닥이 어두워져요. 체크포인트는 이 센서에게 일반 바닥과 거의 똑같이 보이므로, 자신의 월드에 있는 체크포인트에서 보정하세요.

이 프로그램은 앞으로 이동하면서 바닥이 바뀔 때마다 출력하고, 구멍을 보면 멈춰요.

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
colorSensor = robot.getDevice("color")
colorSensor.enable(timeStep)
leftWheel = robot.getDevice("left wheel motor")
rightWheel = robot.getDevice("right wheel motor")
leftWheel.setPosition(float("inf"))
rightWheel.setPosition(float("inf"))
def floorType(r, g, b):
# Tune these ranges for your own robot and worlds
brightness = (r + g + b) / 3
if brightness < 80:
return "hole"
if r > 220 and g > 200 and b < 190:
return "swamp"
if b > 220 and g < 130:
return "purple passage" if r > 150 else "blue passage"
if r > 220 and g < 130 and b < 130:
return "red passage"
if g > 220 and r < 100 and b < 100:
return "green passage"
return "floor"
leftWheel.setVelocity(3.0)
rightWheel.setVelocity(3.0)
last = None
while robot.step(timeStep) != -1:
image = colorSensor.getImage()
floor = floorType(colorSensor.imageGetRed(image, 1, 0, 0),
colorSensor.imageGetGreen(image, 1, 0, 0),
colorSensor.imageGetBlue(image, 1, 0, 0))
if floor != last:
print("Floor:", floor)
last = floor
if floor == "hole": # Stop before falling in
leftWheel.setVelocity(0)
rightWheel.setVelocity(0)
break