Go to Post You can lay out all the disclaimers that you want, but the bottom line is you are representing your team. - Karthik [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 12-07-2011, 10:24
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Cartesian to angle



Given arbitrary (x,y) Cartesian coordinates, how do you program the calculation of the 0-to-2pi angle clockwise from the +Y axis?


Note: I accidentally wiped out the original post. If the reconstructed wording above is not exactly what I originally posted, let me know and I will fix it.



Last edited by Ether : 12-07-2011 at 12:45.
  #2   Spotlight this post!  
Unread 12-07-2011, 10:51
James Critchley James Critchley is offline
Registered User
no team
Team Role: Mentor
 
Join Date: Apr 2011
Rookie Year: 2010
Location: Lake Orion, Michigan
Posts: 45
James Critchley is an unknown quantity at this point
Re: Cartesian to angle

I use "atan2(x,y)". It's not 100% what you're looking for (measured (-pi,pi] from the x-axis clockwise) but it does all of the "hard work" in the standard library so it should be efficient in the general case. The conversion from there to what you want is straight forward.

Don't feed it (0,0)

Wikipedia gives a good description.

How do you?
  #3   Spotlight this post!  
Unread 12-07-2011, 11:33
Jogo Jogo is offline
Registered User
FRC #0353 (Pobots)
Team Role: Programmer
 
Join Date: May 2009
Rookie Year: 2007
Location: NY
Posts: 83
Jogo is on a distinguished road
Re: Cartesian to angle

from the +y axis? atan2(-x,y) seems to do the trick.
  #4   Spotlight this post!  
Unread 12-07-2011, 12:10
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Cartesian to angle



atan2(x,y) gives +/-pi clockwise from the +Y axis. That puts the discontinuity on the -Y axis, which then requires you to add conditional logic to fix it. There's a better way.


BTW, concerning atan2(0,0), many modern implementations, such as the microcode in the FP unit of the Pentium*, helpfully return zero when both arguments are zero. Does anyone know what the FRC versions of LabVIEW, C++, and Java do?


* here's a short test code written in Delphi:

USES windows, sysutils;


Function FPatan2(y : extended; x : extended): Extended;
Assembler;
asm fld [y] ; fld [x] ; fpatan end ;


BEGIN

write(FPatan2(0,0),' Pentium FP processor built-in microcode');

END.




Last edited by Ether : 12-07-2011 at 12:27.
  #5   Spotlight this post!  
Unread 12-07-2011, 12:25
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Cartesian to angle

Quote:
Originally Posted by Jogo View Post
from the +y axis? atan2(-x,y) seems to do the trick.
atan2(-x,y) gives you angles counter-clockwise from the +Y axis.


  #6   Spotlight this post!  
Unread 12-07-2011, 12:47
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Cartesian to angle


Just to be clear, all references to atan2 in any of my posts, UOS, refer to the standard definition of the order of the parameters, not the reversed-order of, say, Microsoft Excel spreadsheet.


  #7   Spotlight this post!  
Unread 12-07-2011, 12:53
Dmentor's Avatar
Dmentor Dmentor is offline
Registered User
AKA: Daniel Bray
FRC #1895 (Lambda Corps)
Team Role: Mentor
 
Join Date: Mar 2007
Rookie Year: 2007
Location: Manassas, VA
Posts: 85
Dmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant futureDmentor has a brilliant future
Re: Cartesian to angle

atan2(-x,-y)+pi seems to avoid excessive logic.
__________________
Dan was here.


2014 VA Semi-Finalist (2363, 1533), Johnson & Johnson Gracious Professionalism Award
2013 Johnson & Johnson Gracious Professionalism Award, Woodie Flowers Finalist - James Gillespie
2012 Chesapeake Finalist (358, 714), Johnson & Johnson Gracious Professionalism Award
2011 VA Semi-Finalist (122, 1111), Johnson & Johnson Gracious Professionalism Award
2010 DC Semi-Finalist (2912, 449), Dean's List Finalist - Chris Dorick, Xerox Creativity Award
2009 VA Semi-Finalist (612, 1908)
2009 DC Semi-Finalist (1712, 176), Imagery Award
2007 CMP Newton Semi-Finalist (68, 111)
2007 VA Rookie All-Star Award, Regional Semi-Finalist (343, 612), Highest Rookie Seed Award (#2), Website Award
  #8   Spotlight this post!  
Unread 12-07-2011, 13:04
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Cartesian to angle

Quote:
Originally Posted by Dmentor View Post
atan2(-x,-y)+pi seems to avoid excessive logic.
That's what I use. Here's a cheatsheet for other angle choices.


  #9   Spotlight this post!  
Unread 12-07-2011, 15:19
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
Re: Cartesian to angle

A look up table?
__________________
Do not say what can or cannot be done, but, instead, say what must be done for the task at hand must be accomplished.
  #10   Spotlight this post!  
Unread 12-07-2011, 15:21
AdamHeard's Avatar
AdamHeard AdamHeard is offline
Lead Mentor
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Atascadero
Posts: 5,511
AdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond repute
Send a message via AIM to AdamHeard
Re: Cartesian to angle

Quote:
Originally Posted by davidthefat View Post
A look up table?
Why? That's terribly cumbersome compared to the methods already posted.
  #11   Spotlight this post!  
Unread 12-07-2011, 15:40
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
Re: Cartesian to angle

Quote:
Originally Posted by AdamHeard View Post
Why? That's terribly cumbersome compared to the methods already posted.
Well, it takes less clock cycles to just retrieve data from an array compared to calculating the angle on the fly. Even though a look up table does take up considerable amount of space in the RAM (Not really since we are dealing with 64 MB of RAM...) Also given the level of accuracy we only really need, about ± pi/180 radian. I think it should be fine.
__________________
Do not say what can or cannot be done, but, instead, say what must be done for the task at hand must be accomplished.
  #12   Spotlight this post!  
Unread 12-07-2011, 16:04
AdamHeard's Avatar
AdamHeard AdamHeard is offline
Lead Mentor
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Atascadero
Posts: 5,511
AdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond repute
Send a message via AIM to AdamHeard
Re: Cartesian to angle

Quote:
Originally Posted by davidthefat View Post
Well, it takes less clock cycles to just retrieve data from an array compared to calculating the angle on the fly. Even though a look up table does take up considerable amount of space in the RAM (Not really since we are dealing with 64 MB of RAM...) Also given the level of accuracy we only really need, about ± pi/180 radian. I think it should be fine.
The cycles are a nonissue on the cRIO, and using the function is substantially less code/setup time.
  #13   Spotlight this post!  
Unread 12-07-2011, 16:11
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Cartesian to angle

Quote:
Originally Posted by davidthefat View Post
Well, it takes less clock cycles to just retrieve data from an array compared to calculating the angle on the fly.
Where do you get your information ?


  #14   Spotlight this post!  
Unread 12-07-2011, 16:27
EricVanWyk EricVanWyk is offline
Registered User
no team
 
Join Date: Jan 2007
Rookie Year: 2000
Location: Boston
Posts: 1,597
EricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond repute
Send a message via AIM to EricVanWyk
Re: Cartesian to angle

Quote:
Originally Posted by Ether View Post

Where do you get your information ?

You must spread some Reputation around before giving it to Ether again.
  #15   Spotlight this post!  
Unread 12-07-2011, 17:20
James Critchley James Critchley is offline
Registered User
no team
Team Role: Mentor
 
Join Date: Apr 2011
Rookie Year: 2010
Location: Lake Orion, Michigan
Posts: 45
James Critchley is an unknown quantity at this point
Re: Cartesian to angle

Fantastic!

I though you were going to post the trig logic to do all of the calculations. "atan2" is certainly the way to go, transforming coordinates before using it is great.

I need to look at a clock again... right hand rule = CCW!!! See my earlier post.


From a readability perspective, consider defining a macro statement such that.

angle = HEADING(0,0);



atan2(0,0) is undefined in the standard. So for cross platform use, you should check for this as an arbitrary compiler can throw an exception.

if y == 0.0 then y = 1e-8.

Assuming you aren't working with nano-bots this will do nicely and leave your (0,0) point at 0. Unfortunately this adds a comparison operation which, as implied here, are commonly the most costly operations and what you are trying to avoid. This is a prety good arguement for changing the standard.


I did some reading, and as Ether pointed out, ALL recent Intel chips define (0,0) as 0 and this is on the chip and part of the FPU. If you use a look-up table, you need an unbounded (or large) 2D table in x and y... You can manage this by normalization or conversion to a one dimensional problem but atan2 solves it right there in the silicon. There are a host of other issues with the look-up table but the one that strikes me is the effort associated with continually moving a table back and forth through the layers of cache on the chip (Cache is MUCH smaller than RAM). It's not a good option and should not be faster (coding or execution with other intensive code in the loop) or portable to higher resolution applications. In the interest of exhausting all alternatives, the look-up table certainly is "out of the box" and I like it. Keep up the good work!

With the Intel FPU, most modern PC's and Macs will get (0,0) = 0 "right" provided the compiler actually accesses the FPU directly for this calculation (that would be most compilers including gcc). But what does the cRIO do? Is the FPGA intel based? Looks like "Xilinx Virtex" is a custom processor for FPGA? So you need documentation of a firm commitment to define the result as 0, or you need to test and a re-test every time the chip is upgraded which could happen without a "model" change to cRIO.
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


All times are GMT -5. The time now is 18:58.

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