boost format & cleanup (#1126)

This commit is contained in:
Matthew Kennedy 2020-02-04 17:17:31 -08:00 committed by GitHub
parent 9517da33c3
commit 20465107ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 65 additions and 69 deletions

View File

@ -40,78 +40,67 @@ static SimplePwm boostPwmControl("boost");
static pid_s *boostPidS = &persistentState.persistentConfiguration.engineConfiguration.boostPid; static pid_s *boostPidS = &persistentState.persistentConfiguration.engineConfiguration.boostPid;
static Pid boostControlPid(boostPidS); static Pid boostControlPid(boostPidS);
static bool shouldResetPid = false; static bool shouldResetPid = false;
#if EFI_TUNER_STUDIO #if EFI_TUNER_STUDIO
extern TunerStudioOutputChannels tsOutputChannels; extern TunerStudioOutputChannels tsOutputChannels;
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
static void pidReset(void) { static void pidReset(void) {
boostControlPid.reset(); boostControlPid.reset();
} }
class BoostControl: public PeriodicTimerController { class BoostControl: public PeriodicTimerController {
int getPeriodMs() int getPeriodMs() override {
override {
return GET_PERIOD_LIMITED(&engineConfiguration->boostPid); return GET_PERIOD_LIMITED(&engineConfiguration->boostPid);
} }
void PeriodicTask() override { void PeriodicTask() override {
if (shouldResetPid) { if (shouldResetPid) {
pidReset(); pidReset();
shouldResetPid = false; shouldResetPid = false;
}
#if EFI_TUNER_STUDIO
boostControlPid.postState(&tsOutputChannels);
#endif /* EFI_TUNER_STUDIO */
float rpm = GET_RPM_VALUE;
float mapValue = getMap(PASS_ENGINE_PARAMETER_SIGNATURE);
if (!engineConfiguration->isBoostControlEnabled)
return;
bool enabledAtEngineRunning = rpm > engineConfiguration->cranking.rpm;
if (!enabledAtEngineRunning) {
boostControlPid.reset();
return;
}
percent_t openLoopDuty = boostMapOpen.getValue(rpm / RPM_1_BYTE_PACKING_MULT, mapValue/ LOAD_1_BYTE_PACKING_MULT) * LOAD_1_BYTE_PACKING_MULT;
percent_t duty, closedLoopDuty = 0;
if (engineConfiguration->boostType == OPEN_LOOP) {
duty = openLoopDuty;
}
else if (engineConfiguration->boostType == CLOSED_LOOP) {
float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
float targetBoost = boostMapClosed.getValue(rpm / RPM_1_BYTE_PACKING_MULT, tps / TPS_1_BYTE_PACKING_MULT) * LOAD_1_BYTE_PACKING_MULT;
closedLoopDuty = openLoopDuty + boostControlPid.getOutput(targetBoost, mapValue);
duty = closedLoopDuty;
}
boostControlPid.iTermMin = -50;
boostControlPid.iTermMax = 50;
if (engineConfiguration->debugMode == DBG_BOOST) {
#if EFI_TUNER_STUDIO
tsOutputChannels.debugFloatField1 = openLoopDuty;
tsOutputChannels.debugFloatField7 = closedLoopDuty;
#endif /* EFI_TUNER_STUDIO */
}
boostPwmControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(duty));
} }
float rpm = GET_RPM_VALUE;
float mapValue = getMap(PASS_ENGINE_PARAMETER_SIGNATURE);
if (!engineConfiguration->isBoostControlEnabled)
return;
bool engineRunning = rpm > engineConfiguration->cranking.rpm;
if (!engineRunning) {
boostControlPid.reset();
return;
}
percent_t openLoopDuty = boostMapOpen.getValue(rpm / RPM_1_BYTE_PACKING_MULT, mapValue / LOAD_1_BYTE_PACKING_MULT) * LOAD_1_BYTE_PACKING_MULT;
percent_t closedLoopDuty = 0;
percent_t duty = openLoopDuty;
if (engineConfiguration->boostType == CLOSED_LOOP) {
float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
float targetBoost = boostMapClosed.getValue(rpm / RPM_1_BYTE_PACKING_MULT, tps / TPS_1_BYTE_PACKING_MULT) * LOAD_1_BYTE_PACKING_MULT;
closedLoopDuty = openLoopDuty + boostControlPid.getOutput(targetBoost, mapValue);
duty += closedLoopDuty;
}
boostControlPid.iTermMin = -50;
boostControlPid.iTermMax = 50;
if (engineConfiguration->debugMode == DBG_BOOST) {
#if EFI_TUNER_STUDIO
boostControlPid.postState(&tsOutputChannels);
tsOutputChannels.debugFloatField1 = openLoopDuty;
tsOutputChannels.debugFloatField7 = closedLoopDuty;
#endif /* EFI_TUNER_STUDIO */
}
boostPwmControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(duty));
}
}; };
static BoostControl BoostController; static BoostControl BoostController;
void setBoostPFactor(float value) { void setBoostPFactor(float value) {
engineConfiguration->boostPid.pFactor = value; engineConfiguration->boostPid.pFactor = value;
boostControlPid.reset(); boostControlPid.reset();
@ -123,15 +112,12 @@ void setBoostIFactor(float value) {
boostControlPid.reset(); boostControlPid.reset();
} }
void setBoostDFactor(float value) { void setBoostDFactor(float value) {
engineConfiguration->boostPid.dFactor = value; engineConfiguration->boostPid.dFactor = value;
boostControlPid.reset(); boostControlPid.reset();
} }
void setDefaultBoostParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE) { void setDefaultBoostParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE) {
engineConfiguration->isBoostControlEnabled = true; engineConfiguration->isBoostControlEnabled = true;
engineConfiguration->boostPwmFrequency = 55; engineConfiguration->boostPwmFrequency = 55;
engineConfiguration->boostPid.offset = 0; engineConfiguration->boostPid.offset = 0;
@ -146,33 +132,40 @@ void setDefaultBoostParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE) {
setLinearCurve(config->boostRpmBins, 0, 8000 / RPM_1_BYTE_PACKING_MULT, 1); setLinearCurve(config->boostRpmBins, 0, 8000 / RPM_1_BYTE_PACKING_MULT, 1);
setLinearCurve(config->boostMapBins, 0, 300 / LOAD_1_BYTE_PACKING_MULT, 1); setLinearCurve(config->boostMapBins, 0, 300 / LOAD_1_BYTE_PACKING_MULT, 1);
for (int loadIndex = 0;loadIndex<BOOST_LOAD_COUNT;loadIndex++) { for (int loadIndex = 0;loadIndex<BOOST_LOAD_COUNT;loadIndex++) {
for (int rpmIndex = 0;rpmIndex<BOOST_RPM_COUNT;rpmIndex++) { for (int rpmIndex = 0;rpmIndex<BOOST_RPM_COUNT;rpmIndex++) {
config->boostTableOpenLoop[loadIndex][rpmIndex] = config->boostMapBins[loadIndex]; config->boostTableOpenLoop[loadIndex][rpmIndex] = config->boostMapBins[loadIndex];
}
} }
}
setLinearCurve(config->boostTpsBins, 0, 100 / TPS_1_BYTE_PACKING_MULT, 1); setLinearCurve(config->boostTpsBins, 0, 100 / TPS_1_BYTE_PACKING_MULT, 1);
for (int loadIndex = 0;loadIndex<BOOST_LOAD_COUNT;loadIndex++) { for (int loadIndex = 0;loadIndex<BOOST_LOAD_COUNT;loadIndex++) {
for (int rpmIndex = 0;rpmIndex<BOOST_RPM_COUNT;rpmIndex++) { for (int rpmIndex = 0;rpmIndex<BOOST_RPM_COUNT;rpmIndex++) {
config->boostTableClosedLoop[loadIndex][rpmIndex] = config->boostTpsBins[loadIndex]; config->boostTableClosedLoop[loadIndex][rpmIndex] = config->boostTpsBins[loadIndex];
} }
} }
} }
static void turnBoostPidOn() { static void turnBoostPidOn() {
if (CONFIG(boostControlPin) == GPIO_UNASSIGNED){ if (CONFIG(boostControlPin) == GPIO_UNASSIGNED){
return; return;
} }
startSimplePwmExt(&boostPwmControl, "Boost", &engine->executor,
CONFIG(boostControlPin), &enginePins.boostPin, startSimplePwmExt(
engineConfiguration->boostPwmFrequency, 0.5f, &boostPwmControl,
(pwm_gen_callback*) applyPinState); "Boost",
&engine->executor,
CONFIG(boostControlPin),
&enginePins.boostPin,
engineConfiguration->boostPwmFrequency,
0.5f,
(pwm_gen_callback*) applyPinState
);
} }
void startBoostPin(void) { void startBoostPin(void) {
turnBoostPidOn(); turnBoostPidOn();
} }
void stopBoostPin(void) { void stopBoostPin(void) {
brain_pin_markUnused(activeConfiguration.boostControlPin); brain_pin_markUnused(activeConfiguration.boostControlPin);
} }
@ -181,14 +174,17 @@ void onConfigurationChangeBoostCallback(engine_configuration_s *previousConfigur
shouldResetPid = !boostControlPid.isSame(&previousConfiguration->boostPid); shouldResetPid = !boostControlPid.isSame(&previousConfiguration->boostPid);
} }
void initBoostCtrl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX){ void initBoostCtrl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
if (CONFIG(boostControlPin) == GPIO_UNASSIGNED){
return;
}
logger = sharedLogger; logger = sharedLogger;
boostMapOpen.init(config->boostTableOpenLoop, config->boostMapBins, config->boostRpmBins); boostMapOpen.init(config->boostTableOpenLoop, config->boostMapBins, config->boostRpmBins);
boostMapClosed.init(config->boostTableClosedLoop, config->boostTpsBins, config->boostRpmBins); boostMapClosed.init(config->boostTableClosedLoop, config->boostTpsBins, config->boostRpmBins);
boostControlPid.reset(); boostControlPid.reset();
startBoostPin(); startBoostPin();
BoostController.Start(); BoostController.Start();
} }
#endif #endif