Skip to content

Commit

Permalink
Merge pull request #116 from SpartanWorks/adc-cube-fit
Browse files Browse the repository at this point in the history
Added more optional factors to the ADC config for cube fitting.
  • Loading branch information
Idorobots authored Dec 24, 2023
2 parents 99db0db + bbd8285 commit 7582d8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion device/src/sensors/ADC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@ ADCChannel::ADCChannel(JSONVar &config, Reading<float> *s, Reading<float> *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;
Expand Down
2 changes: 2 additions & 0 deletions device/src/sensors/ADC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct ADCChannel {
float min;
float offset;
float factor;
float factorSquared;
float factorCubed;
float baseline;

Reading<float> *scaled;
Expand Down

0 comments on commit 7582d8a

Please sign in to comment.