#* ------------------------------------------------------------------------- *# # Sensors: prox, mic # Actuators: motor.right.target, motor.right.target, LEDs # Timers: timer0, timer1 # Other local events: # Externat events: # emit : # onevent: #* ------------------------------------------------------------------------- *# #* ------------------------------------------------------------------------- *# # CONSTS #* ------------------------------------------------------------------------- *# # Max speed : high speed makes too much noise ! var VMAX = 350 var SPEEDSTEP = 20 var MOTORUPDATETIME = 50 # in ms var OBSDIST = 100 # LEDs var led[8] var led_state = 0 var fixed var led_pulse #* ------------------------------------------------------------------------- *# # 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 #* ------------------------------------------------------------------------- *# #* ------------------------------------ TIMER1 LED animation. Based on Thymio's default behaviour code ------------------------------------ *# #### Timer 0 set to 20 ms timer.period[1] = 20 #### Event triggered on timer 1 period (here, 20 ms) onevent timer1 # Led ring animation call math.fill(led, 0) led_state = (led_state + 2) & 0xff fixed = led_state / 32 led[fixed] = 32 led[(fixed - 2) & 0x7] = 32 - (led_state & 0x1F) led[(fixed + 3) & 0x7] = led_state & 0x1F call leds.circle(led[0], led[1], led[2], led[3], led[4], led[5], led[6], led[7]) # Body color pulse led_pulse = led_pulse + 1 if led_pulse > 0 then call leds.top(led_pulse, 0, 0) call leds.bottom.left(led_pulse,0,0) call leds.bottom.right(led_pulse,0,0) call leds.buttons(led_pulse,led_pulse,led_pulse,led_pulse) if led_pulse > 32 then led_pulse = -32 end else call leds.top(-led_pulse, 0, 0) call leds.bottom.left(-led_pulse,0,0) call leds.bottom.right(-led_pulse,0,0) call leds.buttons(-led_pulse,-led_pulse,-led_pulse,-led_pulse) end #* ------------------------------------ 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