Diferencia entre revisiones de «Butialo: Ejemplo 7 - Robot que no se caiga de la mesa con eventos, utilizando un sensor de grises y uno de distancia - Código»

De Proyecto Butiá
Saltar a: navegación, buscar
(Página creada con '<syntaxhighlight lang="lua"> local COLOR_MESA = 55000 local INTERVALO_CHEQUEO = 0.05 local TIEMPO_RETROCESO = 0.1 local VELOCIDAD_RETROCESO = 500 local VELOCIDAD_GIRO = 500 loca...')
 
 
(No se muestran 2 ediciones intermedias del mismo usuario)
Línea 14: Línea 14:
 
local tiempo_volver_inicio = 2.0
 
local tiempo_volver_inicio = 2.0
 
local ADELANTE_FUERA_MESA, ATRAS_FUERA_MESA, EN_LA_MESA = 1, 2, 3
 
local ADELANTE_FUERA_MESA, ATRAS_FUERA_MESA, EN_LA_MESA = 1, 2, 3
--local ret = 3
 
  
 
local function chequear_avance_o_retrocedo()
 
local function chequear_avance_o_retrocedo()
-- Avanzar = true, Retriceder = false
+
    -- Avanzar = true, Retriceder = false
local sentido = math.random (0, 1)
+
    local sentido = math.random (0, 1)
return sentido > 0.5
+
    return sentido > 0.5
 
end
 
end
  
  
 
local function moverse()
 
local function moverse()
  local sentido = chequear_avance_o_retrocedo();
+
    local sentido = chequear_avance_o_retrocedo();
  if(sentido) then
+
    if(sentido) then
    Motors.setvel2mtr(0, MOTOR_VEL, 0, MOTOR_VEL)
+
        Motors.setvel2mtr(0, MOTOR_VEL, 0, MOTOR_VEL)
  else
+
    else
    Motors.setvel2mtr(1, MOTOR_VEL, 1, MOTOR_VEL)
+
Motors.setvel2mtr(1, MOTOR_VEL, 1, MOTOR_VEL)
  end
+
    end
 
end
 
end
  
 
local function puedo_seguir()
 
local function puedo_seguir()
  
  if Grey_2.getValue() > COLOR_MESA then
+
    if Grey_2.getValue() > COLOR_MESA then
    --retroceder
+
        --retroceder
    ret = ADELANTE_FUERA_MESA
+
        ret = ADELANTE_FUERA_MESA
  else
+
    else
    if Distanc_6.getValue() < DISTANCIA_MESA then
+
        if Distanc_6.getValue() < DISTANCIA_MESA then
      --avanzar
+
            --avanzar
      ret = ATRAS_FUERA_MESA
+
            ret = ATRAS_FUERA_MESA
    else
+
        else
      --puedo seguir
+
            --puedo seguir
      ret = EN_LA_MESA
+
            ret = EN_LA_MESA
    end
+
        end
  end
+
    end
  return ret   
+
    return ret   
 
 
 
 
 
end
 
end
  
 
local function chequear_giro_izquierda()
 
local function chequear_giro_izquierda()
-- Para girar random izq o der, uso un entero del 0-1
+
    -- Para girar random izq o der, uso un entero del 0-1
-- Si valor random > 0.5 es izq, sino der
+
    -- Si valor random > 0.5 es izq, sino der
-- DERECHA = false, IZQUIERDA = true
+
    -- DERECHA = false, IZQUIERDA = true
local sentido = math.random (0, 1)
+
    local sentido = math.random (0, 1)
return sentido > 0.5
+
    return sentido > 0.5
 
end
 
end
  
 
local function girar_aleatorio()
 
local function girar_aleatorio()
local tiempo_giro = math.random(MIN_TIEMPO_GIRO, MAX_TIEMPO_GIRO)
+
    local tiempo_giro = math.random(MIN_TIEMPO_GIRO, MAX_TIEMPO_GIRO)
local izquierda = chequear_giro_izquierda()
+
    local izquierda = chequear_giro_izquierda()
if (izquierda) then
+
    if (izquierda) then
Motors.setvel2mtr(0, VELOCIDAD_GIRO, 1, VELOCIDAD_GIRO)
+
        Motors.setvel2mtr(0, VELOCIDAD_GIRO, 1, VELOCIDAD_GIRO)
else
+
    else
Motors.setvel2mtr(1, VELOCIDAD_GIRO, 0, VELOCIDAD_GIRO)
+
        Motors.setvel2mtr(1, VELOCIDAD_GIRO, 0, VELOCIDAD_GIRO)
