Quote:
Originally Posted by John Gutmann
I guess the part I am really interested in is this, because this is what I am having troubles with:
Code:
/*************************************************
Timer ISR to get Gyro value at rate
determined in SetuoTimer2
**************************************************/
ISR(TIMER2_OVF_vect)
{
//Toggle the IO pin to the other state.
//digitalWrite(TOGGLE_IO,!digitalRead(TOGGLE_IO));
sample_accum += analogRead(GyroPin);
sample_counter ++;
if (sample_counter == SAMPLES_PER_UPDATE)
{
val = (sample_accum/SAMPLES_PER_UPDATE)-gyroBias;
if (val < -DEADBAND || val > DEADBAND)
{
gyro_angle += (long)val;
}
sample_accum = 0;
sample_counter = 0;
}
//Capture the current timer value. This is how much error we
//have due to interrupt latency and the work in this function
latency=TCNT2;
//Reload the timer and correct for latency.
TCNT2=(latency + timerLoadValue);
}
What why are you toggling a pin?
The latency part is just accounting for the time spent processing?
Other then those 2 things my ISR is basically the same.
I cant really post the code now because I am at work, but I can give a brief pseudo coding of it.
Code:
get adc reading
if it is less then dead band
then subtract from position
if it is greater then dead band
then add to position
else do nothing
send position over USART
I know I am technically using an accumulated rate but lets assume I am using a Riemann sum with a coefficient of 1.
Thanks,
John Gutmann
EDIT: I think when I get home I will try to eliminate the dead band, and see if that will help any.
|
Well, that is what happens when you have compiled bits and pieces of different code, from various times during the creation process together and forget to remove the commented out sections you don't use anymore.
If you will notice, those two lines are commented out. I had originally used them to toggle an LED on and off so I could measure the rate at which I was generating the ISR. I just attached an o-scope to the LED pins to do the measuring and verification. Once I proved to my self I actually made good assumptions, I commented those lines out. Sorry for the confusion.
The latency part is exactly as you described. It adjusts for the various time spent in the ISR and keeps the sample rate consistent. I suppose I could have streamlined it by doing this.
Code:
TCNT2 += timerLoadValue;
I'll have to verify this later.