Go to Post I can tell you for a fact that students don't need smartphones in order to unproductively entertain themselves in a robotics lab. - remulasce [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #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.
  #2   Spotlight this post!  
Unread 29-02-2008, 20:18
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,567
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Get_Gyro_Angle() overflows beyond about -20000 deci-decgrees?

Quote:
Originally Posted by gnormhurst View Post
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.
gyro_angle * GYRO_SENSITIVITY * 5L is overflowing a long. Kevin's latest gyro code gives a little more headroom by rearranging the order operations are done to not overflow as early.

Last edited by Joe Ross : 29-02-2008 at 20:21.
  #3   Spotlight this post!  
Unread 29-02-2008, 20:31
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
Re: Get_Gyro_Angle() overflows beyond about -20000 deci-decgrees?

Okay, I'll check out Kevin's newer code....

BTW, for my current code I dereferenced the macros and got

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);
        // translates to:
	//return(((gyro_angle * 800L * 5L) / (1024L * ADC_SAMPLE_RATE/(ADC_SAMPLES_PER_UPDATE * NUM_ADC_CHANNELS))) * 958/1000);
        // which translates to ...
	//return(((gyro_angle * 800L * 5L) / (1024L * 200/(4 * 1))) * 958/1000);

	
}
I guess I could make my own Get_Gyro_Angle_Low_Resolution() that doesn't use such a large value for GYRO_SENSITIVITY.
__________________
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.
  #4   Spotlight this post!  
Unread 29-02-2008, 23:11
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
Re: Get_Gyro_Angle() overflows beyond about -20000 deci-decgrees?

I've seen the gyro.c,h code in the new 3.0 compatible code. I am using 2.4. Can I just drop the gyro.c,h files into my code and expect everything to be fine? Or are there 3.0/2.4 issues?
__________________
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
'Robocup, soccerdogs, and beyond' JaneYoung Robotics Education and Curriculum 0 24-02-2008 13:44
Drivetrains beyond the KOP twolf Kit & Additional Hardware 5 08-06-2006 17:25
Beyond the rules: helpful suggestions? Billfred CD Forum Support 1 25-05-2005 10:34
Team 33--Above and Beyond Beth Sweet Thanks and/or Congrats 3 04-04-2005 21:55
Moving beyond boxes on wheels. jon General Forum 29 07-04-2003 06:14


All times are GMT -5. The time now is 00:54.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi