rusefi-1/firmware/controllers/algo/aux_pid.cpp

147 lines
3.5 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
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
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"
2017-05-02 15:05:29 -07:00
#include "pin_repository.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?
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
2016-06-30 20:01:36 -07:00
extern TunerStudioOutputChannels tsOutputChannels;
#endif /* EFI_TUNER_STUDIO */
2016-06-30 20:01:36 -07:00
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-05-29 20:15:07 -07:00
static Pid auxPid(auxPidS);
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;
}
}
2017-04-10 19:07:51 -07:00
static void pidReset(void) {
auxPid.reset();
}
2016-06-27 19:02:41 -07:00
static msg_t auxPidThread(int param) {
UNUSED(param);
chRegSetThreadName("AuxPidController");
while (true) {
auxPid.sleep();
2016-06-27 19:02:41 -07:00
if (parametersVersion.isOld(getGlobalConfigurationVersion())) {
2017-04-10 19:07:51 -07:00
pidReset();
2017-04-10 12:16:00 -07:00
}
2016-06-30 20:01:36 -07:00
2017-07-08 04:19:26 -07:00
float rpm = GET_RPM();
2016-07-02 17:01:32 -07:00
// todo: make this configurable?
bool enabledAtCurrentRpm = rpm > engineConfiguration->cranking.rpm;
if (!enabledAtCurrentRpm) {
// we need to avoid accumulating iTerm while engine is not running
2017-04-10 19:07:51 -07:00
pidReset();
2016-07-02 17:01:32 -07:00
continue;
}
2017-05-15 20:33:22 -07:00
float value = engine->triggerCentral.vvtPosition; // getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); // that's temporary
float targetValue = fsioTable1.getValue(rpm, getEngineLoadT(PASS_ENGINE_PARAMETER_SIGNATURE));
2016-06-30 20:01:36 -07:00
2017-09-21 18:14:50 -07:00
percent_t pwm = auxPid.getValue(targetValue, value);
2016-07-21 21:04:09 -07:00
if (engineConfiguration->isVerboseAuxPid1) {
2018-01-23 09:05:14 -08:00
scheduleMsg(logger, "aux duty: %.2f/value=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", pwm, value,
2016-06-30 20:01:36 -07:00
auxPid.getP(), auxPid.getI(), auxPid.getD(), auxPid.getIntegration());
}
2016-06-30 19:02:49 -07:00
2017-11-24 16:16:25 -08:00
if (engineConfiguration->debugMode == DBG_AUX_PID_1) {
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
2016-06-30 20:01:36 -07:00
auxPid.postState(&tsOutputChannels);
2016-12-17 06:02:59 -08:00
tsOutputChannels.debugIntField3 = (int)(10 * targetValue);
#endif /* EFI_TUNER_STUDIO */
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",
&engine->executor,
engineConfiguration->auxPidPins[index],
2016-07-21 21:04:09 -07:00
&auxPidPin[0],
engineConfiguration->auxPidFrequency[index], 0.1, applyPinState);
}
2017-05-02 15:05:29 -07:00
void startAuxPins(void) {
for (int i = 0;i <AUX_PID_COUNT;i++) {
turnAuxPidOn(i);
}
}
void stopAuxPins(void) {
for (int i = 0;i < AUX_PID_COUNT;i++) {
unmarkPin(activeConfiguration.auxPidPins[i]);
}
}
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,
2018-12-27 06:40:40 -08:00
(tfunc_t)(void*) auxPidThread, NULL);
2016-06-26 10:02:23 -07:00
2016-06-30 20:01:36 -07:00
logger = sharedLogger;
2017-05-02 15:05:29 -07:00
startAuxPins();
2016-06-26 10:02:23 -07:00
}
2016-06-27 19:02:41 -07:00
#endif