Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
howto:thymio:thymio_barcode_reader [2023/06/24 15:33] – supprimée - modification externe (Unknown date) 127.0.0.1 | howto:thymio:thymio_barcode_reader [2023/06/27 16:12] (Version actuelle) – [Specifications] marie | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Thymio, barcode reader ====== | ||
+ | ===== How to use the code ===== | ||
+ | Requirements: | ||
+ | * a Thymio II | ||
+ | * Aseba Studio | ||
+ | * a micro-SD card (note: while the site does [[http:// | ||
+ | * an adaptator, if your computer doesn' | ||
+ | * wav mono sound files under 8000hz, without metadata, encoded " | ||
+ | * a printer, or a sheet of paper and a black pen (not every ink works. if your ink reflects too much ir light, Thymio won't be able to recognize it. to avoid this, prefer carbon black ink.) | ||
+ | Name each of your audio files " | ||
+ | Next, you need a sheet of paper with some black lines drawn on. Note that each line and each space between the lines must be minimum 0,5mm and maximum 9cm wide, and minimum 8cm long. Different notes are registered every 0,5mm, and it may read a total of 20 notes. This may be edited in the code, but keep the motor speeds low. If Thymio moves any faster, it can't keep up as well, and gives false values. | ||
+ | ===== How does it work ===== | ||
+ | ==== Specifications ==== | ||
+ | < | ||
+ | #this code reads barcodes based on bar width | ||
+ | |||
+ | #speed = 0,5 cm/s ; max 20 notes ; different note every 0,5 cm ; 9 different notes ; min width 0,5 cm ; max width 9cm ; min length 5 cm but 8 recommended ; ink must absorb ir light (carbon black) | ||
+ | #prioritize the bars to be too big rather than too small (for example, if a bar should be 3cm; 3,4cm > 2,9cm). in fact, do make them a millimeter or two wider | ||
+ | </ | ||
+ | These are just specifications, | ||
+ | ==== Setup ==== | ||
+ | < | ||
+ | #----- | ||
+ | var state = 0 | ||
+ | var counter = 0 | ||
+ | var deque[2+(1*40)] | ||
+ | var n | ||
+ | var note | ||
+ | var pause | ||
+ | timer.period[0]=500 #half a second | ||
+ | </ | ||
+ | Here, I'm setting my variables and the timer. | ||
+ | |||
+ | Technically, | ||
+ | |||
+ | ==== Buttons ==== | ||
+ | < | ||
+ | #----- | ||
+ | onevent button.center | ||
+ | if button.center == 1 then | ||
+ | state = 1 | ||
+ | end | ||
+ | onevent button.forward #skip straight to playing sounds if impatient | ||
+ | if button.forward == 1 and state == 1 then | ||
+ | state = 3 | ||
+ | end | ||
+ | </ | ||
+ | If you've downloaded the code, you'll notice this part is actually at the end. It doesn' | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
+ | ==== onevent timer0 and state 1 ==== | ||
+ | < | ||
+ | #----- | ||
+ | onevent timer0 | ||
+ | counter++ | ||
+ | #--- "white state" | ||
+ | if state == 1 then | ||
+ | call leds.top(0, | ||
+ | motor.right.target = 25 | ||
+ | motor.left.target = 25 | ||
+ | if prox.ground.delta[0] < 700 or prox.ground.delta[1] < 700 then | ||
+ | call deque.push_back(deque, | ||
+ | state = 2 | ||
+ | counter = 0 | ||
+ | else if counter > 15 then | ||
+ | state = 3 | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | With the line '' | ||
+ | |||
+ | As stated above, state is equal to 1 when the center button is pressed. '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
+ | ==== state 2 and 3 ==== | ||
+ | < | ||
+ | #--- "black state" | ||
+ | if state == 2 then | ||
+ | call leds.top(32, | ||
+ | if prox.ground.delta[0] > 900 or prox.ground.delta[1] > 900 then | ||
+ | call deque.push_back(deque, | ||
+ | state = 1 | ||
+ | counter = 0 | ||
+ | end | ||
+ | end | ||
+ | #--- transition, check if empty | ||
+ | if state == 3 then | ||
+ | call sound.play(-1) | ||
+ | call leds.top(0, | ||
+ | motor.right.target = 0 | ||
+ | motor.left.target = 0 | ||
+ | call deque.size(deque, | ||
+ | if n == 0 then | ||
+ | state = 0 | ||
+ | else | ||
+ | state = 4 | ||
+ | counter = 0 | ||
+ | end | ||
+ | call deque.pop_front(deque, | ||
+ | end | ||
+ | </ | ||
+ | '' | ||
+ | |||
+ | So, it's gotta keep repeating those two states until there' | ||
+ | |||
+ | After that, we have state 3. As stated above, state is equal to 3 when Thymio hasn't encountered a black line in more than 7,5 seconds. The leds turn off and the motors stop moving. | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
+ | ==== state 4 ==== | ||
+ | < | ||
+ | #--- execution | ||
+ | if state == 4 then | ||
+ | if counter == pause then | ||
+ | call deque.pop_front(deque, | ||
+ | if note <= 1 then | ||
+ | call sound.play(11) | ||
+ | call leds.top(31, | ||
+ | end | ||
+ | if note == 2 then | ||
+ | call sound.play(12) | ||
+ | call leds.top(32, | ||
+ | end | ||
+ | if note == 3 then | ||
+ | call sound.play(13) | ||
+ | call leds.top(0, | ||
+ | end | ||
+ | if note == 4 then | ||
+ | call sound.play(14) | ||
+ | call leds.top(32, | ||
+ | end | ||
+ | if note == 5 then | ||
+ | call sound.play(15) | ||
+ | call leds.top(25, | ||
+ | end | ||
+ | if note == 6 then | ||
+ | call sound.play(16) | ||
+ | call leds.top(32, | ||
+ | end | ||
+ | if note == 7 then | ||
+ | call sound.play(17) | ||
+ | call leds.top(0, | ||
+ | end | ||
+ | if note == 8 then | ||
+ | call sound.play(18) | ||
+ | call leds.top(32, | ||
+ | end | ||
+ | if note == 9 then | ||
+ | call sound.play(19) | ||
+ | call leds.top(30, | ||
+ | end | ||
+ | if note == 10 then | ||
+ | call sound.play(110) | ||
+ | call leds.top(20, | ||
+ | end | ||
+ | if note == 11 then | ||
+ | call sound.play(111) | ||
+ | call leds.top(5, | ||
+ | end | ||
+ | if note == 12 then | ||
+ | call sound.play(112) | ||
+ | call leds.top(15, | ||
+ | end | ||
+ | if note == 13 then | ||
+ | call sound.play(113) | ||
+ | call leds.top(12, | ||
+ | end | ||
+ | if note == 14 then | ||
+ | call sound.play(114) | ||
+ | call leds.top(31, | ||
+ | end | ||
+ | if note >= 15 then | ||
+ | call sound.play(115) | ||
+ | call leds.top(2, | ||
+ | end | ||
+ | end | ||
+ | if counter == 1 then | ||
+ | state = 3 | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' |