View Full Version : PID algorithm in C++


S.Tarafdar
12-30-03, 04:51 AM
Hello everyone,
Do anyone of you have an idea about PID algorithm?It is basically like this:

output(i)=Kp*error(i)+Ki*delT*(summation from 1 to i)error(i)+Kd/delT*(e(i)-e(i-1))

This is what I wrote:


#include&ltiostream&gt
using namespace std;


int main(){
float error,Paction,Daction,Iaction,lastError,delT;
float Kp,Ki,Kd,setPoint;
float measurement=0,output=0;
int i;

cout<<"Enter the value of Kp,Ki,Kd,delT:";
cin>>Kp>>Ki>>Kd>>delT;
cout<<"Enter the Value of Set Point:";
cin>>setPoint;
Iaction=0;
lastError=0;

for(i=0;i<4;i++){
error=setPoint-measurement;
Paction=error*Kp;
Iaction+=Ki*delT*error;
Daction=Kd*(error-lastError)/delT;
output=Paction+Iaction+Daction;

lastError=error;
measurement=output;


}

cout<<"Output:"&lt&ltoutput&lt&ltendl;

return 0;
}



I am confused.Is it ok?

Thanks

Absane
12-30-03, 11:45 PM
Before I even try to look at your C++ code... explain the formula.

output(i)=Kp*error(i)+Ki*delT*(summation from 1 to i)error(i)+Kd/delT*(e(i)-e(i-1))

S.Tarafdar
12-31-03, 04:08 AM
ok.
This is a control algorithm.First term is the Proportional part, second is the Integral part and the third is the Derivative part.

delT is the sampling time;error=setPoint-measurement.

measurement is the feedback of the system.

Kp is the proportional constant, Ki is the integral constant and so on.

Paction is the result of integral part.It sums up error from 0 to i(i is last sampling ).
Daction is the result of difference equation.

I hope you will understand it now.

Thanks