Ce fichier doit s’appeler par exemple: FSM_read_bits_from_ground.aesl
Ce code utilise des subroutines pour être mieux structuré et la machine d’états est réalisée à l’aide de deux subroutines: update_state où l’on détermine les transitions d’état et update_output qui détermine les sorties pour chaque état de la machine. Lorsque vous pressez sur le bouton du centre, il démarre et avance à une vitesse de 50. “onevent prox” permet de “lire” le signal d’horloge (capteur gauche) et lorsque l’on détecte le flanc montant de l’horloge, la subroutine read_data va lire la donnée (data) à l’aide du capteur droit. Votre travail consiste à modiifer les subroutines update_state et update_output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
var data var state var clock_value var ground_reading clock_value = 0 data = 0 state = 0 motor.left.target = 0 motor.right.target = 0 #------------------------------------------------ sub update_state # update the states if state == 0 then if data == 0 then state = 0 else state = 1 end elseif state == 1 then if data == 0 then state = 0 else state = 1 end else motor.left.target = 0 # error condition motor.right.target = 0 call leds.top(32,0,0) # signal error condition end #------------------------------------------------ sub update_output # update the outputs if state == 0 then call leds.circle(1,0,0,0,0,0,0,0) # show current state for debugging elseif state == 1 then call leds.circle(0,1,0,0,0,0,0,0) # show current state for debugging call leds.top(0,32,0) # signal goal end #------------------------------------------------ sub read_data # read the right ground sensor if prox.ground.delta[1] < 350 then # black line (data) call leds.bottom.right(0,0,32) # show the data value data = 1 # update the data value else call leds.bottom.right(0,0,0) # show the data value data = 0 # update the data value end #------------------------------------------------ sub update_all callsub read_data # read the data callsub update_state # update the states callsub update_output # update the output #------------------------------------------------ onevent prox ground_reading = prox.ground.delta[0] # read the left ground sensor (clock) if clock_value == 0 then if ground_reading < 340 then # black line clock_value = 1 # update the clock value call leds.bottom.left(0,0,32) # show the clock value callsub update_all # rising clock ! => |read right sensor # |update states # |update outputs end else if ground_reading > 360 then # white line clock_value = 0 # update the clock value call leds.bottom.left(0,0,0) # show the clock value end end #------------------------------------------------ onevent button.center if button.center == 1 then if motor.left.target == 0 then # start moving state = 0 call leds.top(0,0,0) motor.left.target = 50 motor.right.target = 50 else # stop moving motor.left.target = 0 motor.right.target = 0 end end |