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.

Calculating ARCTAN in Oshonsoft

Status
Not open for further replies.
You can use Oshonsofts arctan function, but as I said before it is not accurate: arctan(1) gives ~ 41.5 degrees, should be 45
Have you defined arctn function in your program?
I don't see it in comp. jpg.

You used x_raw.hb, x_raw.lb in comp. jpg and they are defined as single variables in dim. jpg
Why?
 
Hi J,
I prefer to use the most acurate, so not Oshonsoft's 'Arctan' Function.

Is it possible to start again from here?

'arctn' and 'picalc' and x_raw and y_raw are all defined as SINGLE

Does this work without an actual ARCTAN function?

If x_raw < y_raw Then
If x_raw > 0 And y_raw > 0 Then
azi = arctn(x_raw / y_raw) * picalc '180/pi
Endif
Endif

(Again, sorry if I get mixed up)

C.
 
You need to define the function arctn(), which calculates the arcustanget, you have only defined a variable arctn as single.
You need x_raw, y_raw as word. You can't get the bytes from compass directly to a single variable.
Get the x_raw, y_raw as before, then make a single variable X = x_raw Y= y_raw and then calculate the angle.
 
Last edited:
Hi J,
I had to change the XZY by adding 'a' as they were reserved letters.
I made an attempt at the FUNCTION, which all compiles, but gives only AZI = 083456. 8

Here is Oshonsofts FUNCTION from the Manual:
Function square(arg1 As Word) As Word
square = arg1 * arg1
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Here's my DIMs

'READ COMPASS X
Dim x_raw As Word
Dim ax As Single

'READ COMPASS Z
Dim z_raw As Word
Dim az As Single

'READ COMPASS Y
Dim y_raw As Word
Dim ay As Single

Dim azi As Single 'word
Dim strazimuth As String 'For Screen
Dim picalc As Single
Const pi = 3.14159
picalc = 180 / pi
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Here's the section of program:

'READ COMPASS XZY REGISTER
SPICSOn
For i = 0 To 5 'READ XZY Xx2 Zx2 Yx2
compss = 0 'CHIP SELECT COMPASS ON
addr = 0x83 + i
SPISend addr
SPIReceive data
b(i) = data
compss = 1 'CHIP SELECT COMPASS OFF
Next i
SPICSOff

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)

ax = x_raw
az = z_raw
ay = y_raw

'VVVVVVVV 2'S COMPLIMENT CALC VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
If ax > 32767 Then
ax = 65535 - ax
ax = ax + 1
Endif
If ay > 32767 Then
ay = 65535 - ay
ay = ay + 1
Endif
'AAAAAAAAAAAA 2'S COMPLIMENT CALC AAAAAAAAAAAAAAAAAAAAAAAAAAAA

If ax < ay Then
If ax > 0 And ay > 0 Then
azi = arctn(ax, ay)
Endif
Endif
l
l
l
l
END
____________________________________________________
'Azimuth CALC Function
Function arctn(ax As Single, ay As Single) As Single
arctn = (ax / ay) * picalc '180/pi
End Function

NOTE: There is a Z component. Should it be introduced now, or once the XY is working?
EDITED. EDITED.
C.
 
Last edited:
Use the approximation I gave in msg #2
Function arctn( .....
arctn= x/y / (1+0.285125(x/y)^2)*180/pi
End function

Edit: arctn= x/y/( 1+0.28125*( x/y )^2)*180/pi
 
Last edited:
Use the approximation I gave in msg #2
Function arctn( .....
arctn= x/y / (1+0.285125(x/y)^2)*180/pi
End function

Edit: arctn= x/y/( 1+0.28125*( x/y )^2)*180/pi
Hi J,
Thanks for the reminder and the edit.

This compiles, does it look ok? : arctn = x / y / (1 + (0.285125 * (x / y))) * x / y / (1 + (0.285125 * (x / y)))
I'll try it!
C.
 
Hi J,
Thanks for the reminder and the edit.

This compiles, does it look ok? : arctn = x / y / (1 + (0.285125 * (x / y))) * x / y / (1 + (0.285125 * (x / y)))
I'll try it!
C.
No, too many terms.
It has only (x/y)^2, not the whole expression^2
Use the edited arctn = ... in msg# 27
 
Using the equasion in #27. When module is turned CCW the output shows APP 1/20th of the correct reading.

C.
 
No, too many terms.
It has only (x/y)^2, not the whole expression^2
Use the edited arctn = ... in msg# 27
Hi J,
Oshonsoft doesn't support '^' so it needs some modification.

EDIT: Is this ok? arctn = x/y/(1 + 0.28125 * ((x / y) * (x / y))) * picalc '180/PI
It compiles!

C
 
Last edited:
It is ok.
Show values of x, y and the calculated angle
Hi j,
These are apporoximate!

At 1º AZI= 1 X= 10 Y= 510
At 10º AZI= 7.0 X= 64 Y= 506
At 20º AZI= 13 X=112 Y= 491
At 30º AZI= 19 X=158 Y= 472

If this doesn't look immediatelly ok, then I'll recheck the program to make sure it is READing correctly.
C.
 
Last edited:
Hi J,
It looks as though arctn= x/y/ ( 1+0.28125*( x/y )^2) *180/pi
would look like: arctn= x/y/ ( 1+0.28125*( x/y) ) * ( 1+0.28125*( x/y) ) *180/pi
Is this correct?
C.
 
Hi J,
It looks as though arctn= x/y/ ( 1+0.28125*( x/y )^2) *180/pi
would look like: arctn= x/y/ ( 1+0.28125*( x/y) ) * ( 1+0.28125*( x/y) ) *180/pi
Is this correct?
C.
No.
I don't understand.
(x/y)^2 means (x/y)*(x/y) = x*x/y/y
Msg#30 is correct.
 
Hi j,
These are apporoximate!

At 1º AZI= 1 X= 10 Y= 510
At 10º AZI= 7.0 X= 64 Y= 506
At 20º AZI= 13 X=112 Y= 491
At 30º AZI= 19 X=158 Y= 472

If this doesn't look immediatelly ok, then I'll recheck the program to make sure it is READing correctly.
C.
AZI is correct with those x, y values
Probably you get wrong x , y values from compass.
Hi j,
These are apporoximate!

At 1º AZI= 1 X= 10 Y= 510
At 10º AZI= 7.0 X= 64 Y= 506
At 20º AZI= 13 X=112 Y= 491
At 30º AZI= 19 X=158 Y= 472

If this doesn't look immediatelly ok, then I'll recheck the program to make sure it is READing correctly.
C.
AZI ~ correct with these x, y values.
x, y must be wrong.
 
AZI is correct with those x, y values
Probably you get wrong x , y values from compass.

AZI ~ correct with these x, y values.
x, y must be wrong.

EDIT: Hi J,
Am I correct that the AZI values are correct from the X an Y numbers, so the calculation is working but not giving the correct angle, because X and Y are not correct?
C.
 
Last edited:
Yes.
Calculation is ok.
You can check it with a scientific calculator.
Also on the web: https://web2.0calc.com
Hi J,
I've also get good results from the equation, so it must be something to do with either READing the module or Oshonsoft.

I'm going to try a different module. I'm now looking at MPU-9255 modules.

Thanks, C.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top