STMicroelectronics / lps22hb-pid

lps22hb platform independent driver based on Standard C language and compliant with MISRA standard
BSD 3-Clause "New" or "Revised" License
3 stars 2 forks source link

Wrong (?) conversion factor from lsb to hpa #1

Closed andrea-nistico closed 2 years ago

andrea-nistico commented 3 years ago

I have been using this driver for a bit now, in some older version, the conversion between lsband hpa was the following:

float_t lps22hb_from_lsb_to_hpa(int32_t lsb)
{
    return ((float_t)lsb / 4096.0f);
}

In the last release we have:

float_t lps22hb_from_lsb_to_hpa(int32_t lsb)
{
    return ((float_t)lsb / 1048576.0f);
}

with the first scale factor, I get correct values but with the second the results are off. Where the ''1048576.0f'' comes from?

albezanc commented 2 years ago

Hi andrea,

In the latest version of the driver the Press_raw_get function exposes the data in uint32_t format left-aligned (not 24 bit), so an additional 8-bit shift is required: 4096.0f * 256 = 1048576.0f.

thanks for the opportunity for clarification

andrea-nistico commented 2 years ago

thanks for the answer, I solved with the following given your considerations:

typedef union {
    struct {
        uint32_t pressure32bit; /* pressure plus status register */
        int16_t temperature16bit;  /* temperature */
    } p_and_t;
    uint8_t u8bit[FIFO_DATA_SAMPLE_LEN];
} p_and_t_byte_t;

void hub_fifo_baro_axis_read(float_t *axis) {
    static p_and_t_byte_t axisRaw;
    lsm6dsox_fifo_out_raw_get(hub_ctx_ptr, axisRaw.u8bit);
    axisRaw.p_and_t.pressure32bit <<= 8; // left byte shift for correct value 
    *axis = lps22hb_from_lsb_to_hpa(axisRaw.p_and_t.pressure32bit);
}

I haven't noticed the change since I am using fifo_raw_get with sensor hub which is different, thanks! Maybe it is worth mentioning this in the sensor hub examples?