From 0f133f88a14134a75d3f208c03a4f00d2decbe80 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 21 Dec 2021 15:52:46 -0800 Subject: [PATCH] Map3D accepts any scaled_channel type (#3712) * Map3D accepts any scaled_channel type * consistency * reinstate test * fix ratio --- firmware/util/containers/table_helper.h | 26 +++++++++---------- .../test_basic_math/test_interpolation_3d.cpp | 18 ++++++------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/firmware/util/containers/table_helper.h b/firmware/util/containers/table_helper.h index cbac7f0914..165dbc9bfe 100644 --- a/firmware/util/containers/table_helper.h +++ b/firmware/util/containers/table_helper.h @@ -25,7 +25,7 @@ public: /** * this helper class brings together 3D table with two 2D axis curves */ -template> +template class Map3D : public ValueProvider3D { public: template @@ -47,7 +47,7 @@ public: return interpolate3d(*m_values, *m_rowBins, yRow * m_rowMult, *m_columnBins, xColumn * m_colMult) * - TValueMultiplier::asFloat(); + m_valueMult; } void setAll(TValue value) { @@ -55,28 +55,27 @@ public: for (size_t r = 0; r < TRowNum; r++) { for (size_t c = 0; c < TColNum; c++) { - (*m_values)[r][c] = value / TValueMultiplier::asFloat(); + (*m_values)[r][c] = value / m_valueMult; } } } private: - template - void initValues(scaled_channel (&table)[TRowNum][TColNum]) { - static_assert(TValueMultiplier::den == TMult); - static_assert(TValueMultiplier::num == 1); - + template + void initValues(scaled_channel (&table)[TRowNum][TColNum]) { m_values = reinterpret_cast(&table); + m_valueMult = 1 / efi::ratio::asFloat(); } void initValues(TValue (&table)[TRowNum][TColNum]) { m_values = &table; + m_valueMult = 1; } template void initRows(const scaled_channel (&rowBins)[TRowNum]) { m_rowBins = reinterpret_cast(&rowBins); - m_rowMult = (float)TRowMult / TRowDiv; + m_rowMult = efi::ratio::asFloat(); } void initRows(const TRow (&rowBins)[TRowNum]) { @@ -87,7 +86,7 @@ private: template void initCols(const scaled_channel (&columnBins)[TColNum]) { m_columnBins = reinterpret_cast(&columnBins); - m_colMult = (float)TColMult / TColDiv; + m_colMult = efi::ratio::asFloat(); } void initCols(const TColumn (&columnBins)[TColNum]) { @@ -115,16 +114,17 @@ private: float m_rowMult = 1; float m_colMult = 1; + float m_valueMult = 1; }; -typedef Map3D> lambda_Map3D_t; +typedef Map3D lambda_Map3D_t; typedef Map3D fuel_Map3D_t; typedef Map3D baroCorr_Map3D_t; typedef Map3D pedal2tps_t; -typedef Map3D> boostOpenLoop_Map3D_t; +typedef Map3D boostOpenLoop_Map3D_t; typedef Map3D boostClosedLoop_Map3D_t; typedef Map3D gppwm_Map3D_t; -typedef Map3D> mapEstimate_Map3D_t; +typedef Map3D mapEstimate_Map3D_t; /** * @param precision for example '0.1' for one digit fractional part. Default to 0.01, two digits. diff --git a/unit_tests/test_basic_math/test_interpolation_3d.cpp b/unit_tests/test_basic_math/test_interpolation_3d.cpp index c37927426a..721da7e9ce 100644 --- a/unit_tests/test_basic_math/test_interpolation_3d.cpp +++ b/unit_tests/test_basic_math/test_interpolation_3d.cpp @@ -18,16 +18,12 @@ float mafBins[4] = { 1, 2, 3, 4 }; scaled_channel mafBinsScaledInt[4] = { 1, 2, 3, 4 }; scaled_channel mafBinsScaledByte[4] = { 1, 2, 3, 4 }; - -scaled_channel mapScaledByByte; -/* - = { +scaled_channel mapScaledChannel[4][5] = { { 1, 2, 3, 4, 4}, { 2, 3, 4, 200, 200 }, { 3, 4, 200, 500, 500 }, { 4, 5, 300, 600, 600 }, }; -*/ float map[4][5] = { { 1, 2, 3, 4, 4}, @@ -39,11 +35,6 @@ float map[4][5] = { #define EXPECT_NEAR_M4(a, b) EXPECT_NEAR(a, b, 1e-4) static float getValue(float rpm, float maf) { - Map3D<5, 4, uint8_t, float, float> x0; -// x0.init(mapScaledByByte, mafBins, rpmBins); -// float result0 = x0.getValue(rpm, maf); - - Map3D<5, 4, float, float, float> x1; x1.init(map, mafBins, rpmBins); float result1 = x1.getValue(rpm, maf); @@ -72,6 +63,12 @@ static float getValue(float rpm, float maf) { ); EXPECT_NEAR_M4(result1, result5); + // Test with values stored in scaled bytes + Map3D<5, 4, uint32_t, float, float> x6; + x6.init(mapScaledChannel, mafBins, rpmBins); + float result6 = x6.getValue(rpm, maf); + EXPECT_NEAR(result1, result6, 1e-3); + return result1; } @@ -84,6 +81,7 @@ static void newTestToComfirmInterpolation() { //______|__2|__3|_LOAD map[1][2] = 10; + mapScaledChannel[1][2] = 10; // let's start by testing corners