Anda di halaman 1dari 15

Clap Tracking for a Robot Drummer Neil MacMillan For G. Tzanetakis, CSC 475University of Victorianrqm@uvic.

ca Abstract This document describes a project involving amodification to the existing robot drummer, allowing it torespond in real time to audio stimulus (hand clapping)without needing external equipment. The stimulusdetection, timing considerations, clap tracking andprediction, and results are all discussed. 1. Introduction To satisfy the requirements for a past course at UVic, I andtwo other engineering students, Matthew Loisel and DanielPartridge, designed and built a robot drummer that couldstrike a drum in response to Musical Instrument DigitalInterface (MIDI) input. [1] That project was mostlysuccessful, but once it was completed there were still manyways that the robot could be made more useful orinteresting. One problem with the robot is that it is limitedto MIDI input. It can accept commands from any MIDIsource, but it requires such a source for example asynthesizer or computer to be present. In other words,the robot is not a standalone machine.The robot can be improved by adding capability todrum in direct response to live audio input such as handclapping, instead of requiring some complex external pieceof equipment to translate analog signals to MIDIcommands. It would be even more interesting if the robotcould, when a regular rhythm is being clapped, drum alongwith the rhythm not in response to the claps, but byanticipating each clap and striking the drum at the precisemoment of the clap instead of shortly after it.This document will describe the three core tasks that Iundertook for this project:Designed and built a sensor for detecting a handclap and outputting a digital signal. This turnedout to be the main task.Measured the delays between a stimulus to therobot drummer and the drum strike.Modified the robots firmware to accept the clapsensors digital signal, and to predict when the

next clap in a simple beat will occur. 2. Clap Detection The robot must be able to gather input before it canrespond to claps. I looked for an off-the-shelf clap sensor,but could not find one that was cheap and producedappropriate output. I decided to build a clap sensor fromscratch. This involved designing a circuit for the sensorand laying it out as a circuit board, purchasing parts,etching and assembling the circuit board, installing thecomponents, and writing the firmware.Without the clap sensor, there would be two choices forclap detection. If the robot drummer did clap detection,then the signal processing component would add asignificant processing a nd memory load to the drummers firmware. The sensor offloads this work onto acoprocessor that transforms the analog input into a digitaloutput that is very easy for the drummer to read. Anotherway to do clap detection would be with a computer.Clearly this would undermine one of the points of thisproject, which is to make the robot drummer moreindependent. In addition to that, my experiments showthat sending MIDI commands over USB incurs atransmission delay between 10 and 30 ms. The mainproblem is the delay variance, which is impossible topredict or to correct. 2.1 Clap Sensor Circuit Board 10 FATtiny84MicTRSJack 2.2 k 1.5 k ISPLED 10 k 5 V100 Output + Figure 1. Clap sensor circuit diagram.

The clap sensor is a simple circuit to interface anelectret microphone with a microcontroller. [2]Figure 1shows the circuit I used. The symbol at the top representsa tip-ring-sleeve (TRS) jack, which is what the microphoneplugs into. The sleeve is ground, the ring is V+, and the tipis the signal line. The V+ line is connected to powerthrough a resistor, which is what causes the output voltageto vary when the electret is stimulated. In the middle of Figure 1,the six-pin connector is for the in-systemprogrammer (ISP) plug, which is how code is loaded ontothe microcontroller. The ISP plug includes a 10 k pull up resistor connected to the ISPs RESET pin. The block to the right represents the ATtiny84 microcontroller that Ichose to use (the logical connections to the ATtiny84 inthe diagram above to not match the physical layout of thechip). To the left is the DC power input, which can rangein voltage from 2.7 V to 5.5 V. The robot drummeroutputs 5 V. Beside the power source is a 10 F capacitor,which protects the circuitry from transients in the powersource. Th e LED is simply a visual output. The LEDs current-limiting resistor value depends on the LED; theLED that I used needed a 1.5 k r esistor to provide its rated current at 3.3 V. It should be 2.5 k for 5 V, but going above the rated current is safe as long as the LEDdoes not spend much time turned on. At the bottom of Figure 1is the digital output that the robot drummer reads,protected by a small current-limiting resistance. Figure 2. Clap sensor circuit board layout. In order to route traces properly, I had to put part of thecircuit on the bottom of the board.Figure 2shows the finalcircuit board layout for the top layer (left) and the bottomlayer (right).I used a toner transfer method to print the layout to acopperclad PCB, and etched it with sodium persulfate(one can find many printing and etching instructions on theInternet). The top layer inFigure 2is mirroredhorizontally from the actual circuit to compensate for thetoner transfer. The wide 1 mm traces are power lines (V+and ground), and the narrow 0.5 mm traces are signal lines. The circuit boards through

