Alternator controller is engine module (#3663)

* alternator is engine module

* user can't set pid period
This commit is contained in:
Matthew Kennedy 2021-12-05 15:33:50 -08:00 committed by GitHub
parent da890de9b3
commit 7dc20b368a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 57 deletions

View File

@ -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 */

View File

@ -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);

View File

@ -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,

View File

@ -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}