Hello, I think I figured it out. There were two things that were problematic: 1. BMM150 API returns the data in uT format, it does not return LSB, which is needed for the BSX library. This was solved by removing the division by 16 (since BMM150 resolution is 16 LSB/uT) in functions: compensate_x, compensate_y and compensate_z. static int16_t compensate_x(int16_t mag_data_x, uint16_t data_rhall, const struct bmm150_dev *dev)
{
...
retval = (retval + (((int16_t)dev->trim_data.dig_x1) * 8));// / 16;
...
}
static int16_t compensate_y(int16_t mag_data_y, uint16_t data_rhall, const struct bmm150_dev *dev)
{
...
retval = (retval + (((int16_t)dev->trim_data.dig_y1) * 8));// / 16;
...
}
static int16_t compensate_z(int16_t mag_data_z, uint16_t data_rhall, const struct bmm150_dev *dev)
{
...
//retval = retval / 16;
...
} This made the API return data in LSB format. 2. In BMM150 API, when performing the compensation for the z axis, unsigned and signed integers are compared. if (retval > BMM150_POSITIVE_SATURATION_Z) {
retval = BMM150_POSITIVE_SATURATION_Z;
} else ... Variable retval is of type int32_t, but BMM150_POSITIVE_SATURATION_Z is defined in bmm150_defs.h as UINT16_C(32767). I observed that the comparsion between retval = -916 and BMM150_POSITIVE_SATURATION_Z returns true, although it shouldn't. I believe this relates to comparing signed and unsigned integers. This was mitigated by defining BMM150_POSITIVE_SATURATION_Z as INT16_C(32767). Since the max value of INT16 is 32767, this should not be a problem. After this modification the former comparision between retval and BMM150_POSITIVE_SATURATION_Z returns false, as expected. Magnetometer calibration status now successfully reaches 3 and the offsets are non-zero. In addition, here's some information that might be useful: a) BMM150 magnetometer data is most easily fetched by using this function: bmm150_read_mag_data(&bmm150); where bmm150 is of type 'struct bmm150_dev'. b) After mag data fetch, the data (now in LSB values) is fed to the BSX library like this: libraryInput_ts.mag.data.x = bmm150.data.x;
libraryInput_ts.mag.data.y = bmm150.data.y;
libraryInput_ts.mag.data.z = bmm150.data.z; c) Here's a few samples of raw magnetometer data (fetched by the function mentioned in 'a') for testing purposes (the sensor was standing still): x: -124, y: -11, z: -984
x: -124, y: -11, z: -984
x: -124, y: -11, z: -984
x: -124, y: -11, z: -984
x: -148, y: -35, z: -927
x: -148, y: -35, z: -927
x: -148, y: -35, z: -927
x: -148, y: -35, z: -927
x: -130, y: -35, z: -896
x: -130, y: -35, z: -896
x: -130, y: -35, z: -896
x: -130, y: -35, z: -895
x: -130, y: -17, z: -921
x: -130, y: -17, z: -921
x: -130, y: -17, z: -921
x: -130, y: -17, z: -921
x: -142, y: -23, z: -921
x: -142, y: -23, z: -921
x: -142, y: -23, z: -921
x: -142, y: -23, z: -921
x: -118, y: -23, z: -921
x: -118, y: -23, z: -921 d) Here's a few samples of BSX Lib magnetometer data fetched by the function 'bsx_get_magcordata' for testing purposes (the sensor was standing still): x: -8.125000, y: -1.812500, z: -57.937500
x: -8.125000, y: -1.812500, z: -57.937500
x: -8.125000, y: -1.812500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -9.625000, y: -1.437500, z: -58.312500
x: -9.625000, y: -1.437500, z: -58.312500
x: -9.625000, y: -1.437500, z: -58.312500
x: -9.625000, y: -1.437500, z: -58.312500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.875000, y: -1.062500, z: -57.937500
x: -8.125000, y: -1.812500, z: -56.750000
x: -8.125000, y: -1.812500, z: -56.750000
x: -8.125000, y: -1.812500, z: -56.750000
x: -8.125000, y: -1.812500, z: -56.750000
x: -9.625000, y: -1.062500, z: -58.375000
x: -9.625000, y: -1.062500, z: -58.375000
x: -9.625000, y: -1.062500, z: -58.375000 Kind regards, Marko Njirjak
... View more