NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

%% 实验2 PID控制算法编程 完整代码
clear all;
clc;
close all;

%% ========== 1. 基础参数定义(和老师课堂完全一致) ==========
T1 = 0; % 被控对象参数,对应G(s)=1/(0.02s+1),T1=0
T2 = 0.02; % 被控对象时间常数
T = 0.01; % 采样周期(比0.1更精细)
Kp = 0.2; % 比例系数
Ti = 0.0015; % 积分时间常数
Td = 0.04; % 微分时间常数
duration = 1; % 仿真总时长 1s
N = duration / T; % 总采样点数

% 初始化数组
t = zeros(1, N); % 时间序列
SV = zeros(1, N); % 设定值(阶跃输入)
u_inc = zeros(1, N);% 增量式PID控制量
u_pos = zeros(1, N);% 位置式PID控制量
y = zeros(1, N); % 系统输出

%% ========== 实验1:位置式/增量式PID 阶跃响应(开环,无反馈) ==========
% --- 1.1 增量式PID(老师课堂原版公式) ---
e = 0; e1 = 0; e2 = 0;
u1 = 0; du = 0;
for k = 1:N
t(k) = (k-1)*T;
SV(k) = 1; % 阶跃输入
e = SV(k) - 0; % 开环测试,无反馈,误差=设定值

% 增量式PID核心公式(老师课堂原版)
du = Kp * ( (e - e1) + T/Ti*e + Td/T*(e - 2*e1 + e2) );
u_inc(k) = u1 + du;

% 更新历史误差
e2 = e1;
e1 = e;
u1 = u_inc(k);
end

% --- 1.2 位置式PID(实验要求补充) ---
e_pos = zeros(1, N);
esum = 0;
for k = 1:N
e_pos(k) = SV(k) - 0; % 开环测试,无反馈
esum = esum + e_pos(k); % 积分项累加

% 位置式PID公式
u_pos(k) = Kp * ( e_pos(k) + T/Ti*esum + Td/T*(e_pos(k)-e_pos(max(k-1,1))) );
end

% --- 1.3 绘制实验1 6张图(老师要求) ---
% 图1:增量式PID阶跃响应
figure(1);
plot(t, SV, 'r', t, u_inc, 'g', 'LineWidth',1.5);
legend('SV(设定值)', 'u(控制量)');
title('实验1-图1:增量式PID阶跃输入响应');
xlabel('时间 t/s'); ylabel('输出');
axis([0 duration 0 2]); grid on;

% 图2:位置式PID阶跃响应
figure(2);
plot(t, SV, 'r', t, u_pos, 'b', 'LineWidth',1.5);
legend('SV(设定值)', 'u(控制量)');
title('实验1-图2:位置式PID阶跃输入响应');
xlabel('时间 t/s'); ylabel('输出');
axis([0 duration 0 2]); grid on;

% 图3:增量式纯P控制(Ti=inf, Td=0)
e = 0; e1 = 0; e2 = 0; u1 = 0; du = 0; u_p = zeros(1,N);
for k = 1:N
e = SV(k) - 0;
du = Kp * (e - e1); % 仅比例项
u_p(k) = u1 + du;
e2 = e1; e1 = e; u1 = u_p(k);
end
figure(3);
plot(t, SV, 'r', t, u_p, 'g', 'LineWidth',1.5);
legend('SV(设定值)', 'u(控制量)');
title('实验1-图3:增量式纯P控制阶跃响应');
xlabel('时间 t/s'); ylabel('输出');
axis([0 duration 0 2]); grid on;

% 图4:增量式纯PI控制(Td=0)
e = 0; e1 = 0; e2 = 0; u1 = 0; du = 0; u_pi = zeros(1,N);
for k = 1:N
e = SV(k) - 0;
du = Kp * ( (e - e1) + T/Ti*e ); % 比例+积分
u_pi(k) = u1 + du;
e2 = e1; e1 = e; u1 = u_pi(k);
end
figure(4);
plot(t, SV, 'r', t, u_pi, 'g', 'LineWidth',1.5);
legend('SV(设定值)', 'u(控制量)');
title('实验1-图4:增量式纯PI控制阶跃响应');
xlabel('时间 t/s'); ylabel('输出');
axis([0 duration 0 2]); grid on;

