refactoring: no logic changes, just nicer variable and method names

This commit is contained in:
rusefi 2019-03-12 18:54:46 -04:00
parent 02df960de7
commit 653a735eab
10 changed files with 57 additions and 49 deletions

View File

@ -84,7 +84,7 @@ private:
float value = engine->triggerCentral.vvtPosition; // getVBatt(PASS_ENGINE_PARAMETER_SIGNATURE); // that's temporary
float targetValue = fsioTable1.getValue(rpm, getEngineLoadT(PASS_ENGINE_PARAMETER_SIGNATURE));
percent_t pwm = auxPid.getValue(targetValue, value);
percent_t pwm = auxPid.getOutput(targetValue, value);
if (engineConfiguration->isVerboseAuxPid1) {
scheduleMsg(logger, "aux duty: %.2f/value=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", pwm, value,
auxPid.getP(), auxPid.getI(), auxPid.getD(), auxPid.getIntegration());

View File

@ -153,7 +153,7 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
cltFuelCorrection = 1;
warmupAfrPid.reset();
} else {
cltFuelCorrection = warmupAfrPid.getValue(warmupTargetAfr, engine->sensors.currentAfr, 1);
cltFuelCorrection = warmupAfrPid.getOutput(warmupTargetAfr, engine->sensors.currentAfr, 1);
}
if (engineConfiguration->debugMode == DBG_WARMUP_ENRICH) {
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)

View File

@ -93,7 +93,7 @@ private:
}
currentAltDuty = altPid.getValue(targetVoltage, vBatt);
currentAltDuty = altPid.getOutput(targetVoltage, vBatt);
if (CONFIGB(isVerboseAlternator)) {
scheduleMsg(logger, "alt duty: %.2f/vbatt=%.2f/p=%.2f/i=%.2f/d=%.2f int=%.2f", currentAltDuty, vBatt,
altPid.getP(), altPid.getI(), altPid.getD(), altPid.getIntegration());

View File

@ -187,7 +187,7 @@ private:
tuneWorkingPid.updateFactors(autoTune.output, 0, 0);
float value = tuneWorkingPid.getValue(50, actualThrottlePosition);
float value = tuneWorkingPid.getOutput(50, actualThrottlePosition);
scheduleMsg(&logger, "AT input=%f output=%f PID=%f", autoTune.input,
autoTune.output,
value);
@ -218,7 +218,7 @@ private:
}
*/
currentEtbDuty = feedForward +
pid.getValue(targetPosition, actualThrottlePosition);
pid.getOutput(targetPosition, actualThrottlePosition);
etb1.dcMotor.Set(PERCENT_TO_DUTY(currentEtbDuty));
/*

View File

@ -230,7 +230,7 @@ static percent_t automaticIdleController() {
// If errorAmpCoef > 1.0, then PID thinks that RPM is lower than it is, and controls IAC more aggressively
idlePid.setErrorAmplification(errorAmpCoef);
percent_t newValue = idlePid.getValue(targetRpm, rpm, engineConfiguration->idleRpmPid.periodMs);
percent_t newValue = idlePid.getOutput(targetRpm, rpm, engineConfiguration->idleRpmPid.periodMs);
// the state of PID has been changed, so we might reset it now, but only when needed (see idlePidDeactivationTpsThreshold)
mightResetPid = true;

View File

@ -353,7 +353,7 @@ static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
return;
}
engine->engineState.fuelPidCorrection = fuelPid.getValue(ENGINE(engineState.targetAFR), ENGINE(sensors.currentAfr), 1);
engine->engineState.fuelPidCorrection = fuelPid.getOutput(ENGINE(engineState.targetAFR), ENGINE(sensors.currentAfr), 1);
if (engineConfiguration->debugMode == DBG_FUEL_PID_CORRECTION) {
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
tsOutputChannels.debugFloatField1 = engine->engineState.fuelPidCorrection;

View File

@ -416,7 +416,7 @@ static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) {
* error value as the difference of (target - input). and if we swap them we'll just get a sign inversion. If target=vUrCal, and input=vUr, then error=vUrCal-vUr, i.e. if vUr<vUrCal then the error will cause the heater to increase it's duty cycle. But it's not exactly what we want! Lesser vUr means HOTTER cell. That's why we even have this safety check for overheating: (vUr < CJ125_UR_OVERHEAT_THR)...
* So the simple trick is to inverse the error by swapping the target and input values.
*/
float duty = globalInstance.heaterPid.getValue(globalInstance.vUr, globalInstance.vUrCal);
float duty = globalInstance.heaterPid.getOutput(globalInstance.vUr, globalInstance.vUrCal);
globalInstance.heaterPid.showPidStatus(logger, "cj");
instance->SetHeater(duty PASS_ENGINE_PARAMETER_SUFFIX);
cjPrintData();

