Butialo: Ejemplo 1 - Seguidor de líneas con un sensor sin eventos - Código
De Proyecto Butiá
local COLOR_NEGRO = 35000
local MOTOR_VEL = 500
local MOTOR_GIRO_VEL = 500
local TIEMPO_CENTRADO = 0.1
local sentido = "D"
local tiempo
local function invertir_sentido()
if sentido == "I" then
sentido = "D"
else
sentido = "I"
end
end
local function avanzar()
Motors.setvel2mtr(0, MOTOR_VEL, 0, MOTOR_VEL)
end
local function es_negro()
local color = Grey_1.getValue()
return (color > COLOR_NEGRO)
end
local function girar()
if sentido == "I" then
Motors.setvel2mtr(1,MOTOR_GIRO_VEL,0,MOTOR_GIRO_VEL)
else
Motors.setvel2mtr(0,MOTOR_GIRO_VEL,1,MOTOR_GIRO_VEL)
end
invertir_sentido()
end
local function buscar_negro()
-- Se inicializa el tiempo en 0.5 segundo
tiempo = 0.5
local tiempo_girando
while true do
tiempo_girando = 0
girar()
-- El robot gira hasta encontrar el color negro o si no por x tiempo
while (not es_negro() and tiempo_girando <= tiempo) do
util.wait(0.1)
tiempo_girando = tiempo_girando + 0.1
end
-- Si es negro no se debe seguir iterando
if es_negro() then
break
else
tiempo = tiempo * 2
end
end
end
while true do
avanzar()
while (es_negro()) do
util.wait(0.1)
end
buscar_negro()
util.wait(TIEMPO_CENTRADO)
invertir_sentido()
end