end
+
    end
util.wait(tiempo_giro)
+
    util.wait(tiempo_giro)
 
end
 
end
  
 
local function volver_inicio()
 
local function volver_inicio()
-- se que los posibles valores de ret son 1 o 2
+
    -- se que los posibles valores de ret son: ADELANTE_FUERA_MESA o ATRAS_FUERA_MESA
  if (ret == ATRAS_FUERA_MESA) then
+
    if (ret == ATRAS_FUERA_MESA) then
    Motors.setvel2mtr(0, VELOCIDAD_RETROCESO, 0, VELOCIDAD_RETROCESO)
+
        Motors.setvel2mtr(0, VELOCIDAD_RETROCESO, 0, VELOCIDAD_RETROCESO)
else
+
    else
  Motors.setvel2mtr(1, VELOCIDAD_RETROCESO, 1, VELOCIDAD_RETROCESO)  
+
Motors.setvel2mtr(1, VELOCIDAD_RETROCESO, 1, VELOCIDAD_RETROCESO)  
end
+
    end
util.wait(tiempo_volver_inicio)
+
    util.wait(tiempo_volver_inicio)
girar_aleatorio()
+
    girar_aleatorio()
 
end
 
end
  

Revisión actual del 11:54 15 oct 2012

local COLOR_MESA = 55000
local INTERVALO_CHEQUEO = 0.05
local TIEMPO_RETROCESO = 0.1
local VELOCIDAD_RETROCESO = 500
local VELOCIDAD_GIRO = 500
local MOTOR_VEL = 500
local MIN_TIEMPO_GIRO = 0.5
local MAX_TIEMPO_GIRO = 2
local MIN_TIEMPO_RETROCESO = 1.5
local MAX_TIEMPO_RETROCESO = 2.5
local DISTANCIA_MESA = 21000
local debe_avanzar = true
local tiempo_volver_inicio = 2.0
local ADELANTE_FUERA_MESA, ATRAS_FUERA_MESA, EN_LA_MESA = 1, 2, 3

local function chequear_avance_o_retrocedo()
    -- Avanzar = true, Retriceder = false
    local sentido = math.random (0, 1)
    return sentido > 0.5
end


local function moverse()
    local sentido = chequear_avance_o_retrocedo();
    if(sentido) then
        Motors.setvel2mtr(0, MOTOR_VEL, 0, MOTOR_VEL)
    else
	Motors.setvel2mtr(1, MOTOR_VEL, 1, MOTOR_VEL)
    end
end

local function puedo_seguir()

    if Grey_2.getValue() > COLOR_MESA then
        --retroceder
        ret = ADELANTE_FUERA_MESA
    else
        if Distanc_6.getValue() < DISTANCIA_MESA then
            --avanzar
            ret = ATRAS_FUERA_MESA
        else
            --puedo seguir
            ret = EN_LA_MESA
        end
    end
    return ret   
	 
end

local function chequear_giro_izquierda()
    -- Para girar random izq o der, uso un entero del 0-1
    -- Si valor random > 0.5 es izq, sino der
    -- DERECHA = false, IZQUIERDA = true
    local sentido = math.random (0, 1)
    return sentido > 0.5
end

local function girar_aleatorio()
    local tiempo_giro = math.random(MIN_TIEMPO_GIRO, MAX_TIEMPO_GIRO)
    local izquierda = chequear_giro_izquierda()
    if (izquierda) then
        Motors.setvel2mtr(0, VELOCIDAD_GIRO, 1, VELOCIDAD_GIRO)
    else
        Motors.setvel2mtr(1, VELOCIDAD_GIRO, 0, VELOCIDAD_GIRO)
    end
    util.wait(tiempo_giro)
end

local function volver_inicio()
    -- se que los posibles valores de ret son: ADELANTE_FUERA_MESA o ATRAS_FUERA_MESA
    if (ret == ATRAS_FUERA_MESA) then
        Motors.setvel2mtr(0, VELOCIDAD_RETROCESO, 0, VELOCIDAD_RETROCESO)
    else
	Motors.setvel2mtr(1, VELOCIDAD_RETROCESO, 1, VELOCIDAD_RETROCESO) 
    end
    util.wait(tiempo_volver_inicio)
    girar_aleatorio()
end

events.add(puedo_seguir, '==', ADELANTE_FUERA_MESA, volver_inicio)
events.add(puedo_seguir, '==', ATRAS_FUERA_MESA, volver_inicio)
events.add(puedo_seguir, '==', EN_LA_MESA, moverse)

events.go()