Shop OBEX P1 Docs P2 Docs Learn Events
polar to rectangular conversion — Parallax Forums

polar to rectangular conversion

ArchiverArchiver Posts: 46,084
edited 2001-11-12 19:41 in General Discussion
I need help converting an A/D output that corresponds to 0 to 360 degrees of
rotation to X and Y co-ordinates that I will use to move the cursor on a
computer screen around using the Mouse interface. I know how to implement the
Mouse protocol but am not real sure of the best way to transform the 0 to 360
degrees (0 to 1023 bits from a 10 bit A/D) into the corresponding X and Y
values. I would appreciate any help or suggestions from anyone in the group!
Thanks!
George (azeasi@a...)

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-11-11 19:50
    At 14:27 11/11/01, azeasi@a... wrote:
    >I need help converting an A/D output that corresponds to 0 to 360 degrees of
    >rotation to X and Y co-ordinates that I will use to move the cursor on a
    >computer screen around using the Mouse interface. I know how to implement the
    >Mouse protocol but am not real sure of the best way to transform the 0 to 360
    >degrees (0 to 1023 bits from a 10 bit A/D) into the corresponding X and Y
    >values. I would appreciate any help or suggestions from anyone in the group!

    If you mean you want to make a polar to rectangular coordinate conversion,
    you don't have enough information. You have an angle, but you need a
    distance from the vertex of that angle before you can make a P-R
    conversion. How do you want the 0-359 degrees, or 0-1023 value, to be
    mapped onto your screen?


    Jim H
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-11 20:38
    I am assuming a unit circle, so the absolute distance is 1. Therefore all of
    my X and Y values will be from -1 to +1. For instance, if I am at 0 degrees,
    I want X = 0 and Y =1. at 360 degrees same values, at 90 degrees X = -1 and Y
    =0, at 180 degrees X = 0 and Y = +1, and at 270 degrees X=+1 and Y=0. Hope
    this clarifies what I am trying to accomplish. I thought I could use X=sin
    (angle) and Y= cos (angle) since radius is 1. Since Stamp can't deal with
    fractions I could scale everything by 10 and still get reasonable resolution
    (I think). Am I on the right track? Do you have any ideas on a better way to
    do this. I will probably want to use a PIC 16F876 or one of the other parts
    with a built in A/D later on. For now I just want to get my algorithm nailed
    down.
    Thanks again for your quick response!
    George (azeasi@a...)
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-11 22:06
    At 15:38 11/11/01, azeasi@a... wrote:

    >I am assuming a unit circle, so the absolute distance is 1. Therefore all of
    >my X and Y values will be from -1 to +1. For instance, if I am at 0 degrees,
    >I want X = 0 and Y =1. at 360 degrees same values, at 90 degrees X = -1 and Y
    >=0, at 180 degrees X = 0 and Y = +1, and at 270 degrees X=+1 and Y=0. Hope
    >this clarifies what I am trying to accomplish.

    Yes and no. I think you might have some typographical errors in the signs
    on some of the values you just gave. You can have it any way you want, but
    the norm for me is to measure the angle from the horizontal, and work
    counterclockwise, with X being the horizontal axis and Y the vertical. So
    for me:

    0 deg - X = 1, Y = 0
    90 deg - X = 0, Y = 1
    180 deg - X = -1, Y = 0
    270 deg - X = 0, Y = -1

    I think you are measuring from the vertical in which case and working
    counterclockwise:

    0 deg - X = 0, Y = 1
    90 deg - X = -1, Y = 0
    180 deg - X = 0, Y = -1 (you wrote +1)
    270 deg - X = 1, Y = 0

    >I thought I could use X=sin(angle) and Y= cos (angle)
    >since radius is 1.

    I agree.

    >Since Stamp can't deal with fractions I could scale everything by 10 and
    >still get reasonable resolution (I think). Am I on the right track?

    You're definitely on the ( or at least a) right track. But I don't know if
    you'll get reasonable resolution. How do you plan to display the
    result? Does the display have enough resolution that the plotted point
    should be in a different place with only a 1 degree change? If not, how
    many degrees change is needed to make a difference in what the display can
    show? The Stamp, because of the games you're playing with accuracy and
    precision, may not produce a unique output for each unique input. If I
    were you I'd write a little program on the Stamp to take values from 0 -
    1023 and calculate the output you want and see if that output will produce
    the values you need to get an acceptable plot. If it passes that test it
    looks like you have a good plan!

    >Do you have any ideas on a better way to do this. I will
    >probably want to use a PIC 16F876 or one of the other
    >parts with a built in A/D later on. For now I just want to
    >get my algorithm nailed down.

    The algorithm sounds fine, but... on your Stamp you'll be playing the games
    you mentioned to get accuracy and precision. On the PIC 16F876 you have 8K
    of program memory. I bet you can find the code to implement a reasonably
    accurate sine and cosine directly so you don't have to play games. The
    display resolution may well be the deciding or limiting factor.

    Good luck.

    Jim H
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-12 01:12
    Thanks to Jim H., Peter, and all who have responded. The pot I am using is
    not really a "pot" but electrically simulates a pot (i.e., voltage divider
    output based on position of "wiper") so it is linear. Yes, I will need to add
    an offset value to make my zero position correspond to zero degrees. My
    desired zero position corresponds to the 6 o'clock position with 90 degrees
    at 9 o'clock , 180 degrees at 12 o'clock and 270 degrees at 3 o'clock. My
    actual zero position is more like 5 o'clock. The signs I described earlier
    were based on the signs used by a standard Mouse (Up is +Y, Down is -Y, Right
    is +X, and Left is -X) Thanks for the bit on screen resolution, it brings up
    something I hadn't considered earlier. I know the Stamp is not fast enough to
    use as a Mouse interface but I figured I could use the technique I developed
    and optimized using the Stamp and build the final circuit using the PIC or
    perhaps even an SX. Thanks again to all who responded!
    George (azeasi@a...)
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-12 08:36
    Hi,

    I think you should start with the definition of your display.
    Assume a 2N+1 by 2N+1 pixel square with the origin in the middle.
    So your X lies between -N and +N and so does your Y.
    Assuming the 1023 output reflects a full circle you must translate that
    to the argument for the sin and cos functions which take 0-255 radial units
    for a full circle.
    So, divide your AD value 0..1023 by 4, giving you 0..255.
    The sin and cos function return -127 to +127, probably using 2-complement
    i.e 255 equals -1 and 128 equals -127.

    The X and Y function become (mathematical)
    X=N*cos(AD/4)/127
    Y=N*sin(AD/4)/127

    You must split this into positive and negative parts
    If cos(AD/4)<128 then X=N*cos(AD/4)/127 else X=-N*(255-cos(AD/4))/127
    If sin(AD/4)<128 then Y=N*sin(AD/4)/127 else Y=-N*(255-sin(AD/4))/127

    Your display origin is located at XD,YD of your screen
    So your screen coordinates become (assuming screen origin left below)
    SX=X+XD
    SY=Y+YD
    Note: X,Y,XD,YD should be byte variables.

    There are still some details to fill in, such as: does your AD value 0
    correspond to
    angle 0, if not you must add a offset angle, rounding errors etc. but it may
    give you a start.
    The resolution you get is mainly defined by N with N=127 giving max.
    resolution.

    Regards peter


    Oorspronkelijk bericht
    Van: Jim Higgins [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=e1T18eVY9TsP97BC3CF9MW_oMiDKNSs4v__zU9N2dTjmnsV51y12_2VrVtpZfBB6QEs02b9njBxY]HigginsJ@s...[/url
    Verzonden: zondag 11 november 2001 14:07
    Aan: basicstamps@yahoogroups.com
    Onderwerp: Re: [noparse][[/noparse]basicstamps] polar to rectangular conversion

    At 15:38 11/11/01, azeasi@a... wrote:

    >I am assuming a unit circle, so the absolute distance is 1. Therefore all
    of
    >my X and Y values will be from -1 to +1. For instance, if I am at 0
    degrees,
    >I want X = 0 and Y =1. at 360 degrees same values, at 90 degrees X = -1 and
    Y
    >=0, at 180 degrees X = 0 and Y = +1, and at 270 degrees X=+1 and Y=0. Hope
    >this clarifies what I am trying to accomplish.

    Yes and no. I think you might have some typographical errors in the signs
    on some of the values you just gave. You can have it any way you want, but
    the norm for me is to measure the angle from the horizontal, and work
    counterclockwise, with X being the horizontal axis and Y the vertical. So
    for me:

    0 deg - X = 1, Y = 0
    90 deg - X = 0, Y = 1
    180 deg - X = -1, Y = 0
    270 deg - X = 0, Y = -1

    I think you are measuring from the vertical in which case and working
    counterclockwise:

    0 deg - X = 0, Y = 1
    90 deg - X = -1, Y = 0
    180 deg - X = 0, Y = -1 (you wrote +1)
    270 deg - X = 1, Y = 0

    >I thought I could use X=sin(angle) and Y= cos (angle)
    >since radius is 1.

    I agree.

    >Since Stamp can't deal with fractions I could scale everything by 10 and
    >still get reasonable resolution (I think). Am I on the right track?

    You're definitely on the ( or at least a) right track. But I don't know if
    you'll get reasonable resolution. How do you plan to display the
    result? Does the display have enough resolution that the plotted point
    should be in a different place with only a 1 degree change? If not, how
    many degrees change is needed to make a difference in what the display can
    show? The Stamp, because of the games you're playing with accuracy and
    precision, may not produce a unique output for each unique input. If I
    were you I'd write a little program on the Stamp to take values from 0 -
    1023 and calculate the output you want and see if that output will produce
    the values you need to get an acceptable plot. If it passes that test it
    looks like you have a good plan!

    >Do you have any ideas on a better way to do this. I will
    >probably want to use a PIC 16F876 or one of the other
    >parts with a built in A/D later on. For now I just want to
    >get my algorithm nailed down.

    The algorithm sounds fine, but... on your Stamp you'll be playing the games
    you mentioned to get accuracy and precision. On the PIC 16F876 you have 8K
    of program memory. I bet you can find the code to implement a reasonably
    accurate sine and cosine directly so you don't have to play games. The
    display resolution may well be the deciding or limiting factor.

    Good luck.

    Jim H


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-12 19:41
    Adding to Jim Higgin's excellent suggestions about resolution, an old
    graphics trick is to use reflective symmetry about both X and Y axes,
    whenever it is appropriate. For example, to draw or track a circle, compute
    only points from 0 to 45 degrees, and then plot their reflected mirror
    images about both axes into all 4 quadrants. Saves both computing and
    memory for any stored tables.
    Dennis

    Original Message
    From: "Jim Higgins" <HigginsJ@s...>
    To: <basicstamps@yahoogroups.com>
    Sent: Sunday, November 11, 2001 2:06 PM
    Subject: Re: [noparse][[/noparse]basicstamps] polar to rectangular conversion


    > At 15:38 11/11/01, azeasi@a... wrote:
    >
    > >I am assuming a unit circle, so the absolute distance is 1. Therefore all
    of
    > >my X and Y values will be from -1 to +1. For instance, if I am at 0
    degrees,
    > >I want X = 0 and Y =1. at 360 degrees same values, at 90 degrees X = -1
    and Y
    > >=0, at 180 degrees X = 0 and Y = +1, and at 270 degrees X=+1 and Y=0.
    Hope
    > >this clarifies what I am trying to accomplish.
    >
    > Yes and no. I think you might have some typographical errors in the signs
    > on some of the values you just gave. You can have it any way you want,
    but
    > the norm for me is to measure the angle from the horizontal, and work
    > counterclockwise, with X being the horizontal axis and Y the vertical. So
    > for me:
    >
    > 0 deg - X = 1, Y = 0
    > 90 deg - X = 0, Y = 1
    > 180 deg - X = -1, Y = 0
    > 270 deg - X = 0, Y = -1
    >
    > I think you are measuring from the vertical in which case and working
    > counterclockwise:
    >
    > 0 deg - X = 0, Y = 1
    > 90 deg - X = -1, Y = 0
    > 180 deg - X = 0, Y = -1 (you wrote +1)
    > 270 deg - X = 1, Y = 0
    >
    > >I thought I could use X=sin(angle) and Y= cos (angle)
    > >since radius is 1.
    >
    > I agree.
    >
    > >Since Stamp can't deal with fractions I could scale everything by 10 and
    > >still get reasonable resolution (I think). Am I on the right track?
    >
    > You're definitely on the ( or at least a) right track. But I don't know
    if
    > you'll get reasonable resolution. How do you plan to display the
    > result? Does the display have enough resolution that the plotted point
    > should be in a different place with only a 1 degree change? If not, how
    > many degrees change is needed to make a difference in what the display can
    > show? The Stamp, because of the games you're playing with accuracy and
    > precision, may not produce a unique output for each unique input. If I
    > were you I'd write a little program on the Stamp to take values from 0 -
    > 1023 and calculate the output you want and see if that output will produce
    > the values you need to get an acceptable plot. If it passes that test it
    > looks like you have a good plan!
    >
    > >Do you have any ideas on a better way to do this. I will
    > >probably want to use a PIC 16F876 or one of the other
    > >parts with a built in A/D later on. For now I just want to
    > >get my algorithm nailed down.
    >
    > The algorithm sounds fine, but... on your Stamp you'll be playing the
    games
    > you mentioned to get accuracy and precision. On the PIC 16F876 you have
    8K
    > of program memory. I bet you can find the code to implement a reasonably
    > accurate sine and cosine directly so you don't have to play games. The
    > display resolution may well be the deciding or limiting factor.
    >
    > Good luck.
    >
    > Jim H
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
Sign In or Register to comment.