From bbd82851908a07e5c48f8317c0a8349d3290d032 Mon Sep 17 00:00:00 2001 From: Kajetan Rzepecki Date: Sun, 24 Dec 2023 00:52:46 +0100 Subject: [PATCH] Added more optional factors to the ADC config for cube fitting. --- device/src/sensors/ADC.cpp | 16 +++++++++++++++- device/src/sensors/ADC.hpp | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/device/src/sensors/ADC.cpp b/device/src/sensors/ADC.cpp index c37ca8e..5b856da 100644 --- a/device/src/sensors/ADC.cpp +++ b/device/src/sensors/ADC.cpp @@ -12,12 +12,26 @@ ADCChannel::ADCChannel(JSONVar &config, Reading *s, Reading *r): this->min = (double) config["min"]; this->offset = (double) config["offset"]; this->factor = (double) config["factor"]; + if (config["factorSquared"] == undefined) { + this->factorSquared = 0; + } else { + this->factorSquared = (double) config["factorSquared"]; + } + if (config["factorCubed"] == undefined) { + this->factorCubed = 0; + } else { + this->factorCubed = (double) config["factorCubed"]; + } this->baseline = 0; } void ADCChannel::add(uint16_t raw, float volts) { if (this->scaled != nullptr) { - float result = this->factor * volts + this->offset + this->baseline; + float result = this->factorCubed * volts * volts * volts + + this->factorSquared * volts * volts + + this->factor * volts + + this->offset + + this->baseline; if (result < this->min) { this->baseline += this->min - result; diff --git a/device/src/sensors/ADC.hpp b/device/src/sensors/ADC.hpp index 395d0ad..c430fde 100644 --- a/device/src/sensors/ADC.hpp +++ b/device/src/sensors/ADC.hpp @@ -17,6 +17,8 @@ struct ADCChannel { float min; float offset; float factor; + float factorSquared; + float factorCubed; float baseline; Reading *scaled;