Quote:
|
Originally Posted by Kevin Watson
One way around this is to double the timer 2 interrupt rate and then interleave your ADC measurements within the timer 2 ISR. If there's interest, I can modify the code to include accelerometer measurements too.
-Kevin
|
Everyone serious about autonomous and capping this year needs a gyro and some kind of arm feedback (ie, potentiometer) yet I couldn't find any real code on reading two analog inputs. So, assuming the sample rate is doubled (800 Hz), will this code work?
Our RC is a few thousand miles away in some spooky Caribbean harbor and our EDU is locked up somewhere over at the school, so I can't really test it.
Code:
void Timer_2_Int_Handler(void)
{
unsigned int adc_result;
unsigned int adc_result_2;
if (last_conversion == POTENCIOMETRO)
{
ADCON0bits.CHS0 = 0;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;
// get the latest ADC conversion
adc_result = ADRESH;
adc_result <<= 8;
adc_result += ADRESL;
// add the ADC data to the accumulator
accum += adc_result;
// increment the sample counter
samples++;
// start another analog to digital conversion
ADCON0bits.GO = 1;
// check to see if we've got a full sample set
if(samples >= GYRO_SAMPLES_PER_UPDATE)
{
// should the completed sample set be used to calculate the gyro bias?
if(calc_gyro_bias == TRUE)
{
// convert the accumulator to an integer and update gyro_bias
avg_accum += accum;
avg_samples++;
}
else
{
// update the gyro rate
gyro_rate = (int)accum - gyro_bias;
// integrate the gyro rate to derive the heading
gyro_angle += (long)gyro_rate;
}
// reset the accumulator to zero
accum = 0;
// start a fresh sample set
samples = 0;
}
last_conversion == GYRO;
}
else
{
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;
// get the latest ADC conversion
adc_result_2 = ADRESH;
adc_result_2 <<= 8;
adc_result_2 += ADRESL;
ADCON0bits.GO = 1;
potenciometro = adc_result_2;
last_conversion = POTENCIOMETRO;
}
}