fome-fw/firmware/controllers/algo/aux_pid.cpp

127 lines
3.2 KiB
C++
Raw Normal View History

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
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2016-06-26 10:02:23 -07:00
*/
#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-07-02 17:01:32 -07:00
#include "fsio_impl.h"
#include "engine_math.h"
2016-06-27 19:02:41 -07:00
EXTERN_ENGINE
;
2016-07-02 17:01:32 -07:00
extern fsio8_Map3D_f32t fsioTable1;
2016-06-27 19:02:41 -07:00
// 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;
2016-07-21 21:04:09 -07:00
static SimplePwm auxPidPwm[AUX_PID_COUNT];
static OutputPin auxPidPin[AUX_PID_COUNT];
static pid_s *auxPidS = &persistentState.persistentConfiguration.engineConfiguration.auxPid[0];
2017-01-22 09:06:10 -08:00
static Pid auxPid(auxPidS, 0, 90);
2016-06-30 20:01:36 -07:00
static Logging *logger;
2016-06-30 19:02:49 -07:00
2016-07-21 21:04:09 -07:00
static bool isEnabled(int index) {
// todo: implement bit arrays for configuration
switch(index) {
case 0:
return engineConfiguration->activateAuxPid1;
case 1:
return engineConfiguration->activateAuxPid2;
case 2:
return engineConfiguration->activateAuxPid3;
default:
return engineConfiguration->activateAuxPid4;
}
}
2016-06-27 19:02:41 -07:00
static msg_t auxPidThread(int param) {
UNUSED(param);
chRegSetThreadName("AuxPidController");
while (true) {
2016-07-21 20:03:53 -07:00
int dt = maxI(10, engineConfiguration->auxPidDT[0]);
2016-06-27 19:02:41 -07:00
chThdSleepMilliseconds(dt);
2016-06-30 19:02:49 -07:00
if (parametersVersion.isOld())
2016-06-30 20:01:36 -07:00
auxPid.reset();
2016-07-02 17:01:32 -07:00
float rpm = engine->rpmCalculator.rpmValue;
// todo: make this configurable?
bool enabledAtCurrentRpm = rpm > engineConfiguration->cranking.rpm;
if (!enabledAtCurrentRpm) {
// we need to avoid accumulating iTerm while engine is not running
auxPid.reset();
continue;
}
2017-01-22 09:06:10 -08:00
float value = engine->triggerCentral.vvtPosition; // getVBatt(PASS_ENGINE_PARAMETER_F); // that's temporary
2016-07-02 17:01:32 -07:00
float targetValue = fsioTable1.getValue(rpm, getEngineLoadT(PASS_ENGINE_PARAMETER_F));
2016-06-30 20:01:36 -07:00
float pwm = auxPid.getValue(targetValue, value, 1);
2016-07-21 21:04:09 -07:00
if (engineConfiguration->isVerboseAuxPid1) {
2016-06-30 20:01:36 -07:00
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);
2016-12-17 06:02:59 -08:00
tsOutputChannels.debugIntField3 = (int)(10 * targetValue);
2016-06-30 20:01:36 -07:00
}
2016-07-21 21:04:09 -07:00
auxPidPwm[0].setSimplePwmDutyCycle(pwm / 100);
2016-06-30 20:01:36 -07:00
2016-06-27 19:02:41 -07:00
}
#if defined __GNUC__
return -1;
#endif
}
2016-07-21 21:04:09 -07:00
static void turnAuxPidOn(int index) {
if (!isEnabled(index)) {
return;
}
if (engineConfiguration->auxPidPins[index] == GPIO_UNASSIGNED) {
return;
}
startSimplePwmExt(&auxPidPwm[index], "Aux PID", engineConfiguration->auxPidPins[index],
&auxPidPin[0],
engineConfiguration->auxPidFrequency[index], 0.1, applyPinState);
}
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-07-21 21:04:09 -07:00
for (int i = 0;i< AUX_PID_COUNT;i++) {
turnAuxPidOn(i);
2016-06-30 19:02:49 -07:00
}
2016-06-26 10:02:23 -07:00
}
2016-06-27 19:02:41 -07:00
#endif