[TLV493D] Method to calibrate temperature offset

pull/9/head
Benjamin Bezine 5 years ago committed by bezineb5
parent 7cccb95509
commit 23229f3d33

@ -30,6 +30,9 @@ const (
HighPrecisionWithTemperature Precision = 0 HighPrecisionWithTemperature Precision = 0
// LowPrecision reads only 8-bits for each axis. Temperature is not read. // LowPrecision reads only 8-bits for each axis. Temperature is not read.
LowPrecision Precision = 1 LowPrecision Precision = 1
// DefaultTemperatureOffsetCompensation is the temperature offset compensation used by default, following the documentation advices.
// For accurate results, please calibrate it using CalibrateTemperatureOffsetCompensation method.
DefaultTemperatureOffsetCompensation = 340
) )
const ( const (
@ -137,7 +140,7 @@ var DefaultOpts = Opts{
EnableTemperatureMeasurement: true, EnableTemperatureMeasurement: true,
InterruptPadEnabled: false, InterruptPadEnabled: false,
ParityTestEnabled: true, ParityTestEnabled: true,
TemperatureOffsetCompensation: 340, // As per the documentation, can be calibrated for better precision TemperatureOffsetCompensation: DefaultTemperatureOffsetCompensation, // As per the documentation, can be calibrated for better precision
} }
// Sample contains the metrics measured by the sensor // Sample contains the metrics measured by the sensor
@ -422,6 +425,18 @@ func (d *Dev) StopContinousRead() {
d.continuousReadWG.Wait() d.continuousReadWG.Wait()
} }
// CalibrateTemperatureOffsetCompensation computes the temperature offset compensation based upon a reading from the sensor compared to the actual temperature.
// The returned value must be stored and passed in Opts.TemperatureOffsetCompensation in future uses of TLV493D driver.
// See TLV493D user manual, "3.2 Calculation of the temperature" for more information
func CalibrateTemperatureOffsetCompensation(temperatureOffsetCompensation int, measuredTemperature physic.Temperature, actualTemperature physic.Temperature) int {
// Compute measurement based upon reference documentation
rawTemp := int((measuredTemperature-referenceTemperature)/temperatureScaling) + temperatureOffsetCompensation
// Compute the new offset by matching the raw measurement between the measure and the actual value
newOffset := rawTemp - int((actualTemperature-referenceTemperature)/temperatureScaling)
return newOffset
}
func bestModeForFrequency(frequency physic.Frequency) (Mode, error) { func bestModeForFrequency(frequency physic.Frequency) (Mode, error) {
allowed := []*Mode{&FastMode, &LowPowerMode, &UltraLowPowerMode} allowed := []*Mode{&FastMode, &LowPowerMode, &UltraLowPowerMode}

@ -326,6 +326,17 @@ func TestTLV493D_Configuration(t *testing.T) {
} }
} }
func Test_TLV493D_TemperatureCalibration(t *testing.T) {
measure := 25*physic.Celsius + physic.ZeroCelsius
actual := 19*physic.Celsius + physic.ZeroCelsius
offset := CalibrateTemperatureOffsetCompensation(DefaultTemperatureOffsetCompensation, measure, actual)
expectedOffset := 345
if offset != expectedOffset {
t.Fatalf("Temperature offset: Found %d, expected %d", offset, expectedOffset)
}
}
func assertSample(t *testing.T, expected Sample, actual Sample) { func assertSample(t *testing.T, expected Sample, actual Sample) {
if actual.Bx != expected.Bx { if actual.Bx != expected.Bx {
t.Fatalf("Bx: Found %d, expected %d", actual.Bx, expected.Bx) t.Fatalf("Bx: Found %d, expected %d", actual.Bx, expected.Bx)

Loading…
Cancel
Save