rusefi/firmware/controllers/actuators/boost_control.cpp

248 lines
6.7 KiB
C++
Raw Normal View History

/*
* boost_control.cpp
*
* Created on: 13. des. 2019
* Author: Ola Ruud
*/
#include "pch.h"
#if EFI_BOOST_CONTROL
#include "boost_control.h"
#include "pid_auto_tune.h"
#include "electronic_throttle.h"
#define NO_PIN_PERIOD 500
#if defined(HAS_OS_ACCESS)
#error "Unexpected OS ACCESS HERE"
#endif
static boostOpenLoop_Map3D_t boostMapOpen;
static boostOpenLoop_Map3D_t boostMapClosed;
static SimplePwm boostPwmControl("boost");
void BoostController::init(IPwm* pwm, const ValueProvider3D* openLoopMap, const ValueProvider3D* closedLoopTargetMap, pid_s* pidParams) {
m_pwm = pwm;
m_openLoopMap = openLoopMap;
m_closedLoopTargetMap = closedLoopTargetMap;
m_pid.initPidClass(pidParams);
}
void BoostController::onConfigurationChange(pid_s* previousConfiguration) {
if (!m_pid.isSame(previousConfiguration)) {
m_shouldResetPid = true;
}
}
expected<float> BoostController::observePlant() const {
return Sensor::get(SensorType::Map);
}
expected<float> BoostController::getSetpoint() {
// If we're in open loop only mode, disregard any target computation.
// Open loop needs to work even in case of invalid closed loop config
2021-11-24 10:08:24 -08:00
isNotClosedLoop = engineConfiguration->boostType != CLOSED_LOOP;
if (isNotClosedLoop) {
closedLoopPart = 0;
return closedLoopPart;
}
2022-01-20 19:22:52 -08:00
float rpm = Sensor::getOrZero(SensorType::Rpm);
auto tps = Sensor::get(SensorType::DriverThrottleIntent);
2022-01-01 22:17:22 -08:00
isTpsInvalid = !tps.Valid;
2020-02-04 17:17:31 -08:00
2022-01-01 22:17:22 -08:00
if (isTpsInvalid) {
return unexpected;
}
2022-01-01 22:17:22 -08:00
efiAssert(OBD_PCM_Processor_Fault, m_closedLoopTargetMap != nullptr, "boost closed loop target", unexpected);
return m_closedLoopTargetMap->getValue(rpm, tps.Value);
}
expected<percent_t> BoostController::getOpenLoop(float target) {
2021-09-07 11:07:47 -07:00
// Boost control open loop doesn't care about target - only TPS/RPM
UNUSED(target);
2022-01-20 19:22:52 -08:00
float rpm = Sensor::getOrZero(SensorType::Rpm);
auto tps = Sensor::get(SensorType::DriverThrottleIntent);
2022-01-01 22:17:22 -08:00
isTpsInvalid = !tps.Valid;
2022-01-01 22:17:22 -08:00
if (isTpsInvalid) {
return unexpected;
}
2022-01-01 22:17:22 -08:00
efiAssert(OBD_PCM_Processor_Fault, m_openLoopMap != nullptr, "boost open loop", unexpected);
2020-02-04 17:17:31 -08:00
2022-01-01 22:17:22 -08:00
openLoopPart = m_openLoopMap->getValue(rpm, tps.Value);
2020-02-04 17:17:31 -08:00
#if EFI_TUNER_STUDIO
2022-01-01 22:17:22 -08:00
engine->outputChannels.boostControllerOpenLoopPart = openLoopPart;
#endif
2022-01-01 22:17:22 -08:00
return openLoopPart;
}
2020-02-04 17:17:31 -08:00
2021-09-07 11:07:47 -07:00
percent_t BoostController::getClosedLoopImpl(float target, float manifoldPressure) {
// If we're in open loop only mode, make no closed loop correction.
2021-11-24 10:08:24 -08:00
isNotClosedLoop = engineConfiguration->boostType != CLOSED_LOOP;
if (isNotClosedLoop) {
return 0;
2020-03-26 16:22:14 -07:00
}
// Reset PID if requested
if (m_shouldResetPid) {
m_pid.reset();
m_shouldResetPid = false;
2020-02-04 17:17:31 -08:00
}
// If the engine isn't running, don't correct.
2022-01-20 19:22:52 -08:00
isZeroRpm = Sensor::getOrZero(SensorType::Rpm) == 0;
2022-01-01 22:17:22 -08:00
if (isZeroRpm) {
m_pid.reset();
return 0;
}
isBelowClosedLoopThreshold = manifoldPressure < engineConfiguration->minimumBoostClosedLoopMap;
if (isBelowClosedLoopThreshold) {
// We're below the CL threshold, inhibit CL for now
m_pid.reset();
return 0;
}
return m_pid.getOutput(target, manifoldPressure, SLOW_CALLBACK_PERIOD_MS / 1000.0f);
2021-09-07 11:07:47 -07:00
}
expected<percent_t> BoostController::getClosedLoop(float target, float manifoldPressure) {
closedLoopPart = getClosedLoopImpl(target, manifoldPressure);
engine->outputChannels.boostControllerClosedLoopPart = closedLoopPart;
m_pid.postState(engine->outputChannels.boostStatus);
#if EFI_TUNER_STUDIO
2022-01-01 22:17:22 -08:00
engine->outputChannels.boostControlTarget = target;
#endif /* EFI_TUNER_STUDIO */
return closedLoopPart;
}
void BoostController::setOutput(expected<float> output) {
percent_t percent = output.value_or(engineConfiguration->boostControlSafeDutyCycle);
2021-09-07 11:07:47 -07:00
if (!engineConfiguration->isBoostControlEnabled) {
// If not enabled, force 0% output
percent = 0;
}
2021-09-07 11:07:47 -07:00
#if EFI_TUNER_STUDIO
2022-01-01 22:17:22 -08:00
engine->outputChannels.boostControllerOutput = percent;
2021-09-07 11:07:47 -07:00
#endif /* EFI_TUNER_STUDIO */
float duty = PERCENT_TO_DUTY(percent);
if (m_pwm) {
m_pwm->setSimplePwmDutyCycle(duty);
}
setEtbWastegatePosition(percent);
}
void BoostController::update() {
m_pid.iTermMin = -50;
m_pid.iTermMax = 50;
ClosedLoopController::update();
}
static bool hasInitBoost = false;
void updateBoostControl() {
if (hasInitBoost) {
2021-11-24 09:59:57 -08:00
engine->boostController.update();
}
}
void setDefaultBoostParameters() {
engineConfiguration->boostPwmFrequency = 33;
engineConfiguration->boostPid.offset = 0;
engineConfiguration->boostPid.pFactor = 0.5;
engineConfiguration->boostPid.iFactor = 0.3;
engineConfiguration->boostPid.maxValue = 20;
engineConfiguration->boostPid.minValue = -20;
engineConfiguration->boostControlPinMode = OM_DEFAULT;
setLinearCurve(config->boostRpmBins, 0, 8000, 1);
setLinearCurve(config->boostTpsBins, 0, 100, 1);
for (int loadIndex = 0; loadIndex < BOOST_LOAD_COUNT; loadIndex++) {
for (int rpmIndex = 0; rpmIndex < BOOST_RPM_COUNT; rpmIndex++) {
config->boostTableOpenLoop[loadIndex][rpmIndex] = (float)config->boostTpsBins[loadIndex];
config->boostTableClosedLoop[loadIndex][rpmIndex] = (float)config->boostTpsBins[loadIndex];
2020-02-04 17:17:31 -08:00
}
}
2020-11-22 15:30:19 -08:00
// Defaults for ETB-style wastegate actuator
engineConfiguration->etbWastegatePid.pFactor = 1;
engineConfiguration->etbWastegatePid.minValue = -60;
engineConfiguration->etbWastegatePid.maxValue = 60;
}
void startBoostPin() {
2020-03-23 20:06:52 -07:00
#if !EFI_UNIT_TEST
2020-11-22 15:30:19 -08:00
// Only init if a pin is set, no need to start PWM without a pin
if (!engineConfiguration->isBoostControlEnabled || !isBrainPinValid(engineConfiguration->boostControlPin)) {
return;
}
2020-02-04 17:17:31 -08:00
startSimplePwm(
2020-02-04 17:17:31 -08:00
&boostPwmControl,
"Boost",
&engine->executor,
&enginePins.boostPin,
engineConfiguration->boostPwmFrequency,
0
2020-02-04 17:17:31 -08:00
);
2020-03-23 20:06:52 -07:00
#endif /* EFI_UNIT_TEST */
}
void onConfigurationChangeBoostCallback(engine_configuration_s *previousConfiguration) {
2021-11-24 09:59:57 -08:00
engine->boostController.onConfigurationChange(&previousConfiguration->boostPid);
}
void initBoostCtrl() {
// todo: why do we have 'isBoostControlEnabled' setting exactly?
// 'initAuxPid' is an example of a subsystem without explicit enable
if (!engineConfiguration->isBoostControlEnabled) {
2020-11-22 15:30:19 -08:00
return;
}
bool hasAnyEtbWastegate = false;
for (size_t i = 0; i < efi::size(engineConfiguration->etbFunctions); i++) {
hasAnyEtbWastegate |= engineConfiguration->etbFunctions[i] == ETB_Wastegate;
2020-11-22 15:30:19 -08:00
}
// If we have neither a boost PWM pin nor ETB wastegate, nothing more to do
if (!isBrainPinValid(engineConfiguration->boostControlPin) && !hasAnyEtbWastegate) {
2020-02-04 17:17:31 -08:00
return;
}
// Set up open & closed loop tables
boostMapOpen.init(config->boostTableOpenLoop, config->boostTpsBins, config->boostRpmBins);
boostMapClosed.init(config->boostTableClosedLoop, config->boostTpsBins, config->boostRpmBins);
// Set up boost controller instance
2021-11-24 09:59:57 -08:00
engine->boostController.init(&boostPwmControl, &boostMapOpen, &boostMapClosed, &engineConfiguration->boostPid);
2020-03-23 20:51:51 -07:00
#if !EFI_UNIT_TEST
startBoostPin();
hasInitBoost = true;
2020-03-23 20:06:52 -07:00
#endif
}
#endif