PID implementation using code
14 views (last 30 days)
Show older comments
Sanjeet Kulkarni on 24 Nov 2022
Commented: Sanjeet Kulkarni on 28 Nov 2022
I have implemented a PID controller using simulink pid block for a system. I was trying to implement the same thing using a PID code (formula) in a matlab function block and compare the performances. However, I am getting differences in both the cases. Unable to figure out where exactly is the problem.
For simulink block I have implemented antiwindup, similarly I have done it in the code.
PID code as in the matlab function is as below:
function y = fcn(error,kp,ki)
dt = sampletime;
persistent integral newintegral
integral = 0;
newintegral = 0;
newintegral = integral + (error*dt);
output = kp*error + ki * integral;
integral = newintegral;
y = output;
Is there anything that I am implementing wrong in the code? Requesting some help/direction.
Sam Chak on 25 Nov 2022
Wouldn't it be simpler if you obtain the signal directly from the Integrator block, and then feed it into the MATLAB function block? Something like this
function y = fcn(err, errint, Kp, Ki)
where err is the error signal and errint is the intergral of error signal.
Find more on Real-Time PID Autotuning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!