diff --git a/firmware/controllers/actuators/boost_control.cpp b/firmware/controllers/actuators/boost_control.cpp index 077cc8b81b..deab87aa1d 100644 --- a/firmware/controllers/actuators/boost_control.cpp +++ b/firmware/controllers/actuators/boost_control.cpp @@ -32,8 +32,8 @@ EXTERN_ENGINE; static Logging *logger; -static boostOpenLoop_Map3D_t boostMapOpen("boostmapopen", 1); -static boostOpenLoop_Map3D_t boostMapClosed("boostmapclosed", 1); +static boostOpenLoop_Map3D_t boostMapOpen("boostmapopen"); +static boostOpenLoop_Map3D_t boostMapClosed("boostmapclosed"); static SimplePwm boostPwmControl("boost"); void BoostController::init(SimplePwm* pwm, const ValueProvider3D* openLoopMap, const ValueProvider3D* closedLoopTargetMap, pid_s* pidParams) { @@ -82,7 +82,7 @@ expected BoostController::getSetpoint() const { return unexpected; } - return m_closedLoopTargetMap->getValue(rpm / RPM_1_BYTE_PACKING_MULT, tps.Value / TPS_1_BYTE_PACKING_MULT) * LOAD_1_BYTE_PACKING_MULT; + return m_closedLoopTargetMap->getValue(rpm / RPM_1_BYTE_PACKING_MULT, tps.Value / TPS_1_BYTE_PACKING_MULT); } expected BoostController::getOpenLoop(float target) const { @@ -100,7 +100,7 @@ expected BoostController::getOpenLoop(float target) const { return unexpected; } - percent_t openLoop = m_openLoopMap->getValue(rpm / RPM_1_BYTE_PACKING_MULT, tps.Value / TPS_1_BYTE_PACKING_MULT) * LOAD_1_BYTE_PACKING_MULT; + percent_t openLoop = m_openLoopMap->getValue(rpm / RPM_1_BYTE_PACKING_MULT, tps.Value / TPS_1_BYTE_PACKING_MULT); #if EFI_TUNER_STUDIO if (engineConfiguration->debugMode == DBG_BOOST) { diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 24c71ebf0b..3de9b24252 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -91,7 +91,7 @@ #endif /* ETB_MAX_COUNT */ static LoggingWithStorage logger("ETB"); -static pedal2tps_t pedal2tpsMap("Pedal2Tps", 1); +static pedal2tps_t pedal2tpsMap("Pedal2Tps"); EXTERN_ENGINE; diff --git a/firmware/controllers/math/speed_density.cpp b/firmware/controllers/math/speed_density.cpp index ec97e15fad..07227a0eb1 100644 --- a/firmware/controllers/math/speed_density.cpp +++ b/firmware/controllers/math/speed_density.cpp @@ -28,7 +28,7 @@ EXTERN_ENGINE; fuel_Map3D_t veMap("VE"); fuel_Map3D_t ve2Map("VE2"); -afr_Map3D_t afrMap("AFR", 1.0f / PACK_MULT_AFR_CFG); +afr_Map3D_t afrMap("AFR"); baroCorr_Map3D_t baroCorrMap("baro"); #define tpMin 0 diff --git a/firmware/util/containers/table_helper.h b/firmware/util/containers/table_helper.h index ee887b19e9..5b9411540d 100644 --- a/firmware/util/containers/table_helper.h +++ b/firmware/util/containers/table_helper.h @@ -11,6 +11,7 @@ #include "error_handling.h" #include "interpolation.h" #include "efilib.h" +#include "efi_ratio.h" // popular left edge of CLT-based correction curves #define CLT_CURVE_RANGE_FROM -40 @@ -24,22 +25,20 @@ public: /** * this helper class brings together 3D table with two 2D axis curves */ -template +template> class Map3D : public ValueProvider3D { public: explicit Map3D(const char*name); - Map3D(const char*name, float multiplier); void init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE], const kType loadBins[LOAD_BIN_SIZE], const kType rpmBins[RPM_BIN_SIZE]); float getValue(float xRpm, float y) const override; void setAll(vType value); vType *pointers[LOAD_BIN_SIZE]; private: - void create(const char*name, float multiplier); + void create(const char*name); const kType *loadBins = NULL; const kType *rpmBins = NULL; bool initialized = false; const char *name; - float multiplier; }; /* @@ -80,8 +79,8 @@ void Table2D::preCalc(float *bin, float *values) { } */ -template -void Map3D::init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE], +template +void Map3D::init(vType table[RPM_BIN_SIZE][LOAD_BIN_SIZE], const kType loadBins[LOAD_BIN_SIZE], const kType rpmBins[RPM_BIN_SIZE]) { // this method cannot use logger because it's invoked before everything @@ -96,45 +95,39 @@ void Map3D::init(vType table[RPM_BIN_ this->rpmBins = rpmBins; } -template -float Map3D::getValue(float xRpm, float y) const { +template +float Map3D::getValue(float xRpm, float y) const { efiAssert(CUSTOM_ERR_ASSERT, initialized, "map not initialized", NAN); if (cisnan(y)) { warning(CUSTOM_PARAM_RANGE, "%s: y is NaN", name); return NAN; } // todo: we have a bit of a mess: in TunerStudio, RPM is X-axis - return multiplier * interpolate3d(y, loadBins, LOAD_BIN_SIZE, xRpm, rpmBins, RPM_BIN_SIZE, pointers); + return interpolate3d(y, loadBins, LOAD_BIN_SIZE, xRpm, rpmBins, RPM_BIN_SIZE, pointers) * TValueMultiplier::asFloat(); } -template -Map3D::Map3D(const char *name) { - create(name, 1); +template +Map3D::Map3D(const char *name) { + create(name); } -template -Map3D::Map3D(const char *name, float multiplier) { - create(name, multiplier); -} - -template -void Map3D::create(const char *name, float multiplier) { +template +void Map3D::create(const char *name) { this->name = name; - this->multiplier = multiplier; memset(&pointers, 0, sizeof(pointers)); } -template -void Map3D::setAll(vType value) { +template +void Map3D::setAll(vType value) { efiAssertVoid(CUSTOM_ERR_6573, initialized, "map not initialized"); for (int l = 0; l < LOAD_BIN_SIZE; l++) { for (int r = 0; r < RPM_BIN_SIZE; r++) { - pointers[l][r] = value / multiplier; + pointers[l][r] = value / TValueMultiplier::asFloat(); } } } -template +template> void copy2DTable(const vType source[LOAD_BIN_SIZE][RPM_BIN_SIZE], vType destination[LOAD_BIN_SIZE][RPM_BIN_SIZE]) { for (int k = 0; k < LOAD_BIN_SIZE; k++) { for (int rpmIndex = 0; rpmIndex < RPM_BIN_SIZE; rpmIndex++) { @@ -143,12 +136,12 @@ void copy2DTable(const vType source[LOAD_BIN_SIZE][RPM_BIN_SIZE], vType destinat } } -typedef Map3D afr_Map3D_t; +typedef Map3D> afr_Map3D_t; typedef Map3D ign_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 iacPidMultiplier_t; typedef Map3D gppwm_Map3D_t; diff --git a/firmware/util/math/efi_ratio.h b/firmware/util/math/efi_ratio.h new file mode 100644 index 0000000000..65942b7992 --- /dev/null +++ b/firmware/util/math/efi_ratio.h @@ -0,0 +1,17 @@ +#pragma once + +namespace efi { + +template +class ratio { +private: + static constexpr int num = TNum; + static constexpr int den = TDenom; + +public: + static float asFloat() { + return (float)num / den; + } +}; + +} // namespace efi diff --git a/unit_tests/tests/test_boost.cpp b/unit_tests/tests/test_boost.cpp index f6f4c8720d..f8258c9464 100644 --- a/unit_tests/tests/test_boost.cpp +++ b/unit_tests/tests/test_boost.cpp @@ -12,7 +12,7 @@ TEST(BoostControl, Setpoint) { // Just pass TPS input to output EXPECT_CALL(targetMap, getValue(_, _)) - .WillRepeatedly([](float xRpm, float tps) { return tps; }); + .WillRepeatedly([](float xRpm, float tps) { return tps * 2; }); WITH_ENGINE_TEST_HELPER(TEST_ENGINE); @@ -54,7 +54,7 @@ TEST(BoostControl, OpenLoop) { // Just pass MAP input to output EXPECT_CALL(openMap, getValue(_, _)) - .WillRepeatedly([](float xRpm, float tps) { return tps; }); + .WillRepeatedly([](float xRpm, float tps) { return tps * 2; }); WITH_ENGINE_TEST_HELPER(TEST_ENGINE);