Continue to Site

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.

  • 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.

Compass radians to direction calculation

Status
Not open for further replies.

camerart

Well-Known Member
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.
 
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
 
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
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.
 
Hi,
I'm using a PIC to control a compass module, that gives X Z and Y coordinates in RADIANS.
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?
 
< 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 ??
 
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?
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:
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.
 

Attachments

  • 18F4520 XTL 20MHz BUTTON-BASE LOCATION 240816 1600 TEST.bas
    16.9 KB · Views: 210
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:
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
 
Hi J,
Thanks, but as mentioned ABS isn't supported in Oshonsoft
C.
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:
You can also do something like,
Code:
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.
 
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 )
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.
 
You can also do something like,
Code:
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.
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.
 
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.
First write a function ABS exactly as in msg #14 and then use it.
 
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.
 

Attachments

  • ABS.jpg
    ABS.jpg
    195.9 KB · Views: 226
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top