View File

@ -34,34 +34,38 @@ bool Pid::isSame(pid_s *pid) {
&& this->pid->periodMs == pid->periodMs;
}
float Pid::getValue(float target, float input) {
return getValue(target, input, 1);
/**
* @param Controller input / process output
* @returns Output from the PID controller / the input to the process
*/
float Pid::getOutput(float target, float input) {
return getOutput(target, input, 1);
}
float Pid::getRawValue(float target, float input, float dTime) {
float Pid::getUnclampedOutput(float target, float input, float dTime) {
float error = (target - input) * errorAmplificationCoef;
prevTarget = target;
prevInput = input;
this->target = target;
this->input = input;
float pTerm = pid->pFactor * error;
updateITerm(pid->iFactor * dTime * error);
dTerm = pid->dFactor / dTime * (error - prevError);
dTerm = pid->dFactor / dTime * (error - previousError);
prevError = error;
previousError = error;
return pTerm + iTerm + dTerm + pid->offset;
}
float Pid::getValue(float target, float input, float dTime) {
float result = getRawValue(target, input, dTime);
float Pid::getOutput(float target, float input, float dTime) {
float output = getUnclampedOutput(target, input, dTime);
if (result > pid->maxValue) {
result = pid->maxValue;
} else if (result < pid->minValue) {
result = pid->minValue;
if (output > pid->maxValue) {
output = pid->maxValue;
} else if (output < pid->minValue) {
output = pid->minValue;
}
prevResult = result;
return result;
this->output = output;
return output;
}
void Pid::updateFactors(float pFactor, float iFactor, float dFactor) {
@ -73,7 +77,7 @@ void Pid::updateFactors(float pFactor, float iFactor, float dFactor) {
void Pid::reset(void) {
dTerm = iTerm = 0;
prevResult = prevInput = prevTarget = prevError = 0;
output = input = target = previousError = 0;
errorAmplificationCoef = 1.0f;
resetCounter++;
}
@ -87,7 +91,7 @@ float Pid::getI(void) {
}
float Pid::getPrevError(void) {
return prevError;
return previousError;
}
float Pid::getIntegration(void) {
@ -115,7 +119,7 @@ void Pid::postState(TunerStudioOutputChannels *tsOutputChannels) {
* see https://rusefi.com/wiki/index.php?title=Manual:Debug_fields
*/
void Pid::postState(TunerStudioOutputChannels *tsOutputChannels, int pMult) {
tsOutputChannels->debugFloatField1 = prevResult;
tsOutputChannels->debugFloatField1 = output;
tsOutputChannels->debugFloatField2 = iTerm;
tsOutputChannels->debugFloatField3 = getPrevError();
tsOutputChannels->debugFloatField4 = getI();
@ -148,9 +152,9 @@ void Pid::showPidStatus(Logging *logging, const char*msg) {
scheduleMsg(logging, "%s status: value=%.2f input=%.2f/target=%.2f iTerm=%.5f dTerm=%.5f",
msg,
prevResult,
prevInput,
prevTarget,
output,
input,
target,
iTerm, dTerm);
}
@ -196,8 +200,8 @@ void PidCic::reset(void) {
iTermInvNum = 1.0f / (float)PID_AVG_BUF_SIZE;
}
float PidCic::getValue(float target, float input, float dTime) {
return getRawValue(target, input, dTime);
float PidCic::getOutput(float target, float input, float dTime) {
return getUnclampedOutput(target, input, dTime);
}
void PidCic::updateITerm(float value) {

View File

@ -27,11 +27,15 @@ public:
void initPidClass(pid_s *pid);
bool isSame(pid_s *pid);
float getValue(float target, float input);
/**
* @param Controller input / process output
* @returns Output from the PID controller / the input to the process
*/
float getOutput(float target, float input);
// todo: dTime should be taken from pid_s
virtual float getValue(float target, float input, float dTime);
virtual float getOutput(float target, float input, float dTime);
// doesn't limit the result (used in incremental CIC PID, see below)
float getRawValue(float target, float input, float dTime);
float getUnclampedOutput(float target, float input, float dTime);
void updateFactors(float pFactor, float iFactor, float dFactor);
virtual void reset(void);
float getP(void);
@ -58,11 +62,11 @@ public:
private:
pid_s *pid;
float prevError;
float previousError;
// these are only used for logging
float prevTarget;
float prevInput;
float prevResult;
float target;
float input;
float output;
float errorAmplificationCoef;
private:
@ -83,7 +87,7 @@ public:
PidCic(pid_s *pid);
virtual void reset(void);
virtual float getValue(float target, float input, float dTime);
virtual float getOutput(float target, float input, float dTime);
private:
// Circular running-average buffer for I-term, used by CIC-like filter

View File

@ -21,20 +21,20 @@ TEST(idle, pid) {
Pid pid(&pidS);
ASSERT_EQ( 90, pid.getValue(14, 12, 0.1)) << "getValue#90";
ASSERT_EQ( 90, pid.getOutput(14, 12, 0.1)) << "getValue#90";
ASSERT_EQ( 10, pid.getValue(14, 16, 0.1)) << "getValue#10";
ASSERT_EQ(10, pid.getValue(14, 16, 1));
ASSERT_EQ( 10, pid.getOutput(14, 16, 0.1)) << "getValue#10";
ASSERT_EQ(10, pid.getOutput(14, 16, 1));
pid.updateFactors(29, 0, 0);
ASSERT_EQ(10, pid.getValue(14, 16, 1));
ASSERT_EQ(10, pid.getOutput(14, 16, 1));
// ASSERT_EQ(68, pid.getIntegration());
ASSERT_EQ(10, pid.getValue(14, 16, 1));
ASSERT_EQ(10, pid.getOutput(14, 16, 1));
// ASSERT_EQ(0, pid.getIntegration());
ASSERT_EQ(10, pid.getValue(14, 16, 1));
ASSERT_EQ(10, pid.getOutput(14, 16, 1));
// ASSERT_EQ(68, pid.getIntegration());
@ -49,16 +49,16 @@ TEST(idle, pid) {
pid.reset();
ASSERT_EQ( 50, pid.getValue(/*target*/50, /*input*/0)) << "target=50, input=0";
ASSERT_EQ( 50, pid.getOutput(/*target*/50, /*input*/0)) << "target=50, input=0";
ASSERT_EQ( 0, pid.iTerm) << "target=50, input=0 iTerm";
ASSERT_EQ( 0, pid.getValue(/*target*/50, /*input*/70)) << "target=50, input=70";
ASSERT_EQ( 0, pid.getOutput(/*target*/50, /*input*/70)) << "target=50, input=70";
ASSERT_EQ( 0, pid.iTerm) << "target=50, input=70 iTerm";
ASSERT_EQ( 0, pid.getValue(/*target*/50, /*input*/70)) << "target=50, input=70 #2";
ASSERT_EQ( 0, pid.getOutput(/*target*/50, /*input*/70)) << "target=50, input=70 #2";
ASSERT_EQ( 0, pid.iTerm) << "target=50, input=70 iTerm #2";
ASSERT_EQ( 0, pid.getValue(/*target*/50, /*input*/50)) << "target=50, input=50";
ASSERT_EQ( 0, pid.getOutput(/*target*/50, /*input*/50)) << "target=50, input=50";
ASSERT_EQ( 0, pid.iTerm) << "target=50, input=50 iTerm";
}