From 9ccda6bbe7fa15964d1736c77106365d7060936b Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 6 Dec 2020 13:55:06 -0600 Subject: [PATCH] add isRedundant (#2031) Co-authored-by: Matthew Kennedy --- .../controllers/sensors/redundant_sensor.h | 5 +++++ firmware/controllers/sensors/sensor.cpp | 20 +++++++++++++++++-- firmware/controllers/sensors/sensor.h | 13 ++++++++++++ unit_tests/tests/sensor/redundant.cpp | 4 ++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/firmware/controllers/sensors/redundant_sensor.h b/firmware/controllers/sensors/redundant_sensor.h index 2b2f03710b..fa3b052564 100644 --- a/firmware/controllers/sensors/redundant_sensor.h +++ b/firmware/controllers/sensors/redundant_sensor.h @@ -15,6 +15,11 @@ public: SensorResult get() const override; + bool isRedundant() const override { + // This sensor is redundant when not ignoring the second channel + return !m_ignoreSecond; + } + void showInfo(Logging* logger, const char* sensorName) const override; private: diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index ed6cdf4eb9..7c6597bce2 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -118,14 +118,24 @@ public: float getRaw() const { const auto sensor = m_sensor; - if (m_sensor) { - return m_sensor->getRaw(); + if (sensor) { + return sensor->getRaw(); } // We've exhausted all valid ways to return something - sensor not found. return 0; } + bool isRedundant() const { + const auto sensor = m_sensor; + + if (sensor) { + return sensor->isRedundant(); + } + + return false; + } + private: bool m_useMock = false; float m_mockValue; @@ -181,6 +191,12 @@ bool Sensor::Register() { return entry ? entry->getRaw() : 0; } +/*static*/ bool Sensor::isRedundant(SensorType type) { + const auto entry = getEntryForType(type); + + return entry ? entry->isRedundant() : false; +} + /*static*/ bool Sensor::hasSensor(SensorType type) { const auto entry = getEntryForType(type); diff --git a/firmware/controllers/sensors/sensor.h b/firmware/controllers/sensors/sensor.h index 19e10b8504..ddaac940f2 100644 --- a/firmware/controllers/sensors/sensor.h +++ b/firmware/controllers/sensors/sensor.h @@ -92,6 +92,11 @@ public: */ static float getRaw(SensorType type); + /* + * Get whether a sensor is redundant (a composite of multiple other sensors that can check consistency between them) + */ + static bool isRedundant(SensorType type); + /* * Query whether there is a sensor of a particular type currently registered. */ @@ -137,6 +142,14 @@ public: return 0; } + /* + * Get whether this sensor is redundant (backed by multiple other sensors) + */ + virtual bool isRedundant() const { + // By default sensors are not redundant + return false; + } + protected: // Protected constructor - only subclasses call this explicit Sensor(SensorType type) diff --git a/unit_tests/tests/sensor/redundant.cpp b/unit_tests/tests/sensor/redundant.cpp index 0d9203cfb7..eb2ddbf24b 100644 --- a/unit_tests/tests/sensor/redundant.cpp +++ b/unit_tests/tests/sensor/redundant.cpp @@ -71,6 +71,8 @@ TEST_F(SensorRedundant, SetTwoSensors) auto result = dut.get(); EXPECT_TRUE(result.Valid); EXPECT_FLOAT_EQ(result.Value, 25.0f); + + EXPECT_TRUE(dut.isRedundant()); } } @@ -176,6 +178,8 @@ TEST_F(SensorRedundantIgnoreSecond, OnlyFirst) auto result = dut.get(); EXPECT_TRUE(result.Valid); EXPECT_FLOAT_EQ(result.Value, 44.0f); + + EXPECT_FALSE(dut.isRedundant()); } }