rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
redundant_sensor.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "redundant_sensor.h"
4
6 : Sensor(outputType)
7 , m_first(first)
8 , m_second(second)
9{
10}
11
12void RedundantSensor::configure(float /*split threshold*/maxDifference, bool ignoreSecondSensor) {
13 m_maxDifference = maxDifference;
14 m_ignoreSecond = ignoreSecondSensor;
15}
16
18 auto sensor1 = Sensor::get(m_first);
19
20 // If we're set to disable redundancy, just pass thru the first sensor
21 if (m_ignoreSecond) {
22 return sensor1;
23 }
24
25 auto sensor2 = Sensor::get(m_second);
26
27 // If either result is invalid, return invalid.
28 if (!sensor1.Valid || !sensor2.Valid) {
29 return UnexpectedCode::Inconsistent;
30 }
31
32 // If both are valid, check that they're near one another
33 float delta = absF(sensor1.Value - sensor2.Value);
34 if (delta > m_maxDifference) {
35 return UnexpectedCode::Inconsistent;
36 }
37
38 // Both sensors are valid, and their readings are close. All is well.
39 // Return the average
40 return (sensor1.Value + sensor2.Value) / 2;
41}
const SensorType m_first
void configure(float maxDifference, bool ignoreSecondSensor)
SensorResult get() const override
const SensorType m_second
RedundantSensor(SensorType outputType, SensorType firstSensor, SensorType secondSensor)
virtual SensorResult get() const =0
expected< float > SensorResult
Definition sensor.h:46
SensorType
Definition sensor_type.h:18