From 8156e8f75205fedf536d747ecdf7978e4b07f7b4 Mon Sep 17 00:00:00 2001 From: rusefi Date: Thu, 22 Nov 2018 23:42:30 -0500 Subject: [PATCH] PID auto tune unit test --- firmware/controllers/electronic_throttle.cpp | 19 +++++++++++++++++-- firmware/controllers/math/pid_auto_tune.cpp | 4 +++- unit_tests/test_pid_auto.cpp | 6 +++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/firmware/controllers/electronic_throttle.cpp b/firmware/controllers/electronic_throttle.cpp index 990ec72a1e..b8505a0af5 100644 --- a/firmware/controllers/electronic_throttle.cpp +++ b/firmware/controllers/electronic_throttle.cpp @@ -66,6 +66,8 @@ extern TunerStudioOutputChannels tsOutputChannels; #endif /* EFI_TUNER_STUDIO */ static bool shouldResetPid = false; +static pid_s tuneWorkingPidSettings; +static Pid tuneWorkingPid(&tuneWorkingPidSettings); static PID_AutoTune autoTune; static LoggingWithStorage logger("ETB"); @@ -122,9 +124,13 @@ static msg_t etbThread(void *arg) { autoTune.input = actualThrottlePosition; autoTune.Runtime(&logger); - etbPwmUp.setSimplePwmDutyCycle(autoTune.output); + tuneWorkingPid.updateFactors(autoTune.output, 0, 0); - pid.sleep(); + float value = tuneWorkingPid.getValue(50, actualThrottlePosition); + scheduleMsg(&logger, "output %f value=%f", autoTune.output, value); + etbPwmUp.setSimplePwmDutyCycle(value); + + tuneWorkingPid.sleep(); continue; } @@ -290,6 +296,15 @@ void initElectronicThrottle(void) { // addConsoleActionF("set_etb", setThrottleDutyCycle); + + tuneWorkingPidSettings.pFactor = 1; + tuneWorkingPidSettings.iFactor = 0; + tuneWorkingPidSettings.dFactor = 0; + tuneWorkingPidSettings.offset = 10; // todo: not hard-coded value + //todo tuneWorkingPidSettings.period = 10; + tuneWorkingPidSettings.minValue = 0; + tuneWorkingPidSettings.maxValue = 100; + // this is useful one you do "enable etb_auto" addConsoleActionF("set_etb_output", setTempOutput); addConsoleActionF("set_etb_step", setTempStep); diff --git a/firmware/controllers/math/pid_auto_tune.cpp b/firmware/controllers/math/pid_auto_tune.cpp index 897a986b2f..c87a0f265c 100644 --- a/firmware/controllers/math/pid_auto_tune.cpp +++ b/firmware/controllers/math/pid_auto_tune.cpp @@ -49,6 +49,7 @@ PID_AutoTune::PID_AutoTune() { noiseBand = 0.5; setState(AUTOTUNER_OFF); oStep = 10.0; + input = output = 0; SetLookbackSec(10); } @@ -112,7 +113,7 @@ void PID_AutoTune::setPeakType(PidAutoTune_Peak peakType) { /** * returns true when done, otherwise returns false */ -bool PID_AutoTune::Runtime(Logging *logging) +bool PID_AutoTune::Runtime(Logging *logger) { // check ready for new input unsigned long now = currentTimeMillis(); @@ -738,6 +739,7 @@ bool PID_AutoTune::Runtime(Logging *logging) #if EFI_UNIT_TEST printf("Happy end AMIGOF_PI!\r\n"); #endif /* EFI_UNIT_TEST */ + scheduleMsg(logger, "output %f", output); // converged return true; } diff --git a/unit_tests/test_pid_auto.cpp b/unit_tests/test_pid_auto.cpp index c98a2c8fae..07168bde12 100644 --- a/unit_tests/test_pid_auto.cpp +++ b/unit_tests/test_pid_auto.cpp @@ -175,11 +175,11 @@ static void testPidAutoZigZagZero() { printf("loop=%d %d\r\n", i, startMockMs); for (; mockTimeMs < CYCLE + startMockMs; mockTimeMs++) { at.input = zigZagValue(mockTimeMs); -// bool result = at.Runtime(&logging); -// assertFalseM("should be false#4", result); + bool result = at.Runtime(&logging); + assertFalseM("should be false#4", result); } - oscRange *= 1.5; } + // nothing happens in this test since we do not allow time play a role } void testPidAutoZigZag() {