Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
I have written a program in mikroC that performs the task (PID control algorithm), but the result was not correct. The step response of the system was not as that theoritcally expected
Here is the code
float measured,desired;
float error,kp,ki,ek_1,uk,uk_1,T,ta,k,ts,a,b;
unsigned int uk_o;
unsigned char mV[6];
void main()
{
TRISA=0xFF;
TRISC=0;
TRISD=0;
ADCON1=0x80;
//// First order system parameters and calculation of PI parameters
T=1;
ts=5;
k=1;
ta=10;
kp=5*ta/(k*ts);
ki=5/(k*ts);
ek_1=0;
uk_1=0;
error=(float)((desired-measured)*5000/1024);
uk=kp*error+ki*(error+ek_1)*T; // Positional form of a PI
if(uk>5) uk=5;
if(uk<0) uk=0;
//uk=(uk_1+(kp+ki*T)*error-kp*ek_1); // Velocity form
ek_1=error;
uk_1=uk;
uk_o=(int)(uk*1024/5000);
PORTC=uk_o;
PORTD=uk_o/4;
}
}
and this is a file that represents the circuit diagram
// Error = setpoint - process value
e = pid->sp - pid->pv;
// Calculate integral term
pid->i += e * dt;
// Check integral term for upper limit
if (pid->i > pid->i_max)
{
pid->i = pid->i_max;
}
// Check integral term for lower limit
if (pid->i < -pid->i_max)
{
pid->i = -pid->i_max;
}
// Derivative part = (process value - last process value) / dt
if (dt > 0.00001) // Avoid division by zero
{
d = (pid->pv - pid->pv_last) / dt;
}
else
{
d = 0.0;
}
// Control value = gain * (kp * e + ki * i - kd * d)
pid->cv = pid->gain * (pid->kp * e + pid->ki * pid->i - pid->kd * d);
// Add min to control value
if (pid->cv >= 0.0 && pid->cv < pid->cv_min)
{
pid->cv += pid->cv_min;
}
else if (pid->cv < 0.0 && pid->cv > -pid->cv_min)
{
pid->cv -= pid->cv_min;
}
// Check control value upper limits
if (pid->cv > pid->cv_max)
{
pid->cv = pid->cv_max;
}
if (pid->cv < -pid->cv_max)
{
pid->cv = -pid->cv_max;
}
// Store reference value for next control cycle
pid->pv_last = pid->pv;
}
typedef struct sPID
{
float gain; // Controller gain
float kp; // Coefficient for proportional term
float ki; // Coefficient for integral term
float kd; // Coefficient for derivative term
float sp; // Setpoint
float i; // Integral term
float i_max; // Integral part limit
float pv; // Process value
float pv_last; // Last process value
float cv; // Control value
float cv_min; // Minimum control value
float cv_max; // Control value limit
} tPID;
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.