PID Matlab scripts is not running
1 次查看(过去 30 天)
显示 更早的评论
%Closed loop Algorthim
%Error=Setpoint -Feedback
%Setpoint:5
%Feedback:0,1,2,3,4,5(my assumption)
previous_error=0;
integral=0;
kp=1;
ki=1;
kd=1;
sp=[5,5,5,5,5,5];
fb=[0,1,2,3,4,5];
error=[5,4,3,2,1,0];
dt=[0,1,2,3,4,5]
error=sp-fb
integral=(integral + error) * dt
derivative= (error - previous_error) / dt
output=(er*kp)+(ki*integral)+(kd*derivative)
previous_error= error
plot(output,dt)
0 个评论
回答(1 个)
Walter Roberson
2020-12-31
Change all * to .* and all / to ./
1 个评论
Walter Roberson
2020-12-31
format long g
%Closed loop Algorthim
%Error=Setpoint -Feedback
%Setpoint:5
%Feedback:0,1,2,3,4,5(my assumption)
previous_error=0;
integral=0;
kp=1;
ki=1;
kd=1;
sp=[5,5,5,5,5,5];
fb=[0,1,2,3,4,5];
error=[5,4,3,2,1,0];
dt=[0,1,2,3,4,5]
error=sp-fb
integral=(integral + error) .* dt
derivative= (error - previous_error) ./ (dt+(dt==0)/5)
output=(error.*kp)+(ki.*integral)+(kd.*derivative)
previous_error= error
plot(dt,output)
The (dt+(dt==0)/5) clause is effectively: dt if dt is non-zero, 1/5 if dt is zero. It is there to prevent division by 0, which would give infinity. The 1/5 was chosen arbitrarily to not skew the plot too high but stil emphasize that the value is much higher than the others.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 PID Controller Tuning 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!