diff --git a/firmware/controllers/algo/accel_enrichment.h b/firmware/controllers/algo/accel_enrichment.h index e34b18b738..54b7930c84 100644 --- a/firmware/controllers/algo/accel_enrichment.h +++ b/firmware/controllers/algo/accel_enrichment.h @@ -14,7 +14,7 @@ #include "cyclic_buffer.h" #include "table_helper.h" -typedef Map3D tps_tps_Map3D_t; +typedef Map3D tps_tps_Map3D_t; /** * this object is used for MAP rate-of-change and TPS rate-of-change corrections diff --git a/firmware/controllers/core/fsio_impl.h b/firmware/controllers/core/fsio_impl.h index de363d5a25..8bd1dcaf29 100644 --- a/firmware/controllers/core/fsio_impl.h +++ b/firmware/controllers/core/fsio_impl.h @@ -13,7 +13,7 @@ #include "engine.h" #include "table_helper.h" -typedef Map3D fsio8_Map3D_t; +typedef Map3D fsio8_Map3D_t; /** * In human language that's diff --git a/firmware/controllers/core/interpolation.cpp b/firmware/controllers/core/interpolation.cpp index e871da55a2..a3c3b1f43a 100644 --- a/firmware/controllers/core/interpolation.cpp +++ b/firmware/controllers/core/interpolation.cpp @@ -207,12 +207,10 @@ float interpolate2d(float value, float bin[], float values[], int size) { return interpolateMsg("2d", bin[index], values[index], bin[index + 1], values[index + 1], value); } -typedef float (*getTableValue_t)(int x, int y, void *table); - /** * @brief Two-dimensional table lookup with linear interpolation */ -float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[], int yBinSize, void* map, getTableValue_t getTableValue) { +float interpolate3d(float x, float xBin[], int xBinSize, float y, float yBin[], int yBinSize, float* map[]) { if (cisnan(x)) { warning(OBD_PCM_Processor_Fault, "%f: x is NaN in interpolate3d", x); return NAN; @@ -233,7 +231,7 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] if (needInterpolationLogging) printf("X and Y are smaller than smallest cell in table: %d\r\n", xIndex); #endif - return getTableValue(0, 0, map); + return map[0][0]; } if (xIndex < 0) { @@ -242,11 +240,11 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] printf("X is smaller than smallest cell in table: %dr\n", xIndex); #endif if (yIndex == yBinSize - 1) - return getTableValue(0, yIndex, map); + return map[0][yIndex]; float keyMin = yBin[yIndex]; float keyMax = yBin[yIndex + 1]; - float rpmMinValue = getTableValue(0, yIndex, map); - float rpmMaxValue = getTableValue(0, yIndex + 1, map); + float rpmMinValue = map[0][yIndex]; + float rpmMaxValue = map[0][yIndex + 1]; return interpolateMsg("3d", keyMin, rpmMinValue, keyMax, rpmMaxValue, y); } @@ -257,7 +255,7 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] printf("Y is smaller than smallest cell in table: %d\r\n", yIndex); #endif // no interpolation should be fine here. - return getTableValue(xIndex, 0, map); + return map[xIndex][0]; } if (xIndex == xBinSize - 1 && yIndex == yBinSize - 1) { @@ -265,7 +263,7 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] if (needInterpolationLogging) printf("X and Y are larger than largest cell in table: %d %d\r\n", xIndex, yIndex); #endif - return getTableValue(xBinSize - 1, yBinSize - 1, map); + return map[xBinSize - 1][yBinSize - 1]; } if (xIndex == xBinSize - 1) { @@ -274,7 +272,7 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] printf("TODO BETTER LOGGING x overflow %d\r\n", yIndex); #endif // todo: implement better handling - y interpolation - return getTableValue(xBinSize - 1, yIndex, map); + return map[xBinSize - 1][yIndex]; } if (yIndex == yBinSize - 1) { @@ -283,7 +281,7 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] printf("Y is larger than largest cell in table: %d\r\n", yIndex); #endif // todo: implement better handling - x interpolation - return getTableValue(xIndex, yBinSize - 1, map); + return map[xIndex][yBinSize - 1]; } /* @@ -293,8 +291,8 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] float xMin = xBin[xIndex]; float xMax = xBin[xIndex + 1]; - float rpmMinKeyMinValue = getTableValue(xIndex, yIndex, map); - float rpmMaxKeyMinValue = getTableValue(xIndex + 1, yIndex, map); + float rpmMinKeyMinValue = map[xIndex][yIndex]; + float rpmMaxKeyMinValue = map[xIndex + 1][yIndex]; float keyMinValue = interpolate(xMin, rpmMinKeyMinValue, xMax, rpmMaxKeyMinValue, x); @@ -308,8 +306,8 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] int keyMaxIndex = yIndex + 1; float keyMin = yBin[yIndex]; float keyMax = yBin[keyMaxIndex]; - float rpmMinKeyMaxValue = getTableValue(xIndex, keyMaxIndex, map); - float rpmMaxKeyMaxValue = getTableValue(rpmMaxIndex, keyMaxIndex, map); + float rpmMinKeyMaxValue = map[xIndex][keyMaxIndex]; + float rpmMaxKeyMaxValue = map[rpmMaxIndex][keyMaxIndex]; float keyMaxValue = interpolateMsg("3d", xMin, rpmMinKeyMaxValue, xMax, rpmMaxKeyMaxValue, x); @@ -324,17 +322,6 @@ float interpolate3d_g(float x, float xBin[], int xBinSize, float y, float yBin[] return result; } -float getFloatTableValue(int x, int y, void* pointer) { - float** map = (float**)pointer; - return map[x][y]; -} - -float interpolate3d(float x, float xBin[], int xBinSize, float y, float yBin[], int yBinSize, float* map[]) { - - return interpolate3d_g(x, xBin, xBinSize, y, yBin, yBinSize, map, getFloatTableValue); -} - - void setTableValue(float bins[], float values[], int size, float key, float value) { int index = findIndex(bins, size, key); if (index == -1) diff --git a/firmware/controllers/core/table_helper.h b/firmware/controllers/core/table_helper.h index 651cd823b9..2aad9f23f7 100644 --- a/firmware/controllers/core/table_helper.h +++ b/firmware/controllers/core/table_helper.h @@ -15,15 +15,15 @@ /** * this helper class brings together 3D table with two 2D axis curves */ -template +template class Map3D { public: Map3D(const char*name); - void init(float table[RPM_BIN_SIZE][LOAD_BIN_SIZE], float loadBins[LOAD_BIN_SIZE], float rpmBins[RPM_BIN_SIZE]); + void init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE], float loadBins[LOAD_BIN_SIZE], float rpmBins[RPM_BIN_SIZE]); float getValue(float xRpm, float y); - void setAll(float value); + void setAll(vType value); private: - float *pointers[LOAD_BIN_SIZE]; + vType *pointers[LOAD_BIN_SIZE]; float *loadBins; float *rpmBins; bool initialized; @@ -66,8 +66,8 @@ void Table2D::preCalc(float *bin, float *values) { } -template -void Map3D::init(float table[RPM_BIN_SIZE][LOAD_BIN_SIZE], +template +void Map3D::init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE], float loadBins[LOAD_BIN_SIZE], float rpmBins[RPM_BIN_SIZE]) { // this method cannot use logger because it's invoked before everything @@ -82,8 +82,8 @@ void Map3D::init(float table[RPM_BIN_SIZE][LOAD_BIN this->rpmBins = rpmBins; } -template -float Map3D::getValue(float xRpm, float y) { +template +float Map3D::getValue(float xRpm, float y) { efiAssert(initialized, "map not initialized", NAN); if (cisnan(y)) { warning(OBD_PCM_Processor_Fault, "%s: y is NaN", name); @@ -93,8 +93,8 @@ float Map3D::getValue(float xRpm, float y) { return interpolate3d(y, loadBins, LOAD_BIN_SIZE, xRpm, rpmBins, RPM_BIN_SIZE, pointers); } -template -Map3D::Map3D(const char *name) { +template +Map3D::Map3D(const char *name) { this->name = name; initialized = false; memset(&pointers, 0, sizeof(pointers)); @@ -102,8 +102,8 @@ Map3D::Map3D(const char *name) { rpmBins = NULL; } -template -void Map3D::setAll(float value) { +template +void Map3D::setAll(vType value) { efiAssertVoid(initialized, "map not initialized"); for (int l = 0; l < LOAD_BIN_SIZE; l++) { for (int r = 0; r < RPM_BIN_SIZE; r++) { @@ -112,9 +112,9 @@ void Map3D::setAll(float value) { } } -typedef Map3D ign_Map3D_t; -typedef Map3D fuel_Map3D_t; -typedef Map3D baroCorr_Map3D_t; +typedef Map3D ign_Map3D_t; +typedef Map3D fuel_Map3D_t; +typedef Map3D baroCorr_Map3D_t; void setRpmBin(float array[], int size, float idleRpm, float topRpm);