cmicrocontrollermpu6050

Need explanation for InvenSenses Motion Driver


I'm working on a project using InvenSense's Motion Driver API to read values from the MPU-6050. As a start I'm trying to get the temperature.

Reading the register documentation there is the following sentence.

The temperature in degrees C for a given register value may be computed as: Temperature in degrees C = (TEMP_OUT Register Value as a signed quantity)/340 + 36.53

In the source code of this API there is the following function

int mpu_get_temperature(long *data, unsigned long *timestamp)
{
    unsigned char tmp[2];
    short raw;

    if (!(st.chip_cfg.sensors))
        return -1;

    if (i2c_read(st.hw->addr, st.reg->temp, 2, tmp))
        return -1;
    raw = (tmp[0] << 8) | tmp[1];
    if (timestamp)
        get_ms(timestamp);

    data[0] = (long)((35 + ((raw - (float)st.hw->temp_offset) / st.hw->temp_sens)) * 65536L);
    return 0;
}

When I search the internet I usually come across this snippet which seems to work

uint8_t buf[2];
mpu_read_reg(0x41, &buf[0]);
mpu_read_reg(0x42, &buf[1]);
uint16_t raw = (((uint16_t) buf[0]) << 8) | buf[1];
float temperature = raw / 340.0f + 36.53f;

Can somebody please explain the line (long)((35 + ((raw - (float)st.hw->temp_offset) / st.hw->temp_sens)) * 65536L) to me? Is this some sort of type conversion or am I getting something wrong?


Solution

  • This is actually the same.

    1. 65536 is convertion to Q-format
    2. Both documentation and source say: .temp_sens = 340, .temp_offset = -521
    3. Now we have: 35 + (R - -521) / 340 == 35 + R / 340 + 521 / 340 == 35 + 1.53 + R/340