rusefi-1/firmware/controllers/math/pid.cpp

138 lines
3.1 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
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-07-10 06:01:56 -07:00
*/
#include "pid.h"
2016-02-06 09:02:24 -08:00
#include "math.h"
Pid::Pid() {
init(NULL, NAN, NAN);
}
2015-07-10 06:01:56 -07:00
2016-01-20 20:03:03 -08:00
Pid::Pid(pid_s *pid, float minResult, float maxResult) {
2016-02-06 09:02:24 -08:00
init(pid, minResult, maxResult);
}
void Pid::init(pid_s *pid, float minResult, float maxResult) {
2016-01-20 20:03:03 -08:00
this->pid = pid;
2015-07-10 06:01:56 -07:00
this->minResult = minResult;
this->maxResult = maxResult;
2016-09-20 18:02:46 -07:00
dTerm = iTerm = 0;
2017-05-25 05:56:36 -07:00
prevResult = prevInput = prevTarget = prevError = 0;
2015-07-10 06:01:56 -07:00
}
2016-09-15 20:01:48 -07:00
bool Pid::isSame(pid_s *pid) {
return this->pid->dFactor == pid->dFactor && this->pid->iFactor == pid->iFactor &&
this->pid->offset == pid->offset && this->pid->pFactor == pid->pFactor;
}
2017-01-22 14:03:31 -08:00
float Pid::getValue(float target, float input) {
return getValue(target, input, 1);
}
2015-07-10 06:01:56 -07:00
float Pid::getValue(float target, float input, float dTime) {
float error = target - input;
2017-05-25 05:56:36 -07:00
prevTarget = target;
prevInput = input;
2015-07-10 06:01:56 -07:00
2016-01-20 20:03:03 -08:00
float pTerm = pid->pFactor * error;
iTerm += pid->iFactor * dTime * error;
2016-09-20 18:02:46 -07:00
dTerm = pid->dFactor / dTime * (error - prevError);
2015-07-10 06:01:56 -07:00
prevError = error;
2017-02-13 08:03:16 -08:00
/**
* 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
*/
if (iTerm > maxResult - (pTerm + dTerm + pid->offset))
iTerm = maxResult - (pTerm + dTerm + pid->offset);
if (iTerm < minResult - (pTerm + dTerm + pid->offset))
iTerm = minResult - (pTerm + dTerm + pid->offset);
2016-02-06 13:02:41 -08:00
float result = pTerm + iTerm + dTerm + pid->offset;
2015-07-10 06:01:56 -07:00
if (result > maxResult) {
result = maxResult;
} else if (result < minResult) {
result = minResult;
}
2017-05-25 05:56:36 -07:00
prevResult = result;
2015-07-10 06:01:56 -07:00
return result;
}
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) {
iTerm = 0;
prevError = 0;
}
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 prevError;
}
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
2016-02-06 09:02:24 -08:00
#if EFI_PROD_CODE || EFI_SIMULATOR
void Pid::postState(TunerStudioOutputChannels *tsOutputChannels) {
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();
2017-04-10 19:07:51 -07:00
tsOutputChannels->debugFloatField6 = minResult;
tsOutputChannels->debugFloatField7 = maxResult;
2016-02-06 09:02:24 -08:00
tsOutputChannels->debugIntField1 = getP();
tsOutputChannels->debugIntField2 = getOffset();
2016-09-20 18:02:46 -07:00
tsOutputChannels->debugFloatField6 = dTerm;
2016-02-06 09:02:24 -08:00
}
#endif
2017-05-25 05:49:04 -07:00
void Pid::showPidStatus(Logging *logging, const char*msg, int dTime) {
// todo: dTime should be taken from pid_s
scheduleMsg(logging, "%s o=%f P=%.5f I=%.5f D=%.5f dT=%d",
msg,
pid->offset,
pid->pFactor,
pid->iFactor,
pid->dFactor,
dTime);
2017-05-25 05:56:36 -07:00
scheduleMsg(logging, "%f input=%d/target=%f iTerm=%.5f dTerm=%.5f",
prevResult,
prevInput,
prevTarget,
iTerm, dTerm);
2017-05-25 05:49:04 -07:00
}