From 7d5df29fbdcdef67e355103a76c827e20201a1ed Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 5 Apr 2020 06:11:25 -0700 Subject: [PATCH] yet more tps consumers (#1258) * advance_map * tests * unneeded * idle * use driver intent instead * and obd and lcd * engine load * ve lookup * unused * oops we needed that * oops needed that too * mocking * test mocks * oops * helps to use the right sensor * and cylinder cleanup * fuel math * typo * old tests * kill dead stuff * cleanup * more * fix * test * s * fix signature * fix test * comment * priming pulse * remove mock tps * remove more dead Co-authored-by: Matthew Kennedy --- .../actuators/electronic_throttle.cpp | 4 +- .../controllers/algo/accel_enrichment.cpp | 3 +- firmware/controllers/algo/engine.h | 3 - firmware/controllers/algo/fuel_math.cpp | 6 +- firmware/controllers/engine_controller.h | 2 - .../controllers/engine_controller_misc.cpp | 4 - .../engine_cycle/main_trigger_callback.cpp | 7 +- firmware/controllers/sensors/tps.cpp | 81 +------------------ firmware/controllers/sensors/tps.h | 27 ++----- firmware/controllers/settings.cpp | 3 +- unit_tests/tests/test_accel_enrichment.cpp | 6 +- unit_tests/tests/test_fuelCut.cpp | 9 +-- unit_tests/tests/test_sensors.cpp | 31 ------- 13 files changed, 27 insertions(+), 159 deletions(-) diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 43d98e4edb..7e2cecc4e4 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -311,7 +311,7 @@ static void showEthInfo(void) { scheduleMsg(&logger, "etbAutoTune=%d", engine->etbAutoTune); - scheduleMsg(&logger, "TPS=%.2f", getTPS(PASS_ENGINE_PARAMETER_SIGNATURE)); + scheduleMsg(&logger, "TPS=%.2f", Sensor::get(SensorType::Tps1).value_or(0)); scheduleMsg(&logger, "etbControlPin1=%s duty=%.2f freq=%d", @@ -549,7 +549,7 @@ void doInitElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return; } - engine->etbActualCount = hasSecondThrottleBody(PASS_ENGINE_PARAMETER_SIGNATURE) ? 2 : 1; + engine->etbActualCount = Sensor::hasSensor(SensorType::Tps2) ? 2 : 1; for (int i = 0 ; i < engine->etbActualCount; i++) { auto motor = initDcMotor(i PASS_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/algo/accel_enrichment.cpp b/firmware/controllers/algo/accel_enrichment.cpp index 1d2e788334..9c5f9098e7 100644 --- a/firmware/controllers/algo/accel_enrichment.cpp +++ b/firmware/controllers/algo/accel_enrichment.cpp @@ -285,8 +285,7 @@ void AccelEnrichment::onNewValue(float currentValue DECLARE_ENGINE_PARAMETER_SUF } void TpsAccelEnrichment::onEngineCycleTps(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - // we update values in handleFuel() directly - //onNewValue(getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) PASS_ENGINE_PARAMETER_SUFFIX); + // we update values in handleFuel() directly by calling onNewValue() onUpdateInvocationCounter++; diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 963ae08d45..f1a18cc2f7 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -106,9 +106,6 @@ public: #if !EFI_PROD_CODE float mockMapValue = 0; - // for historical reasons we have options to mock TPS on different layers :( - int mockTpsAdcValue = 0; - float mockTpsValue = NAN; #endif int getGlobalConfigurationVersion(void) const; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 00c42ac16d..bc1a1c6660 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -350,7 +350,11 @@ float getFuelCutOffCorrection(efitick_t nowNt, int rpm DECLARE_ENGINE_PARAMETER_ // coasting fuel cut-off correction if (CONFIG(coastingFuelCutEnabled)) { - percent_t tpsPos = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); + auto [valid, tpsPos] = Sensor::get(SensorType::Tps1); + if (!valid) { + return 1.0f; + } + float map = getMap(PASS_ENGINE_PARAMETER_SIGNATURE); // gather events diff --git a/firmware/controllers/engine_controller.h b/firmware/controllers/engine_controller.h index da82b6bb72..c70d28fbe1 100644 --- a/firmware/controllers/engine_controller.h +++ b/firmware/controllers/engine_controller.h @@ -26,8 +26,6 @@ void setMockVoltage(int hwChannel, float voltage DECLARE_ENGINE_PARAMETER_SUFFIX void setMockVBattVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); -// throttle body sensor -void setMockThrottlePositionSensorVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMafVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockIatVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index ab8d7fecec..4b13150f41 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -51,10 +51,6 @@ void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { setMockVoltage(engineConfiguration->afr.hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); } -void setMockThrottlePositionSensorVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { - setMockVoltage(engineConfiguration->tps1_1AdcChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); -} - void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { setMockVoltage(engineConfiguration->map.sensor.hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); } diff --git a/firmware/controllers/engine_cycle/main_trigger_callback.cpp b/firmware/controllers/engine_cycle/main_trigger_callback.cpp index 7770104bb0..112ac03e52 100644 --- a/firmware/controllers/engine_cycle/main_trigger_callback.cpp +++ b/firmware/controllers/engine_cycle/main_trigger_callback.cpp @@ -48,6 +48,7 @@ #include "event_queue.h" #include "engine.h" #include "perf_trace.h" +#include "sensor.h" #include "backup_ram.h" @@ -276,7 +277,7 @@ static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #if ! EFI_UNIT_TEST if (GET_RPM_VALUE < CONFIG(fuelClosedLoopRpmThreshold) || getCoolantTemperature() < CONFIG(fuelClosedLoopCltThreshold) || - getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) > CONFIG(fuelClosedLoopTpsThreshold) || + Sensor::get(SensorType::Tps1).value_or(100) > CONFIG(fuelClosedLoopTpsThreshold) || ENGINE(sensors.currentAfr) < CONFIG(fuelClosedLoopAfrLowThreshold) || ENGINE(sensors.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) { engine->engineState.running.pidCorrection = 0; @@ -323,7 +324,7 @@ static ALWAYS_INLINE void handleFuel(const bool limitedFuel, uint32_t trgEventIn scheduleMsg(logger, "handleFuel ind=%d %d", trgEventIndex, getRevolutionCounter()); #endif /* FUEL_MATH_EXTREME_LOGGING */ - ENGINE(tpsAccelEnrichment.onNewValue(getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) PASS_ENGINE_PARAMETER_SUFFIX)); + ENGINE(tpsAccelEnrichment.onNewValue(Sensor::get(SensorType::Tps1).value_or(0) PASS_ENGINE_PARAMETER_SUFFIX)); if (trgEventIndex == 0) { ENGINE(tpsAccelEnrichment.onEngineCycleTps(PASS_ENGINE_PARAMETER_SIGNATURE)); ENGINE(engineLoadAccelEnrichment.onEngineCycle(PASS_ENGINE_PARAMETER_SIGNATURE)); @@ -453,7 +454,7 @@ static void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEvent static bool isPrimeInjectionPulseSkipped(DECLARE_ENGINE_PARAMETER_SIGNATURE) { if (!engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE)) return true; - return CONFIG(isCylinderCleanupEnabled) && (getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) > CLEANUP_MODE_TPS); + return CONFIG(isCylinderCleanupEnabled) && (Sensor::get(SensorType::Tps1).value_or(0) > CLEANUP_MODE_TPS); } /** diff --git a/firmware/controllers/sensors/tps.cpp b/firmware/controllers/sensors/tps.cpp index 7d332cf5af..0e554aaa67 100644 --- a/firmware/controllers/sensors/tps.cpp +++ b/firmware/controllers/sensors/tps.cpp @@ -11,63 +11,11 @@ EXTERN_ENGINE; -#if !EFI_PROD_CODE -/** - * this allows unit tests to simulate TPS position - */ -void setMockTpsAdc(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX) { - engine->mockTpsAdcValue = tpsPosition; -} - -void setMockTpsValue(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX) { - engine->mockTpsValue = tpsPosition; -} -#endif /* EFI_PROD_CODE */ - -/** - * We are using one instance for read and another for modification, this is how we get lock-free thread-safety - * - */ -static tps_roc_s states[2]; - -// todo if TPS_FAST_ADC -//int tpsFastAdc = 0; - -static volatile int tpsRocIndex = 0; - -/** - * this method is lock-free thread-safe if invoked only from one thread - */ -void saveTpsState(efitimeus_t now, float curValue) { - int tpsNextIndex = (tpsRocIndex + 1) % 2; - tps_roc_s *cur = &states[tpsRocIndex]; - tps_roc_s *next = &states[tpsNextIndex]; - - next->prevTime = cur->curTime; - next->prevValue = cur->curValue; - next->curTime = now; - next->curValue = curValue; - - //int diffSysticks = overflowDiff(now, cur->curTime); - float diffSeconds = 0;// TODO: do we need this? diffSysticks * 1.0 / CH_FREQUENCY; - next->rateOfChange = (curValue - cur->curValue) / diffSeconds; - - // here we update volatile index - tpsRocIndex = tpsNextIndex; -} - -/** - * this read-only method is lock-free thread-safe - */ -float getTpsRateOfChange(void) { - return states[tpsRocIndex].rateOfChange; -} - /* * Return current TPS position based on configured ADC levels, and adc * * */ -percent_t getTpsValue(int index, float adc DECLARE_ENGINE_PARAMETER_SUFFIX) { +static percent_t getTpsValue(int index, float adc DECLARE_ENGINE_PARAMETER_SUFFIX) { DISPLAY_STATE(Engine) DISPLAY_TAG(TPS_SECTION); @@ -132,11 +80,6 @@ percent_t getTpsValue(int index, float adc DECLARE_ENGINE_PARAMETER_SUFFIX) { * @param index [0, ETB_COUNT) */ static float getTPS10bitAdc(int index DECLARE_ENGINE_PARAMETER_SUFFIX) { -#if !EFI_PROD_CODE - if (engine->mockTpsAdcValue != MOCK_UNDEFINED) { - return engine->mockTpsAdcValue; - } -#endif if (engineConfiguration->tps1_1AdcChannel == EFI_ADC_NONE) return -1; #if EFI_PROD_CODE @@ -195,34 +138,16 @@ static percent_t getPrimaryRawTPS(int index DECLARE_ENGINE_PARAMETER_SUFFIX) { #define NO_TPS_MAGIC_VALUE 66.611 -bool hasSecondThrottleBody(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return engineConfiguration->tps2_1AdcChannel != EFI_ADC_NONE; -} - static bool hasTpsSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return engineConfiguration->tps1_1AdcChannel != EFI_ADC_NONE; } -percent_t getTPSWithIndex(int index DECLARE_ENGINE_PARAMETER_SUFFIX) { -#if !EFI_PROD_CODE - if (!cisnan(engine->mockTpsValue)) { - return engine->mockTpsValue; - } -#endif /* EFI_PROD_CODE */ +percent_t getTPS(DECLARE_ENGINE_PARAMETER_SIGNATURE) { if (!hasTpsSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) return NO_TPS_MAGIC_VALUE; // todo: if (config->isDualTps) // todo: blah blah // todo: if two TPS do not match - show OBD code via malfunction_central.c - return getPrimaryRawTPS(index PASS_ENGINE_PARAMETER_SUFFIX); -} - -percent_t getTPS(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - return getTPSWithIndex(0 PASS_ENGINE_PARAMETER_SUFFIX); -} - -int convertVoltageTo10bitADC(float voltage) { - // divided by 2 because of voltage divider, then converted into 10bit ADC value (TunerStudio format) - return (int) (voltage * TPS_TS_CONVERSION); + return getPrimaryRawTPS(0 PASS_ENGINE_PARAMETER_SUFFIX); } diff --git a/firmware/controllers/sensors/tps.h b/firmware/controllers/sensors/tps.h index d74ef6e89c..69fea33c82 100644 --- a/firmware/controllers/sensors/tps.h +++ b/firmware/controllers/sensors/tps.h @@ -21,29 +21,12 @@ * @return Current TPS position, percent of WOT. 0 means idle and 100 means Wide Open Throttle */ percent_t getTPS(DECLARE_ENGINE_PARAMETER_SIGNATURE); -percent_t getTPSWithIndex(int index DECLARE_ENGINE_PARAMETER_SUFFIX); -int convertVoltageTo10bitADC(float voltage); -bool hasSecondThrottleBody(DECLARE_ENGINE_PARAMETER_SIGNATURE); -percent_t getTpsValue(int index, float adc DECLARE_ENGINE_PARAMETER_SUFFIX); -void setMockTpsAdc(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX); -void setMockTpsValue(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX); + +constexpr inline int convertVoltageTo10bitADC(float voltage) { + return (int) (voltage * TPS_TS_CONVERSION); +} + void grabTPSIsClosed(); void grabTPSIsWideOpen(); void grabPedalIsUp(); void grabPedalIsWideOpen(); - -typedef struct { - efitimeus_t prevTime; - // value 0-100% - float prevValue; - efitimeus_t curTime; - // value 0-100% - float curValue; - // % per second - float rateOfChange; -} tps_roc_s; - -//void saveTpsState(efitimeus_t now, float curValue); -float getTpsRateOfChange(void); - - diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index c05030ff2a..f735bac272 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -408,7 +408,7 @@ static void printTpsSenser(const char *msg, SensorType sensor, int16_t min, int1 raw, getPinNameByAdcChannel(msg, channel, pinNameBuffer)); - scheduleMsg(&logger, "current 10bit=%d value=%.2f rate=%.2f", convertVoltageTo10bitADC(raw), tps.Value, getTpsRateOfChange()); + scheduleMsg(&logger, "current 10bit=%d value=%.2f", convertVoltageTo10bitADC(raw), tps.Value); } void printTPSInfo(void) { @@ -1174,7 +1174,6 @@ const command_f_s commandsF[] = { #if EFI_ENGINE_CONTROL #if EFI_ENABLE_MOCK_ADC {MOCK_IAT_COMMAND, setMockIatVoltage}, - {MOCK_TPS_COMMAND, setMockThrottlePositionSensorVoltage}, {MOCK_MAF_COMMAND, setMockMafVoltage}, {MOCK_AFR_COMMAND, setMockAfrVoltage}, {MOCK_MAP_COMMAND, setMockMapVoltage}, diff --git a/unit_tests/tests/test_accel_enrichment.cpp b/unit_tests/tests/test_accel_enrichment.cpp index b61e1c1e7f..fffbf3895d 100644 --- a/unit_tests/tests/test_accel_enrichment.cpp +++ b/unit_tests/tests/test_accel_enrichment.cpp @@ -10,7 +10,7 @@ #include "engine_test_helper.h" #include "accel_enrichment.h" -#include "tps.h" +#include "sensor.h" TEST(fuel, testTpsAccelEnrichmentMath) { printf("====================================================================================== testAccelEnrichment\r\n"); @@ -47,14 +47,14 @@ TEST(fuel, testTpsAccelEnrichmentScheduling) { eth.setTriggerType(TT_ONE PASS_ENGINE_PARAMETER_SUFFIX); - setMockTpsValue(7 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, 7); eth.fireTriggerEvents2(/* count */ 5, 25 /* ms */); ASSERT_EQ( 1200, GET_RPM()) << "RPM"; int expectedInvocationCounter = 1; ASSERT_EQ(expectedInvocationCounter, ENGINE(tpsAccelEnrichment).onUpdateInvocationCounter); - setMockTpsValue(70 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, 70); eth.fireTriggerEvents2(/* count */ 1, 25 /* ms */); float expectedAEValue = 29.2; diff --git a/unit_tests/tests/test_fuelCut.cpp b/unit_tests/tests/test_fuelCut.cpp index c56c2e4a9a..73a25cd5cf 100644 --- a/unit_tests/tests/test_fuelCut.cpp +++ b/unit_tests/tests/test_fuelCut.cpp @@ -8,7 +8,7 @@ #include "engine_math.h" #include "engine_test_helper.h" #include "event_queue.h" -#include "tps.h" +#include "sensor.h" #include "fsio_impl.h" TEST(fuelCut, coasting) { @@ -25,9 +25,6 @@ TEST(fuelCut, coasting) { engineConfiguration->coastingFuelCutMap = 100; // set cranking threshold engineConfiguration->cranking.rpm = 999; - // configure TPS - engineConfiguration->tpsMin = 0; - engineConfiguration->tpsMax = 10; // basic engine setup setupSimpleTestEngineWithMafAndTT_ONE_trigger(ð); @@ -35,7 +32,7 @@ TEST(fuelCut, coasting) { // mock CLT - just above threshold ('hot engine') float hotClt = engine->sensors.clt = engineConfiguration->coastingFuelCutClt + 1; // mock TPS - throttle is opened - setMockTpsAdc(6 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, 60); // set 'running' RPM - just above RpmHigh threshold engine->rpmCalculator.mockRpm = engineConfiguration->coastingFuelCutRpmHigh + 1; // 'advance' time (amount doesn't matter) @@ -53,7 +50,7 @@ TEST(fuelCut, coasting) { assertEqualsM("inj dur#1 norm", normalInjDuration, ENGINE(injectionDuration)); // 'releasing' the throttle - setMockTpsAdc(0 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::Tps1, 0); eth.engine.periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE); // Fuel cut-off is enabled now diff --git a/unit_tests/tests/test_sensors.cpp b/unit_tests/tests/test_sensors.cpp index afc59d5bba..1689c962fc 100644 --- a/unit_tests/tests/test_sensors.cpp +++ b/unit_tests/tests/test_sensors.cpp @@ -10,8 +10,6 @@ #include "allsensors.h" #include "engine_test_helper.h" - - TEST(sensors, mapDecoding) { WITH_ENGINE_TEST_HELPER(FORD_INLINE_6_1995); @@ -26,35 +24,6 @@ TEST(sensors, mapDecoding) { ASSERT_FLOAT_EQ(58.4, decodePressure(1, &s PASS_ENGINE_PARAMETER_SUFFIX)); } -TEST(sensors, tps) { - print("************************************************** testTps\r\n"); - - WITH_ENGINE_TEST_HELPER(DODGE_RAM); - - engineConfiguration->tpsMax = 193; - engineConfiguration->tpsMin = 43; - - ASSERT_NEAR(49.3333, getTpsValue(0, 117 PASS_ENGINE_PARAMETER_SUFFIX), EPS4D); - - - engineConfiguration->tpsMax = 43; - engineConfiguration->tpsMin = 193; - assertEqualsM("test#2", 50.6667, getTpsValue(0, 117 PASS_ENGINE_PARAMETER_SUFFIX)); -} - -TEST(sensors, testTpsRateOfChange) { - print("************************************************** testTpsRateOfChange\r\n"); -// saveTpsState(0, 0); -// saveTpsState(CH_FREQUENCY, 50); -// assertEquals(50, getTpsRateOfChange()); -// -// saveTpsState(2 * CH_FREQUENCY, 50); -// assertEquals(0, getTpsRateOfChange()); -// -// saveTpsState(3 * CH_FREQUENCY, 75); -// assertEquals(25, getTpsRateOfChange()); -} - TEST(sensors, Thermistor1) { ThermistorMath tm;