# Twos compliment in Oshonsoft and Compass calibration

#### jjw

##### Member
Take away the space between - and Xc
It does'nt work.
This works: Xc = - 1 * Xc

#### camerart

##### Active Member
It does'nt work.
This works: Xc = - 1 * Xc
Hi J,
Compiling C.

#### sagor1

##### Active Member
Hi S,
Regarding Y=0: So far I'm looking at the first OCTANT of the circle, so it's possible that in other OCTANTS there may be 'If y = 0 then'
I'm using Oshonsoft BASIC, and there is a SYNTAX '>=' What I'm following here, I think is written in C+?, and I'm not sure if '> 0' may also mean '>='. Perhaps you know?

At the moment, I'm not concerned with magnetic/true, I'll look farther into this later. I think the deviation here is <1 Deg.
C.
All I'm saying is that in the math logic, you must test for Y>0 first, before testing if X/Y <1.
The example you show from C+ is a compound "if", testing all 3 conditions in one statement, which Oshonsoft cannot do. With Oshonsoft you have to do each test individually, as you show in your code, but your math logic should test for Y>0 first before the rest of it....

#### camerart

##### Active Member
All I'm saying is that in the math logic, you must test for Y>0 first, before testing if X/Y <1.
The example you show from C+ is a compound "if", testing all 3 conditions in one statement, which Oshonsoft cannot do. With Oshonsoft you have to do each test individually, as you show in your code, but your math logic should test for Y>0 first before the rest of it....
Hi S,
Like this?:

If y_c > 0 Then
If x_c > 0 Then
If x_c / y_c < 1 Then
deg = Arctan(x_c / y_c) * 57.3
Endif
Endif
Endif

C.

#### camerart

##### Active Member
Negate: Xc = - Xc
If changed to 14bit, maybe the 2's complement has to be changed too?
The variation is smaller now.
Can you rotate the full circle.
Hi J,
The compass I am using is AK8963C. It looks as though it can only do 14BIT, and from my poor understanding of 2's compliment, uses different numbers. Is this true?
C.

#### Attachments

• 27.2 KB Views: 3
• 166.5 KB Views: 2

#### jjw

##### Member
The first picture says AK8963C 14 pin package The second picture, data can be in 14bit or 16bit format.

#### camerart

##### Active Member
The first picture says AK8963C 14 pin package The second picture, data can be in 14bit or 16bit format.
Hi J,
I'm sure it said BIT earlier C.

#### sagor1

##### Active Member
Hi S,
Like this?:

If y_c > 0 Then
If x_c > 0 Then
If x_c / y_c < 1 Then
deg = Arctan(x_c / y_c) * 57.3
Endif
Endif
Endif

C.
Yes, that is the correct logic to prevent dividing by zero accidentally.
Good luck with the rest...

#### camerart

##### Active Member
Hi,
I've 'corrected the OCTANT READ order 0-44DEG, thanks S.

The 14BIT choice doesn't work without changing something else. perhaps 2's compliment as J suggested.

First I'll finish the OCTANTs, but I'm stuck with the algebra. Would someone edit the attached TXT please? I've copied and pasted all 8x OCTANT fields, if that helps.

There's still something wrong, and the results are too varied. Once I have results form 360 DEG rotation, I'll re-look at the way the 2x XYZ BYTES are READ. There is a READY register, also a READY PIN, to look at.

C.

#### Attachments

• 1.2 KB Views: 5

#### jjw

##### Member
Haven't looked all the octants, but you can't directly compare x and y if they have different signs, should then compare their absolute values.

Simpler way for if x/y < 1 is if x<y

You have 4 cases for the signs of x, y:
++ 0-90 degrees
+- 90-180
-- 180-270
-+ 270-360

#### camerart

##### Active Member
Haven't looked all the octants, but you can't directly compare x and y if they have different signs, should then compare their absolute values.

Simpler way for if x/y < 1 is if x<y

You have 4 cases for the signs of x, y:
++ 0-90 degrees
+- 90-180
-- 180-270
-+ 270-360
Hi J,
I've just realised that we've had a similar conversation 4 years ago, also the ++ -- you refer to is where each signwave is on the graph, attached:

EDIT: On closer inspection, this doesn't appear true.

C.

#### Attachments

• 197.2 KB Views: 2

#### jjw

##### Member
I found an approximation of arctan, which works in entire quadrant. ( 0-90 degrees )
https://www.embedded.com/design/other/4216719/Performing-efficient-arctangent-approximation

"There are rational approximations that are defined in an entire quadrant. Although they are more complex, their advantage is that they not require to discriminate the octant.

This is for x in [0, Infinity). When x is negative just use -atan_approx(-x).
Second order:
atan_approx(x) = (Pi/2)*(b*x + x*x)/(1 + 2*b*x + x*x) where b = 0.596227, with a maximum approximation error of 0.1620º "

#### camerart

##### Active Member
Hi J,
This appears to be getting more complicated!

Can Oshonsoft deal with 'atan_approx' ? What is second order? where did 'b' come from?
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Here is a Honeywell equation:

To account for the tangent function being valid over 180° and not allowing the y=0 division calculation, the following equations can be used:
Azimuth (x=0, y<0) = 90.0
Azimuth (x=0, y>0) = 270.0
Azimuth (x<0) = 180 - [arcTan(y/x)]*180/¹
Azimuth (x>0, y<0) = - [arcTan(y/x)]*180/¹
Azimuth (x>0, y>0) = 360 - [arcTan(y/x)]*180/¹
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Am I correct that the choice of Octant or Quadrant is for accuracy? And whichever you use there is some error. There are ways of making them more accurate, but this is more complicated to do?

C.

#### jjw

##### Member
Hi J,
This appears to be getting more complicated!

Can Oshonsoft deal with 'atan_approx' ? What is second order? where did 'b' come from?
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Here is a Honeywell equation:

To account for the tangent function being valid over 180° and not allowing the y=0 division calculation, the following equations can be used:
Azimuth (x=0, y<0) = 90.0
Azimuth (x=0, y>0) = 270.0
Azimuth (x<0) = 180 - [arcTan(y/x)]*180/¹
Azimuth (x>0, y<0) = - [arcTan(y/x)]*180/¹
Azimuth (x>0, y>0) = 360 - [arcTan(y/x)]*180/¹
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Am I correct that the choice of Octant or Quadrant is for accuracy? And whichever you use there is some error. There are ways of making them more accurate, but this is more complicated to do?

C.
Value of b is in the message.
The second order is the order of the approximation.
Could be third, fourth etc. which are more complicated, but 0.16° accuracy is good enough.
Using quadrants is to make things easier, only checking the signs of x, y.
With octants you have to check also if x<y
Oshonsoft should handle the approximation. 