The scariest part of Halloween is failing...
Last week I gave myself one day to build a motor driven-device that would drop a spider on unsuspecting trick-or-treaters whenever the sensor detected them. What I learned informs future projects that need high-speed timing..
Last week I gave myself one day to build a motor driven-device that would drop a spider on unsuspecting trick-or-treaters whenever the sensor detected them. What I learned informs future projects that need high-speed timing.
I gave myself only the day of Halloween itself Saturday, the 31st. Knowing that if I started the project on Wednesday, it would take four days to finish.
The project consisted of
- Two 3D printed parts, a pulley and a bracket to hold the motor.
- A 12V, 78:1 gearmotor with encoder, scavenged from an old school project.
- A Pololu 8384 motor driver board
- An ESP32
- A VL530LX Range Finder
I started the day by assembling the motor driver. That went quickly because I had already soldered theheader pins to the daughterboard.
The basic concept was simple: two pins control whether the motor driver moves the motor forward or backward.
To operate one motor, the board needs seven connections:
- Logic power
- Motor power
- Ground
- Motor A forward (input)
- Motor A reverse (input)
- Motor A + (output)
- Motor A - (output)
The encoder required connections for power and ground, along with two signal wires to the microcontroller.
The range sensor worked pretty much out of the box with libraries that I found for MicroPython and ESP32. It required only four connections: power, ground, SCL and SDA.
Putting the code together was also quick. The script I wrote in Micropython did the following:
- Wait at the starting position until the sensor detects an object blocking the path.
- Rotate the motor 500 encoder counts in one direction.
- Pause for two seconds.
- Rotate the motor 500 counts in the other direction.
- Start the loop again and check the distance sensor for another obstacle.
To make this work, I used MicroPython's pin interrupt functionality, which is not especially well-documented. `
Failure
The code was simple. The hardware was simple. The motor still would not rotate the same distance in both directions.
The problem was inaccurate encoder counting.
Apparently, Micropython's implementation of interrupts is heavily dependent on the hardware that it's connected to. The ESP32 port of micropython only supports "soft" interrupts. Micropython's overhead also makes it impossible for interrupt code to run quickly enough -- even following best practices -- such another encoder transition will come before the last function is finished executing.
The result was that the motor moved a different number of counts in one direction than it did in the other. I suspect that other hardware operations running in MicroPython were also delaying the interrupt handler.
The fix
I didn't get my project finished before Halloween.
Next year, I will be working in either Rust or Arduinio (C), to ensure that I have low-level access to device I/O that is fast enough to keep up with the devices that I'm using.
That change will come with a cost, which was why I started with MicroPython. MicroPython is super convenient for prototyping because of its vast trove of libraries that work out of the box and with little extra programming effort. migrating to C or Rust will require a lot more integration between libraries, and extra functionality that I want to tack on will come at a much higher cost
The lesson is useful, even if the spider never dropped: language and runtime matter when the hardware depends on timing. MicroPython made it easy to get most of the project working, but it could not reliably keep up with the encoder.
Lesson Learned!
