PID algorithm in C++

Discussion in 'Computer Science & Culture' started by S.Tarafdar, Dec 30, 2003.

Thread Status:
Not open for further replies.
  1. S.Tarafdar Registered Senior Member

    Messages:
    35
    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:
    Code:
    
    #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
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Absane Rocket Surgeon Valued Senior Member

    Messages:
    8,989
    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))
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. S.Tarafdar Registered Senior Member

    Messages:
    35
    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
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
Thread Status:
Not open for further replies.

Share This Page