test lambda monitor response #75

This commit is contained in:
Matthew Kennedy 2023-07-26 00:35:28 -07:00 committed by rusefillc
parent 4219925282
commit 858a13264b
3 changed files with 69 additions and 4 deletions

View File

@ -12,6 +12,10 @@ float LambdaMonitor::getMaxAllowedLambda(float rpm, float load) const {
);
}
float LambdaMonitor::getTimeout() const {
return engineConfiguration->lambdaProtectionTimeout;
}
bool LambdaMonitorBase::isCut() const {
return lambdaMonitorCut;
}
@ -25,7 +29,7 @@ void LambdaMonitorBase::update(float rpm, float load) {
lambdaTimeSinceGood = m_timeSinceGoodLambda.getElapsedSeconds();
if (m_timeSinceGoodLambda.hasElapsedSec(engineConfiguration->lambdaProtectionTimeout)) {
if (m_timeSinceGoodLambda.hasElapsedSec(getTimeout())) {
// Things have been bad long enough, cut!
lambdaMonitorCut = true;
}

View File

@ -12,12 +12,14 @@ protected:
// Returns false if the current lambda reading is leaner than allowable.
// Returns true in any other case (rich enough, bad sensor, recent fuel cut, rpm to low, load too low, etc)
bool isCurrentlyGood(float rpm, float load) const;
virtual bool isCurrentlyGood(float rpm, float load) const;
virtual float getMaxAllowedLambda(float rpm, float load) const = 0;
virtual float getTimeout() const = 0;
// Determine whether fuel should be restored after a cut occurs
// Returns true if OK to leave the "cut" state
bool restoreConditionsMet(float rpm, float load) const;
virtual bool restoreConditionsMet(float rpm, float load) const;
private:
Timer m_timeSinceGoodLambda;
@ -25,4 +27,5 @@ private:
class LambdaMonitor : public LambdaMonitorBase {
float getMaxAllowedLambda(float rpm, float load) const override;
float getTimeout() const override;
};

View File

@ -1,5 +1,63 @@
#include "pch.h"
TEST(LambdaMonitor, x) {
struct MockLambdaMonitor : public LambdaMonitorBase {
bool isGood = true;
bool isCurrentlyGood(float /*rpm*/, float /*load*/) const override {
return isGood;
}
bool isRestore = false;
bool restoreConditionsMet(float /*rpm*/, float /*load*/) const override {
return isRestore;
}
float getTimeout() const override {
// Timeout of 1 second
return 1;
}
MOCK_METHOD(float, getMaxAllowedLambda, (float rpm, float load), (const, override));
};
TEST(LambdaMonitor, Response) {
MockLambdaMonitor mlm;
extern int timeNowUs;
int startTime = 1e6;
timeNowUs = startTime;
mlm.isGood = true;
mlm.isRestore = false;
mlm.update(2000, 50);
EXPECT_TRUE(mlm.lambdaCurrentlyGood);
EXPECT_FALSE(mlm.isCut());
// now lambda will be bad, but we don't cut yet
mlm.isGood = false;
mlm.update(2000, 50);
EXPECT_FALSE(mlm.lambdaCurrentlyGood);
EXPECT_FALSE(mlm.isCut());
// 0.9 second later, still not cut
timeNowUs = startTime + 0.9e6;
mlm.update(2000, 50);
EXPECT_FALSE(mlm.lambdaCurrentlyGood);
EXPECT_FALSE(mlm.isCut());
// 1.1 second later, cut!
timeNowUs = startTime + 1.1e6;
mlm.update(2000, 50);
EXPECT_FALSE(mlm.lambdaCurrentlyGood);
EXPECT_TRUE(mlm.isCut());
// Lambda goes back to normal, but restore conditions not met so we should stay cut
mlm.isGood = true;
mlm.update(2000, 50);
EXPECT_TRUE(mlm.lambdaCurrentlyGood);
EXPECT_TRUE(mlm.isCut());
mlm.isRestore = true;
mlm.update(2000, 50);
EXPECT_TRUE(mlm.lambdaCurrentlyGood);
EXPECT_FALSE(mlm.isCut());
}