2016-06-26 10:02:23 -07:00
|
|
|
/*
|
|
|
|
* @file aux_pid.cpp
|
|
|
|
*
|
2019-03-31 13:56:13 -07:00
|
|
|
* This class is a copy-paste of alternator_controller.cpp TODO: do something about it? extract more common logic?
|
2016-06-30 20:01:36 -07:00
|
|
|
*
|
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"
|
2019-03-31 13:56:13 -07:00
|
|
|
#include "local_version_holder.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"
|
2019-03-29 06:11:13 -07:00
|
|
|
#include "periodic_controller.h"
|
2016-06-27 19:02:41 -07:00
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
2016-07-02 17:01:32 -07:00
|
|
|
extern fsio8_Map3D_f32t fsioTable1;
|
|
|
|
|
2019-03-31 13:56:13 -07:00
|
|
|
// todo: this is to some extent a copy-paste of alternator_controller. maybe same loop
|
2016-06-27 19:02:41 -07:00
|
|
|
// for all PIDs?
|
|
|
|
|
2018-11-16 04:40:06 -08:00
|
|
|
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
|
2016-06-30 20:01:36 -07:00
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2016-06-30 20:01:36 -07:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-02-10 20:54:41 -08:00
|
|
|
class AuxPidController : public PeriodicController<UTILITY_THREAD_STACK_SIZE> {
|
|
|
|
public:
|
|
|
|
AuxPidController() : PeriodicController("AuxPidController") { }
|
|
|
|
private:
|
|
|
|
void PeriodicTask(efitime_t nowNt) override {
|
2019-02-21 02:44:45 -08:00
|
|
|
UNUSED(nowNt);
|
2019-02-10 20:54:41 -08:00
|
|
|
setPeriod(NOT_TOO_OFTEN(10 /* ms */, engineConfiguration->auxPid[0].periodMs));
|
2016-06-27 19:02:41 -07:00
|
|
|
|
2019-01-15 18:51:09 -08:00
|
|
|
if (parametersVersion.isOld(engine->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
|
|
|
|
2019-01-21 17:33:21 -08:00
|
|
|
float rpm = GET_RPM_VALUE;
|
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();
|
2019-02-10 20:54:41 -08:00
|
|
|
return;
|
2016-07-02 17:01:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-12 15:54:46 -07:00
|
|
|
percent_t pwm = auxPid.getOutput(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) {
|
2018-11-16 04:40:06 -08:00
|
|
|
#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);
|
2018-11-16 04:40:06 -08:00
|
|
|
#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
|
|
|
|
|
|
|
}
|
2019-02-10 20:54:41 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static AuxPidController instance;
|
2016-06-27 19:02:41 -07:00
|
|
|
|
2016-07-21 21:04:09 -07:00
|
|
|
static void turnAuxPidOn(int index) {
|
|
|
|
if (!isEnabled(index)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engineConfiguration->auxPidPins[index] == GPIO_UNASSIGNED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-09 05:50:51 -08:00
|
|
|
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-30 20:01:36 -07:00
|
|
|
logger = sharedLogger;
|
|
|
|
|
2017-05-02 15:05:29 -07:00
|
|
|
startAuxPins();
|
2019-02-10 20:54:41 -08:00
|
|
|
instance.Start();
|
2016-06-26 10:02:23 -07:00
|
|
|
}
|
|
|
|
|
2016-06-27 19:02:41 -07:00
|
|
|
#endif
|