# Twos compliment in Oshonsoft and Compass calibration

#### camerart

##### Active Member
Hi J,
Here is the BIAS program:
Also the result:
but something's wrong.
C

#### Attachments

• 11.2 KB Views: 2
• 1.5 KB Views: 2

#### jjw

##### Member
This is wrong, look at msg #99
x_c = (m11 * x_0) + (m12 * x_0) + (m13 * x_0)
y_c = (m21 * y_0) + (m22 * y_0) + (m23 * y_0)
z_c = (m31 * z_0) + (m32 * z_0) + (m33 * z_0)

There are others, but correct this first.

#### jjw

##### Member
There are no values for m11 - m13, b_x, b_y, b_z

Take out Xnc= Xnc +1, Ync= Ync +1, Znc +1
It is wrong, when you have Xnc = Xnc - 65536

Last edited:

#### camerart

##### Active Member
This is wrong, look at msg #99
x_c = (m11 * x_0) + (m12 * x_0) + (m13 * x_0)
y_c = (m21 * y_0) + (m22 * y_0) + (m23 * y_0)
z_c = (m31 * z_0) + (m32 * z_0) + (m33 * z_0)

There are others, but correct this first.
Hi J,
Do you mean:
My use of underscores X_C instead of XC?
My use of brackets? (I tried without first then with)
In your message #99 you used X0, (number) and yo (letter) I chose number although I tried letter first.

Or was the error something else?
C.

#### jjw

##### Member
Underscores don't matter. You can use any variable names you want.
Look again #99. Calibrated values should be calculated like this

Xc = m11 * X0 + m12 * Y0 + m13 * Z0
Yc = m21 * X0 + m22 * Y0 + m23 * Z0
Zc = m31 * X0 + m32 * Y0 + m33 * Z0

Then you have not given any values for m11- m33 and bias values Bx By Bz
Try first to send the calibrated values to terminal and look if they are reasonable.

#### camerart

##### Active Member
Underscores don't matter. You can use any variable names you want.
Look again #99. Calibrated values should be calculated like this

Xc = m11 * X0 + m12 * Y0 + m13 * Z0
Yc = m21 * X0 + m22 * Y0 + m23 * Z0
Zc = m31 * X0 + m32 * Y0 + m33 * Z0

Then you have not given any values for m11- m33 and bias values Bx By Bz
Try first to send the calibrated values to terminal and look if they are reasonable.
Hi J,
I see! Hidden in plain sight.

Success!.
I also found a typo (y instead of an x)

Here is the latest program for Magmaster:
plus a Terminal READout:
plus 3x Magviewer views:

Thanks again, C.

#### Attachments

• 1.2 KB Views: 5
• 96.3 KB Views: 3
• 101.2 KB Views: 3
• 102.8 KB Views: 2
• 11.5 KB Views: 2

#### jjw

##### Member
Fine!
Could you test it now:
Start with y pointing to north and rotate 360 degrees clockwise, for example in 45 degrees steps.
Display x, y for each step.

#### camerart

##### Active Member
Fine!
Could you test it now:
Start with y pointing to north and rotate 360 degrees clockwise, for example in 45 degrees steps.
Display x, y for each step.
Hi J,
Here's a test, showing X,Y,Z READings in 45DEG steps on a terminal.
C.

#### Attachments

• 8.9 KB Views: 5
Last edited:

#### camerart

##### Active Member
Hi,
Does this look correct for the first OCTANT?

'0 - 45 degrees x / y < 1 And x > 0, y > 0
'angle = Arctan( x / y ) × 57.3

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

This gives double digit incorrect figures, with a max of 41.38 when Y is approx 0-90 DEG.
C.

#### sagor1

##### Active Member
Hi,
Does this look correct for the first OCTANT?

'0 - 45 degrees x / y < 1 And x > 0, y > 0
'angle = Arctan( x / y ) × 57.3

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

This gives double digit incorrect figures, with a max of 41.38 when Y is approx 0-90 DEG.
C.
You state in comments:
'angle = Arctan( x / y ) × 57.3
but your code has the x and y reversed:
deg = Arctan(y_c / x_c) * 57.3

Pick the right one?

#### camerart

##### Active Member
You state in comments:
'angle = Arctan( x / y ) × 57.3
but your code has the x and y reversed:
deg = Arctan(y_c / x_c) * 57.3

Pick the right one?
Hi S,
I switched x_c and y_c, now at 0Deg 11 shows, 22.5Deg shows 31 and 35DEg shows 40. 35-45 shows 41DEG.

NOTE: When I was 7, I was kept in after school, because I couldn't see the 'carry' when adding tens and units. 38+38 = 66. Even though I've since passed 'O' level maths and physics, it never went way

Thanks,
C

#### sagor1

##### Active Member
Can't help much more than looking at the basic math. Also, if Y happens to be exactly zero (0), your logic/formula will fail. One has to test for zero divisor first, before any other math where one divides by a variable.
Of course if Y=0, then your answer will be more limited as to solutions.

#### jjw

##### Member
Hi J,
Here's a test, showing X,Y,Z READings in 45DEG steps on a terminal.
C.
If you want to have the angle clockwise from north, the x values should be negated.
Then with the values from msg #108, 0 is about - 9 or 351 degrees.
45 ~ 35 degrees and 90 ~ 86 degrees.
There is quite a lot deviation between values with the same angle of the sensor.

#### sagor1

##### Active Member
Does any of this take into account magnetic north vs geographic north? That is, if pointing to geographic north, -9Deg (351 Deg) may make sense if that points to the magnetic pole. These external devices can only sense magnetic north.

#### camerart

##### Active Member
Does any of this take into account magnetic north vs geographic north? That is, if pointing to geographic north, -9Deg (351 Deg) may make sense if that points to the magnetic pole. These external devices can only sense magnetic north.
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.

Last edited:

#### camerart

##### Active Member
If you want to have the angle clockwise from north, the x values should be negated.
Then with the values from msg #108, 0 is about - 9 or 351 degrees.
45 ~ 35 degrees and 90 ~ 86 degrees.
There is quite a lot deviation between values with the same angle of the sensor.
Hi J,
How do I negate X values? Can this be done at the two's compliment stage?

I noticed the deviation in values, also when using MAGMASTER. I can perhaps average them out in some way.
I slowed down the number of READings/sec, also lowered the sensitivity from 16BIT to 14BIT, has this helped?
C.

#### Attachments

• 1 KB Views: 3
Last edited:

#### jjw

##### Member
Hi J,
How do I negate X values? Can this be done at the two's compliment stage?

I noticed the deviation in values, also when using MAGMASTER. I can perhaps average them out in some way.
I slowed down the number of READings/sec, also lowered the sensitivity from 16BIT to 14BIT, has this helped?
C.
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.

#### 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,
x_c = - x_c Doesn't compile in OSH BASIC, I'll come back to it later.

I think 14BIT only changes '1' BIT of a BYTE, so I'm guessing that 2's compliment isn't affected. Here's a bit of D/S:
Do you agree?

I tried rotating 360DEg, but something's not right, I'll re-try once the above has been checked.

C.

#### Attachments

• 115.1 KB Views: 2

#### jjw

##### Member
Hi J,
x_c = - x_c Doesn't compile in OSH BASIC, I'll come back to it later.

....

C.
Take away the space between - and Xc

#### Attachments

• 25 KB Views: 2