Go to Post Embrace the challenge, dont shoot it down. - waialua359 [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 11-01-2006, 21:25
kaszeta's Avatar
kaszeta kaszeta is offline
Registered User
FRC #0095 (Grasshoppers)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Lebanon, NH
Posts: 334
kaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of light
Trig functions and type conversion

I'm trying to use the CORDIC function to do some basic trig (calculating an angle given an x and y coordinate from a joystick), and I'm getting some odd results.

In short, if I calculate the angle using the atan() function, the results work when y>0, but give incorrect and discontinous results if y<0.

Here's the simplest code that I came up with the recreates the problem:

#include "cordic-math.h" // renamed so that I know it's not the Microchip one

short long joyx,joyy;
short long theta;

joyx=(255-p1_x)-127;
joyy=p1_y-127;

theta=atan(joyy,joyx);

printf("theta: %d ",(int)(theta>>16));

Anyone have some thoughts? I at first thought it was a problem with negative numbers and the >> operator, but theta/65536 gives the same
numbers.
  #2   Spotlight this post!  
Unread 12-01-2006, 10:34
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Trig functions and type conversion

Quote:
Originally Posted by kaszeta
In short, if I calculate the angle using the atan() function, the results work when y>0, but give incorrect and discontinous results if y<0.
That's just the nature of arc tangents. You need to determine which quadrant you're in and offset the answer accordingly. Some trig libraries provide an atan2() function that deals with the issue for you.
  #3   Spotlight this post!  
Unread 12-01-2006, 10:57
kaszeta's Avatar
kaszeta kaszeta is offline
Registered User
FRC #0095 (Grasshoppers)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Lebanon, NH
Posts: 334
kaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of light
Re: Trig functions and type conversion

Quote:
Originally Posted by Alan Anderson
That's just the nature of arc tangents. You need to determine which quadrant you're in and offset the answer accordingly. Some trig libraries provide an atan2() function that deals with the issue for you.
Hmmm, I assumed since the CORDIC library atan function took two arguments it was an atan2 function, but it appears I assumed incorrectly. Thanks.
  #4   Spotlight this post!  
Unread 12-01-2006, 17:40
TechnocratiK TechnocratiK is offline
Registered User
AKA: Patrick Diez
None #0296 (Northern Knights)
Team Role: Programmer
 
Join Date: Sep 2003
Location: Montreal, Canada
Posts: 22
TechnocratiK is an unknown quantity at this point
Re: Trig functions and type conversion

Did you write the library, or get it somewhere?
__________________
TechnocratiK out.
  #5   Spotlight this post!  
Unread 14-01-2006, 16:40
p1r0o2g7 p1r0o2g7 is offline
Registered User
#1027
 
Join Date: Mar 2005
Location: West Springfield, MA
Posts: 3
p1r0o2g7 is an unknown quantity at this point
Re: Trig functions and type conversion

why don't you use the math.h header. i'm pretty sure it has all the trig functions.

http://www.cplusplus.com/ref/cmath/
  #6   Spotlight this post!  
Unread 14-01-2006, 17:22
kaszeta's Avatar
kaszeta kaszeta is offline
Registered User
FRC #0095 (Grasshoppers)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Lebanon, NH
Posts: 334
kaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of light
Re: Trig functions and type conversion

Quote:
Originally Posted by TechnocratiK
Did you write the library, or get it somewhere?
It's the CORDIC library from the Whitepapers section here on CD. You can get it here

I was trying to use it instead of the standard math.h since it uses integer math instead of floating point, so it's much faster (and smaller).

For now, I'm using the standard math.h, but if our team's code gets too complicated, I might revisit this.
  #7   Spotlight this post!  
Unread 14-01-2006, 23:34
TechnocratiK TechnocratiK is offline
Registered User
AKA: Patrick Diez
None #0296 (Northern Knights)
Team Role: Programmer
 
Join Date: Sep 2003
Location: Montreal, Canada
Posts: 22
TechnocratiK is an unknown quantity at this point
Re: Trig functions and type conversion

OK, although my good friend Pat Fairbank posted the algorithm, I wrote it, and can assure you that there is a flaw in it (although I haven't had the time to look at it). Moreover, although theoretically the integer math should be faster, in empirical tests, the stock math library proved to be about 6 times faster than the CORDIC algorithm I wrote. Although at the moment I have no plans to rewrite the CORDIC algorithm, given enough public pressure, I might consider doing so. Sorry for the inconvenience.
__________________
TechnocratiK out.
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
Auton + Functions ten3brousone Programming 0 27-02-2005 20:11
Overloaded functions jgannon Programming 5 31-12-2004 11:04


All times are GMT -5. The time now is 19:27.

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