MPU-9255 with AK8963C looking for data comparison

Thread Starter

misha666

Joined Feb 6, 2020
3
" Hi Guys,
firstly I want to say Hello to everybody and thanks for adding me to the group :)
I am working on electronic compass project, my HW are: Arduino Uno, MPU-9255, and 1.8 inch LCD module from Waveshare.
MPU is conected by I2C, LCD by HW_SPI. Everything is working fine but...
My general problem is with raw data readings from magnetometer AK8963C. It connects, reads and show me results but this data are strange. Range of raw data should be from -32760 to 32760. And in some cases readings are almost good in some not.
1. Q
Is my byte shifting for mx, my, mz(those variables are ints) good? -> int16_t mx = ((xl << 8) | xh );
Because in this kind of shiftings where low byte goes to high position in int values of mx/my/mz are in range..almost.
If I change it to int16_t mx = ((xh << 8) | xl ); which most of libraries on github have, the values of mx are not in range they are from -150 to +31 with two times 0. And this is strange.
For checking why I barely read values from xl/yl/zl and xh/yh/zh and those values were:
xl, yl, yz - range from 0 to 255 like should have unsigned 8 bit int, while
xh- has only two values 0 or 255, yh- 0 or 1 and zh - 0 or 255 and what I am thinking, they only change the sign of value, especially that it happens when rotating for about 180 degrees in every axis.
2. Q
Can anybody check those values in his/hers sensor and we can compare? Maybe my MPU is broken or something else. Maybe I am doing something wrong? If You need more code please tell. I am attaching sensor datasheet. All other sensors are switched off(gyroscope and accelerometr).

Here is a sample of code from my library which is change fragment from somebody`s lib. I know that it is not very good but firstly i want to get good readings from registers than make good code. "

Code:
C:
uint8_t MPU9255::read_mag()
{ 
  byte drdy_dor = read(MAG_address, 0x02);
  dor_status = drdy_dor;
  if(drdy_dor==1 || drdy_dor==3)
  {
     byte xl = read(MAG_address, 0x03);//reading from mag registers from 0x03-0x08
     byte xh = read(MAG_address, 0x04);
     byte yl = read(MAG_address, 0x05);
     byte yh = read(MAG_address, 0x06);
     byte zl = read(MAG_address, 0x07);
     byte zh = read(MAG_address, 0x08);
     byte hofl = read(MAG_address, 0x09);//has to be read to acknowledge status reg that can continue Measurements
     hofl_status = hofl; //overflow status
     if(hofl!=1)
    {
      int16_t mx = ((xl << 8) | xh );
      int16_t my = ((yl << 8) | yh );
      int16_t mz = ((zl << 8) | zh );
      return 0;
     } return 1;
  } return 2;  
  
}
 

Attachments

mckenney

Joined Nov 10, 2018
125
If I change it to int16_t mx = ((xh << 8) | xl );
This is the form you want. xh and xl should be unsigned, else sign propagation will make a mess. (Your library code uses "byte", which is traditionally unsigned.)
xh- has only two values 0 or 255
A signed value which is jittering between -1 and 1 will show bit (unsigned) values alternating 0xFFFF to 0x0001, so don't read too much into the bit representations.
 

Thread Starter

misha666

Joined Feb 6, 2020
3
A signed value which is jittering between -1 and 1 will show bit (unsigned) values alternating 0xFFFF to 0x0001, so don't read too much into the bit representations.
Thanks for reply,
but please look into the attachment where is a part of datasheet. Values in mx/my/mz should be in range from -32760 to 32760 after converting, two onebyte of data read from register, to one 16 bit variable(for example mx). My readings from registers after converting with this scheme int16_t mx = ((xh << 8) | xl ) are in range from ca. -150 to 31, and those values are not even good. I have checked my readings using 3 different libraries and all the time they were almost the same. So again if Anybody could check Yours readings and tell how it looks like I will appreciate. I am considering if maybe the sensor is broken....but when I`ve done self control test it has good readings....
 

Attachments

mckenney

Joined Nov 10, 2018
125
How do you know that [-150,+31] is incorrect? I think 1.5 Gauss is in the right order of magnitude for the Earth's magnetic field. Did you try holding a magnet nearby?

I don't have your equipment, so I have to defer to others there.
 

Thread Starter

misha666

Joined Feb 6, 2020
3
Maybe You are right,
I have just misinterpreted readings....I was thinking that max value should be on north or south and when turning around the magnetometer, value should be lesser or greater....Thank You! I will check this out. If You are right now i need to make callibration of compass and it will be working :)
 
Top