diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index 4a197843e5..0852266ba1 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -231,7 +231,7 @@ static bool isOutOfAutomaticIdleCondition(DECLARE_ENGINE_PARAMETER_SIGNATURE) { /** * @return idle valve position percentage for automatic closed loop mode */ -static percent_t automaticIdleController(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +static percent_t automaticIdleController(float tpsPos DECLARE_ENGINE_PARAMETER_SUFFIX) { if (isOutOfAutomaticIdleCondition(PASS_ENGINE_PARAMETER_SIGNATURE)) { // Don't store old I and D terms if PID doesn't work anymore. // Otherwise they will affect the idle position much later, when the throttle is closed. @@ -287,7 +287,6 @@ static percent_t automaticIdleController(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } // Apply PID Deactivation Threshold as a smooth taper for TPS transients. - percent_t tpsPos = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); // if tps==0 then PID just works as usual, or we completely disable it if tps>=threshold newValue = interpolateClamped(0.0f, newValue, CONFIG(idlePidDeactivationTpsThreshold), engine->engineState.idle.baseIdlePosition, tpsPos); @@ -326,7 +325,9 @@ static percent_t automaticIdleController(DECLARE_ENGINE_PARAMETER_SIGNATURE) { idlePid.iTermMin = engineConfiguration->idlerpmpid_iTermMin; idlePid.iTermMax = engineConfiguration->idlerpmpid_iTermMax; - engine->engineState.isAutomaticIdle = engineConfiguration->idleMode == IM_AUTO; + SensorResult tps = Sensor::get(SensorType::DriverThrottleIntent); + + engine->engineState.isAutomaticIdle = tps.Valid && engineConfiguration->idleMode == IM_AUTO; if (engineConfiguration->isVerboseIAC && engine->engineState.isAutomaticIdle) { // todo: print each bit using 'getIdle_state_e' method @@ -402,19 +403,21 @@ static percent_t automaticIdleController(DECLARE_ENGINE_PARAMETER_SIGNATURE) { lastCrankingCyclesCounter = engine->rpmCalculator.getRevolutionCounterSinceStart(); engine->engineState.idle.baseIdlePosition = iacPosition; } else { - if (engineConfiguration->idleMode == IM_MANUAL) { + if (!tps.Valid || engineConfiguration->idleMode == IM_MANUAL) { // let's re-apply CLT correction iacPosition = manualIdleController(cltCorrection PASS_ENGINE_PARAMETER_SUFFIX); } else { - iacPosition = automaticIdleController(PASS_ENGINE_PARAMETER_SIGNATURE); + iacPosition = automaticIdleController(tps.Value PASS_ENGINE_PARAMETER_SUFFIX); } // store 'base' iacPosition without adjustments engine->engineState.idle.baseIdlePosition = iacPosition; - - percent_t tpsPos = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); + float additionalAir = (float)engineConfiguration->iacByTpsTaper; - iacPosition += interpolateClamped(0.0f, 0.0f, CONFIG(idlePidDeactivationTpsThreshold), additionalAir, tpsPos); + + if (tps.Valid) { + iacPosition += interpolateClamped(0.0f, 0.0f, CONFIG(idlePidDeactivationTpsThreshold), additionalAir, tps.Value); + } // taper transition from cranking to running (uint32_t to float conversion is safe here) if (engineConfiguration->afterCrankingIACtaperDuration > 0) diff --git a/firmware/controllers/algo/advance_map.cpp b/firmware/controllers/algo/advance_map.cpp index 16ab54a05c..0f7eeb35a5 100644 --- a/firmware/controllers/algo/advance_map.cpp +++ b/firmware/controllers/algo/advance_map.cpp @@ -24,7 +24,7 @@ #include "advance_map.h" #include "interpolation.h" #include "engine_math.h" -#include "tps.h" +#include "sensor.h" #include "idle_thread.h" #include "allsensors.h" #include "launch_control.h" @@ -86,8 +86,9 @@ static angle_t getRunningAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAME float advanceAngle; if (CONFIG(useTPSAdvanceTable)) { - float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); - advanceAngle = advanceTpsMap.getValue((float) rpm, tps); + // TODO: what do we do about multi-TPS? + float tps = Sensor::get(SensorType::Tps1).value_or(0); + advanceAngle = advanceTpsMap.getValue(rpm, tps); } else { advanceAngle = advanceMap.getValue((float) rpm, engineLoad); } @@ -95,9 +96,12 @@ static angle_t getRunningAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAME // get advance from the separate table for Idle if (CONFIG(useSeparateAdvanceForIdle)) { float idleAdvance = interpolate2d("idleAdvance", rpm, config->idleAdvanceBins, config->idleAdvance); - // interpolate between idle table and normal (running) table using TPS threshold - float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); - advanceAngle = interpolateClamped(0.0f, idleAdvance, CONFIG(idlePidDeactivationTpsThreshold), advanceAngle, tps); + + auto [valid, tps] = Sensor::get(SensorType::DriverThrottleIntent); + if (valid) { + // interpolate between idle table and normal (running) table using TPS threshold + advanceAngle = interpolateClamped(0.0f, idleAdvance, CONFIG(idlePidDeactivationTpsThreshold), advanceAngle, tps); + } } @@ -130,8 +134,11 @@ angle_t getAdvanceCorrections(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) { if (CONFIG(useIdleTimingPidControl)) { int targetRpm = getTargetRpmForIdleCorrection(PASS_ENGINE_PARAMETER_SIGNATURE); int rpmDelta = absI(rpm - targetRpm); - float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); - if (tps >= CONFIG(idlePidDeactivationTpsThreshold)) { + + auto [valid, tps] = Sensor::get(SensorType::Tps1); + + // If TPS is invalid, or we aren't in the region, so reset state and don't apply PID + if (!valid || tps >= CONFIG(idlePidDeactivationTpsThreshold)) { // we are not in the idle mode anymore, so the 'reset' flag will help us when we return to the idle. shouldResetTimingPid = true; } diff --git a/firmware/controllers/can/obd2.cpp b/firmware/controllers/can/obd2.cpp index dbf74c8b7f..6825e73c2d 100644 --- a/firmware/controllers/can/obd2.cpp +++ b/firmware/controllers/can/obd2.cpp @@ -34,7 +34,7 @@ #include "vehicle_speed.h" #include "map.h" #include "maf.h" -#include "tps.h" +#include "sensor.h" #include "engine_math.h" #include "fuel_math.h" #include "thermistors.h" @@ -167,7 +167,7 @@ static void handleGetDataRequest(const CANRxFrame& rx) { break; case PID_THROTTLE: scheduleMsg(&logger, "Got throttle request"); - obdSendValue(1, pid, 1, getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) * 2.55f); // (A*100/255) + obdSendValue(1, pid, 1, Sensor::get(SensorType::Tps1).value_or(0) * 2.55f); // (A*100/255) break; case PID_FUEL_RATE: scheduleMsg(&logger, "Got fuel rate request"); diff --git a/firmware/controllers/gauges/lcd_controller.cpp b/firmware/controllers/gauges/lcd_controller.cpp index 9621b62c2d..7c45d9e73b 100644 --- a/firmware/controllers/gauges/lcd_controller.cpp +++ b/firmware/controllers/gauges/lcd_controller.cpp @@ -40,6 +40,7 @@ #include "mmc_card.h" #include "idle_thread.h" #include "fuel_math.h" +#include "sensor.h" EXTERN_ENGINE; @@ -190,7 +191,7 @@ static void showLine(lcd_line_e line, int screenY) { case LL_TPS: getPinNameByAdcChannel("tps", engineConfiguration->tps1_1AdcChannel, buffer); - lcdPrintf("Throttle %s %.2f%%", buffer, getTPS()); + lcdPrintf("Throttle %s %.2f%%", buffer, Sensor::get(SensorType::Tps1).value_or(0)); return; case LL_FUEL_CLT_CORRECTION: lcdPrintf("CLT corr %.2f", getCltFuelCorrection(PASS_ENGINE_PARAMETER_SIGNATURE)); diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index f1f33783ed..ef768c110b 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -14,6 +14,7 @@ #include "allsensors.h" #include "engine_controller.h" #include "advance_map.h" +#include "sensor.h" extern int timeNowUs; extern WarningCodeState unitTestWarningCodeState; @@ -28,6 +29,7 @@ EngineTestHelperBase::EngineTestHelperBase() { } EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback) { + Sensor::resetRegistry(); unitTestWarningCodeState.clear(); testMafValue = 0; diff --git a/unit_tests/tests/test_idle_controller.cpp b/unit_tests/tests/test_idle_controller.cpp index 4e1ee6c778..912feb42b0 100644 --- a/unit_tests/tests/test_idle_controller.cpp +++ b/unit_tests/tests/test_idle_controller.cpp @@ -14,6 +14,7 @@ #include "allsensors.h" #include "engine_controller.h" #include "electronic_throttle.h" +#include "sensor.h" extern IdleController idleControllerInstance; extern int timeNowUs; @@ -80,10 +81,8 @@ TEST(idle, timingPid) { eth.engine.engineState.cltTimingCorrection = 0; // configure TPS - engineConfiguration->tpsMin = 0; - engineConfiguration->tpsMax = 100; engineConfiguration->idlePidDeactivationTpsThreshold = 10; - setMockTpsAdc(0 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, 0); // disable temperature sensors eth.engine.sensors.clt = NAN; @@ -117,12 +116,12 @@ TEST(idle, timingPid) { ASSERT_FLOAT_EQ(-5.75f, corr) << "getAdvanceCorrections#5"; // check if PID correction is disabled in running mode (tps > threshold): - setMockTpsAdc(engineConfiguration->idlePidDeactivationTpsThreshold + 1 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, engineConfiguration->idlePidDeactivationTpsThreshold + 1); corr = getAdvanceCorrections(idleRpmTarget PASS_ENGINE_PARAMETER_SUFFIX); ASSERT_EQ(0, corr) << "getAdvanceCorrections#6"; // check if PID correction is interpolated for transient idle-running TPS positions - setMockTpsAdc(engineConfiguration->idlePidDeactivationTpsThreshold / 2 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, engineConfiguration->idlePidDeactivationTpsThreshold / 2); corr = getAdvanceCorrections(baseTestRpm PASS_ENGINE_PARAMETER_SUFFIX); ASSERT_FLOAT_EQ(-5.0f, corr) << "getAdvanceCorrections#7";