Counting spins on an ESP32
I’ve had some time during my no-compete period between jobs; one distraction has been this little project, which aims to count how many times I spin around. The inspiration for this is twofold:
- Over Christmas my family like to play a game where we make a bet on the behaviour of one of the dogs (how long will they sleep?, how long can we get them to stay?, etc). One that was floated was how many times will Bella spin around?. Bella you see, is a very excitable dog, something she communicates with pacing, barking, and spinning. This idea was discarded because keeping track would be onerous, but for next year it struck me that a technological solution was available.
I bought an M5StickC Plus2 (photo below), which is a small self-contained device containing an ESP32, 6-axis IMU, LiPo, and screen. It’s small and light enough to attach to a dog collar without bothering the dog. Because of the constraints of dog-mounted devices, it was important that the device work regardless of its mounting orientation. My rough implementation plan for this was:
- Use the gyroscope to keep track of the device’s orientation
. - The accelerometer measures the “up” direction and uses this as an anchor to correct drift in orientation.
- The rotation rate measured by the gyroscope is transformed into the lab frame, and if the rotation is in the
plane, added to an accumulated rotation angle. - If the accumulated angle goes past a full turn in either direction, add a turn to the counter.
For numerical convenience, I tracked orientation as a Rotor, which actually lives in
An aside on topology: point 3 is somewhat more complex than I’ve stated. Imagine the device on a turntable; if the turntable is flat, rotating in the
In 2D, the orientation of an object is a point in
In my own code, I just counted the rotation if it was within 45 deg of the
To test this, I took it with me dancing. During the class and freestyle, I apparently did 352 clockwise and 138 anticlockwise turns, totalling a spin every ~15s. This feels high, but the (short) routine we were practicing did actually have quite a few spins and the ratio is right, so I think it’s probably close to accurate.
I’m not going to get a chance to visit my parents and their dog for quite a while, so dog-based experiments will have to wait.