diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 13863cf7ff..86a70b4f8e 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -269,6 +269,11 @@ expected EtbController::getSetpointWastegate() const { } expected EtbController::getSetpointEtb() const { + // Autotune runs with 50% target position + if (m_isAutotune) { + return 50.0f; + } + // A few extra preconditions if throttle control is invalid if (startupPositionError) { return unexpected; @@ -326,9 +331,10 @@ expected EtbController::getOpenLoop(percent_t target) const { return ff; } -expected EtbController::getClosedLoopAutotune(percent_t actualThrottlePosition) { - // Estimate gain at 60% position - this should be well away from the spring and in the linear region - bool isPositive = actualThrottlePosition > 60.0f; +expected EtbController::getClosedLoopAutotune(percent_t target, percent_t actualThrottlePosition) { + // Estimate gain at current position - this should be well away from the spring and in the linear region + // GetSetpoint sets this to 50% + bool isPositive = actualThrottlePosition > target; float autotuneAmplitude = 20; @@ -444,10 +450,8 @@ expected EtbController::getClosedLoop(percent_t target, percent_t obs } // Only allow autotune with stopped engine, and on the first throttle - if (GET_RPM() == 0 - && engine->etbAutoTune - && m_function == ETB_Throttle1) { - return getClosedLoopAutotune(observation); + if (m_isAutotune) { + return getClosedLoopAutotune(target, observation); } else { // Check that we're not over the error limit float errorIntegral = m_errorAccumulator.accumulate(target - observation); @@ -539,6 +543,11 @@ void EtbController::update() { m_pid.showPidStatus(&logger, "ETB"); } + // Update local state about autotune + m_isAutotune = GET_RPM() == 0 + && engine->etbAutoTune + && m_function == ETB_Throttle1; + ClosedLoopController::update(); DISPLAY_STATE(Engine) diff --git a/firmware/controllers/actuators/electronic_throttle_impl.h b/firmware/controllers/actuators/electronic_throttle_impl.h index 8d2da53f45..a23a0c7e0b 100644 --- a/firmware/controllers/actuators/electronic_throttle_impl.h +++ b/firmware/controllers/actuators/electronic_throttle_impl.h @@ -52,7 +52,7 @@ public: expected getOpenLoop(percent_t target) const override; expected getClosedLoop(percent_t setpoint, percent_t observation) override; - expected getClosedLoopAutotune(percent_t actualThrottlePosition); + expected getClosedLoopAutotune(percent_t setpoint, percent_t actualThrottlePosition); void setOutput(expected outputValue) override; @@ -83,6 +83,9 @@ private: float m_idlePosition = 0; float m_wastegatePosition = 0; + // This is set if automatic PID cal shoudl be run + bool m_isAutotune = false; + // Autotune helpers bool m_lastIsPositive = false; efitick_t m_cycleStartTime = 0;