This was undertaken as a solo challenge for my second electronics module. We were given the parts to assemble the segway, and then set the challenge to write the code to control it, such that it self-balanced and danced in time to music.
The videos below show my finished attempts at the challenges. The self-balancing highlights my understanding of proportional-integral-derivative (PID) control - using the pitch angle as the set-point variable to control the motors such that the pitch angle is maintained at zero (segway is upright).
The dancing then adds another challenge in the form of beat detection. This algorithm is implemented using a sample buffer that relies on a timer interrupt to periodically sample the music. Another buffer then stores the instantaneous energies of the past 50 epochs (20 ms time periods), and if the ratio of the current instantaneous energy to the sum of the past 50 epochs is above a certain threshold, this counts as a beat detected and the next dance move is queued.