From 52ab8424e75dcb6fb7101c02ce6572350f1cbe63 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 19 Aug 2018 11:27:59 -0400 Subject: [PATCH] #604 fix --- firmware/controllers/core/interpolation.h | 30 +++++++++--- firmware/controllers/engine_controller.cpp | 2 +- .../test_basic_math/test_interpolation_3d.cpp | 48 ++++++++++--------- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/firmware/controllers/core/interpolation.h b/firmware/controllers/core/interpolation.h index 7c98363230..e9aee6ff44 100644 --- a/firmware/controllers/core/interpolation.h +++ b/firmware/controllers/core/interpolation.h @@ -78,8 +78,14 @@ float interpolate3d(float x, float xBin[], int xBinSize, float y, float yBin[], if (needInterpolationLogging()) printf("Y is smaller than smallest cell in table: %d\r\n", yIndex); #endif /* DEBUG_INTERPOLATION */ - // no interpolation should be fine here. - return map[xIndex][0]; + if (xIndex == xBinSize - 1) + return map[xIndex][0]; + float key1 = xBin[xIndex]; + float key2 = xBin[xIndex + 1]; + float value1 = map[xIndex][0]; + float value2 = map[xIndex + 1][0]; + + return interpolateMsg("out3d", key1, value1, key2, value2, x); } if (xIndex == xBinSize - 1 && yIndex == yBinSize - 1) { @@ -95,8 +101,14 @@ float interpolate3d(float x, float xBin[], int xBinSize, float y, float yBin[], if (needInterpolationLogging()) printf("TODO BETTER LOGGING x overflow %d\r\n", yIndex); #endif /* DEBUG_INTERPOLATION */ - // todo: implement better handling - y interpolation - return map[xBinSize - 1][yIndex]; + // here yIndex is less than yBinSize - 1, we've checked that condition already + + float key1 = yBin[yIndex]; + float key2 = yBin[yIndex + 1]; + float value1 = map[xIndex][yIndex]; + float value2 = map[xIndex][yIndex + 1]; + + return interpolateMsg("out3d", key1, value1, key2, value2, y); } if (yIndex == yBinSize - 1) { @@ -104,8 +116,14 @@ float interpolate3d(float x, float xBin[], int xBinSize, float y, float yBin[], if (needInterpolationLogging()) printf("Y is larger than largest cell in table: %d\r\n", yIndex); #endif /* DEBUG_INTERPOLATION */ - // todo: implement better handling - x interpolation - return map[xIndex][yBinSize - 1]; + // here xIndex is less than xBinSize - 1, we've checked that condition already + + float key1 = xBin[xIndex]; + float key2 = xBin[xIndex + 1]; + float value1 = map[xIndex][yIndex]; + float value2 = map[xIndex + 1][yIndex]; + + return interpolateMsg("out3d", key1, value1, key2, value2, x); } /* diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index 8c8b2a5678..bcb7e586af 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -733,5 +733,5 @@ int getRusEfiVersion(void) { if (initBootloader() != 0) return 123; #endif /* EFI_BOOTLOADER_INCLUDE_CODE */ - return 20180808; + return 20180819; } diff --git a/unit_tests/test_basic_math/test_interpolation_3d.cpp b/unit_tests/test_basic_math/test_interpolation_3d.cpp index 8ccc929a6e..2f125e261e 100644 --- a/unit_tests/test_basic_math/test_interpolation_3d.cpp +++ b/unit_tests/test_basic_math/test_interpolation_3d.cpp @@ -61,50 +61,54 @@ static void newTestToComfirmInterpolation() { assertEqualsM("middle cell ", 54.25, getValue(/*rpm*/250, 2.5)); // issue #604: interpolation outside of the table - assertEqualsM("800 @ 2.1 ",200, getValue(/*rpm*/800, 2.1)); - assertEqualsM("800 @ 2.3 ",200, getValue(/*rpm*/800, 2.3)); - assertEqualsM("800 @ 3.3 ",500, getValue(/*rpm*/800, 3.3)); + // X above the range + assertEqualsM("800 @ 2.1 ",230, getValue(/*rpm*/800, 2.1)); + assertEqualsM("800 @ 2.3 ",290, getValue(/*rpm*/800, 2.3)); + assertEqualsM("800 @ 3.3 ",530, getValue(/*rpm*/800, 3.3)); + // X below the range + assertEqualsM("-810 @ 2.1 ",2.1, getValue(/*rpm*/-810, 2.1)); + assertEqualsM("-820 @ 2.3 ",2.3, getValue(/*rpm*/-820, 2.3)); + // Y above the range + assertEqualsM("310 @ 12.1 ", 330, getValue(/*rpm*/310, 12.1)); + assertEqualsM("320 @ 12.3 ", 360, getValue(/*rpm*/320, 12.3)); + + // Y below the range + assertEqualsM("310 @ -12.1 ", 3.1, getValue(/*rpm*/310, -12.1)); + assertEqualsM("320 @ -12.3 ", 3.2, getValue(/*rpm*/320, -12.3)); } void testInterpolate3d(void) { printf("*************************************************** testInterpolate3d\r\n"); - float dwell; + printf("*** no interpolation here 1\r\n"); - dwell = getValue(100, 2); - assertEquals(2, dwell); + assertEquals(2, getValue(100, 2)); printf("*** no interpolation here 2\r\n"); - dwell = getValue(200, 4); - assertEquals(5, dwell); + assertEquals(5, getValue(200, 4)); printf("*** rpm interpolated value expected1\r\n"); - dwell = getValue(150, 2); - assertEquals(2.5, dwell); + assertEquals(2.5, getValue(150, 2)); printf("*** rpm interpolated value expected2\r\n"); - dwell = getValue(250, 3); - assertEquals(102, dwell); + assertEquals(102, getValue(250, 3)); printf("*** both rpm and maf interpolated value expected\r\n"); - dwell = getValue(335.3, 3.551); - assertEquals(361, dwell); + assertEquals(361, getValue(335.3, 3.551)); printf("*** both rpm and maf interpolated value expected 2\r\n"); - dwell = getValue(410.01, 2.012); - assertEquals(203.6, dwell); + assertEquals(203.6, getValue(410.01, 2.012)); printf("*** both rpm and maf interpolated value expected 3\r\n"); - dwell = getValue(1000000, 1000); - assertEquals(600, dwell); + assertEquals(600, getValue(1000000, 1000)); printf("*** both rpm and maf interpolated value expected 4\r\n"); - dwell = getValue(410.01, -1); - assertEquals(4, dwell); - dwell = getValue(-1, -1); - assertEquals(1, dwell); + assertEquals(4, getValue(410.01, -1)); + + + assertEquals(1, getValue(-1, -1)); newTestToComfirmInterpolation();