auto-sync

This commit is contained in:
rusEfi 2014-09-16 16:03:03 -05:00
parent 16c8f53d2b
commit 842d45477f
2 changed files with 17 additions and 3 deletions

View File

@ -9,10 +9,12 @@
#include "pid.h"
Pid::Pid(float pFactor, float iFactor, float dFactor) {
Pid::Pid(float pFactor, float iFactor, float dFactor, float minResult, float maxResult) {
this->pFactor = pFactor;
this->iFactor = iFactor;
this->dFactor = dFactor;
this->minResult = minResult;
this->maxResult = maxResult;
integration = 0;
prevError = 0;
@ -26,6 +28,16 @@ float Pid::getValue(float target, float input, float dTime) {
float dTerm = dFactor / dTime * (error - prevError);
prevError = error;
return pTerm + integration + dTerm;
float result = pTerm + integration + dTerm;
if (result > maxResult) {
integration -= result - maxResult;
result = maxResult;
} else if (result < minResult) {
integration += minResult - result;
result = minResult;
}
return result;
}

View File

@ -11,13 +11,15 @@
class Pid {
public:
Pid(float pFactor, float iFactor, float dFactor);
Pid(float pFactor, float iFactor, float dFactor, float minResult, float maxResult);
float getValue(float target, float input, float dTime);
private:
float pFactor;
float iFactor;
float dFactor;
float minResult;
float maxResult;
float integration;
float prevError;