-holes were difficult tohandle. I drilled them out with two different drill bits, a#60 (1.016 mm) bit for the large holes and a #69 (0.742mm) bit for the small holes. I carved out the TRS jack pins which are very wide and thin with the small drillbit.It was impossible to solder the top-layer traces for holeslocated underneath the TRS jack and the ISP header. Isolved this problem by drilling extra via holes next to thethrough holes, connecting the top copper to metal posts inthe vias, and then connecting the component leads to theposts on the bottom of the board, after the componentswere installed. This problem could be solved bypurchasing the boards from a professional manufacturer.The manufacturer would be able to tin inside the throughholes, which would make it very easy to solder all of thecomponents from the bottom of the board.The small holes inFigure 2,on the right side of the toplayer, correspond from top to bottom to the capacitor, the LED, and the three resistors (1.5 k, 100 , and 2.2 k). The small holes on the microcontroller interface are vias toconnect the bottom layer traces to the top layer. The threelarge holes on the outer edge of the top layer correspondfrom top to bottom to V+, ground, and the output. Thelarge holes in the middle of the top and bottom layers arefor the six ISP header pins.Figure 3shows the finished clap sensor. As of thiswriting, there is still a bug in the microphone jack thatcauses the mic signal pin to stay at around 0.3 V.One problem with the circuit board layout is the pull-upresistor that drives the ISP RESET pin high. InFigure 3one can see that I added the resistor after creating thecircuit board. I have not found a way to integrate a full-sized resistor naturally into the board. A surface-mountresistor, which is much smaller than the Watt axialresistor that I used, would fit easily next to the ISP port butit would make the sensor more difficult to assemble. Figure 3. Finished clap sensor. 2.2 Board Cost If the board went into large-scale production, the followingcosts would be involved (prices are as of December 112009, per unit at the 1000-unit price break, fromhttp://www.digikey.ca):

Table 1. Sensor component prices. Component Part Number Price (CAD$) CPU ATTINY84-20SSU 2.03730TRS jack SJ1-3533N 0.34158 10k Res. CFR-50JB-10K 0.01532 2.2k Res. CFR-50JB-2K2 0.01532 1.5k Res. CFR-50JB-1K5 0.01532 100 Res. CFR-50JB-100R 0.01532Capacitor ECE-A1CKS100 0.03739LED* LTW420D7 0.237471-Row Hdr 4-102972-0 0.09853**2-Row Hdr 4-1037830 0.14689** * This LED is different from the one I used; it will use a 100

current-limiting resistor, and it will be cheaper than the low-current LED I used.** The headers are packaged in lengths of 40 rows. The price listed is theprice of the three positions used for the circuit board. I used a free software package from ExpressPCB to designthe circuit board. The company that makes ExpressPCBwill manufacture boards designed with their software. Thesoftwar e s price estimator gives an estimate of US$1638.39 to manufacture 1000 circuit boards, which asof this writing works out to CAD$1.74 per board. Thatbrings the total material cost to $4.70 for a single sensor ina batch of 1000 units, not including the cost of themicrophone. [3] 2.3 Clap Detection 2.3.1 Microphone Ouptut For my experiments I used a PC electret microphone.Such microphones have amplifiers built in, so that theyproduce a usable signal without having externalamplification. 1 [2] Small, unamplified microphones willnot work with the clap sensor. Microphones other than theone I used might amplify differently, which would changethe values that I give in this document. Figure 4. Low pitch clap waveform. 1 Mics with amplifiers typically have three conductors (power,ground, and signal). Mics without amplifiers have twoconductors (ground and signal) or four conductors whenintegrated with earphones (ground, signal, left earphone, rightearphone). Mics made for the iPod do not have amplifiers. The microphone output signal is idle at about 95% of the input voltage. If the microphone is being powered by 5V, the output will be idle at around 4.75 V. When themicrophone reads sound, the output will oscillate aroundthe idle level, peaking at the 5 V maximum and dipping to about 3.5 V. The sensors CPU can read this signal using its analog-digital converter (ADC) module.Figure 4shows the output of the microphone when itdetects a low pitch clap (e.g. a deep, palm-to-palm clap).The horizontal scale is time, and the vertical scale is signalvoltage. By visual inspection, one can see that the signalhas a period of a little under 2 ms, which corresponds to afrequency of a little more than 500 Hz. A spectrumanalysis

