Deadband helper & tests (#1267)

* deadband

* header

* comment
This commit is contained in:
Matthew Kennedy 2020-04-06 06:16:26 -07:00 committed by GitHub
parent 595df15dfd
commit 200bfe3f80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,29 @@
/**
* @file deadband.h
*
* @date April 6, 2020
* @author Matthew Kennedy, (c) 2020
*/
#pragma once
#include "efilib.h"
template <int TDeadband>
class Deadband {
public:
bool gt(float lhs, float rhs) {
// If we're within the deadband, be "sticky" with the previous value
float absError = absF(lhs - rhs);
// If there's enough error, actually compare values
if (absError > TDeadband) {
m_lastState = lhs > rhs;
}
return m_lastState;
}
private:
bool m_lastState =false;
};

View File

@ -0,0 +1,38 @@
#pragma once
#include <gtest/gtest.h>
#include "deadband.h"
TEST(Deadband, OutsideDeadband) {
Deadband<5> d;
EXPECT_TRUE(d.gt(100, 0));
EXPECT_FALSE(d.gt(0, 100));
}
TEST(Deadband, InsideDeadband) {
Deadband<5> d;
// stick the state to true
EXPECT_TRUE(d.gt(10, 0));
// Make sure it stays there while inside the deadband
EXPECT_TRUE(d.gt(0, 0));
EXPECT_TRUE(d.gt(0, 1));
EXPECT_TRUE(d.gt(0, 2));
EXPECT_TRUE(d.gt(0, 3));
EXPECT_TRUE(d.gt(0, 4));
EXPECT_TRUE(d.gt(0, 4.99));
// Flip the state - it should now stick false
EXPECT_FALSE(d.gt(0, 5.01));
// Make sure it stays there while inside the deadband
EXPECT_FALSE(d.gt(0, 4.99));
EXPECT_FALSE(d.gt(0, 1));
EXPECT_FALSE(d.gt(0, -1));
EXPECT_FALSE(d.gt(0, -1));
EXPECT_FALSE(d.gt(0, -4.99));
// Now it should flip back
EXPECT_TRUE(d.gt(0, -5.01));
}

View File

@ -36,6 +36,7 @@ TESTS_SRC_CPP = \
tests/test_tacho.cpp \
tests/test_gpiochip.cpp \
tests/test_multispark.cpp \
tests/test_deadband.cpp \
tests/sensor/basic_sensor.cpp \
tests/sensor/func_sensor.cpp \
tests/sensor/function_pointer_sensor.cpp \