From 4964543f09afc0eebceb38b0e7df6d44b22821c9 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 15 Mar 2022 17:04:49 -0700 Subject: [PATCH] Etb2 trim (#4004) * wire up different logic for etb2 * clamp and actually works * s * happy compiler --- .../actuators/electronic_throttle.cpp | 62 ++++++++++++------- .../actuators/electronic_throttle_impl.h | 12 ++++ 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 714c8af1f8..e42d051ff3 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -315,6 +315,13 @@ expected EtbController::getSetpointEtb() const { // 100% target from table -> 100% target position percent_t targetPosition = interpolateClamped(0, etbIdleAddition, 100, 100, targetFromTable); + // Apply any adjustment from Lua + targetPosition += engine->engineState.luaAdjustments.etbTargetPositionAdd; + + // Apply any adjustment that this throttle alone needs + // Clamped to +-10 to prevent anything too wild + targetPosition += clampF(-10, getThrottleTrim(rpm, targetPosition), 10); + // Lastly, apply ETB rev limiter auto etbRpmLimit = engineConfiguration->etbRevLimitStart; if (etbRpmLimit != 0) { @@ -322,14 +329,6 @@ expected EtbController::getSetpointEtb() const { // Linearly taper throttle to closed from the limit across the range targetPosition = interpolateClamped(etbRpmLimit, targetPosition, fullyLimitedRpm, 0, rpm); } - // todo: this does not mix well with etbRevLimitStart interpolation does it? - targetPosition += engine->engineState.luaAdjustments.etbTargetPositionAdd; - -#if EFI_TUNER_STUDIO - if (m_function == ETB_Throttle1) { - engine->outputChannels.etbTarget = targetPosition; - } -#endif // EFI_TUNER_STUDIO // Keep the throttle just barely off the lower stop, and less than the user-configured maximum float maxPosition = engineConfiguration->etbMaximumPosition; @@ -341,7 +340,20 @@ expected EtbController::getSetpointEtb() const { maxPosition = minF(maxPosition, 100); } - return clampF(1, targetPosition, maxPosition); + targetPosition = clampF(1, targetPosition, maxPosition); + +#if EFI_TUNER_STUDIO + if (m_function == ETB_Throttle1) { + engine->outputChannels.etbTarget = targetPosition; + } +#endif // EFI_TUNER_STUDIO + + return targetPosition; +} + +percent_t EtbController2::getThrottleTrim(float /*rpm*/, percent_t /*targetPosition*/) const { + // TODO: implement me #3680 + return 0; } expected EtbController::getOpenLoop(percent_t target) { @@ -578,23 +590,25 @@ void EtbController::autoCalibrateTps() { * Since ETB is a safety critical device, we need the hard RTOS guarantee that it will be scheduled over other less important tasks. */ #include "periodic_thread_controller.h" -struct EtbImpl final : public EtbController { + +template +struct EtbImpl final : public TBase { void update() override { #if EFI_TUNER_STUDIO - if (m_isAutocal) { + if (TBase::m_isAutocal) { // Don't allow if engine is running! if (Sensor::getOrZero(SensorType::Rpm) > 0) { - m_isAutocal = false; + TBase::m_isAutocal = false; return; } - auto motor = getMotor(); + auto motor = TBase::getMotor(); if (!motor) { - m_isAutocal = false; + TBase::m_isAutocal = false; return; } - auto myFunction = getFunction(); + auto myFunction = TBase::getFunction(); // First grab open motor->set(0.5f); @@ -619,7 +633,7 @@ struct EtbImpl final : public EtbController { // Check that the calibrate actually moved the throttle if (absF(primaryMax - primaryMin) < 0.5f) { firmwareError(OBD_Throttle_Position_Sensor_Circuit_Malfunction, "Auto calibrate failed, check your wiring!\r\nClosed voltage: %.1fv Open voltage: %.1fv", primaryMin, primaryMax); - m_isAutocal = false; + TBase::m_isAutocal = false; return; } @@ -640,17 +654,21 @@ struct EtbImpl final : public EtbController { engine->outputChannels.calibrationMode = (uint8_t)TsCalMode::None; - m_isAutocal = false; + TBase::m_isAutocal = false; return; } #endif /* EFI_TUNER_STUDIO */ - EtbController::update(); + TBase::update(); } }; // real implementation (we mock for some unit tests) -static EtbImpl etbControllers[ETB_COUNT]; +static EtbImpl etb1; +static EtbImpl etb2; + +static_assert(ETB_COUNT == 2); +static EtbController* etbControllers[] = { &etb1, &etb2 }; struct EtbThread final : public PeriodicController<512> { EtbThread() : PeriodicController("ETB", PRIO_ETB, ETB_LOOP_FREQUENCY) {} @@ -658,7 +676,7 @@ struct EtbThread final : public PeriodicController<512> { void PeriodicTask(efitick_t) override { // Simply update all controllers for (int i = 0 ; i < ETB_COUNT; i++) { - etbControllers[i].update(); + etbControllers[i]->update(); } } }; @@ -862,7 +880,7 @@ void setDefaultEtbParameters() { void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) { #if !EFI_UNIT_TEST for (int i = 0; i < ETB_COUNT; i++) { - etbControllers[i].onConfigurationChange(&previousConfiguration->etb); + etbControllers[i]->onConfigurationChange(&previousConfiguration->etb); } #endif } @@ -983,7 +1001,7 @@ void initElectronicThrottle() { #if !EFI_UNIT_TEST for (int i = 0; i < ETB_COUNT; i++) { - engine->etbControllers[i] = &etbControllers[i]; + engine->etbControllers[i] = etbControllers[i]; } #endif diff --git a/firmware/controllers/actuators/electronic_throttle_impl.h b/firmware/controllers/actuators/electronic_throttle_impl.h index 00dcf6493d..d65c59ce92 100644 --- a/firmware/controllers/actuators/electronic_throttle_impl.h +++ b/firmware/controllers/actuators/electronic_throttle_impl.h @@ -60,6 +60,11 @@ public: // Use the throttle to automatically calibrate the relevant throttle position sensor(s). void autoCalibrateTps() override; + // Override if this throttle needs special per-throttle adjustment (bank-to-bank trim, for example) + virtual percent_t getThrottleTrim(float /*rpm*/, percent_t /*targetPosition*/) const { + return 0; + } + protected: // This is set if an automatic TPS calibration should be run bool m_isAutocal = false; @@ -98,3 +103,10 @@ private: uint8_t m_autotuneCounter = 0; uint8_t m_autotuneCurrentParam = 0; }; + +class EtbController1 : public EtbController { }; + +class EtbController2 : public EtbController { +public: + percent_t getThrottleTrim(float rpm, percent_t /*targetPosition*/) const override; +};