Share
Embed
Hamlet The Pig
The story of the twice resurrected hog
who reignited my love for coding.
Diego Quiroga
November 12, 2019
Transcript
31 slides
-
Working as a manager you can spend a long time
without touching code and it is not unusual to start questioning yourself about your technical fit. Sometimes, it takes someone or something else to remind you why you got into this in the first place. -
Hamlet The story of the twice resurrected hog who reignited
my love for coding. -
Hamlet was a life-sized robotic piggy bank for donation campaigns.
It was created by the Vancouver Community College Electronics Department back in 1991. -
After many years of dedicated service to the Mount Pleasant
Neighbourhood House, Hamlet was abandoned in a basement, in very bad shape and ready for its retirement. -
My artist-wife Mariana volunteered there and as the passionate animal
lover that she is, decided to bring the pig home for a facelift. -
Once we plugged him in. a weird barely understandable robotic
voice thanked us when we put coins in the slot. Thirty years later and still working like the first day. -
As Mariana started to do her magic on him I
began to think: ” I could have some fun with this.” -
I opened the base box and found an electronics board
hanging from the bottom. -
It had a microcontroller, an I/O interface, a Texas Instruments
speech synthesizer and an amplifier. -
My idea was to modernize it with an Arduino board,
an MP3 shield , an SD card reader, a couple of speakers, a servo, perhaps a new IR sensor for coins. -
Putting together a prototype as a quick proof of concept
was not so hard.. -
But the more code I added, the more I hated
the Arduino IDE. It was too basic for any semi-serious project. I had no way to pinpoint problems in my tests. It really sucked. -
Luckily, I found PlatformIO, an ecosystem for embedded development. It
nicely integrated with VS Code and made my life much easier. So I had no more excuses to deliver. Except I had not really considered memory usage. -
The Arduino Uno is very limited. You have 32K of
ROM to flash your code into. And the incredible amount of 2,048 bytes of RAM split in two chunks. The first, for static and global variables. The second, for the heap and the stack to fight for space. Running out of memory during execution is very easy. The Arduino goes coo coo and starts doing all kinds of crazy s.. and resetting itself. -
You can, for example, move some constant data to the
flash ROM, freeing up some in the first chunk of RAM dedicated to static and global variables. -
Here, I’m saving one byte for each one of these
constants by using ‘byte’ instead of the default int you may have seen in all Arduino examples. -
This was the most challenging one. I had to resort
to the C strings library and using pointers to avoid the heap fragmentation caused by the String object. The last time I touched C was in college twenty years ago. -
Later, with my product hat on, I had to consider
my users. It should be easy to manage for volunteers and others working for the neighbourhood house. -
The pig had to be plug-and-play and survive without software
updates for a long time. -
I created an additional board to power the Arduino, connect
sensors and the other external devices. -
Color coded connectors, labels everywhere, everything had to be easy
to plug in and plug out. -
It was dark inside the pig, so I used a
white LED as the power indicator. I added a red one to blink different patterns whenever a problem occurred (like missing SD card, wrong files, etc..) -
That was.. NOT easy! Also had to reverse-engineer the IR
sensor to reuse the coin slot, find out how to it blinked the eyes in two colors with just two wires, introduce random variations to make tail wagging more 'natural', implement non-blocking interrupts to smooth audio playing, handle abnormal states and degraded performance gracefully, use proto-threading to simulate concurrent events.. In the end, it took longer than I first estimated. But at the same time, learning how to overcome the all the roadblocks that presented themselves made the process fulfilling beyond my expectations And a bunch of other stuff that I have no clue before. -
The End..? And this could have been the happy ending
of our story. Unfortunately, a month after Hamlet’s triumphant return we received bad news. -
Burglars had broken in and hurt Hamlet badly to steal
the money. The police dusted it looking for fingerprints and attempting to solve the mystery. For us, well, it was just another trip home. -
Mariana patched the little guy again. I took the opportunity
to add a few new features. -
Hamlet 2019 Hardened power supply Kidproof magnetic tail New blinking
error codes Volume control Reinforced structure/door I’m particularly proud of the magnetic attachment for the tail, kids kept breaking the initial version. Hamlet has been collecting money for Mount Pleasant for a year. They are very happy with him. -
What I found out Some pigs can live forever Using
pointers is just like riding a bike Keep looking until you find the right tool Save the RAM Constraints make us incredibly creative Don’t stop learnin’ ♫ ♬ ♪ This is still my thing and I love it. There is joy to be found in limitations. Nothing like extracting all the juice from a small piece of hardware, working very close to the machine without a ton of abstraction layers in between. -
Learning new stuff is always fun, but in this particular
case the most important outcome for me was to knock out my imposter syndrome and find out that I still love what I do. Thank you.