there really is no reason for an if statement in a PID! PID's in of their own aren't really that complicated, and if you start adding if statements and things like that it can only have the potential to cause problems. if you don't want to use the D component just don't give it a constant, If you want negative feedback,make the constant negative. here is the class I wrote last year in c++. Simplicity is king!
Code:
class myPID{
private:
double Kp,Ki,Kd;
double error, error1 = 0;
double integral;
float value;
public:
void Update(double,double);
float GetValue(void);
void SetConstants(double,double,double);
};
void myPID::Update(double target,double actual){
error = target - actual;
integral += error*.01; //todo add actualy eleapsed time
value = (error*Kp) + (integral*Ki) + ((error-error1)/(.01)*Kd);
error1 = error;
}
float myPID::GetValue(){
return value;
}
void myPID::SetConstants(double Kp,double Ki,double Kd){
this->Kp=Kp;
this->Ki=Ki;
this->Kd=Kd;
}