pi3g / bme68x-python-library

Python 3 Library for BME688 and BME680 (Bosch Sensortec sensors), supports Bosch BSEC
MIT License
44 stars 17 forks source link

Why is temp_offset an int8_t? #23

Open onlyblackstars opened 11 months ago

onlyblackstars commented 11 months ago

Question in title refers to ./bme68xmodule.c. Shouldn't temp_offset be a float rather than an int?

mcalisterkm commented 11 months ago

The temperature offset is part of the ambient temp calculation, and is to compensate for the impact of the internal heater in the BME688x. The compensated ambient temp is fed into the BSEC pressure and humidity calculations. As far as I can see it is an int in the C code as part of the bme68x_dev struct and later on deeper in the Bosch code it is cast to other types.

So here we are in the python module.

 int t_offs;
 …….
 …….
self->temp_offset = t_offs;
    self->bme.amb_temp = 25 - self->temp_offset;

You might also ask why 25?
My guess is that this is copied from the Arduino BSEC Integration examples.

Here we are in the temp compensation where is it cast to int32_t.

var1 = (((int32_t)dev->amb_temp * dev->calib.par_gh3) / 1000) * 256;

And here it is cast to a float for heater resistance

 var5 = (var4 + (var3 * (float)dev->amb_temp));

Bosch tend to use examples rather than documenting their API's, and as the core is closed source some of it is puzzling. You could ask on the Bosch Sensortec forum, and there are some questions in this area like this one:

Q: The driver uses bme680_dev::amb_temp and BSEC BSEC_INPUT_HEATSOURCE. Do both have to be set for correct compensation? Example: amb_temp = 22C and BSEC_INPUT_HEATSOURCE 5C because the PCB is 5C warmer

A: If you know clearly about the temperature offset coming from PCB self heating, then you can set this temperature offset as heat source as input when calling the lib to process the data. Then you just need to use temperature value read from sensor and heat source ( 2 input of lib). no need to set amb temperature into bsec lib.