performed using Audacity confirms that most of the information in a low pitch clap is contained between500 and 600 Hz.The waveform for a high pitch clap (e.g. a sharp,finger-to-palm clap) is shown inFigure 5.It has a shorterspike in energy than the low pitch clap, and is at a higherfrequency, but otherwise it is similar to the low pitch clap. A spectrum analysis shows that most of the claps information is stored between 3 kHz and 6 kHz. Figure 5. High pitch clap waveform 2.3.2 Signal Analysis As shown by Repp [4], the frequency spectra of hand clapsare not very useful for clap detection; the spectrumfeatures are not distinct. Fortunately, asFigure 4andFigure 5show, the time-domain characteristics of clapsounds are quite distinct. Furthermore, it is much easierfor a general microcontroller like the ATtiny84 to analyzethe time domain characteristics than for it to analyze thefrequency domain characteristics. The frequency domainwould have to be calculated with a Fourier transform,which is impractical to do in firmware because it takes aconsiderable amount of calculation time and memory.The clap waveforms suggest a heuristic for detecting aclap: look for a strong enough spike in signal energy thatlasts for between 1 and 4 ms. If the signal is too weak,then it is not a clap. Likewise, if the spike in the signal takes too long to settle or settles too quickly, its not a clap.

The obvious flaw with this approach is that it is impossibleto distinguish between claps and sounds that have similartime-domain properties. I think its an acceptable risk though. The wave might also get subsumed by a stronger

signal such as a drumbeat, and thats a greater risk. Itdoesnt affect this project though, since there wont be a drum striking at the same time as a clap. The usual method for calculating a signals energy is toremove the signals direct current (DC) offset and takeroot-mean-square (RMS) values over sample windows, butthat technique is not feasible for this project becausedifferent approaches to the square root function take toomuch memory, or too much calculation time, or both. Thesquare root function is an interesting topic, but a discussionof it is beyond the scope of this report. 2 [5]The sensor takes the same approach to the energyproblem as the RMS method uses, but it takes the absoluteaverage of the sample windows instead of the RMS value.The absolute average runs quickly and does not need muchmemory. The firmware program removes the 4.7 V DCoffset so that the sample values are centred about 0, andaverages the absolute values of all the samples over 1 mswindows.The absolute averages of the windows are stored in aqueue, and when the sensor has enqueued 8 averages itscans through them to find one that is above a certainthreshold. If there is such a window, and if there are atleast four windows remaining in the queue, and if thewindow three samples later is below the threshold, thenthat is a clap. In other words, the signal must have a highaverage followed by anything, followed by a near-zeroaverage in order to decide that it has detected a clap. Thishas proven quite reliable, although very long and strongsignals (such as blowing into the microphone) will not bemasked properly. 2.3.3 Output When the firmware detects a clap, it outputs a 50 to 100 shigh pulse on its output pin. It also turns on the LED for100 ms to provide a visual output. The sensor does notprocess samples during the output phases of the controlloop, which means that if two claps are less than 100.1 msapart the second clap will be missed. This will bedesirable in most situations; the delay will mask extrastimuli (perhaps including, the drum strike). The fastestthat I can clap twice is slower than 100 ms, and the robotdrummer cannot strike twice with very much force thatquickly, so there is little risk.The output pulse has an arbitrary width that isguaranteed to be at least 50 s and at most 100 s. This isbecause the system clock that the sensor uses ticks at 50 sintervals. I chose this interval because sampling once per 2

I found that the fastest and most memory-efficient option forinteger square root is by far a nearest-neighbour binary searchon a constant lookup table stored in Flash ROM. tick produces a sampling rate of 20 kHz, which is fastenough to read any clap signal (recall that a high pitch claphas most of its information between 3 kHz and 6 kHz).The pulse should span at least one tick, to guarantee that itwill have a minimum width; otherwise there is a dangerthat the pulse will go high immediately before a tick, andwill be brought low a few cycles later when the systemclock ticks.In the future, it might be useful to add some moresophisticated analysis to the sensor, to allow it to detect thestrength of claps. If that is desired, I propose an output inwhich the pulse width is proportional to the clap strength.For example, a weak clap might output a 5 s pulse, amedium clap might correspond to 50 s, and a strong clapmight correspond to 100 s. In that case, it would bepreferable to use a busy wait delay instead of the systemtimer, to avoid timer overhead (a 5 s tick requires a timerinterrupt every 40 CPU cycles on an 8 MHz processor). 3. Latency Compensation One part of this project was to measure the robot drummers mechanical delays; that is, the time it takes for the drummer to move the drum stick from its rest positionto the drum head. This is useful because in order to predicta clap, the drummer needs to start its motion before theclap occurs. With information about how long it takes forthe drum stick to move, the drummer can know when tostart its strikes.I measured the timing using a drum pad that actuates apush-to-make (PTM) switch when it is struck. When thetest program on the robot drummer starts, it records atimestamp and begins a drum strike. When the drum stick closes the PTM switch, the drummer takes anothertimestamp. It calculates the difference between the twotimestamps (in milliseconds) and prints it out to the serialport. Figure 6. Drum strike times at different velocities. The drummers mechanical delay depends on two factors: the strike velocity and the strike displacement. Formy purposes, the strike velocity is always set to the

