summaryrefslogtreecommitdiff
path: root/src/modelli
diff options
context:
space:
mode:
Diffstat (limited to 'src/modelli')
-rw-r--r--src/modelli/lego-piano/.gitignore2
-rw-r--r--src/modelli/lego-piano/document.en.rest.txt258
-rw-r--r--src/modelli/lego-piano/document.it.rest.txt257
l---------src/modelli/lego-piano/du2html.xsl1
4 files changed, 518 insertions, 0 deletions
diff --git a/src/modelli/lego-piano/.gitignore b/src/modelli/lego-piano/.gitignore
new file mode 100644
index 0000000..08d33d4
--- /dev/null
+++ b/src/modelli/lego-piano/.gitignore
@@ -0,0 +1,2 @@
+/document.en.du.xml
+/document.it.du.xml
diff --git a/src/modelli/lego-piano/document.en.rest.txt b/src/modelli/lego-piano/document.en.rest.txt
new file mode 100644
index 0000000..309d5c6
--- /dev/null
+++ b/src/modelli/lego-piano/document.en.rest.txt
@@ -0,0 +1,258 @@
+======================================
+Making the LEGO Grand Piano play music
+======================================
+:CreationDate: 2020-08-28 10:39:28
+:Id: modelli/lego-piano
+:tags: - 3dPrint
+ - hardware
+ - software
+ - models
+
+The `LEGO Grand Piano 21323
+<https://www.lego.com/en-gb/product/grand-piano-21323>`_ is a great
+model, full of clever details and impressive engineering.
+
+It contains a bit of electronics, that allow it to talk with a phone
+app via Bluetooth.
+
+.. image:: lego-electronics.jpg
+ :width: 100%
+ :alt: a Lego optical sensor, motor, power&control brick, mounted on
+ a wide base of sideways pieces; the motor moves a long axle
+ with many pegs at right angles to each other
+
+The app can play a few built-in tunes while running the motor inside
+the piano, which pulls some keys down in a regular pattern, so you can
+pretend to have a "player piano". The app can also wait for you to
+press any key on the piano (they all move a little flag in front of
+the optical sensor) before playing the next note, so you can pretend
+to be playing.
+
+That's fun, but not really enough: there are 25 independent keys,
+surely we can make them play the right note!
+
+.. image:: keys.jpg
+ :width: 100%
+ :alt: the full keyboard of the Lego piano, 2 octaves + 1, with
+ correctly-alternating white and black keys, each key has a
+ matching hammer
+
+.. video:: keys-hammers.mp4
+ :width: 100%
+ :type: video/mp4
+
+After some research, I decided to use optical sensors and a big
+microcontroller. How hard can it be? ☺
+
+As sensor I picked the `QRD1114
+<https://learn.sparkfun.com/tutorials/qrd1114-optical-detector-hookup-guide/all>`_,
+which is dead easy to use: infrared LED plus phototransistor, with a
+very narrow and short area of sensitivity, so there's very little
+chance of nearby keys setting off the wrong sensor.
+
+The controller I used is a `Lilygo TTGO-T7 v1.3
+<http://www.lilygo.cn/prod_view.aspx?TypeId=50033&Id=1258&FId=t3:50033:3>`_,
+just because I had bought a bunch of them. It's a very small ESP32
+board, with 40 pins broken out, and support for charging a lithium
+battery from USB.
+
+.. note::
+
+ The v1.3 is a ``d1_mini32`` board as far as the `esp32-arduino
+ <https://github.com/espressif/arduino-esp32>`_ libraries are
+ concerned, v1.4 is a ``esp32wrover``. There are probably other
+ variants, so you may need to adjust something if you want to
+ replicate my build
+
+
+First step was figuring out how to connect the sensors to the
+controller. The LED in the QRD1114 can easily work with about 20mA,
+so I can drive one directly from the ESP32, whose GPIO pins can drive
+up to about 30mA. 25 sensors fit nicely in a 5×5 matrix, so something
+like this should work:
+
+.. image:: lego-piano-scanner-schematic.svg
+ :width: 100%
+ :alt: schematic drawing of an electronic circuit; four QRD1114 are
+ arranged in a 2×2 grid; a pin labelled "row1" is connected to
+ 2 LED anodes via a 330Ω resistor and to 2 phototransistor
+ collectors via a 10kΩ resistor; a pin labelled "row2" is
+ similarly connected to the other pair; a pin labelled "col1"
+ is connected to the LED cathode and phototransistor emitter
+ of two sensors, one per "row"; a pin labelled "col2" is
+ similarly connected to the other pair; between each 10kΩ
+ resistor and the phototransistor collectors are pins labelled
+ "sense1" and "sense2"
+
+Pulling a "row" pin high (with the others low) and a "col" pin low
+(with the others in high-impedance / tristate), we can turn on one
+sensor at a time, so we can scan the matrix. When the phototransistor
+sense light reflecting from an object in front of it, it will pull
+down the "sense" pin, which we can read via the ADC in the controller.
+
+Testing that design on a breadboard proved that it can work!
+
+.. image:: sensor-matrix-test.jpg
+ :width: 100%
+ :alt: breadboard with a ESP32 dev board, 4 QRD1114 sensors, four
+ resistors, and a bunch of wires. One QRD1114's LED is shining
+ a weak pink, the others are off
+
+How are we going to keep the sensors in the right place inside the
+piano, though? We're going to print a support that's compatible with
+Lego pieces!
+
+.. image:: sensor-mount-test.jpg
+ :width: 100%
+ :alt: white plastic 3D-printed rectangle, 6×2 Lego studs in size,
+ with standard-sized studs only along one long side; 5 QRD1114
+ sensors occupy one long side, and 2 Lego 1×1 plates with C
+ clip are on the other side
+
+My printer (a `Prusa i3 MK3S
+<https://shop.prusa3d.com/en/3d-printers/180-original-prusa-i3-mk3s-kit.html>`_)
+can print holes of the right size for standard vias, and can print the
+whole sensor support in one go (29 studs long). The sensors need to be
+aligned with the hammers (there's no space behind the stems of the
+keys, also the hammers are white so more visible to the
+phototransistor, and move more, so it's less probable we'll have false
+positives). I used `OpenSCAD <http://www.openscad.org/>`_ to `model
+the support
+<https://www.thenautilus.net/cgit/lego-piano/tree/3d-print/qrd-holder.scad>`_.
+
+.. image:: sensor-mount-size-check.jpg
+ :width: 100%
+ :alt: white plastic 3D-printer rectangle, 29×2 Lego studs in size,
+ with 25 groups of 4 small holes to hold the sensors, placed
+ on top of the "strings" on the piano, showing how the holes
+ for the sensors align with the hammers
+
+We also must check positioning on the other two axes, of course.
+
+.. image:: sensor-position-test-1.jpg
+ :width: 100%
+ :alt: one QRD1114 mounted on the 3D-printer plastic holder,
+ suspended below one of the "strings" with a Lego clip piece;
+ the sensor sits just above and behind the rightmost hammer on
+ the keyboard; the hammer is in its resting position
+
+.. image:: sensor-position-test-2.jpg
+ :width: 100%
+ :alt: same pieces as before; the hammer is in its raised position,
+ precisely in front of the sensor
+
+At this point someone must be asking: how are we going to solder those
+sensors on a *plastic* board? And the answer is, we're not going to! I
+decided to go with `wire-wrapping
+<https://en.wikipedia.org/wiki/Wire_wrap>`_!
+
+.. image:: sensors-mount-wiring.jpg
+ :width: 100%
+ :alt: reverse side of the 29×2 rectangle, with 10 sensors inserted
+ into their holes from the front, and many thin electrical
+ wires, insulated in yellow plastic, connecting some of their
+ pins
+
+The sensors have 4 pins, numbered counter-clockwise looking at the LED
+/ phototransistor faces:
+
+1. phototransistor collector, to connect to pull-up resistor and to
+ "row" pin
+2. phototransistor emitter, to connect to "column" pin
+3. LED anode, to connect to current-limiter resistor and to "row" pin
+4. LED cathode, to connect to "column" pin
+
+so the whole wiring looks like this::
+
+ aM aN aO aP aQ cM cN cO cP cQ
+ 14 14 14 14 14 14 14 14 14 14 …
+ 23 23 23 23 23 23 23 23 23 23 …
+ Mb Nb Ob Pb Qb Md Nd Od Pd Qd
+
+where ``a``, ``c`` go to the pull-up resistors; ``b``, ``d`` go to the
+limiter resistors, and ``M``, ``N``, ``O``, ``P``, ``Q`` go to the
+column pins.
+
+The controller board is built in a similar way: 3D-printed and
+wire-wrapped (see `the model
+<https://www.thenautilus.net/cgit/lego-piano/tree/3d-print/controller.scad>`_).
+
+.. image:: board.jpg
+ :width: 100%
+ :alt: white plastic 3D-printed rectangle, with two groups of 10x2
+ holes around a label "esp", two groups of 5×2 holes around
+ labels "220Ω" and "10kΩ", three groups of 5 holes beside
+ labels "led", "gnd", "pht", and one group of 9 holes beside a
+ label "amp"; there is a notch in the rectangle near the "esp"
+ label, and a small raised block near the "amp" label
+
+The notch in the board corresponds to the battery connector, and the
+raised block is to hold up the small `AdaFruit audio amplifier
+<https://learn.adafruit.com/stereo-3-7w-class-d-audio-amplifier/inputs-and-outputs>`_
+
+.. image:: board-populated.jpg
+ :width: 100%
+ :alt: same board, with all the components placed on it. The "led",
+ "gnd" and "pht" holes hold connectors
+
+.. image:: board-wired.jpg
+ :width: 100%
+ :alt: reverse of the populated board, many electrical wires
+ insulated in yellow plastic connect various pins
+
+.. image:: board-full.jpg
+ :width: 100%
+ :alt: same populated board, with a small naked speaker connected to
+ the amplifier
+
+I had some problems with the row / column connections, because not all the
+GPIO pins can actually be used. After some trial and error, I settled
+on:
+
+- row pins: 05 23 19 18 26
+- colums pins: 17 33 16 21 22
+- ADC pins: 02 04 12 27 14
+- DAC pin: 25
+- amp enable: 32
+
+The program was a bit fiddly to get right, but not particularly
+complicated, `you can see it in my Git repository
+<https://www.thenautilus.net/cgit/lego-piano/tree/esp32/lego-piano.ino>`_.
+
+.. note::
+
+ The TTGO board gave me some problems with uploading the compiled
+ image, with errors like ``A fatal error occurred: Timed out waiting
+ for packet content`` or ``Invalid head of packet (0xE0)``. To fix
+ those, I had to `set the upload speed by hand
+ <https://www.thenautilus.net/cgit/lego-piano/tree/esp32/Makefile>`_
+ in the ``Makefile``.
+
+First test with a few sensors on a breadboard:
+
+.. video:: sound-test-1.mp4
+ :width: 100%
+ :type: video/mp4
+
+and with all the sensors, mounted inside the piano:
+
+.. video:: sound-test-2.mp4
+ :width: 100%
+ :type: video/mp4
+
+I removed the Lego electronics to make space for the wires and the
+controller board.
+
+And, finally, the whole assembled set:
+
+.. video:: sound-test-3.mp4
+ :width: 100%
+ :type: video/mp4
+
+At the moment the program can't really deal with more than one key
+pressed at a time, as you may have noticed at the end of that last
+video. The next step is to use a soundfont library, probably
+`TinySoundFount <https://github.com/schellingb/TinySoundFont>`_ or `the
+ESP-optimised version
+<https://github.com/earlephilhower/ESP8266Audio/tree/master/src/libtinysoundfont>`_,
+which should have no problems mixing multiple notes.
diff --git a/src/modelli/lego-piano/document.it.rest.txt b/src/modelli/lego-piano/document.it.rest.txt
new file mode 100644
index 0000000..a6c0ea9
--- /dev/null
+++ b/src/modelli/lego-piano/document.it.rest.txt
@@ -0,0 +1,257 @@
+===================================
+Come far suonare in Pianoforte LEGO
+===================================
+:CreationDate: 2020-08-28 10:39:28
+:Id: modelli/lego-piano
+:tags: - 3dPrint
+ - hardware
+ - software
+ - models
+
+Il `Pianoforte a coda LEGO 21323
+<https://www.lego.com/it-it/product/grand-piano-21323>`_ è un gran bel
+modello, con un sacco di dettagli interessanti e un livello di
+ingegneria veramente notevole.
+
+Ci sono un po' di componenti elettronici, che parlano via Bluetooth
+con una app per telefono.
+
+.. image:: lego-electronics.jpg
+ :width: 100%
+ :alt: un sensore ottico Lego, motore, e scatola di controllo,
+ su una larga base di pezzi montati di lato; il motore fa girare
+ un lungo asse con camme ortogonali
+
+La app può suonare alcuni motivi predefiniti mentre fa girare il
+motore dentro al pianoforte, cosa che fa abbassare un po' dei tasti in
+una sequenza costante, per far finta che il pianoforte stia suonando
+la musica. Oppure, la app può aspettare che premiate un tasto
+qualsiasi (tutti i tasti muovono un pezzo di fronte al sensore ottico)
+per suonare ciascuna nota, per far finta che stiate suonando voi.
+
+Carino, ma un po' tanto limitato: abbiamo 25 tasti indipendenti, ci
+deve essere un modo di fargli suonare la nota giusta!
+
+.. image:: keys.jpg
+ :width: 100%
+ :alt: la tastiera del pianoforte Lego piano, 2 ottave + 1, con
+ tasti bianchi e neri in sequenza corretta; ciascun tasto ha
+ il suo martelletto
+
+.. video:: keys-hammers.mp4
+ :width: 100%
+ :type: video/mp4
+
+Dopo un po' di ricerca, ho deciso di usare sensori ottici e un
+microcontroller potente. Non può essere tanto difficile, vero? ☺
+
+Ho usato come sensore il `QRD1114
+<https://learn.sparkfun.com/tutorials/qrd1114-optical-detector-hookup-guide/all>`_,
+che è molto semplice da usare: LED infrarosso più fototransistor, con
+campo visivo molto stretto e corto, per cui è difficile che tasti
+adiacenti producano falsi positivi.
+
+Come controller ho usato un `Lilygo TTGO-T7 v1.3
+<http://www.lilygo.cn/prod_view.aspx?TypeId=50033&Id=1258&FId=t3:50033:3>`_,
+principalmente perché ne avevo una manciata. È un ESP32, espone 40
+piedini, e può caricare una batteria al litio da USB.
+
+.. note::
+
+ La versione 1.3 è un ``d1_mini32`` per quanto riguarda le librerie `esp32-arduino
+ <https://github.com/espressif/arduino-esp32>`_, la 1.4 è un
+ ``esp32wrover``. Son sicuro che ci siano altre versioni in giro,
+ potreste dover fare qualche aggiustamento su volete riprodurre quel
+ che ho fatto
+
+Per cominciare ho dovuto trovare il sistema di collegare i sensori al
+controller. Il LED del QRD1114 si accontenta di 20mA, per cui posso
+alimentarlo direttamente dai GPIO del ESP32, che reggono fino a circa
+30mA. 25 sensori fanno una matrice 5×5, per cui qualcosa di simile a
+questo circuito dovrebbe funzionare:
+
+.. image:: lego-piano-scanner-schematic.svg
+ :width: 100%
+ :alt: schema elettrico; quattro QRD1114 sono disposti a quadrato;
+ un filo etichettato "row1" è collegato agli anodi di due LED
+ tramite una resistenza di 330Ω e ai collettori di due
+ fototransistor tramite una resistenza di 10kΩ; un filo "row2"
+ è collegato in maniera simile agli altri due sensori; un filo
+ "col1" è collegato al catodo del LED e all'emettitore del
+ fototransistor di un sensore per riga; un filo "col2" è
+ collegato in maniera simile agli altri due; tra ciascuna
+ resistenza da 10kΩ e i fototransistor ci sono fili
+ etichettati "sense1" e "sense2"
+
+Mettendo "alto" un piedino di riga (con gli altri "bassi") e "basso"
+un piedino di colonna (con gli altri ad alta impedenza / tristate),
+possiamo accendere un sensore alla volta. Quando un fototransistor
+vede la luce riflessa da un oggetto che gli sta davanti, abbassa il
+piedino "sense", che possiamo leggere con l'ADC del controller.
+
+Testiamo il circuito su una breadboard per dimostrare che funziona!
+
+.. image:: sensor-matrix-test.jpg
+ :width: 100%
+ :alt: breadboard con un ESP32, 4 sensori QRD1114, quattro
+ resistenze, e un po' di fili. Un LED fa una debole luce rosa,
+ gli altri sono spenti
+
+Come facciamo a tenere i sensori al loro posto dentro il pianoforte?
+Stampiamo un supporto compatibile coi pezzi Lego!
+
+.. image:: sensor-mount-test.jpg
+ :width: 100%
+ :alt: rettangolo di plastica bianca prodotto da una stampante 3D,
+ di dimensioni pari a 6×2 pezzi Lego; ci sono pispoli di
+ dimensione standard su uno dei lati lunghi, con 2 manine 1×1
+ attaccate; cinque sensori QRD1114 sull'altro lato
+
+La mia stampante (una `Prusa i3 MK3S
+<https://shop.prusa3d.com/en/3d-printers/180-original-prusa-i3-mk3s-kit.html>`_)
+riesce a stampare fori della dimensione giusta per i componenti
+elettronici, e l'intero supporto (lungo 29 unità Lego) in una passata
+sola. I sensori devono essere allineati ai martelletti (non c'è posto
+dietro le stecche dei tasti, inoltre i martelletti sono bianchi per
+cui più visibili ai sensori, e si muovono di più per cui riducono la
+probabilità di letture sbagliate). Ho usato `OpenSCAD
+<http://www.openscad.org/>`_ per `costruire il modello del supporto
+<https://www.thenautilus.net/cgit/lego-piano/tree/3d-print/qrd-holder.scad>`_.
+
+.. image:: sensor-mount-size-check.jpg
+ :width: 100%
+ :alt: rettangolo di plastica bianca prodotto da una stampante 3D,
+ di dimensioni pari a 29×2 pezzi Lego, con 25 gruppi di 4 fori
+ ciascuno per reggere i sensori, appoggiato sopra le "corde"
+ del pianoforte; i fori sono allineati coi martelletti
+
+Dobbiamo anche controllare l'allineamento sugli altri due assi,
+ovviamente.
+
+.. image:: sensor-position-test-1.jpg
+ :width: 100%
+ :alt: un sensore QRD1114 inserito nel supporto stampato, appeso a
+ una delle "corde" con una manina Lego; il sensore è appena
+ sopra e dietro l'ultimo martelletto a destra; il martelletto
+ è a riposo
+
+.. image:: sensor-position-test-2.jpg
+ :width: 100%
+ :alt: stessi pezzi di prima; il martelletto è sollevato, proprio
+ davanti al sensore
+
+A questo punto qualcuno si starà domandando: come si fa a saldare
+componenti elettronici su una basetta di *plastica*? E la risposta è
+che non saldiamo nulla! Ho usato la tecnica del `wire-wrapping
+<https://en.wikipedia.org/wiki/Wire_wrap>`_!
+
+.. image:: sensors-mount-wiring.jpg
+ :width: 100%
+ :alt: retro del rettangolo 29×2, con 10 sensori montati dal davanti
+ nei loro fori, e tanti fili elettrici isolati in giallo che
+ collegano alcuni dei piedini
+
+Ciascun sensore ha 4 piedini, numerati in senso antiorario guardando
+dalla faccia del LED / fototransistor:
+
+1. collettore del fototransistor, da collegare a una resistenza di
+ pull-up e a un piedino "riga"
+2. emettitore del fototransistor, da collegare a un piedino "colonna"
+3. anodo del LED, da collegare a una resistenza di limitazione e a un
+ piedino "riga"
+4. catodo del LED, da collegare a un piedino "colonna"
+
+per cui i collegamenti vengono più o meno così::
+
+ aM aN aO aP aQ cM cN cO cP cQ
+ 14 14 14 14 14 14 14 14 14 14 …
+ 23 23 23 23 23 23 23 23 23 23 …
+ Mb Nb Ob Pb Qb Md Nd Od Pd Qd
+
+dove ``a``, ``c`` vanno alle resistenze pull-up; ``b``, ``d`` vanno
+alle resistenze di limitazione, e ``M``, ``N``, ``O``, ``P``, ``Q``
+vanno ai piedini colonna.
+
+Ho costruito la basetta del controller nella stessa maniera: stampa e
+wire-wrap (`modello
+<https://www.thenautilus.net/cgit/lego-piano/tree/3d-print/controller.scad>`_).
+
+.. image:: board.jpg
+ :width: 100%
+ :alt: rettangolo di plastica bianca prodotto da una stampante 3D,
+ con due gruppi di fori 10×2 intorno a una scritta "esp", due
+ gruppi di fori 5×2 intorno a scritte "220Ω" e "10kΩ", tre
+ gruppi di 5 fori ciascuno accanto a scritte "led", "gnd",
+ "pht", e un gruppo di 9 fori accanto a una scritta "amp"; c'è
+ uno svaso vicino alla scritta "esp" e un rettangolo rialzato
+ vicino alla scritta "amp"
+
+La svasatura serve a far spazio per il connettore della batteria, e il
+blocchetto rialzato regge l'`amplificatore AdaFruit
+<https://learn.adafruit.com/stereo-3-7w-class-d-audio-amplifier/inputs-and-outputs>`_
+
+.. image:: board-populated.jpg
+ :width: 100%
+ :alt: stessa basetta, con tutti i componenti al loro posto; i fori
+ "led", "gnd" e "pht" hanno dei connettori
+
+.. image:: board-wired.jpg
+ :width: 100%
+ :alt: retro della basetta, un sacco di fili elettrici isolati in
+ giallo collegano i piedini
+
+.. image:: board-full.jpg
+ :width: 100%
+ :alt: stessa basetta, con un piccolo altoparlante collegato
+ all'amplificatore
+
+Ho avuto un po' di problemi con i piedini per righe e colonne, perché
+non tutti i GPIO sono davvero usabili. Alla fine ho usato questi:
+
+- righe: 05 23 19 18 26
+- colonne: 17 33 16 21 22
+- ADC: 02 04 12 27 14
+- DAC: 25
+- abilitazione dell'amplificatore: 32
+
+Il programma ha richiesto un po' di attenzione, ma non è
+particolarmente complicato, `potete leggerlo nel mio repository Git
+<https://www.thenautilus.net/cgit/lego-piano/tree/esp32/lego-piano.ino>`_.
+
+.. note::
+
+ La circuiteria del TTGO mi ha dato un po' di problemi nel caricare
+ il programma compilato, producendo errori tipo ``A fatal error
+ occurred: Timed out waiting for packet content`` o ``Invalid head
+ of packet (0xE0)``. Per evitarli, ho dovuto `impostare
+ esplicitamente la velocità di trasferimento
+ <https://www.thenautilus.net/cgit/lego-piano/tree/esp32/Makefile>`_
+ nel ``Makefile``.
+
+Primo test con pochi sensori su una breadboard:
+
+.. video:: sound-test-1.mp4
+ :width: 100%
+ :type: video/mp4
+
+e con tutti i sensori, montati al loro posto nel pianoforte:
+
+.. video:: sound-test-2.mp4
+ :width: 100%
+ :type: video/mp4
+
+Ho rimosso i componenti elettronici Lego per far posto ai fili e al controller.
+
+E, finalmente, il risultato assemblato:
+
+.. video:: sound-test-3.mp4
+ :width: 100%
+ :type: video/mp4
+
+Al momento il programma non sa gestire più di un tasto premuto alla
+volta, come probabilmente avete notato alla fine dell'ultimo filmato.
+Il passo successivo è di usare una libreria soundfont, probabilmente
+`TinySoundFount <https://github.com/schellingb/TinySoundFont>`_ o `la
+versione ottimizzata per ESP
+<https://github.com/earlephilhower/ESP8266Audio/tree/master/src/libtinysoundfont>`_,
+che dovrebbe riuscire a mixare note multiple.
diff --git a/src/modelli/lego-piano/du2html.xsl b/src/modelli/lego-piano/du2html.xsl
new file mode 120000
index 0000000..e2487e0
--- /dev/null
+++ b/src/modelli/lego-piano/du2html.xsl
@@ -0,0 +1 @@
+../../../templates/du2html.xsl \ No newline at end of file