The goal for this project was to get the SegBot to “dance” back and forth to the beat of a song. I thought this would be a cool project because sensing and responding to audio is a key part of making robots feel more alive. This is especially useful in the focus of improving human-robot interactions.
The SegBot consists of a custom PCB board (designed by Dr. Dan Block) and a TMS320F28379D Launchpad from Texas Instruments. Two motors are attached to the board, and ePWM6 (Enhanced Pulse Width Modulator peripheral #6) on the Launchpad is linked to the motors to control the rate at which they spin. Wheels are attached to the motors so the SegBot can move around, and the SWI_ISR function in the code programs the SegBot so it can balance on two wheels.
To make the SegBot dance, the microphone on the board was connected with ePWM4 and ADC-B (Analog-to-Digital Converter B) on the Launchpad to receive sound. The microphone, Launchpad, and wiring to connect the two can be seen in the video below:
Getting the SegBot to balance on its wheels was done prior to the rest of the work on this project. For the SegBot to balance, the ADC-A interrupt function and SPI are used to transmit and receive three accelerometer readings and three gyrometer readings. The SPIB interrupt function reads these values and controls the motor angles. Additionally, a Kalman filter was applied to increase accelerometer accuracy and create a responsive tilt measurement that does not drift.
As mentioned before, the microphone was set up using ePWM4 and ADC-B of the Launchpad. The ePWM4 peripheral runs simultaneously with the peripherals used to balance the SegBot and drive the motors. In volts, the microphone outputs values in a range of 0.5 to 2.5 V.
Next, a beat detection algorithm needed to be implemented into the code. The algorithm used is based on the first algorithm mentioned in the link below (designed by Marco Ziccardi):
https://mziccard.me/2015/05/28/beats-detection-algorithms-1/
First, ePWM4 was set up with a TBPRD value of 5000, meaning sampling would occur every 0.1 milliseconds (10 kHz). Each time sampling takes place, the instant energy that the microphone detects is calculated. This value is the ADC audio detection reading (in volts) squared, based on the formula below. It should be noted that this formula is intended for stereo songs, but there is only one reading squared for this project since the microphone has one speaker.
Instant energy was then summed up for 232 samples in a row. This total sum after 232 samples is inserted into a 43-element array (as 232*43 is approximately equal to the sampling frequency of 10 kHz). Every 232 readings, the elements in the array are added together and divided by 43 (the size of the array). This yields a value for the average energy reading every 23.2 milliseconds.
To detect a beat, the microphone needs to detect average energy values that are above a certain threshold. This threshold was determined by plugging the average energy values into an array and plotting them in Code Composer Studio. Based on these graphs, it was decided that a beat would be found if the average energy value were greater than 10.0. Amplitudes for average energy can be seen in the following plots:
Finally, the SegBot was commanded to move every time a beat is detected. This was done using the "turnref" variable that indicates the angle the SegBot is at from its initial position. This variable was incremented positively for an even-numbered beat and negatively for an odd-numbered beat. Additionally, a "beat delay" of 500 milliseconds was implemented using the Launchpad's CPU Timer 0. This was done to prevent the SegBot from dancing back and forth too quickly if extra noise is picked up by the microphone.
Demo videos of the SegBot dancing to a beat can be seen below. The SegBot rotates more each dance move in the first video since turnref was incremented by 10, while it was incremented by 5 in the second video.
The time for the "beat delay" may need to be adjusted for different songs. Although 500 milliseconds seemed to work for a variety of songs tested, this value may need to be increased for slower songs and decreased for faster songs to avoid beat skipping. Similarly, the average energy threshold can be adjusted based on how heavy the beat of a given song is. Further testing can be performed with the code to yield results with even higher accuracy.













Comments