maximum of 0x7F (the strike velocity value corresponds to the velocity argument in a MIDI note on command). The displacement is the physical distance that the tip of thedrum stick travels to go from its rest position to the drumhead. A larger displacement causes a larger mechanicaldelay. Its difficult to predict the displacement accurately, because it depends on where the user positions thedrummer and the drum. It might be possible to put a distance sensor on the drum sticks tip, but that would not be durable or extensible. I chose to make a reasonableguess at the displacement and came up with 25 mm, whichcorresponds to a delay of around 40 ms. 4. Clap Tracking The final part of the project was to program the robotdrummer to strike in time with a clapped rhythm. Thescenario that I finished in time for this report was to havethe drummer respond to a simple, regular rhythm. Thedrummer calculates the time between the first four claps,stores the three times, and averages them to produce a finalbeat period. It adds the beat period to the time of the lastclap to predict the time of the next clap. Then it subtracts 40 ms to compensate for the drummers mechanical delay. That way, the drum stick will strike the drum within a fewmilliseconds of the next predicted strike time (as long asthe drum stick is positioned 25 mm above the drum head).The drummer could handle more complex beat patternswith a more intelligent algorithm. One simple option is toallow the user to pre-configure the

length of the pattern.This is an extension to my implementation. The drummerwould count N time differences between claps, and thenstart to output at the recorded times when it reaches clapN+1. Perhaps it could average three patterns of length Nbefore striking the drum. 3 If the clap sensor microphone is positioned so that itcan ignore the drum strike and detect only claps, then therobot could continually average the clap times it detects.This would allow the user to change the patterndynamically, but it would be confusing to the user. Duringmy tests, I found it difficult to carry on my own clappingbeat when the drummer was playing something slightlydifferent.Ideally, a beat tracking or beat histogram algorithmwould be used. I did not have time to investigate theoptions deeply. I think especially the beat histogram couldbe a feasible approach, because it requires lesscomputation than artificial intelligence mechanisms.There is around 1 KB of writable memory available on the robots microcontroller, which places a significant limit on the effectiveness of any beat tracking algorithms. Thehistogram method would need to strike a balance betweenhistogram size and the number of samples being tracked 3 The algorithm I implemented is a special case of this approach,with N=1. for calculating the histogram; further investigation isrequired to determine how the available memory limits the histograms effectivenes s.Incorporating beat strength complicates the problemeven more. For example, two sections of a beat patternthat have the same period but different strengths should beconsidered different, and the histogram approach cannothandle that elegantly. Another way to look at the data is tointerleave strength measurements with times. Forexample, weak and strong alternating beats at a frequencyof 2 Hz would look like this: 127 500 110 500 127 500 110 In the above sequence, the strong (127) and weak (110)beats are interleaved with 500 ms beat periods. This doesnot lend itself to a beat histogram, but a more generalnumerical pattern matching approach would be able topredict, for example, a 500 ms gap preceded by a strongbeat and succeeded by a weak beat. 5. Conclusion

Augmenting the robot drummer with the ability to track claps predictively makes the system more interesting anduseful. A helpful component is a clap-sensing coprocessorthat extracts beat information from hand claps. Byanalysing a simple beat and predicting its future, the robotcan now strike the drum in time with the beat whilecompensating for mechanical latency. I did not get achance to investigate this problem fully, and further work can be done in the areas of clap detection and beattracking. 6. References [1] N. MacMillan, M. Loisel, D. Partridge. Final Design Reportfor a Robot Drummer, [Web site] August, 2009, Available: http://www.scribd.com/doc/19161969 [2] T. Engdahl. Powering Microphones, [Web site] 2000, Available:http://www.epanorama.net/circuits/microphone_powering.html [3] ExpressPCB. Free PCB layout software Low cost circuitboards Top quality PCB manufacturing, [Web site] 2009,Available: http://expresspcb.com/ [4] B.H. Repp. The sound of two hands clapping: Anexploratory study , J. Acoust. Soc. Am. Volume 81, Issue 4,pp. 1100-1109, April 1987. [5] J.W. Crenshaw. Integer Square Roots, [Web site] February 1998, Available: http://www.embedded.com/98/9802fe2.htm

Anda mungkin juga menyukai