% 图5:增量式纯PD控制(Ti=inf)
e = 0; e1 = 0; e2 = 0; u1 = 0; du = 0; u_pd = zeros(1,N);
for k = 1:N
e = SV(k) - 0;
du = Kp * ( (e - e1) + Td/T*(e - 2*e1 + e2) ); % 比例+微分
u_pd(k) = u1 + du;
e2 = e1; e1 = e; u1 = u_pd(k);
end
figure(5);
plot(t, SV, 'r', t, u_pd, 'g', 'LineWidth',1.5);
legend('SV(设定值)', 'u(控制量)');
title('实验1-图5:增量式纯PD控制阶跃响应');
xlabel('时间 t/s'); ylabel('输出');
axis([0 duration 0 2]); grid on;

% 图6:4种参数对比总图
figure(6);
plot(t, SV, 'k--', 'LineWidth',1.5); hold on;
plot(t, u_p, 'g-', 'DisplayName','纯P');
plot(t, u_pi, 'm-', 'DisplayName','纯PI');
plot(t, u_pd, 'c-', 'DisplayName','纯PD');
plot(t, u_inc, 'r-', 'DisplayName','完整PID');
legend; grid on;
title('实验1-图6:增量式PID不同参数阶跃响应对比');
xlabel('时间 t/s'); ylabel('输出');
axis([0 duration 0 2]);

%% ========== 实验2:闭环控制(对象G(s)=1/(0.02s+1),不同PID参数对比) ==========
% 定义3组不同PID参数,对比控制效果
Kp_set = [0.1, 0.2, 0.5]; % 比例系数
Ti_set = [0.003, 0.0015, 0.00075]; % 积分时间常数
Td_set = [0.02, 0.04, 0.08]; % 微分时间常数
color_set = ['r', 'g', 'b'];
label_set = {'Kp=0.1, Ti=0.003, Td=0.02', 'Kp=0.2, Ti=0.0015, Td=0.04(老师参数)', 'Kp=0.5, Ti=0.00075, Td=0.08'};

figure(7);
hold on; grid on;
plot(t, SV, 'k--', 'LineWidth',1.5, 'DisplayName','阶跃设定值SV');

for idx = 1:3
Kp = Kp_set(idx);
Ti = Ti_set(idx);
Td = Td_set(idx);

% 初始化变量
e = 0; e1 = 0; e2 = 0;
u1 = 0; du = 0; y1 = 0;
y_cl = zeros(1, N); % 闭环系统输出
u_cl = zeros(1, N); % 闭环控制量

for k = 1:N
t(k) = (k-1)*T;
SV(k) = 1;
e = SV(k) - y1; % 闭环误差:设定值 - 上一时刻输出

% 增量式PID计算
du = Kp * ( (e - e1) + T/Ti*e + Td/T*(e - 2*e1 + e2) );
u_cl(k) = u1 + du;

% 被控对象离散化计算(老师课堂公式,T1=0)
y_cl(k) = 1/(T2 + T) * ( u_cl(k)*(T1 + T) - T1*u1 + T2*y1 );

% 更新历史值
e2 = e1;
e1 = e;
u1 = u_cl(k);
y1 = y_cl(k);
end

% 绘制当前参数的闭环响应
plot(t, y_cl, color_set(idx), 'LineWidth',1.5, 'DisplayName',label_set{idx});
end

xlabel('时间 t/s'); ylabel('系统输出 y(t)');
title('实验2:不同PID参数下的闭环阶跃响应 (对象G(s)=1/(0.02s+1))');
legend('Location','southeast');
axis([0 duration 0 1.2]);
hold off;

%% ========== 老师课堂原版闭环测试代码(单独运行版) ==========
% 取消注释即可运行,和老师PPT完全一致
% clear all; clc; close all;
% T1=0; T2=0.02; T=0.01;
% Kp=0.2; Ti=0.0015; Td=0.04;
% duration=1;
% e=0;e1=0;e2=0;esum=0;
% u1=0;du=0;y1=0;
% N=duration/T;
% t=zeros(1,N); SV=zeros(1,N); u=zeros(1,N); y=zeros(1,N);
%
% for k=1:N
% t(k)=(k-1)*T;
% SV(k)=1;
% e=SV(k)-y1;
% du=Kp*(e-e1+T/Ti*e+Td/T*(e-2*e1+e2));
% u(k)=u1+du;
% e2=e1;
% e1=e;
% % 被控对象计算(
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.