#* ------------------------------------------------------------------------- *# # Sensors: prox, mic # Actuators: motor.right.target, motor.right.target # Timers: timer0 # Other local events: # Externat events: # emit : # onevent: #* ------------------------------------------------------------------------- *# #* ------------------------------------------------------------------------- *# # CONSTS #* ------------------------------------------------------------------------- *# # Max speed : high speed makes too much noise ! var VMAX = 300 var SPEEDSTEP = 10 var MOTORUPDATETIME = 50 # in ms var OBSDIST = 100 #* ------------------------------------------------------------------------- *# # GLOBAL VARS #* ------------------------------------------------------------------------- *# var lmotortarget = 0 var rmotortarget = 0 #* ------------------------------------------------------------------------- *# # LOCAL VARS for subs #* ------------------------------------------------------------------------- *# #* ------------------------------------------------------------------------- *# # INIT #* ------------------------------------------------------------------------- *# # reset outputs call sound.system(-1) call leds.top(0,0,0) call leds.bottom.left(0,0,0) call leds.bottom.right(0,0,0) call leds.circle(0,0,0,0,0,0,0,0) motor.left.target = 0 motor.right.target = 0 # mic threshold = max - 1 mic.threshold = 254 #* ------------------------------------------------------------------------- *# # SUBS #* ------------------------------------------------------------------------- *# #* ------------------------------------------------------------------------- *# # EVENTS #* ------------------------------------------------------------------------- *# #* ------------------------------------ MIC change motor target speed ------------------------------------ *# onevent mic call math.rand(lmotortarget) lmotortarget = lmotortarget % VMAX call math.rand(rmotortarget) rmotortarget = rmotortarget % VMAX timer.period[0] = 1 #* ------------------------------------ TIMER0 update motor.l/r.target step by step ------------------------------------ *# onevent timer0 if (motor.left.target < lmotortarget) then motor.left.target += SPEEDSTEP elseif (motor.left.target > lmotortarget) then motor.left.target -= SPEEDSTEP end if (motor.right.target < rmotortarget) then motor.right.target += SPEEDSTEP elseif (motor.right.target > rmotortarget) then motor.right.target -= SPEEDSTEP end timer.period[0] = MOTORUPDATETIME #* ------------------------------------ PROX very simple obstacle management ------------------------------------ *# onevent prox if (prox.horizontal[0] > OBSDIST) then lmotortarget = - VMAX timer.period[0] = 1 end if (prox.horizontal[4] > OBSDIST) then rmotortarget = - VMAX timer.period[0] = 1 end if (prox.horizontal[5] > OBSDIST) then lmotortarget = VMAX timer.period[0] = 1 end if (prox.horizontal[6] > OBSDIST) then rmotortarget = VMAX timer.period[0] = 1 end