rusefi/firmware/controllers/sensors/redundant_sensor.cpp

42 lines
1.1 KiB
C++
Raw Normal View History

#include "pch.h"
#include "redundant_sensor.h"
RedundantSensor::RedundantSensor(SensorType outputType, SensorType first, SensorType second)
: Sensor(outputType)
, m_first(first)
, m_second(second)
{
}
2023-10-30 19:13:29 -07:00
void RedundantSensor::configure(float /*split threshold*/maxDifference, bool ignoreSecondSensor) {
m_maxDifference = maxDifference;
m_ignoreSecond = ignoreSecondSensor;
}
SensorResult RedundantSensor::get() const {
2024-06-03 11:34:37 -07:00
auto sensor1 = Sensor::get(m_first);
// If we're set to disable redundancy, just pass thru the first sensor
if (m_ignoreSecond) {
2024-06-03 11:34:37 -07:00
return sensor1;
}
2024-06-03 11:34:37 -07:00
auto sensor2 = Sensor::get(m_second);
// If either result is invalid, return invalid.
2024-06-03 11:34:37 -07:00
if (!sensor1.Valid || !sensor2.Valid) {
return UnexpectedCode::Inconsistent;
}
// If both are valid, check that they're near one another
2024-06-03 11:34:37 -07:00
float delta = absF(sensor1.Value - sensor2.Value);
if (delta > m_maxDifference) {
return UnexpectedCode::Inconsistent;
}
// Both sensors are valid, and their readings are close. All is well.
// Return the average
2024-06-03 11:34:37 -07:00
return (sensor1.Value + sensor2.Value) / 2;
}