rusefi-1/firmware/controllers/alternatorController.cpp

166 lines
5.1 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file alternatorController.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
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
2015-07-10 06:01:56 -07:00
*/
#include "main.h"
#include "engine.h"
#include "rpm_calculator.h"
#include "pwm_generator.h"
#include "alternatorController.h"
#include "pin_repository.h"
#include "voltage.h"
#include "pid.h"
2016-01-22 10:02:51 -08:00
#include "LocalVersionHolder.h"
2016-01-22 11:01:29 -08:00
#include "tunerstudio_configuration.h"
2015-07-10 06:01:56 -07:00
2016-01-20 20:03:03 -08:00
#if EFI_ALTERNATOR_CONTROL || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
EXTERN_ENGINE
;
static Logging *logger;
#define ALTERNATOR_VALVE_PWM_FREQUENCY 300
2016-02-25 14:01:37 -08:00
extern pin_output_mode_e DEFAULT_OUTPUT;
2015-07-10 06:01:56 -07:00
static SimplePwm alternatorControl;
static OutputPin alternatorPin;
2016-01-20 20:03:03 -08:00
static pid_s *altPidS = &persistentState.persistentConfiguration.engineConfiguration.alternatorControl;
static Pid altPid(altPidS, 1, 90);
2015-07-10 06:01:56 -07:00
static THD_WORKING_AREA(alternatorControlThreadStack, UTILITY_THREAD_STACK_SIZE);
static float currentAltDuty;
2016-01-22 10:02:51 -08:00
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
static LocalVersionHolder parametersVersion;
extern TunerStudioOutputChannels tsOutputChannels;
#endif
2016-02-25 14:01:37 -08:00
static bool currentPlainOnOffState = false;
2015-07-10 06:01:56 -07:00
static msg_t AltCtrlThread(int param) {
2016-01-22 10:02:51 -08:00
UNUSED(param);
2015-07-10 06:01:56 -07:00
chRegSetThreadName("AlternatorController");
while (true) {
2016-01-22 10:02:51 -08:00
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
if (parametersVersion.isOld())
altPid.reset();
#endif
int dt = maxI(10, engineConfiguration->alternatorDT);
2016-01-21 20:02:49 -08:00
chThdSleepMilliseconds(dt);
2015-07-10 06:01:56 -07:00
2016-01-31 16:01:34 -08:00
// todo: migrate this to FSIO
bool alternatorShouldBeEnabledAtCurrentRpm = engine->rpmCalculator.rpmValue > 400;
engine->isAlternatorControlEnabled = CONFIG(isAlternatorControlEnabled) && alternatorShouldBeEnabledAtCurrentRpm;
if (!engine->isAlternatorControlEnabled) {
// we need to avoid accumulating iTerm while engine is not spinning
altPid.reset();
continue;
}
2016-02-25 14:01:37 -08:00
float vBatt = getVBatt(PASS_ENGINE_PARAMETER_F);
float targetVoltage = engineConfiguration->targetVBatt;
2016-02-27 22:03:22 -08:00
if (boardConfiguration->onOffAlternatorLogic) {
2016-02-25 14:01:37 -08:00
float h = 0.1;
bool newState = (vBatt < targetVoltage - h) || (currentPlainOnOffState && vBatt < targetVoltage);
alternatorPin.setValue(newState);
currentPlainOnOffState = newState;
if (engineConfiguration->debugMode == ALTERNATOR) {
tsOutputChannels.debugIntField1 = newState;
}
continue;
}
currentAltDuty = altPid.getValue(targetVoltage, vBatt, 1);
2015-07-10 06:01:56 -07:00
if (boardConfiguration->isVerboseAlternator) {
2016-06-30 20:01:36 -07:00
scheduleMsg(logger, "alt duty: %f/vbatt=%f/p=%f/i=%f/d=%f int=%f", currentAltDuty, vBatt,
2015-07-10 06:01:56 -07:00
altPid.getP(), altPid.getI(), altPid.getD(), altPid.getIntegration());
}
2016-01-22 10:02:51 -08:00
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
2016-02-04 20:02:52 -08:00
if (engineConfiguration->debugMode == ALTERNATOR) {
tsOutputChannels.debugFloatField1 = currentAltDuty;
2016-02-06 09:02:24 -08:00
altPid.postState(&tsOutputChannels);
2016-02-04 20:02:52 -08:00
}
2016-01-22 10:02:51 -08:00
#endif
2015-07-10 06:01:56 -07:00
alternatorControl.setSimplePwmDutyCycle(currentAltDuty / 100);
}
#if defined __GNUC__
return -1;
#endif
}
void showAltInfo(void) {
scheduleMsg(logger, "alt=%s @%s t=%dms", boolToString(engineConfiguration->isAlternatorControlEnabled),
hwPortname(boardConfiguration->alternatorControlPin),
engineConfiguration->alternatorDT);
2015-11-11 20:01:18 -08:00
scheduleMsg(logger, "p=%f/i=%f/d=%f offset=%f", engineConfiguration->alternatorControl.pFactor,
2016-02-05 12:02:36 -08:00
0, 0, engineConfiguration->alternatorControl.offset); // todo: i & d
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "vbatt=%f/duty=%f/target=%f", getVBatt(PASS_ENGINE_PARAMETER_F), currentAltDuty,
engineConfiguration->targetVBatt);
}
void setAltPFactor(float p) {
2015-11-11 20:01:18 -08:00
engineConfiguration->alternatorControl.pFactor = p;
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "setAltPid: %f", p);
2016-01-22 10:02:51 -08:00
altPid.reset();
2015-07-10 06:01:56 -07:00
showAltInfo();
}
static void applyAlternatorPinState(PwmConfig *state, int stateIndex) {
efiAssertVoid(stateIndex < PWM_PHASE_MAX_COUNT, "invalid stateIndex");
efiAssertVoid(state->multiWave.waveCount == 1, "invalid idle waveCount");
OutputPin *output = state->outputPins[0];
int value = state->multiWave.waves[0].pinStates[stateIndex];
2016-01-31 16:01:34 -08:00
if (!value || engine->isAlternatorControlEnabled)
2015-07-10 06:01:56 -07:00
output->setValue(value);
}
void setDefaultAlternatorParameters(void) {
engineConfiguration->alternatorOffAboveTps = 120;
boardConfiguration->alternatorControlPin = GPIO_UNASSIGNED;
boardConfiguration->alternatorControlPinMode = OM_DEFAULT;
engineConfiguration->targetVBatt = 14;
2016-02-05 12:02:36 -08:00
engineConfiguration->alternatorControl.offset = 0;
2015-11-11 20:01:18 -08:00
engineConfiguration->alternatorControl.pFactor = 30;
2015-07-10 06:01:56 -07:00
engineConfiguration->alternatorDT = 100;
}
void initAlternatorCtrl(Logging *sharedLogger) {
logger = sharedLogger;
addConsoleAction("altinfo", showAltInfo);
if (boardConfiguration->alternatorControlPin == GPIO_UNASSIGNED)
return;
2016-02-27 22:03:22 -08:00
if (boardConfiguration->onOffAlternatorLogic) {
2016-02-25 14:01:37 -08:00
outputPinRegisterExt2("on/off alternator", &alternatorPin, boardConfiguration->alternatorControlPin,
&DEFAULT_OUTPUT);
} else {
startSimplePwmExt(&alternatorControl, "Alternator control", boardConfiguration->alternatorControlPin,
&alternatorPin,
ALTERNATOR_VALVE_PWM_FREQUENCY, 0.1, applyAlternatorPinState);
}
2015-07-10 06:01:56 -07:00
chThdCreateStatic(alternatorControlThreadStack, sizeof(alternatorControlThreadStack), LOWPRIO,
(tfunc_t) AltCtrlThread, NULL);
}
2016-01-20 20:03:03 -08:00
#endif /* EFI_ALTERNATOR_CONTROL */