As you all probably know, the Ardupilot/ArduIMU uses ADC oversampling to achieve greater than 10 bit resolution of the internal ADC. I beleive they achieved alomost 12 bit resolution- not bad, I'd say.When I discovered this, I started using that approach to increase the resolution of cheap ADC's at my work, saving but loads of $ in the process.
Anyway to any curious parties interested, here is a good article that decribes how its done and why.
Comments
Here is a quick example to show a simple framework for a timed loop and interrupt driven ADC sampling. You will have to replace the adc interrupt example code with actual working AVR code and interrupt initialization. Hint: http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html for more information. You can also look at the APM code. I am pretty sure they use free running ADC interrupts.
------------
unsigned long loop_timer;
volatile unsigned long adc_integration;
volatile unsigned int adc_counter;
// Free running ADC interrupt that is called everytime ADC hardware is ready with a new sample
void adc_interrupt( void )
{
// ADC sample integration
adc_integration += //latest adc sample from adc interrupt//;
// Increment adc counter
adc_counter++;
}
// Main loop
void loop( void )
{
// 100hz timed loop check
if( micros() - loop_timer >= 10000 )
{
// Update loop timer
loop_timer += 10000;
// Disable interrupts
cli();
// Calculate ADC mean value
unsigned long adc_value = adc_integration / adc_counter;
// Reset ADC integration and counter
adc_integration = 0;
adc_counter = 0;
// Enable interrupts
sei();
}
}
Newbie question: How can you tell or how you calculate the sampling speed (HZ) based on the hardware and/or software? For example for the APM?
How do I know that (for example) the main loop runs at 100HZ and how do I make sure that my sensor over-sample loop fits within that loop cycle?
About SNR: SNR or S/N of a single sample is typically proportional to the sqrt of time for a single sample. By oversampling you decrease the SNR of a single sample, but when using averaging (aka filtering) you can get back the higher SNR at the cost of decreased temporal resolution (and more 'lag'). The real benefit of oversampling (the main point of the article) is to reduce the quantization error. The noise in our measurements is from both quantization and white noise. As Frederic suggested, there will be a limit (eg once you have reduced quantization error to well below white noise, there is little to no benefit in accuracy when increasing oversampling even further).
@Fokko,
In esence, the software averaging is acting as a low pass filter, and so the frequency response will be biased low.
God Likar,
I can't see why it should produce lag. The sample frequency is increased in a way that when it normaly would do one sample, it takes four and avarages the values. This shouldn't take more time if the clock frequency of the ADC is increased properly.
Improve s/n yes, but can't see how over sampling can increase resolution. A simple or moving average is going to introduce lag and those that don't lag require take at least two iterations of the data plus some math to complete. For example the Hull Moving Average should work on any data and usually lags only one sample.