From 7dc20b368ab722b29108e4f8c18f6fac2b40a1f3 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 5 Dec 2021 15:33:50 -0800 Subject: [PATCH] Alternator controller is engine module (#3663) * alternator is engine module * user can't set pid period --- .../actuators/alternator_controller.cpp | 104 +++++++++--------- .../actuators/alternator_controller.h | 5 + firmware/controllers/algo/engine.h | 5 +- firmware/tunerstudio/rusefi.input | 1 - 4 files changed, 58 insertions(+), 57 deletions(-) diff --git a/firmware/controllers/actuators/alternator_controller.cpp b/firmware/controllers/actuators/alternator_controller.cpp index 436bee734b..ae393f0edf 100644 --- a/firmware/controllers/actuators/alternator_controller.cpp +++ b/firmware/controllers/actuators/alternator_controller.cpp @@ -31,76 +31,72 @@ static void pidReset() { alternatorPid.reset(); } -class AlternatorController : public PeriodicTimerController { - int getPeriodMs() override { - return GET_PERIOD_LIMITED(&engineConfiguration->alternatorControl); +void AlternatorController::onFastCallback() { + if (!isBrainPinValid(engineConfiguration->alternatorControlPin)) { + return; } - void PeriodicTask() override { #if ! EFI_UNIT_TEST - if (shouldResetPid) { - pidReset(); - shouldResetPid = false; - } + if (shouldResetPid) { + pidReset(); + shouldResetPid = false; + } #endif - // this block could be executed even in on/off alternator control mode - // but at least we would reflect latest state + // this block could be executed even in on/off alternator control mode + // but at least we would reflect latest state #if EFI_TUNER_STUDIO - alternatorPid.postState(&tsOutputChannels.alternatorStatus); + alternatorPid.postState(&tsOutputChannels.alternatorStatus); #endif /* EFI_TUNER_STUDIO */ - // todo: migrate this to FSIO - bool alternatorShouldBeEnabledAtCurrentRpm = GET_RPM() > engineConfiguration->cranking.rpm; + // todo: migrate this to FSIO + bool alternatorShouldBeEnabledAtCurrentRpm = GET_RPM() > engineConfiguration->cranking.rpm; - if (!engineConfiguration->isAlternatorControlEnabled || !alternatorShouldBeEnabledAtCurrentRpm) { - // we need to avoid accumulating iTerm while engine is not running - pidReset(); + if (!engineConfiguration->isAlternatorControlEnabled || !alternatorShouldBeEnabledAtCurrentRpm) { + // we need to avoid accumulating iTerm while engine is not running + pidReset(); - // Shut off output if not needed - alternatorControl.setSimplePwmDutyCycle(0); + // Shut off output if not needed + alternatorControl.setSimplePwmDutyCycle(0); - return; - } + return; + } - auto vBatt = Sensor::get(SensorType::BatteryVoltage); - float targetVoltage = engineConfiguration->targetVBatt; - - // todo: I am not aware of a SINGLE person to use this onOffAlternatorLogic - if (engineConfiguration->onOffAlternatorLogic) { - if (!vBatt) { - // Somehow battery voltage isn't valid, disable alternator control - enginePins.alternatorPin.setValue(false); - } - - float h = 0.1; - bool newState = (vBatt.Value < targetVoltage - h) || (currentPlainOnOffState && vBatt.Value < targetVoltage); - enginePins.alternatorPin.setValue(newState); - currentPlainOnOffState = newState; -#if EFI_TUNER_STUDIO - tsOutputChannels.alternatorOnOff = newState; -#endif /* EFI_TUNER_STUDIO */ - - return; - } + auto vBatt = Sensor::get(SensorType::BatteryVoltage); + float targetVoltage = engineConfiguration->targetVBatt; + // todo: I am not aware of a SINGLE person to use this onOffAlternatorLogic + if (engineConfiguration->onOffAlternatorLogic) { if (!vBatt) { // Somehow battery voltage isn't valid, disable alternator control - alternatorPid.reset(); - alternatorControl.setSimplePwmDutyCycle(0); - } else { - currentAltDuty = alternatorPid.getOutput(targetVoltage, vBatt.Value); - if (engineConfiguration->isVerboseAlternator) { - efiPrintf("alt duty: %.2f/vbatt=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", currentAltDuty, vBatt.Value, - alternatorPid.getP(), alternatorPid.getI(), alternatorPid.getD(), alternatorPid.getIntegration()); - } - - alternatorControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(currentAltDuty)); + enginePins.alternatorPin.setValue(false); } - } -}; -static AlternatorController instance; + float h = 0.1; + bool newState = (vBatt.Value < targetVoltage - h) || (currentPlainOnOffState && vBatt.Value < targetVoltage); + enginePins.alternatorPin.setValue(newState); + currentPlainOnOffState = newState; +#if EFI_TUNER_STUDIO + tsOutputChannels.alternatorOnOff = newState; +#endif /* EFI_TUNER_STUDIO */ + + return; + } + + if (!vBatt) { + // Somehow battery voltage isn't valid, disable alternator control + alternatorPid.reset(); + alternatorControl.setSimplePwmDutyCycle(0); + } else { + currentAltDuty = alternatorPid.getOutput(targetVoltage, vBatt.Value, FAST_CALLBACK_PERIOD_MS / 1000.0f); + if (engineConfiguration->isVerboseAlternator) { + efiPrintf("alt duty: %.2f/vbatt=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", currentAltDuty, vBatt.Value, + alternatorPid.getP(), alternatorPid.getI(), alternatorPid.getD(), alternatorPid.getIntegration()); + } + + alternatorControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(currentAltDuty)); + } +} void showAltInfo(void) { efiPrintf("alt=%s @%s t=%dms", boolToString(engineConfiguration->isAlternatorControlEnabled), @@ -135,7 +131,6 @@ void initAlternatorCtrl() { &enginePins.alternatorPin, engineConfiguration->alternatorPwmFrequency, 0); } - instance.Start(); } // todo: start invoking this method like 'startVvtControlPins' @@ -147,5 +142,4 @@ void stopAlternatorPin(void) { // todo: implementation! } - #endif /* EFI_ALTERNATOR_CONTROL */ diff --git a/firmware/controllers/actuators/alternator_controller.h b/firmware/controllers/actuators/alternator_controller.h index c13e7231c1..69d5fed545 100644 --- a/firmware/controllers/actuators/alternator_controller.h +++ b/firmware/controllers/actuators/alternator_controller.h @@ -18,4 +18,9 @@ void setAltIFactor(float p); void setAltDFactor(float p); void showAltInfo(void); +class AlternatorController : public EngineModule { +public: + void onFastCallback() override; +}; + void onConfigurationChangeAlternatorCallback(engine_configuration_s *previousConfiguration); diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 297642d3a7..0cd591cbff 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -34,6 +34,7 @@ #include "ac_control.h" #include "type_list.h" #include "boost_control.h" +#include "alternator_controller.h" #ifndef EFI_UNIT_TEST #error EFI_UNIT_TEST must be defined! @@ -136,7 +137,9 @@ public: #if EFI_HPFP && EFI_ENGINE_CONTROL HpfpController, #endif // EFI_HPFP && EFI_ENGINE_CONTROL - +#if EFI_ALTERNATOR_CONTROL + AlternatorController, +#endif /* EFI_ALTERNATOR_CONTROL */ FuelPumpController, MainRelayController, AcController, diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index b346ce53f2..f2463185b2 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -2773,7 +2773,6 @@ cmd_set_engine_type_default = "@@TS_IO_TEST_COMMAND_char@@\x00\x31\x00\x00" field = "PWM frequency", alternatorPwmFrequency, {isAlternatorControlEnabled == 1 && onOffAlternatorLogic == 0} field = "Off Above TPS", alternatorOffAboveTps, {isAlternatorControlEnabled == 1} field = "Detailed status in console", isVerboseAlternator, {isAlternatorControlEnabled == 1} - field = "control period", alternatorControl_periodMs, {isAlternatorControlEnabled == 1} field = "#PID control" field = "offset", alternatorControl_offset, {isAlternatorControlEnabled == 1 && onOffAlternatorLogic == 0} field = "P factor", alternatorControl_pFactor, {isAlternatorControlEnabled == 1 && onOffAlternatorLogic == 0}