From 8b66bdc86b2841636e851e18712e12b1d54f0c71 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 19 Jan 2019 09:40:39 -0500 Subject: [PATCH] #35 unit tests --- firmware/hw_layer/HIP9011.cpp | 7 ++----- firmware/hw_layer/HIP9011_logic.cpp | 3 ++- firmware/hw_layer/HIP9011_logic.h | 15 +++++++++++---- unit_tests/tests/test_hip9011.cpp | 16 ++++++++-------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index e5cf6cb40a..f69ccfe472 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -299,10 +299,7 @@ void hipAdcCallback(adcsample_t adcValue) { hipValueMax = maxF(engine->knockVolts, hipValueMax); engine->knockLogic(engine->knockVolts); - float angleWindowWidth = - engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart; - - instance.setAngleWindowWidth(angleWindowWidth); + instance.setAngleWindowWidth(); instance.handleValue(GET_RPM() DEFINE_PARAM_SUFFIX(PASS_HIP_PARAMS)); @@ -385,7 +382,7 @@ void initHip9011(Logging *sharedLogger) { return; - instance.setAngleWindowWidth(engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart); + instance.setAngleWindowWidth(); #if EFI_PROD_CODE driver = getSpiDevice(engineConfiguration->hip9011SpiDevice); diff --git a/firmware/hw_layer/HIP9011_logic.cpp b/firmware/hw_layer/HIP9011_logic.cpp index ea76f4ffa7..30e2681b1b 100644 --- a/firmware/hw_layer/HIP9011_logic.cpp +++ b/firmware/hw_layer/HIP9011_logic.cpp @@ -78,7 +78,8 @@ int HIP9011::getIntegrationIndexByRpm(float rpm) { return i == -1 ? INT_LOOKUP_SIZE - 1 : INT_LOOKUP_SIZE - i - 1; } -void HIP9011::setAngleWindowWidth(float angleWindowWidth) { +void HIP9011::setAngleWindowWidth(DEFINE_HIP_PARAMS) { + float angleWindowWidth = GET_CONFIG_VALUE(knockDetectionWindowEnd) - GET_CONFIG_VALUE(knockDetectionWindowStart); // float '==' is totally appropriate here if (this->angleWindowWidth == angleWindowWidth) return; // exit if value has not change diff --git a/firmware/hw_layer/HIP9011_logic.h b/firmware/hw_layer/HIP9011_logic.h index f9d02bbf04..13d4c31893 100644 --- a/firmware/hw_layer/HIP9011_logic.h +++ b/firmware/hw_layer/HIP9011_logic.h @@ -32,17 +32,24 @@ public: #define PASS_HIP_PARAMS CONFIG(knockBandCustom), \ CONFIG(cylinderBore), \ CONFIG(hip9011Gain), \ - CONFIG(hip9011PrescalerAndSDO) + CONFIG(hip9011PrescalerAndSDO), \ + CONFIG(knockDetectionWindowStart), \ + CONFIG(knockDetectionWindowEnd) #define FORWARD_HIP_PARAMS knockBandCustom, \ cylinderBore, \ hip9011Gain, \ - hip9011PrescalerAndSDO + hip9011PrescalerAndSDO, \ + knockDetectionWindowStart, \ + knockDetectionWindowEnd #define DEFINE_HIP_PARAMS float knockBandCustom,\ float cylinderBore, \ float hip9011Gain, \ - int hip9011PrescalerAndSDO + int hip9011PrescalerAndSDO, \ + float knockDetectionWindowStart, \ + float knockDetectionWindowEnd + #define GET_CONFIG_VALUE(x) x #define DEFINE_PARAM_SUFFIX(x) , x @@ -54,7 +61,7 @@ public: void prepareHip9011RpmLookup(float angleWindowWidth); int getIntegrationIndexByRpm(float rpm); void setStateAndCommand(unsigned char cmd); - void setAngleWindowWidth(float angleWindowWidth); + void setAngleWindowWidth(DEFINE_HIP_PARAMS); void handleValue(int rpm DEFINE_PARAM_SUFFIX(DEFINE_HIP_PARAMS)); /** diff --git a/unit_tests/tests/test_hip9011.cpp b/unit_tests/tests/test_hip9011.cpp index dc292329f9..54822bdca3 100644 --- a/unit_tests/tests/test_hip9011.cpp +++ b/unit_tests/tests/test_hip9011.cpp @@ -18,10 +18,10 @@ TEST(hip9011, lookup) { assertEqualsM2("240us 50 degree", 1105.2435, getRpmByAngleWindowAndTimeUs(240, 50), 0.1); assertEqualsM2("240us 50 degree", 6631.4619, getRpmByAngleWindowAndTimeUs(40, 50), 0.1); - EXPECT_EQ(0, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/3, 0)); - EXPECT_EQ(0, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/2, 0)); - EXPECT_EQ(47, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/0.234, 0)); - EXPECT_EQ(63, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/0.000001, 0)); + EXPECT_EQ(0, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/3, 0, NAN, NAN)); + EXPECT_EQ(0, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/2, 0, NAN, NAN)); + EXPECT_EQ(47, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/0.234, 0, NAN, NAN)); + EXPECT_EQ(63, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/0.000001, 0, NAN, NAN)); } @@ -38,10 +38,10 @@ TEST(hip9011, rpmLookup) { TEST(hip9011, band) { - EXPECT_FLOAT_EQ(3, getHIP9011Band(/* knockBandCustom*/3, /*cylinderBore*/76, /*hip9011Gain*/NAN, 0)); - EXPECT_FLOAT_EQ(7.5389242, getHIP9011Band(/* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/NAN, 0)); + EXPECT_FLOAT_EQ(3, getHIP9011Band(/* knockBandCustom*/3, /*cylinderBore*/76, /*hip9011Gain*/NAN, 0, NAN, NAN)); + EXPECT_FLOAT_EQ(7.5389242, getHIP9011Band(/* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/NAN, 0, NAN, NAN)); - EXPECT_EQ(42, getBandIndex(/* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/NAN, 0)); + EXPECT_EQ(42, getBandIndex(/* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/NAN, 0, NAN, NAN)); } @@ -63,7 +63,7 @@ TEST(hip9011, configurationCommands) { instance.prepareHip9011RpmLookup(50); // want to invoke method with same parameters a few times -#define PARAMETERS 600, /* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/1, _8MHZ_PRESCALER +#define PARAMETERS 600, /* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/1, _8MHZ_PRESCALER, NAN, NAN // Not making assumptions on the message send ... EXPECT_CALL(mock, sendSyncCommand(_)).Times(0);