First off, my congratulations to the students of FIRST Tech Challenge team 5445, the Trohawks, for making it all the way to the Iowa State Championship yesterday! Buster (the robot) performed wonderfully. He managed bowling balls, racquetballs, and crates, all while navigating the FTC arena war zone.
Although we didn’t quite make it to the national competition, we certainly had a lot of fun competing. Although I don’t have any video this time (lighting was atrocious), take a look through these exciting action shots from our five matches (mouse over for captions):
As I’ve mentioned in the past, I’m a mentor for a local FIRST Tech Challenge team (Go Trohawks!) here in Waterloo, IA. For the past two months, my students have been busy designing, building, and programming Buster, their robot:
This past Sunday, we were put to the test at the Davenport Regional Qualifier. The students did exceptionally well. They impressed the judges with their knowledge and teamwork, their robot consistently captured their team’s bowling ball (both autonomously and via remote control), and they somehow managed to survive the bad jokes and the cha cha slide performance of our MCs and referees.
We did have a little trouble with the racquetballs though. Those little devils kept getting stuck in our drive mechanisms. Usually it was possible to rock the robot back and forth a bit until they dislodged, but it still caused us quite a bit of delay.
Now our main goal, as you may have guessed from the first picture above, was to retrieve and park our team’s bowling ball. This is something we can do quite well both autonomously and via remote. However, we were so good at capturing the ball that we were left without much to do for the majority of the competition. We’ll have to think of ways to deal with avoiding/collecting racquetballs for the next meet.
At the end of the day, we wound up ranked seventh out of twenty-one teams. Not bad considering the fact that none of us had ever done this before. And yes, I did manage to capture several of our matches on video using my BlackBerry Storm. Unfortunately, the video quality is very poor, and nowhere near HD resolution. Nevertheless, I present to you our fourth match. Feel free to comment below!
Like many people, I have fairly diverse taste in music. My media library holds tracks from Bach, Beethoven, Billy Joel, Bonobo, Brent Lamb, Brahms, Brian Hughes, and the Bee Gees (to name just the “B” section). I love variety. The trouble is, all of these different genres tend to require slightly different volume settings. Worse still, in the case of some classical music, you can get quite a wide range of volumes within a single piece (e.g. O Fortuna). So if I hook up my BlackBerry and set it to shuffle, I find myself having to continually adjust the volume knob – either because I can hardly hear the current track, or because my neighbors are about to come banging on my door.
Well, I’m not the first one to have this problem. Nor am I the only one to attempt to solve it. In fact, it’s already been solved. As you may know, there are plenty of software solutions out there for so-called volume leveling. But before the advent of the BlackBerry, or the personal computer, there was the analog compressor.
The sole purpose of such a device is to compress the dynamic range of an incoming audio signal: amplifying the soft parts and reducing the volume of the loud parts (thus decreasing, or compressing, the range of the track’s volume). It’s quite a simple thing, really. And you can buy one of these units for between $100 and $200. But why buy something when you can build it from spare parts? 🙂
Now for all of you comp-sci majors out there who are planning on commenting on any one of a million different audio leveling programs out there, don’t worry, I know they exist. The trouble is, even if I leveled every single MP3 I possess, what about internet radio? Specifically, the Pandora app for my BlackBerry? Alright, maybe there’s a software-only solution for that too (although I haven’t found it), but you know what? Just for the fun of it, I’m going with hardware this time. Well, mostly. Actually, it’s going to be a mix of analog hardware and an 8-bit Atmel AVR running some embedded C code. Sound good? Alright, then let’s get started!
First Prototype: Powered by LabVIEW
As frequent readers of my blog will know, I’m a big fan of the LabVIEW programming language. I can use it to whip up fairly complex data acquisition and processing applications in a matter of minutes (examples). Much faster than writing code for an MCU. So I decided to use it, along with my handy Mobile Studio IOBoard and Bus Pirate, for my first compressor prototype. Before we get into the brains though, let’s talk about the guts (and by that I mean let’s talk about the analog circuitry which will be common between the LabVIEW and the MCU prototypes).
First off, we need some way of detecting the volume of our incoming audio signal. To do this, you could simply connect your left and right audio channels directly to your ADC inputs. Two problems with this though. First, unless you’ve really cranked up your source’s volume, you may only see peak signals of around 100mV. Such small voltages will mean poor resolution. In other words, without some type of amplification, we won’t be able to detect fine changes in the input’s volume.
To counter this, I have added two operational amplifiers (op-amps, U1) which connect, via high-pass filters, to our incoming left and right audio signals. Each op-amp increases the amplitude of its respective signal by about 50x (click to enlarge):
The purpose of those two AC-coupling high-pass filters is to remove any DC offset from the input signals. Without these filters, we would also be amplifying that offset, such that even a 0.1V bias would saturate this first set of op-amps (which are supplied by +5V and -5V).
Once amplified, there is one more thing we need to consider. Since audio waveforms change rapidly, and swing between positive and negative voltages, our input signal may appear to change wildly between ADC samples. What we need then is a circuit that can smoothly follow the peak amplitude of our input signal. Kindof like an old VU meter.
The amplified input audio signal is shown above in blue, while the output from our “peak hold” circuit is shown in green (this plot displays 1V/div vertically, 50ms/div horizontally). Just how does this work, you ask? Well, scroll back up to my earlier schematic. The key is the two sets of diodes (D1 and D2) and 10uF capacitors (C3 and C4) you see just past the first amplifier stage. Diodes only allow current to flow in one direction (from left to right, in this case). Thus, when the left channel amplifier’s output is greater than the voltage across C4 plus 0.35V (due to the voltage drop across the diode), C4 will be charged to a higher voltage. Once the op-amp’s output drops, the diode D2 prevents C4 from discharging. Thus, it “holds” the op-amp’s peak voltage. However, because we don’t want C4 to hold this voltage indefinitely, I’ve added resistor R15 (R14 on the right channel), which gives C4 a path through which it may slowly (kindof) discharge. The result is the scope plot above.
Now at this point, I could’ve stopped and wired both peak voltage outputs directly to my ADCs. However, I would then have needed to read and average two voltages within my leveling program. Since I’m on a hardware kick, I decided to simply sum the two using an inverting summing amplifier configuration (R7, R8, R9, U2). Because this circuit’s output is inverted (negated), I then passed the signal through a unity gain (1x), inverting amplifier (R10, R11, U2). Now we’re ready for measurements! By the way, in case you have as much trouble as I do remembering what all of these op-amp circuits look like, you should download or bookmark this nifty guide from TI. I use it all the time.
Before I move onto the brains of the device, I should explain how I intend to digitally control volume. Recently, I acquired a Bus Pirate and a few SPI digital potentiometers. My plan here is to use these potentiometers (P/N MCP42010), as 256-position voltage dividers. They will be wired in-line with each audio channel. You can see one of them (one IC containing two pots) shown as a green box in my earlier schematic. The output from each pot will be fed into a buffer op-amp in order to keep the outputs strong (capable of driving headphones or line inputs). For my initial LabVIEW prototype, I will be communicating with the potentiometers via an RS232 connection to the bus pirate.
Alright, so onto the brains! [Insert zombie joke here] The image above is a screenshot of my VI (virtual instrument, also known as a LabVIEW program). As you might expect, it is quite simple. I have a control for the potentiometers’ wiper positions, as well as indicators for the instantaneous and filtered ADC inputs. The analog input is being provided by my old friend, the RPI IOBoard. Its interface consists of just a few blocks on the diagram (for those of you unfamiliar with LabVIEW, what you see here is equivalent to written code):
I have divided the program into two while loops (the large grey-border rectangles), each running at a 50ms rate. The top loop handles communication with the bus pirate (initial bus pirate configuration I perform once, by hand, through the terminal). This communication is quite simple, and consists of just two bytes. The first byte is a command which tells the potentiometers to write the second byte to their wiper position registers.
The bottom loop is responsible for determining the appropriate wiper position, based on the incoming voltage measurements. My logic is quite simple. Perhaps you can determine it from the diagram alone? Basically, I compute a difference between the current (instantaneous) voltage measurement and our last filtered value. Then, if the current measurement is greater than the filtered measurement, I divide the difference by 40 and add it to the filtered measurement. If the current measurement is less than the filtered measurement, I divide the difference (which is negative) by 240 and add it to the filtered measurement. The result of this is that for loud sounds, the filtered measurement increases fairly quickly (within a second or two). However, if the input volume drops, the filter will take perhaps five times longer to respond.
Once the filtered measurement has been computed, the appropriate wiper position is determined via a look-up table. That table, when plotted, looks something like this:
These values were determined empirically. I simply listened to the circuit’s output for various inputs and adjusted the wiper position manually to achieve the best sound. However, as you can see, it implements a log(x) function, as you might expect with an audio signal. But rather than attempting to determine a precise formula for this function, as I am designing this for use on an 8-bit MCU, I chose the look-up table approach.
With my VI wired and ready to go, I proceeded to tune my filtering algorithm to provide leveling over the whole musical spectrum. The results sounded pretty good. But before I demonstrate anything, let’s talk about the final implementation using the ATMega328.
Second Prototype: Powered by Atmel
For my embedded prototype, I again chose an AVR microcontroller. I’ve long been a fan of these chips. They’re just so easy to work with, and quite powerful. Implementing the SPI interface required just a few simple commands, since the protocol is natively supported by the ATMega’s hardware. This particular chip also has a built-in, six-channel ADC, so that was no big deal either. And since I’d already developed the algorithm and settings in LabVIEW, the most time-consuming part of this whole process was wiring up this lovely 10-segment LED bar graph. Now I need more resistors…
Although I would have liked two bar graph displays, I had only enough IO to fully support one of them. So rather than just hard-coding the chip to display one value, I added a small button (you’ll notice it just to the left of the MCU, the largest IC) which allows you to select between displaying the instantaneous voltage input (VU meter mode) and the current wiper position. This proved very helpful in debugging.
For simplicity, I’ve chosen to use the ATMega’s internally 8Mhz RC oscillator. Timing isn’t very critical for this application, so I coded up a simple delay routine which works based on repeating an operation for a calibrated number of iterations. I probably could’ve done a better (more generically useful) job on the look-up table too, but with only a few datapoints, I didn’t feel like spending much time on it. Lazy me. Anybody out there have some good code written for interpolating look-up tables? 🙂 Well, what I do have, you may download via the links at the bottom of this post.
In order to demonstrate the performance of my device, I created an MP3 containing about 90 seconds of audio (music) at different volumes. This file was played back on my laptop, whose volume setting was adjusted until the loudest sound produced just barely hit the maximum input of the ADC. The Audacity screens you see below show the original waveforms on the top and the “leveled” waveforms on the bottom (recorded via my laptop’s microphone input jack). Enjoy:
As you can see and, hopefully, hear (sorry about my lousy sound card – the signal is much clearer than it sounds here) in the video above, the soft parts in the original track were amplified somewhat, while the loud parts were reduced in volume. If not aurally obvious, I suspect the results are pretty easy to see in this screenshot from Audacity:
Overall I think this project turned out alright. I particularly liked the method of first prototyping with LabVIEW, the bus pirate, and my IOBoard. This definitely sped up development of the final application and made debugging easier. The only thing I wish is that I could use my VI to generate code for the AVR. Such a thing is available, but only for 32-bit processors. Oh well, maybe it’s time for me to upgrade by a few bits.
In the future, I may try amplifying the audio outputs. Obviously, potentiometers by themselves can only reduce signals. And that’s fine, as long as my input source is sufficiently loud on even the soft songs (because we can always cut the volume of the loud pieces). But for even greater range, amplification would be nice.
You may wonder why I didn’t include any other adjustments, like an additional volume control, or knobs to adjust the filtering speed (these would be the attack and release knobs, which, by the way, are awesome names). Well frankly, I wanted to keep things simple. The point of this project was to reduce the amount of knob-turning I do. If I left things adjustable, I’d probably be tweaking stuff all the time. It’s just a compulsion.
Anyway, I hope you’ve enjoyed this project as much as I have. As always, if you have questions, comments, or want help building something like this, please leave a comment!
List of Parts Used, with Prices (Excluding Passives)
So recently I’ve been having fun acting as a mentor for a local FIRST Tech Challenge (FTC) team. In case you haven’t heard of FTC, or any of the other FIRST (For Inspiration and Recognition of Science and Technology) robotics programs, they were created by Dean Kamen (inventor of the Segway, among other things) in an effort to get kids interested in careers as engineers and scientists. They are, in essence, sporting events for robots, like this one we call Buster:
This is a robot developed by the Waterloo Upward Bound team (we don’t have an official name yet). Actually, this is just the prototype version of Buster. Our real robot is still in development and isn’t quite driveable yet. Fortunately, we have this very simple prototype which the students can use to create and test their operating programs.
Buster will be competing in a competition which lasts just two and a half minutes. For the first thirty seconds, he must act in complete autonomy – no human interaction is allowed. After that, the students can use up to two different controllers to navigate around the arena. Check out this page for details and video of this year’s challenge.
So I wanted to just quickly post a video I shot from on-board the robot this afternoon. In this clip, we’re testing autonomous operation. Buster is driving completely on his own using an ultrasonic rangefinder and a light sensor. The ultrasonic sensor is always visible in the video, and points forward attempting to detect obstacles. The light sensor is not visible, but points at the ground and helps Buster avoid dark-colored objects on the floor (green tiles, the black mat, and dark spaces). When a wall is encountered, Buster picks a random direction and turns until no obstacles are in range. At this point, he drives forward once again. When a dark-colored object is detected on the floor, the robot stops, reverses, turns slightly to the left, and continues forward again. Check it out:
Pretty neat perspective, right? Feel free to post comments and questions below!
That’s right, I’m now the proud new owner of a Dangerous Prototypes’ Bus Pirate. I’ve been looking at these things for awhile, and finally decided that I should stop being such a cheapskate and just get one. Yea, I know I can always rig up one of my AVRs to do serial communications, but this is just so much more convenient. And at $30 (I got mine from SparkFun), it’s a pretty sweet deal.
So what did I get for my three Hamiltons? Well, check this out (courtesy SFE):
2- and 3-wire libraries with bitwise pin control
Scriptable binary bitbang, 1-Wire, I2C, SPI, and UART modes
0-5.5volt tolerant pins
0-6volt measurement probe
1Hz – 40MHz frequency measurement
1kHz – 4MHz pulse-width modulator, frequency generator
On-board multi-voltage pull-up resistors
On-board 3.3volt and 5volt power supplies with software reset
Macros for common operations
Bus traffic sniffers (SPI, I2C)
A bootloader for easy firmware updates
Transparent USB->serial mode
10Hz – 1MHz low-speed logic analyzer
Scriptable from Perl, Python, etc. (aka everything)
Translations (currently Spanish and Italian)
Enumerates as a virtual COM port over USB
Can operate as AVR STK v2 clone programmer
Access to PIC24FJ64 ICSP programming port
Needless to say, the Bus Pirate’s capabilities are extensive. And open source.
Thus far I’ve tested out its UART and SPI modes, and have been sufficiently impressed. Which leads me to my next new toy, an SPI digital potentiometer, the MCP42010:
This particular part comes in a 14-DIP package, contains two 10k potentiometers with 8-bit resolution, and can be had for just $2.40 from Digi-Key. I’m thinking of using it in an audio compressor / volume control prototype, but it may come in handy elsewhere.
Controlling this digipot is just ridiculously easy when using the Bus Pirate. Once connected and in SPI mode, I simply issue the command “[0b11011111, x]” (without the quotes), where x is a value from 0-255 representing the potentiometers’ wiper positions. The two least significant bits (11 above) of the first byte tell the chip which of its potentiometers to adjust. You can set one at a time, or both simultaneously. Bits 4 and 5 of the first byte (01 above) allow you to either write data to the wiper registers or put the digipot into a high-impedance shutdown mode (which could be great for reducing power consumption). The same effect can be achieved by pulling pin 12 low. Want to reset the potentiometer to its 50% position (this also happens automatically on power-up)? Pin 11 will do just that when pulled low. For all the gory details, take a look at this datasheet.
This little chip is pretty impressive. It’s also available in 50k and 100k versions. The only thing it really can’t do is handle much current (wiper current should be kept under ±1mA). But just toss in some buffer op-amps and you’ll be all set. That’s my plan anyways.