Hello everyone, I am trying to program PID algorithm in c++ first.It is 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<iostream> 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:"<<output<<endl; return 0; } I am confused.Is it ok? Thanks
It seems okay to me. Only that is confusing is "dt" term . You need to describe the system .ie how the error signal changes with respect to time for accurate simulation results. Use eulers Integration formula then. Nitin