I guess the part I am really interested in is this, because this is what I am having troubles with:
Code:
/*************************************************
Timer ISR to grag 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.