From 198ca2dc40abc193482bb350558afb9141c69632 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 4 Jan 2015 21:03:31 -0600 Subject: [PATCH] auto-sync --- .../controllers/sensors/hip9011_lookup.cpp | 60 ++++++++++++++++++- firmware/controllers/sensors/hip9011_lookup.h | 11 ++++ firmware/hw_layer/HIP9011.cpp | 51 ++++------------ unit_tests/test_sensors.cpp | 19 +++++- 4 files changed, 96 insertions(+), 45 deletions(-) diff --git a/firmware/controllers/sensors/hip9011_lookup.cpp b/firmware/controllers/sensors/hip9011_lookup.cpp index 1391eb1247..d99f027bea 100644 --- a/firmware/controllers/sensors/hip9011_lookup.cpp +++ b/firmware/controllers/sensors/hip9011_lookup.cpp @@ -6,6 +6,7 @@ */ #include "hip9011_lookup.h" +#include "interpolation.h" /** * These are HIP9011 magic values - integrator time constants in uS @@ -13,13 +14,66 @@ const int integratorValues[INT_LOOKUP_SIZE] = { 40, 45, 50, 55, 60, 65, 70, 75, 80, 90, 100, 110, 120, 130, 140, 150, 160, 180, 200, 220, 240, 260, 280, 300, 320, 360, 400, 440, 480, 520, 560, 600 }; +const float gainLookupInReverseOrder[GAIN_LOOKUP_SIZE] = { +/* 00 */0.111, 0.118, 0.125, 0.129, 0.133, 0.138, 0.143, 0.148, +/* 08 */0.154, 0.160, 0.167, 0.174, 0.182, 0.190, 0.200, 0.211, +/* 16 */0.222, 0.236, 0.250, 0.258, 0.267, 0.276, 0.286, 0.296, +/* 24 */0.308, 0.320, 0.333, 0.348, 0.364, 0.381, 0.400, 0.421, +/* 32 */0.444, 0.471, 0.500, 0.548, 0.567, 0.586, 0.607, 0.630, +/* 40 */0.654, 0.680, 0.708, 0.739, 0.773, 0.810, 0.850, 0.895, +/* 48 */0.944, 1.000, 1.063, 1.143, 1.185, 1.231, 1.280, 1.333, +/* 56 */1.391, 1.455, 1.523, 1.600, 1.684, 1.778, 1.882, 2.0 }; + +const float bandFreqLookup[BAND_LOOKUP_SIZE] = { 1.22, 1.26, 1.31, 1.35, 1.4, 1.45, 1.51, 1.57, 1.63, 1.71, 1.78, + 1.87, 1.96, 2.07, 2.18, 2.31, 2.46, 2.54, 2.62, 2.71, 2.81, 2.92, 3.03, 3.15, 3.28, 3.43, 3.59, 3.76, 3.95, + 4.16, 4.39, 4.66, 4.95, 5.12, 5.29, 5.48, 5.68, 5.9, 6.12, 6.37, 6.64, 6.94, 7.27, 7.63, 8.02, 8.46, 8.95, 9.5, + 10.12, 10.46, 10.83, 11.22, 11.65, 12.1, 12.6, 13.14, 13.72, 14.36, 15.07, 15.84, 16.71, 17.67, 18.76, 19.98 }; + + +float rpmLookup[INT_LOOKUP_SIZE]; + +/** + * + * We know the set of possible integration times, we know the knock detection window width + */ +void prepareHip9011RpmLookup(float angleWindowWidth) { + /** + * out binary search method needs increasing order thus the reverse order here + */ + for (int i = 0; i < INT_LOOKUP_SIZE; i++) { + rpmLookup[i] = getRpmByAngleWindowAndTimeUs(integratorValues[INT_LOOKUP_SIZE - i - 1], angleWindowWidth); + } +} + + /** * 'TC is typically TINT/(2*Pi*VOUT)' * Knock Sensor Training TPIC8101, page 24 */ float getRpmByAngleWindowAndTimeUs(int timeUs, float angleWindowWidth) { + /** + * TINT = TC * 2 * PI * VOUT + */ + float integrationTimeUs = timeUs * 2 * PIF * DESIRED_OUTPUT_VALUE; + /** + * rpm = 60 seconds / time + * '60000000' because revolutions per MINUTE in uS conversion + */ float windowWidthMult = angleWindowWidth / 360.0f; - // '60000000' because revolutions per MINUTE in uS conversion - - return 60000000.0f / (timeUs * 2 * PIF * DESIRED_OUTPUT_VALUE * windowWidthMult); + return 60000000.0f / integrationTimeUs * windowWidthMult; +} + +int getIntegrationIndexByRpm(float rpm) { + int i = findIndex(rpmLookup, INT_LOOKUP_SIZE, (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; +} + +int getHip9011BandIndex(float bore) { + return findIndex(bandFreqLookup, BAND_LOOKUP_SIZE, BAND(bore)); } diff --git a/firmware/controllers/sensors/hip9011_lookup.h b/firmware/controllers/sensors/hip9011_lookup.h index d1f0f71714..8ae17e57ab 100644 --- a/firmware/controllers/sensors/hip9011_lookup.h +++ b/firmware/controllers/sensors/hip9011_lookup.h @@ -15,7 +15,18 @@ #define DESIRED_OUTPUT_VALUE 5.0f extern const int integratorValues[INT_LOOKUP_SIZE]; +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 bore); +void prepareHip9011RpmLookup(float angleWindowWidth); + +#define GAIN_INDEX(gain) (GAIN_LOOKUP_SIZE - 1 - findIndex(gainLookupInReverseOrder, GAIN_LOOKUP_SIZE, (gain))) +#define BAND(bore) (900 / (PIF * (bore) / 2)) + +extern float rpmLookup[INT_LOOKUP_SIZE]; +int getIntegrationIndexByRpm(float rpm); #endif /* CONTROLLERS_SENSORS_HIP9011_LOOKUP_H_ */ diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index ebaecfbf0d..472a20d60a 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -38,9 +38,12 @@ extern pin_output_mode_e DEFAULT_OUTPUT; +/** + * band index is only send to HIP chip on startup + */ static int bandIndex; -static int gainIndex; -static int intergratorIndex = -1; +static int currentGainIndex; +static int currentIntergratorIndex = -1; /** * Int/Hold pin is controlled from scheduler callbacks which are set according to current RPM @@ -86,6 +89,8 @@ static msg_t ivThread(int param) { while (true) { chThdSleepMilliseconds(10); + // engine->rpmCalculator.rpmValue + // int newValue = INTEGRATOR_INDEX; // if (newValue != intergratorIndex) { // intergratorIndex = newValue; @@ -146,49 +151,13 @@ static msg_t ivThread(int param) { EXTERN_ENGINE ; -static const float gainLookupInReverseOrder[GAIN_LOOKUP_SIZE] = { -/* 00 */0.111, 0.118, 0.125, 0.129, 0.133, 0.138, 0.143, 0.148, -/* 08 */0.154, 0.160, 0.167, 0.174, 0.182, 0.190, 0.200, 0.211, -/* 16 */0.222, 0.236, 0.250, 0.258, 0.267, 0.276, 0.286, 0.296, -/* 24 */0.308, 0.320, 0.333, 0.348, 0.364, 0.381, 0.400, 0.421, -/* 32 */0.444, 0.471, 0.500, 0.548, 0.567, 0.586, 0.607, 0.630, -/* 40 */0.654, 0.680, 0.708, 0.739, 0.773, 0.810, 0.850, 0.895, -/* 48 */0.944, 1.000, 1.063, 1.143, 1.185, 1.231, 1.280, 1.333, -/* 56 */1.391, 1.455, 1.523, 1.600, 1.684, 1.778, 1.882, 2.0 }; - -#define GAIN_INDEX(gain) (GAIN_LOOKUP_SIZE - 1 - findIndex(gainLookupInReverseOrder, GAIN_LOOKUP_SIZE, (gain))) - - -static const float bandFreqLookup[BAND_LOOKUP_SIZE] = { 1.22, 1.26, 1.31, 1.35, 1.4, 1.45, 1.51, 1.57, 1.63, 1.71, 1.78, - 1.87, 1.96, 2.07, 2.18, 2.31, 2.46, 2.54, 2.62, 2.71, 2.81, 2.92, 3.03, 3.15, 3.28, 3.43, 3.59, 3.76, 3.95, - 4.16, 4.39, 4.66, 4.95, 5.12, 5.29, 5.48, 5.68, 5.9, 6.12, 6.37, 6.64, 6.94, 7.27, 7.63, 8.02, 8.46, 8.95, 9.5, - 10.12, 10.46, 10.83, 11.22, 11.65, 12.1, 12.6, 13.14, 13.72, 14.36, 15.07, 15.84, 16.71, 17.67, 18.76, 19.98 }; - -static float rpmLookup[INT_LOOKUP_SIZE]; - -/** - * - * We know the set of possible integration times, we know the knock detection window width - */ -static void prepareRpmLookup(engine_configuration_s *engineConfiguration) { - for (int i = 0; i < INT_LOOKUP_SIZE; i++) { - - rpmLookup[i] = getRpmByAngleWindowAndTimeUs(integratorValues[i], engineConfiguration->knockDetectionWindowEnd - - engineConfiguration->knockDetectionWindowStart); - } -} - -#define BAND(bore) (900 / (PIF * (bore) / 2)) - -#define INTEGRATOR_INDEX findIndex(rpmLookup, INT_LOOKUP_SIZE, engine->rpmCalculator.rpmValue) - static void showHipInfo(void) { printSpiState(&logger, boardConfiguration); scheduleMsg(&logger, "bore=%f freq=%f", engineConfiguration->cylinderBore, BAND(engineConfiguration->cylinderBore)); scheduleMsg(&logger, "band_index=%d gain_index=%d", bandIndex, GAIN_INDEX(boardConfiguration->hip9011Gain)); - scheduleMsg(&logger, "integrator index=%d", INTEGRATOR_INDEX); + scheduleMsg(&logger, "integrator index=%d", currentIntergratorIndex); scheduleMsg(&logger, "spi= int=%s CS=%s", hwPortname(boardConfiguration->hip9011IntHoldPin), hwPortname(boardConfiguration->hip9011CsPin)); @@ -258,6 +227,8 @@ void initHip9011(void) { return; initLogging(&logger, "HIP driver"); +// prepa engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart + // driver = getSpiDevice(boardConfiguration->digitalPotentiometerSpiDevice); spicfg.ssport = getHwPort(boardConfiguration->hip9011CsPin); @@ -280,7 +251,7 @@ void initHip9011(void) { // spiStart(driver, &spicfg); //#endif /* HIP_DEBUG */ - bandIndex = findIndex(bandFreqLookup, BAND_LOOKUP_SIZE, BAND(engineConfiguration->cylinderBore)); + bandIndex = getHip9011BandIndex(engineConfiguration->cylinderBore); addTriggerEventListener(&intHoldCallback, "DD int/hold", engine); diff --git a/unit_tests/test_sensors.cpp b/unit_tests/test_sensors.cpp index 8b5c56f7e9..5f2972e4fa 100644 --- a/unit_tests/test_sensors.cpp +++ b/unit_tests/test_sensors.cpp @@ -41,9 +41,24 @@ void testTpsRateOfChange(void) { static void testHip9011lookup(void) { print("************************************************** testHip9011lookup\r\n"); - assertEqualsM2("", 47746.5195, getRpmByAngleWindowAndTimeUs(40, 360), 0.1); assertEqualsM2("", 3183.1013, getRpmByAngleWindowAndTimeUs(600, 360), 0.1); - assertEqualsM2("", 22918.3301, getRpmByAngleWindowAndTimeUs(600, 50), 0.1); + assertEqualsM2("40us", 47746.5195, getRpmByAngleWindowAndTimeUs(40, 360), 0.1); + + assertEqualsM2("600us 50 degree", 442.0974, getRpmByAngleWindowAndTimeUs(600, 50), 0.1); + assertEqualsM2("240us 50 degree", 1105.2435, getRpmByAngleWindowAndTimeUs(240, 50), 0.1); + assertEqualsM2("240us 50 degree", 6631.4619, getRpmByAngleWindowAndTimeUs(40, 50), 0.1); + + assertEquals(0, getHip9011GainIndex(3)); + assertEquals(0, getHip9011GainIndex(2)); + assertEquals(47, getHip9011GainIndex(0.234)); + assertEquals(63, getHip9011GainIndex(0.000001)); + + prepareHip9011RpmLookup(50); + + assertEquals(31, getIntegrationIndexByRpm(1)); + assertEquals(21, getIntegrationIndexByRpm(1100)); + assertEquals(1, getIntegrationIndexByRpm(6600)); + assertEquals(0, getIntegrationIndexByRpm(16600)); } void testSensors(void) {