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.

Averaging READings BYTEs etc

Status
Not open for further replies.

camerart

Well-Known Member
Hi,
I'm using a compass calibation application (Magmaster) I'ts working ok, but the READings are a bit wild.
Is there a way to average READings, Note, The READings are in BYTEs which are 2's compliment.


Cheers. Camerart.
 
If some of the reading are negative then add the positive ones and subtract the negative ones or just add them all as the negative ones will be subtracted anyway. You may have to cast the bytes to ints so overflow isn't a problem.

Mike.
 
If some of the reading are negative then add the positive ones and subtract the negative ones or just add them all as the negative ones will be subtracted anyway. You may have to cast the bytes to ints so overflow isn't a problem.

Mike.
Hi M,
Am I correct, that: READ, X_LB multiple times 'say' '10', add the positive ones into an Average, Add the negative ones into another Average, then divide them by '10' and do the same with the negative ones, then 2's compliment them?
C
 
Cast the signed bytes to signed ints, add 10 together and divide by 10. Assuming the bytes are signed then all should just work. The link you posted above doesn't appear to use bytes at all.

Mike.
 
Cast the signed bytes to signed ints, add 10 together and divide by 10. Assuming the bytes are signed then all should just work. The link you posted above doesn't appear to use bytes at all.

Mike.
Hi M,
I'll give it a try, thanks. I will have to figure out what cast means? I imagine it means change the BYTEs to INTEGERS.

Sorry! the link in #1 takes you to a post where the program with the BYTES is, at #7. I hope that's not too convoluted.
Thanks, C.
 
Hi,
I'm using a compass calibation application (Magmaster) I'ts working ok, but the READings are a bit wild.
Is there a way to average READings, Note, The READings are in BYTEs which are 2's compliment.


Cheers. Camerart.
If you mean the compass readings, they are 16bit words in 2's complement.
Average the words, but because Oshonsoft does not use signed integers, convert them to floating point and average them.
This conversion has been in your program from the beginning.
 
Didn't realize Oshonsoft doesn't have signed integers. An alternative is to negate negative numbers and subtract them from the (fp?) total. Can the result end up negative?

Mike.
 
Didn't realize Oshonsoft doesn't have signed integers. An alternative is to negate negative numbers and subtract them from the (fp?) total. Can the result end up negative?

Mike.
Hi M,
After years of doing this, each time I try something different is like I had just started programming, and 'signed integers' and positive negative numbers, I'm re-learning.
C.
 
If you mean the compass readings, they are 16bit words in 2's complement.
Average the words, but because Oshonsoft does not use signed integers, convert them to floating point and average them.
This conversion has been in your program from the beginning.
Hi J,
The MAGMASTER program is working fine, but at pressing each button to INPUT each READing, I notice that each READings is very varied.
What I wanted to do, at the moment only for the MAGMASTER BIAS calibrating program, is take many 'say' 10 READings and average them, so that when I press the button they are smoothed out.
So far I'm trying to READ each BYTE as they are generated by the COMPASS so that they can be averaged, before 2s complimenting them, for MAGMASTER.
C
 
Hi J,
The MAGMASTER program is working fine, but at pressing each button to INPUT each READing, I notice that each READings is very varied.
What I wanted to do, at the moment only for the MAGMASTER BIAS calibrating program, is take many 'say' 10 READings and average them, so that when I press the button they are smoothed out.
So far I'm trying to READ each BYTE as they are generated by the COMPASS so that they can be averaged, before 2s complimenting them, for MAGMASTER.
C
If you average the bytes separately (high byte, low byte), you get wrong values.
 
