From fc8755fd5a250d2e51b0d4d37edc2ece14c03201 Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 4 Jan 2019 19:20:32 -0500 Subject: [PATCH] #35 --- firmware/controllers/sensors/hip9011_lookup.cpp | 5 ----- firmware/controllers/sensors/hip9011_lookup.h | 2 +- firmware/hw_layer/HIP9011.cpp | 4 ++-- firmware/hw_layer/HIP9011_logic.cpp | 12 ++++++++++++ firmware/hw_layer/HIP9011_logic.h | 10 +++++++--- unit_tests/test_hip9011.cpp | 14 +++++++------- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/firmware/controllers/sensors/hip9011_lookup.cpp b/firmware/controllers/sensors/hip9011_lookup.cpp index e92528f463..ddcd73ed82 100644 --- a/firmware/controllers/sensors/hip9011_lookup.cpp +++ b/firmware/controllers/sensors/hip9011_lookup.cpp @@ -69,11 +69,6 @@ int getIntegrationIndexByRpm(float rpm) { return i == -1 ? INT_LOOKUP_SIZE - 1 : INT_LOOKUP_SIZE - i - 1; } -int getHip9011GainIndex(float gain) { - int i = GAIN_INDEX(gain); - // GAIN_LOOKUP_SIZE is returned for index which is too low - return i == GAIN_LOOKUP_SIZE ? GAIN_LOOKUP_SIZE - 1 : i; -} /** * @param frequency knock frequencey, in kHz diff --git a/firmware/controllers/sensors/hip9011_lookup.h b/firmware/controllers/sensors/hip9011_lookup.h index 9b55d2ce41..2073b30022 100644 --- a/firmware/controllers/sensors/hip9011_lookup.h +++ b/firmware/controllers/sensors/hip9011_lookup.h @@ -21,7 +21,7 @@ extern const float gainLookupInReverseOrder[GAIN_LOOKUP_SIZE]; extern const float bandFreqLookup[BAND_LOOKUP_SIZE]; float getRpmByAngleWindowAndTimeUs(int timeUs, float angleWindowWidth); -int getHip9011GainIndex(float gain); + int getHip9011BandIndex(float frequency); void prepareHip9011RpmLookup(float angleWindowWidth); diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index e0a62406d6..42d206d163 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -307,8 +307,8 @@ void hipAdcCallback(adcsample_t adcValue) { } int integratorIndex = getIntegrationIndexByRpm(GET_RPM()); - int gainIndex = getHip9011GainIndex(engineConfiguration->hip9011Gain); - int bandIndex = getBandIndex(); + int gainIndex = getHip9011GainIndex(PASS_HIP_PARAMS); + int bandIndex = getBandIndex(PASS_HIP_PARAMS); int prescalerIndex = engineConfiguration->hip9011PrescalerAndSDO; diff --git a/firmware/hw_layer/HIP9011_logic.cpp b/firmware/hw_layer/HIP9011_logic.cpp index 88aeaa54a7..cea1b09773 100644 --- a/firmware/hw_layer/HIP9011_logic.cpp +++ b/firmware/hw_layer/HIP9011_logic.cpp @@ -29,3 +29,15 @@ int getBandIndex(DEFINE_HIP_PARAMS) { return getHip9011BandIndex(freq); } +int getHip9011GainIndex(DEFINE_HIP_PARAMS) { + int i = GAIN_INDEX(GET_CONFIG_VALUE(hip9011Gain)); + // GAIN_LOOKUP_SIZE is returned for index which is too low + return i == GAIN_LOOKUP_SIZE ? GAIN_LOOKUP_SIZE - 1 : i; +} + +int getHip9011GainIndex(float gain) { + int i = GAIN_INDEX(gain); + // GAIN_LOOKUP_SIZE is returned for index which is too low + return i == GAIN_LOOKUP_SIZE ? GAIN_LOOKUP_SIZE - 1 : i; +} + diff --git a/firmware/hw_layer/HIP9011_logic.h b/firmware/hw_layer/HIP9011_logic.h index 4c9353df11..2011b2050d 100644 --- a/firmware/hw_layer/HIP9011_logic.h +++ b/firmware/hw_layer/HIP9011_logic.h @@ -46,13 +46,16 @@ public: #else #define PASS_HIP_PARAMS CONFIG(knockBandCustom), \ - CONFIG(cylinderBore) + CONFIG(cylinderBore), \ + CONFIG(hip9011Gain) #define FORWARD_HIP_PARAMS knockBandCustom, \ - cylinderBore + cylinderBore, \ + hip9011Gain #define DEFINE_HIP_PARAMS float knockBandCustom,\ - float cylinderBore + float cylinderBore, \ + float hip9011Gain #define GET_CONFIG_VALUE(x) x #endif @@ -60,5 +63,6 @@ public: float getHIP9011Band(DEFINE_HIP_PARAMS); int getBandIndex(DEFINE_HIP_PARAMS); +int getHip9011GainIndex(DEFINE_HIP_PARAMS); #endif /* HW_LAYER_HIP9011_LOGIC_H_ */ diff --git a/unit_tests/test_hip9011.cpp b/unit_tests/test_hip9011.cpp index 175488e626..4e64f4f78e 100644 --- a/unit_tests/test_hip9011.cpp +++ b/unit_tests/test_hip9011.cpp @@ -17,10 +17,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(3)); - EXPECT_EQ(0, getHip9011GainIndex(2)); - EXPECT_EQ(47, getHip9011GainIndex(0.234)); - EXPECT_EQ(63, getHip9011GainIndex(0.000001)); + EXPECT_EQ(0, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/3)); + EXPECT_EQ(0, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/2)); + EXPECT_EQ(47, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/0.234)); + EXPECT_EQ(63, getHip9011GainIndex(/* knockBandCustom*/NAN, /*cylinderBore*/NAN, /*hip9011Gain*/0.000001)); prepareHip9011RpmLookup(50); @@ -32,9 +32,9 @@ TEST(hip9011, lookup) { TEST(hip9011, band) { - EXPECT_FLOAT_EQ(3, getHIP9011Band(/* knockBandCustom*/3, /*cylinderBore*/76)); - EXPECT_FLOAT_EQ(7.5389242, getHIP9011Band(/* knockBandCustom*/0, /*cylinderBore*/76)); + EXPECT_FLOAT_EQ(3, getHIP9011Band(/* knockBandCustom*/3, /*cylinderBore*/76, /*hip9011Gain*/NAN)); + EXPECT_FLOAT_EQ(7.5389242, getHIP9011Band(/* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/NAN)); - EXPECT_EQ(42, getBandIndex(/* knockBandCustom*/0, /*cylinderBore*/76)); + EXPECT_EQ(42, getBandIndex(/* knockBandCustom*/0, /*cylinderBore*/76, /*hip9011Gain*/NAN)); }