1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Compass radians to direction calculation

Discussion in 'Oshonsoft' started by camerart, Mar 6, 2018.

  1. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    I'm using a PIC to control a compass module, that gives X Z and Y coordinates in RADIANS.

    Here is a calculation from an Arduino project: '[angle = atan2((double) y,(double) x) * (180 / 3.14159265)+ 180 'angle in degrees]

    My results give e,g, X = 123.

    Can someone explain how to add it into Oshonsoft please

    Camerart.
     
  2. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    If you don't need to know the quadrant you can use Arctan ( the 180 / pi ) converts to degrees..
     
  3. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    You calculate angle in radians from x, y coordinates.
    Angle in degrees = radians × 180 / Pi
    ~ radians × 57.3
    Oshonsoft has arctan function for y / x < 1
    You have to calculate in eight octants ( 45 degrees )
    Octants can be found from signs of x and y and from
    value of y / x if it is less or greater than one.

    For example, if North is 0 degrees and clockwise direction:
    0 - 45 degrees x / y < 1 and x > 0, y > 0
    angle = arctan( x / y ) × 57.3

    45 - 90 degrees x/y > 1 x >0 y>0
    angle = 90 - arctan( y / x ) × 57.3
    etc ...

    Angle, x and y are floating point variables
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi I,
    Thanks, I hope it will give 0 to 360°
    C.
     
  6. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi J,
    Lots of information there, thanks.

    What does the '> and <' do in your explanation?

    In your 0-45 example is: 0 - 45 degrees x / y < 1 and x > 0, y > 0, the explanation
    and: angle = arctan( x / y ) × 57.3, the calculation?
    Or are they both the in calculation?

    What is the Z for?
    C.
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    I have never come across a compass that gives Radians.
    I'm guessing you have three quantities in the X,Y and Z directions.

    To convert two of these quantities to an angle can be done in various ways. I like Roman Blacks method. If you take the time to understand the maths then you will be much wiser.

    Mike.
    P.S. how can 0-360 ever represent anything in 3 dimensions?
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    < 1 means less than 1
    > 0 means greater than 0 or positive
    etc.
    angle = ... is the calculation
    Z can be used to calculate if compass is tilted ??
     
  10. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi M,
    Not RADIANS tick.

    I like RBs method, but it's lost on me I'm afraid as a 'C' none speaker.

    I don't follow your dimensions question.
    EDIT: 0 to 359
    C.
     
    Last edited: Mar 6, 2018
  11. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    This is mighty complicated, but I get a sense of déjà vu, as I've been through this before, and almost forgotten it.
    Thanks for the information. Between that and the attached file, I'll try to work through it all, for Oshonsoft calculation.
    C.
     

    Attached Files:

  12. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    Here is RBs code modified slightly to calculate an angle of 0-255 from two signed words x & y - I needed this for another project - I think it's almost useable in basic. Although it looks long it's very fast.

    Mike.
    Code (text):

    sx=sign(x)
    ux=abs(x)
    sy=sign(y)
    uy=abs(y)
    if ux>uy then
        degrees=uy*32/ux
        oct=1
    else
        degrees=ux*32/uy
        oct=0
    endif
    bodge=0
    if degrees>16
        if degrees<32
            bodge=bodge+1
        endif
        if degrees<28
            bodge=bodge+1
        endif
        if degrees<24
            bodge=bodge+1
        endif
    else
        if degrees>0
            bodge=bodge+1
        endif
        if degrees>3
            bodge=bodge+1
        endif
        if degrees>8
            bodge=bodge+1
        endif
    endif
    degrees=degrees+bodge
    if oct>0
        degrees=64-degrees
    endif
    if sy>0
        if sx>0
            degrees=128+degrees
        else
            degrees=128-degrees
        endif
    else
        if sx>0
            degrees=-degrees
        endif
    endif
     
     
  13. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi M,
    Thanks for the code.

    I'm afraid Oshonsoft doesn't support ABS. I'm not sure how much of this thread is relevant here: https://www.electro-tech-online.com/threads/pic-basic-to-oshonsoft-conversion.147005/
    C.
     
  14. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
  15. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi J,
    Thanks, but as mentioned ABS isn't supported in Oshonsoft
    C.
     
  16. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    That's why I wrote the message #12
    It was an example, how to get absolute value of a variable.
    You can make your own ABS function which is missing in Oshonsoft.

    Function ABS ( arg as single ) as single
    If arg < 0 then
    ABS = -1 * arg
    Else
    ABS = arg
    End function

    And now can call it:

    X1 = ABS ( X )
     
    Last edited: Mar 7, 2018
  17. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    You can also do something like,
    Code (text):

    sx=x.15
    if sx=1 then
        ux=65536-x
    else
        ux=x
    end if
     
    to get sx and ux which are the sign of x, plus or minus and ux the unsigned value of x.

    Mike.
     
  18. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi J,
    For me this is really complicated!
    This and the #12 reply, use the word ABS. If I type ABS into an Oshonsoft program, then it is rejected, so as I understand it, another way must be used.
    C.
     
  19. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi M,
    I almost understand your example.
    Is this correct: Where 'x' is the number being worked on. Use 'sx' and 'ux' as a tempory substitute. If the result 'sx' is a negative, then 'x' [now called either 'sx' or 'ux'] needs this calculation for the result?
    P.S. I can hardly understand my explanation. I'll try this method, and post it, thanks.
    C.
     
  20. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    388
    Likes:
    18
    Location:
    Helsinki, Finland
    First write a function ABS exactly as in msg #14 and then use it.
     
  21. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    Following the above advice, I came across a puzzle.

    Here's a section from my program:
    x_raw.HB = b(0)
    x_raw.LB = b(1)
    z_raw.HB = b(2)
    z_raw.LB = b(3)
    y_raw.HB = b(4)
    y_raw.LB = b(5)
    sttus_raw = b(5)

    If x_raw > 32767 Then
    x_raw = 65535 - x_raw
    x_raw = x_raw + 1
    Endif

    X_raw is the result so far. I now think that this can't be negative. Is this correct?
    C.
     
  22. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,816
    Likes:
    11
    Location:
    Dorset UK.
    Hi J,
    I've made a TEST program:
    C.
     

    Attached Files:

    • ABS.jpg
      ABS.jpg
      File size:
      195.9 KB
      Views:
      5

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice