I think I've found a bug in the Gyro code....
I know... it seems unlikely considering how long it's been in service, but this problem has been bugging me for a while and I finally got the clues I needed to locate it. Here is the function.
Quote:
int Get_Gyro_Rate(void)
{
// Return the calculated gyro rate to the caller.
return((int)((((long)gyro_rate * GYRO_SENSITIVITY * 5L) / ADC_RANGE)) * GYRO_CAL_FACTOR);
}
|
I was calling this function and displaying the return value, but it never got above 32 or below -32, and it seemed to flip signs at the oddest times.
I think the problem is that GYRO_CAL_FACTOR is defined as 1000/1000, but the multiplication is being done AFTER the prior expression is cast to an integer... Therefore if ((((long)gyro_rate * GYRO_SENSITIVITY * 5L) / ADC_RANGE)) calculates to anything above 32 or below -32, then multiplying it by 1000 will cause the number to exceed the valid range of an integer.
I think the second to last bracket is misplaced... I think it should be:
return((int)((((long)gyro_rate * GYRO_SENSITIVITY * 5L) / ADC_RANGE) * GYRO_CAL_FACTOR
));
I'm going to try it tomorrow...
I'm just greatful I didn't have to write this code....
Thankyou Kevin... really.
Phil.