Hi,
I got it to READ all 6xBYTEs shon here:
_____________________________________________________
142, 255, 194, 255, 234, 254
142, 255, 188, 255, 238, 254
143, 255, 191, 255, 243, 254
143, 255, 189, 255, 247, 254
141, 255, 187, 255, 235, 254
135, 255, 185, 255, 243, 254
142, 255, 184, 255, 240, 254
139, 255, 193, 255, 243, 254
136, 255, 190, 255, 236, 254
137, 255, 189, 255, 243, 254
136, 255, 180, 255, 234, 254
146, 255, 190, 255, 240, 254
136, 255, 192, 255, 244, 254
142, 255, 184, 255, 244, 254
134, 255, 186, 255, 230, 254
142, 255, 182, 255, 236, 254
146, 255, 190, 255, 246, 254
143, 255, 185, 255, 229, 254
134, 255, 190, 255, 236, 254
141, 255, 187, 255, 239, 254
137, 255, 179, 255, 245, 254
140, 255, 186, 255, 228, 254
147, 255, 189, 255, 235, 254
139, 255, 185, 255, 237, 254
135, 255, 189, 255, 243, 254
144, 255, 184, 255, 238, 254
147, 255, 191, 255, 233, 254
135, 255, 187, 255, 231, 254
144, 255, 190, 255, 238, 254
142, 255, 188, 255, 238, 254
135, 255, 185, 255, 235, 254
146, 255, 194, 255, 234, 254
140, 255, 184, 255, 234, 254
136, 255, 182, 255, 242, 254
135, 255, 183, 255, 235, 254
137, 255, 181, 255, 239, 254
134, 255, 184, 255, 236, 254
139, 255, 185, 255, 239, 254
148, 255, 188, 255, 236, 254
140, 255, 186, 255, 244, 254
138, 255, 188, 255, 240, 254
134, 255, 188, 255, 238, 254
_____________________________________________________________________________

As can be seen, the HBs are either 0 or 255.
There are occasional 254, which if I, not mistaken shouldn't be so. If they are incorrect, then I recall one of the settings in the AK8963C DATA sheet, shows how to cetralise the READing, so this doean't happen??, but not sure.
C
 
Last edited:
If you average the bytes separately (high byte, low byte), you get wrong values.
Hi J,
My thoughts are: that if I'm correct, then the 255 READings show a negative BYTE. If I note this, can I average the LB, noting whether it's HB is 0 or 255, and act accordingly, whatever that is?

EDIT: Sensitivity change Image added
C
 

Attachments

  • Sensitivity.jpg
    Sensitivity.jpg
    195.5 KB · Views: 250
Last edited:
Hi J,
My thoughts are: that if I'm correct, then the 255 READings show a negative BYTE. If I note this, can I average the LB, noting whether it's HB is 0 or 255, and act accordingly, whatever that is?

EDIT: Sensitivity change Image added
C
I don't think so.
If the high byte is zero, then it is ok.
The average must be a word, because summing the values may exceed 255.
When the high byte changes between 0 and 255 you have anyway to make the 2's complement conversion.

The Y values seems to be faulty, if the compass was in the same position between the readings.
They change between about 0, 98 and 255,112
or 98 and -144 in decimal.
 
I don't think so.
If the high byte is zero, then it is ok.
The average must be a word, because summing the values may exceed 255.
When the high byte changes between 0 and 255 you have anyway to make the 2's complement conversion.

The Y values seems to be faulty, if the compass was in the same position between the readings.
They change between about 0, 98 and 255,112
or 98 and -144 in decimal.
Hi J,

So it's not possible to average the BYTEs at birth, but they must be 2's complimented?

I re-tested, making sure it stayed still, and updated #11. These look better, I think.
C.
 
If your total is a float then you can add any values with bit 15 clear (convert to 16 bit values first). Values with bit 15 set need to be negated (compliment and add 1) and subtracted from the total.

Mike.
 
Hi J,

So it's not possible to average the BYTEs at birth, but they must be 2's complimented?

I re-tested, making sure it stayed still, and updated #11. These look better, I think.
C.
You don't 2's complement the bytes, but the word they belong to ( high byte, low byte)
What did you do differently in #11, when the y values are now almost the same, no positive values?
 
In post #11, all values are negative. You asked "what did you do differently", what are you comparing to?

Mike.
 
In post #11, all values are negative. You asked "what did you do differently", what are you comparing to?

Mike.
He edited them.
About half of the Y values where positive, around 90, now all of them are negative.

Edit: or changed all of them.
 
Last edited:
One question to consider - what values does the compass put out? +/-180 makes sense, so a change from +179 to -179 is actually a small change in measured value.
 
One question to consider - what values does the compass put out? +/-180 makes sense, so a change from +179 to -179 is actually a small change in measured value.
It puts out the x,y,z components of the Earth's magnetic field.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top