2016-06-26 10:02:23 -07:00
|
|
|
/*
|
|
|
|
* @file aux_pid.cpp
|
|
|
|
*
|
2016-06-30 20:01:36 -07:00
|
|
|
* This class is a copy-paste of alternatorController.cpp TODO: do something about it? extract more common logic?
|
|
|
|
*
|
2016-06-26 10:02:23 -07:00
|
|
|
* @date Jun 26, 2016
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2016
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "aux_pid.h"
|
2016-06-30 19:02:49 -07:00
|
|
|
#include "LocalVersionHolder.h"
|
2016-06-30 20:01:36 -07:00
|
|
|
#include "allsensors.h"
|
2016-06-26 10:02:23 -07:00
|
|
|
|
2016-06-27 19:02:41 -07:00
|
|
|
#if EFI_AUX_PID || defined(__DOXYGEN__)
|
2016-06-30 19:02:49 -07:00
|
|
|
#include "pwm_generator.h"
|
2016-06-30 20:01:36 -07:00
|
|
|
#include "tunerstudio_configuration.h"
|
2016-06-27 19:02:41 -07:00
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
|
|
|
// todo: this is to some extent a copy-paste of alternatorController. maybe same loop
|
|
|
|
// for all PIDs?
|
|
|
|
|
2016-06-30 20:01:36 -07:00
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
|
|
|
|
2016-06-27 19:02:41 -07:00
|
|
|
static THD_WORKING_AREA(auxPidThreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
2016-06-30 19:02:49 -07:00
|
|
|
static LocalVersionHolder parametersVersion;
|
|
|
|
static SimplePwm auxPid1;
|
|
|
|
static OutputPin auxPid1Pin;
|
2016-06-30 20:01:36 -07:00
|
|
|
static pid_s *auxPidS = &persistentState.persistentConfiguration.engineConfiguration.auxPid1;
|
|
|
|
static Pid auxPid(auxPidS, 1, 90);
|
|
|
|
static Logging *logger;
|
2016-06-30 19:02:49 -07:00
|
|
|
|
2016-06-27 19:02:41 -07:00
|
|
|
static msg_t auxPidThread(int param) {
|
|
|
|
UNUSED(param);
|
|
|
|
chRegSetThreadName("AuxPidController");
|
|
|
|
while (true) {
|
|
|
|
int dt = maxI(10, engineConfiguration->auxPid1DT);
|
|
|
|
chThdSleepMilliseconds(dt);
|
|
|
|
|
2016-06-30 19:02:49 -07:00
|
|
|
if (parametersVersion.isOld())
|
2016-06-30 20:01:36 -07:00
|
|
|
auxPid.reset();
|
|
|
|
|
|
|
|
float value = getVBatt(PASS_ENGINE_PARAMETER_F); // that's temporary
|
|
|
|
float targetValue = engineConfiguration->targetVBatt; // that's temporary
|
|
|
|
|
|
|
|
float pwm = auxPid.getValue(targetValue, value, 1);
|
|
|
|
if (engineConfiguration->isVerboseAuxPid) {
|
|
|
|
scheduleMsg(logger, "aux duty: %f/value=%f/p=%f/i=%f/d=%f int=%f", pwm, value,
|
|
|
|
auxPid.getP(), auxPid.getI(), auxPid.getD(), auxPid.getIntegration());
|
|
|
|
}
|
2016-06-30 19:02:49 -07:00
|
|
|
|
|
|
|
|
2016-06-30 20:01:36 -07:00
|
|
|
if (engineConfiguration->debugMode == AUX_PID_1) {
|
|
|
|
tsOutputChannels.debugFloatField1 = pwm;
|
|
|
|
auxPid.postState(&tsOutputChannels);
|
|
|
|
}
|
|
|
|
|
|
|
|
auxPid1.setSimplePwmDutyCycle(pwm / 100);
|
|
|
|
|
2016-06-27 19:02:41 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
#if defined __GNUC__
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-26 10:02:23 -07:00
|
|
|
void initAuxPid(Logging *sharedLogger) {
|
2016-06-27 19:02:41 -07:00
|
|
|
chThdCreateStatic(auxPidThreadStack, sizeof(auxPidThreadStack), LOWPRIO,
|
|
|
|
(tfunc_t) auxPidThread, NULL);
|
2016-06-26 10:02:23 -07:00
|
|
|
|
2016-06-30 20:01:36 -07:00
|
|
|
logger = sharedLogger;
|
|
|
|
|
2016-06-30 21:02:20 -07:00
|
|
|
if (!engineConfiguration->activateAuxPid1) {
|
2016-06-30 19:02:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-02 13:03:00 -07:00
|
|
|
if (engineConfiguration->auxPidPins[0] == GPIO_UNASSIGNED) {
|
2016-06-30 19:02:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-02 13:03:00 -07:00
|
|
|
startSimplePwmExt(&auxPid1, "Aux PID", engineConfiguration->auxPidPins[0],
|
2016-06-30 19:02:49 -07:00
|
|
|
&auxPid1Pin,
|
2016-07-02 13:03:00 -07:00
|
|
|
engineConfiguration->auxPidFrequency[0], 0.1, applyPinState);
|
2016-06-26 10:02:23 -07:00
|
|
|
}
|
|
|
|
|
2016-06-27 19:02:41 -07:00
|
|
|
#endif
|