2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2019-09-21 11:33:38 -07:00
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "linear_func.h"
|
2019-09-21 11:33:38 -07:00
|
|
|
|
2019-09-24 18:11:41 -07:00
|
|
|
void LinearFunc::configure(float in1, float out1, float in2, float out2, float minOutput, float maxOutput) {
|
2019-09-21 11:33:38 -07:00
|
|
|
m_minOutput = minOutput;
|
|
|
|
m_maxOutput = maxOutput;
|
|
|
|
|
2020-03-22 14:09:46 -07:00
|
|
|
in1 = in1 / m_divideInput;
|
|
|
|
in2 = in2 / m_divideInput;
|
|
|
|
|
2019-09-21 11:33:38 -07:00
|
|
|
m_a = INTERPOLATION_A(in1, out1, in2, out2);
|
|
|
|
m_b = out1 - m_a * in1;
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:11:41 -07:00
|
|
|
SensorResult LinearFunc::convert(float inputValue) const {
|
2019-09-21 11:33:38 -07:00
|
|
|
float result = m_a * inputValue + m_b;
|
|
|
|
|
2022-07-28 00:04:28 -07:00
|
|
|
// Bounds checks
|
|
|
|
// Flipped error codes in case of m_a < 0 so that they indicate whether the input
|
|
|
|
// voltage is high/low, instead of the output high/low
|
|
|
|
if (result > m_maxOutput) {
|
|
|
|
return m_a > 0 ? UnexpectedCode::High : UnexpectedCode::Low;
|
|
|
|
}
|
2019-09-21 11:33:38 -07:00
|
|
|
|
2022-07-28 00:04:28 -07:00
|
|
|
if (result < m_minOutput) {
|
|
|
|
return m_a > 0 ? UnexpectedCode::Low : UnexpectedCode::High;
|
2020-04-19 05:37:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2019-09-21 11:33:38 -07:00
|
|
|
}
|