2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2019-03-31 13:56:13 -07:00
|
|
|
* @file alternator_controller.cpp
|
2016-02-25 14:01:37 -08:00
|
|
|
* @brief alternator controller - some newer vehicles control alternator with ECU
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* @date Apr 6, 2014
|
|
|
|
* @author Dmitry Sidin
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2019-11-19 23:18:17 -08:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_ALTERNATOR_CONTROL
|
2019-03-31 13:56:13 -07:00
|
|
|
#include "alternator_controller.h"
|
2022-01-31 18:51:32 -08:00
|
|
|
#include "efi_pid.h"
|
2019-03-31 13:56:13 -07:00
|
|
|
#include "local_version_holder.h"
|
2019-07-09 13:08:49 -07:00
|
|
|
#include "periodic_task.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-07-05 16:00:44 -07:00
|
|
|
#if defined(HAS_OS_ACCESS)
|
|
|
|
#error "Unexpected OS ACCESS HERE"
|
2019-11-19 23:18:17 -08:00
|
|
|
#endif /* HAS_OS_ACCESS */
|
2019-07-05 16:00:44 -07:00
|
|
|
|
2019-01-09 05:50:51 -08:00
|
|
|
static SimplePwm alternatorControl("alt");
|
2021-11-27 16:27:57 -08:00
|
|
|
static Pid alternatorPid(&persistentState.persistentConfiguration.engineConfiguration.alternatorControl);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-09-21 18:14:50 -07:00
|
|
|
static percent_t currentAltDuty;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-09-15 20:01:48 -07:00
|
|
|
static bool shouldResetPid = false;
|
2016-02-25 14:01:37 -08:00
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
static void pidReset() {
|
2019-09-03 16:12:04 -07:00
|
|
|
alternatorPid.reset();
|
2017-04-10 19:07:51 -07:00
|
|
|
}
|
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
void AlternatorController::onFastCallback() {
|
|
|
|
if (!isBrainPinValid(engineConfiguration->alternatorControlPin)) {
|
|
|
|
return;
|
2019-07-09 13:08:49 -07:00
|
|
|
}
|
2019-02-10 20:54:41 -08:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if ! EFI_UNIT_TEST
|
2021-12-05 15:33:50 -08:00
|
|
|
if (shouldResetPid) {
|
|
|
|
pidReset();
|
|
|
|
shouldResetPid = false;
|
|
|
|
}
|
2016-01-22 10:02:51 -08:00
|
|
|
#endif
|
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
// this block could be executed even in on/off alternator control mode
|
|
|
|
// but at least we would reflect latest state
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2022-03-24 05:58:55 -07:00
|
|
|
alternatorPid.postState(engine->outputChannels.alternatorStatus);
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2017-02-12 17:05:02 -08:00
|
|
|
|
2022-01-20 19:22:52 -08:00
|
|
|
bool alternatorShouldBeEnabledAtCurrentRpm = Sensor::getOrZero(SensorType::Rpm) > engineConfiguration->cranking.rpm;
|
2016-01-31 16:01:34 -08:00
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
if (!engineConfiguration->isAlternatorControlEnabled || !alternatorShouldBeEnabledAtCurrentRpm) {
|
|
|
|
// we need to avoid accumulating iTerm while engine is not running
|
|
|
|
pidReset();
|
2021-05-07 06:38:41 -07:00
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
// Shut off output if not needed
|
|
|
|
alternatorControl.setSimplePwmDutyCycle(0);
|
2021-05-07 06:38:41 -07:00
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
return;
|
|
|
|
}
|
2016-01-31 16:01:34 -08:00
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
auto vBatt = Sensor::get(SensorType::BatteryVoltage);
|
|
|
|
float targetVoltage = engineConfiguration->targetVBatt;
|
2016-02-25 14:01:37 -08:00
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
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);
|
2023-01-25 23:02:26 -08:00
|
|
|
// see "idle air Bump for AC" comment
|
2023-01-24 07:29:08 -08:00
|
|
|
int acDutyBump = engine->module<AcController>().unmock().acButtonState ? engineConfiguration->acRelayAlternatorDutyAdder : 0;
|
|
|
|
currentAltDuty += acDutyBump;
|
2021-12-05 15:33:50 -08:00
|
|
|
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());
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-12-05 15:33:50 -08:00
|
|
|
alternatorControl.setSimplePwmDutyCycle(PERCENT_TO_DUTY(currentAltDuty));
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
void showAltInfo(void) {
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("alt=%s @%s t=%dms", boolToString(engineConfiguration->isAlternatorControlEnabled),
|
2021-11-17 00:54:21 -08:00
|
|
|
hwPortname(engineConfiguration->alternatorControlPin),
|
2019-02-10 19:47:49 -08:00
|
|
|
engineConfiguration->alternatorControl.periodMs);
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("p=%.2f/i=%.2f/d=%.2f offset=%.2f", engineConfiguration->alternatorControl.pFactor,
|
2016-02-05 12:02:36 -08:00
|
|
|
0, 0, engineConfiguration->alternatorControl.offset); // todo: i & d
|
2021-10-05 16:59:07 -07:00
|
|
|
efiPrintf("vbatt=%.2f/duty=%.2f/target=%.2f", Sensor::getOrZero(SensorType::BatteryVoltage), currentAltDuty,
|
2015-07-10 06:01:56 -07:00
|
|
|
engineConfiguration->targetVBatt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAltPFactor(float p) {
|
2015-11-11 20:01:18 -08:00
|
|
|
engineConfiguration->alternatorControl.pFactor = p;
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("setAltPid: %.2f", p);
|
2017-04-10 19:07:51 -07:00
|
|
|
pidReset();
|
2015-07-10 06:01:56 -07:00
|
|
|
showAltInfo();
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:01:48 -07:00
|
|
|
void onConfigurationChangeAlternatorCallback(engine_configuration_s *previousConfiguration) {
|
2019-09-03 16:12:04 -07:00
|
|
|
shouldResetPid = !alternatorPid.isSame(&previousConfiguration->alternatorControl);
|
2016-09-15 20:01:48 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void initAlternatorCtrl() {
|
2015-07-10 06:01:56 -07:00
|
|
|
addConsoleAction("altinfo", showAltInfo);
|
2021-11-17 00:54:21 -08:00
|
|
|
if (!isBrainPinValid(engineConfiguration->alternatorControlPin))
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
|
2022-09-07 19:59:40 -07:00
|
|
|
startSimplePwm(&alternatorControl,
|
2019-01-09 05:50:51 -08:00
|
|
|
"Alternator control",
|
|
|
|
&engine->executor,
|
2016-09-13 21:03:14 -07:00
|
|
|
&enginePins.alternatorPin,
|
2021-05-07 06:38:41 -07:00
|
|
|
engineConfiguration->alternatorPwmFrequency, 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2016-01-20 20:03:03 -08:00
|
|
|
|
|
|
|
#endif /* EFI_ALTERNATOR_CONTROL */
|