fome-fw/firmware/util/pid.cpp

226 lines
5.3 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file pid.cpp
*
2017-05-25 09:09:07 -07:00
* https://en.wikipedia.org/wiki/Feedback
2015-07-10 06:01:56 -07:00
* http://en.wikipedia.org/wiki/PID_controller
*
* @date Sep 16, 2014
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
#include "pid.h"
2016-02-06 09:02:24 -08:00
#include "math.h"
Pid::Pid() {
2018-07-29 13:30:23 -07:00
initPidClass(NULL);
2016-02-06 09:02:24 -08:00
}
2015-07-10 06:01:56 -07:00
2017-05-29 20:15:07 -07:00
Pid::Pid(pid_s *pid) {
2018-07-29 13:30:23 -07:00
initPidClass(pid);
2016-02-06 09:02:24 -08:00
}
2018-07-29 13:30:23 -07:00
void Pid::initPidClass(pid_s *pid) {
2016-01-20 20:03:03 -08:00
this->pid = pid;
2017-06-02 18:34:00 -07:00
resetCounter = 0;
2015-07-10 06:01:56 -07:00
2017-06-02 18:34:00 -07:00
reset();
2015-07-10 06:01:56 -07:00
}
2016-09-15 20:01:48 -07:00
bool Pid::isSame(pid_s *pid) {
2017-06-04 10:21:07 -07:00
return this->pid->pFactor == pid->pFactor
&& this->pid->iFactor == pid->iFactor
&& this->pid->dFactor == pid->dFactor
&& this->pid->offset == pid->offset
2019-02-10 19:47:49 -08:00
&& this->pid->periodMs == pid->periodMs;
2016-09-15 20:01:48 -07:00
}
/**
* @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);
2017-01-22 14:03:31 -08:00
}
float Pid::getUnclampedOutput(float target, float input, float dTime) {
float error = (target - input) * errorAmplificationCoef;
this->target = target;
this->input = input;
2015-07-10 06:01:56 -07:00
2016-01-20 20:03:03 -08:00
float pTerm = pid->pFactor * error;
2017-12-24 18:17:10 -08:00
updateITerm(pid->iFactor * dTime * error);
dTerm = pid->dFactor / dTime * (error - previousError);
2015-07-10 06:01:56 -07:00
previousError = error;
2015-07-10 06:01:56 -07:00
2017-12-24 18:17:10 -08:00
return pTerm + iTerm + dTerm + pid->offset;
}
2017-02-13 08:03:16 -08:00
float Pid::getOutput(float target, float input, float dTime) {
float output = getUnclampedOutput(target, input, dTime);
2017-02-13 08:03:16 -08:00
if (output > pid->maxValue) {
output = pid->maxValue;
} else if (output < pid->minValue) {
output = pid->minValue;
2015-07-10 06:01:56 -07:00
}
this->output = output;
return output;
2015-07-10 06:01:56 -07:00
}
void Pid::updateFactors(float pFactor, float iFactor, float dFactor) {
2016-01-20 20:03:03 -08:00
pid->pFactor = pFactor;
pid->iFactor = iFactor;
pid->dFactor = dFactor;
2015-07-10 06:01:56 -07:00
reset();
}
void Pid::reset(void) {
2017-06-02 18:34:00 -07:00
dTerm = iTerm = 0;
output = input = target = previousError = 0;
errorAmplificationCoef = 1.0f;
2017-06-02 18:34:00 -07:00
resetCounter++;
2015-07-10 06:01:56 -07:00
}
float Pid::getP(void) {
2016-01-20 20:03:03 -08:00
return pid->pFactor;
2015-07-10 06:01:56 -07:00
}
float Pid::getI(void) {
2016-01-20 20:03:03 -08:00
return pid->iFactor;
2015-07-10 06:01:56 -07:00
}
2016-01-24 13:01:28 -08:00
float Pid::getPrevError(void) {
return previousError;
2016-01-24 13:01:28 -08:00
}
2015-07-10 06:01:56 -07:00
float Pid::getIntegration(void) {
return iTerm;
}
float Pid::getD(void) {
2016-01-20 20:03:03 -08:00
return pid->dFactor;
2015-07-10 06:01:56 -07:00
}
2016-02-06 09:02:24 -08:00
float Pid::getOffset(void) {
return pid->offset;
}
2015-07-10 06:01:56 -07:00
void Pid::setErrorAmplification(float coef) {
errorAmplificationCoef = coef;
}
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
2018-11-16 05:08:20 -08:00
void Pid::postState(TunerStudioOutputChannels *tsOutputChannels) {
2017-07-23 09:12:35 -07:00
postState(tsOutputChannels, 1);
}
2018-01-28 10:14:18 -08:00
/**
* see https://rusefi.com/wiki/index.php?title=Manual:Debug_fields
*/
2017-07-23 09:12:35 -07:00
void Pid::postState(TunerStudioOutputChannels *tsOutputChannels, int pMult) {
tsOutputChannels->debugFloatField1 = output;
2016-09-20 18:02:46 -07:00
tsOutputChannels->debugFloatField2 = iTerm;
2016-02-06 09:02:24 -08:00
tsOutputChannels->debugFloatField3 = getPrevError();
tsOutputChannels->debugFloatField4 = getI();
tsOutputChannels->debugFloatField5 = getD();
2018-07-29 13:30:23 -07:00
tsOutputChannels->debugFloatField6 = dTerm;
2018-01-28 10:14:18 -08:00
// tsOutputChannels->debugFloatField6 = pid->minValue;
2017-05-29 19:51:14 -07:00
tsOutputChannels->debugFloatField7 = pid->maxValue;
2017-07-23 09:12:35 -07:00
tsOutputChannels->debugIntField1 = getP() * pMult;
2016-02-06 09:02:24 -08:00
tsOutputChannels->debugIntField2 = getOffset();
2017-06-03 07:37:26 -07:00
tsOutputChannels->debugIntField3 = resetCounter;
2019-02-10 19:47:49 -08:00
tsOutputChannels->debugIntField4 = pid->periodMs;
2016-02-06 09:02:24 -08:00
}
2018-11-16 05:08:20 -08:00
#endif /* EFI_TUNER_STUDIO */
2017-05-25 05:49:04 -07:00
2017-05-28 19:10:35 -07:00
void Pid::sleep() {
#if !EFI_UNIT_TEST || defined(__DOXYGEN__)
2019-02-10 19:47:49 -08:00
int periodMs = maxI(10, pid->periodMs);
chThdSleepMilliseconds(periodMs);
#endif /* EFI_UNIT_TEST */
2017-05-28 19:10:35 -07:00
}
2017-05-29 20:15:07 -07:00
void Pid::showPidStatus(Logging *logging, const char*msg) {
2017-05-29 19:51:14 -07:00
scheduleMsg(logging, "%s settings: offset=%d P=%.5f I=%.5f D=%.5f dT=%d",
2017-05-25 05:49:04 -07:00
msg,
pid->offset,
pid->pFactor,
pid->iFactor,
pid->dFactor,
2019-02-10 19:47:49 -08:00
pid->periodMs);
2017-05-25 05:56:36 -07:00
2018-01-23 09:05:14 -08:00
scheduleMsg(logging, "%s status: value=%.2f input=%.2f/target=%.2f iTerm=%.5f dTerm=%.5f",
2017-05-29 19:51:14 -07:00
msg,
output,
input,
target,
2017-05-25 05:56:36 -07:00
iTerm, dTerm);
2017-05-25 05:49:04 -07:00
}
2017-12-24 18:17:10 -08:00
void Pid::updateITerm(float value) {
iTerm += value;
/**
* If we have exceeded the ability of the controlled device to hit target, the I factor will keep accumulating and approach infinity.
* Here we limit the I-term #353
*/
2019-03-02 11:00:32 -08:00
if (iTerm > pid->maxValue * 100) {
2017-12-24 18:17:10 -08:00
iTerm = pid->maxValue * 100;
2019-03-02 11:00:32 -08:00
}
if (iTerm > iTermMax) {
iTerm = iTermMax;
}
2017-12-24 18:17:10 -08:00
// this is kind of a hack. a proper fix would be having separate additional settings 'maxIValue' and 'minIValye'
if (iTerm < -pid->maxValue * 100)
iTerm = -pid->maxValue * 100;
2019-03-02 11:00:32 -08:00
if (iTerm < iTermMin) {
iTerm = iTermMin;
}
2017-12-24 18:17:10 -08:00
}
PidCic::PidCic() {
// call our derived reset()
reset();
}
PidCic::PidCic(pid_s *pid) : Pid(pid) {
// call our derived reset()
reset();
}
void PidCic::reset(void) {
Pid::reset();
totalItermCnt = 0;
for (int i = 0; i < PID_AVG_BUF_SIZE; i++)
iTermBuf[i] = 0;
iTermInvNum = 1.0f / (float)PID_AVG_BUF_SIZE;
}
float PidCic::getOutput(float target, float input, float dTime) {
return getUnclampedOutput(target, input, dTime);
2017-12-24 18:17:10 -08:00
}
void PidCic::updateITerm(float value) {
// use a variation of cascaded integrator-comb (CIC) filtering to get non-overflow iTerm
totalItermCnt++;
int localBufPos = (totalItermCnt >> PID_AVG_BUF_SIZE_SHIFT) % PID_AVG_BUF_SIZE;
int localPrevBufPos = ((totalItermCnt - 1) >> PID_AVG_BUF_SIZE_SHIFT) % PID_AVG_BUF_SIZE;
// reset old buffer cell
if (localPrevBufPos != localBufPos)
iTermBuf[localBufPos] = 0;
// integrator stage
iTermBuf[localBufPos] += value;
// return moving average of all sums, to smoothen the result
float iTermSum = 0;
for (int i = 0; i < PID_AVG_BUF_SIZE; i++) {
iTermSum += iTermBuf[i];
}
iTerm = iTermSum * iTermInvNum;
}