View Single Post
  #1   Spotlight this post!  
Unread 29-02-2008, 20:02
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Get_Gyro_Angle() overflows beyond about -20000 deci-decgrees?

I'm trying to make an approximate lap counter by dividing the accumulated gyro angle by 360 (or 3600, since I'm working in decidegrees).

It works up to 5 laps, then the gyro value seems to wrap around to the positive side and start decreasing. I'm using long ints for the calculations. Here is a simplified version of the code....

Code:
  long int gyroAngle;
  unsigned int  numLaps = 0;

  gyroAngle = Get_Gyro_Angle();
  if ( gyroAngle < 0L ) gyroAngle = -gyroAngle;  //absolute value.
  
  numLaps = (unsigned int) ( (gyroAngle) / (long int)3600);
  printf( "gyro: %5ld LAPS = %2d\r\n", (gyroAngle), (int)numLaps );


//   etc.....
I looked at the gyro.c code briefly. Get_Gyro_Angle() (below) returns a long int, but there's a bunch of #defined constants, not all of which sport the decorative "L" at the end. (BTW, does "L" make a literal into a short int (16) or a long int (32)? I checked the compiler manual and didn't see it defined.)

Code:
long Get_Gyro_Angle(void)
{
	// Return the calculated gyro angle to the caller.
	return(((gyro_angle * GYRO_SENSITIVITY * 5L) / (ADC_RANGE * ADC_UPDATE_RATE)) * GYRO_CAL_FACTOR);
}
Why can't I get big values out of Get_Gyro_Angle()? e.g. 6 laps = 6 * -3600 = -21600.
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.