模糊PID控制、BP神经网络控制、PID控制、串级PID控制、 模糊控制 、 粒子群算法PID参数优化。有扰动无扰动(信号干扰)两种模型。有simulink模型 c代码GUI模型。matlab代码。还有一些调参说明。主仿真脚本 (main_simulation.m)功能定义工况有/无扰动循环调用不同控制器绘制对比曲线。matlab编辑1%% main_simulation.m - 多算法控制策略对比仿真2clear; clc; close all;34% 1. 系统参数设置 5Ts 0.001; % 采样时间 1ms6T_sim 2.0; % 仿真总时间 2s7t 0:Ts:T_sim; % 时间向量8target_val 12; % 目标电压/速度 12V910% 2. 工况选择 (0: 无扰动, 1: 有扰动) 11disturbance_mode 1; % — 修改这里切换工况1213% 3. 初始化结果存储 14results struct();15algorithms {‘PID’, ‘FuzzyPID’, ‘BP_NN’, ‘CascadePID’, ‘FuzzyLogic’, ‘PSO_PID’};1617fprintf(‘开始仿真… 工况: %s\n’, ifelse(disturbance_mode, ‘有扰动’, ‘无扰动’));1819for i 1:length(algorithms)20 algo_name algorithms{i};21 fprintf(‘正在运行: %s …\n’, algo_name);2223 % 重置状态24 y_out zeros(size(t));25 u_out zeros(size(t));26 error_hist zeros(size(t));2728 % 控制器初始状态29 ctrl_state init_controller(algo_name);3031 for k 1:length(t)32 % 1. 计算误差33 e target_val - y_out(k);3435 % 2. 注入扰动 (在 t1.0s 时加入阶跃干扰)36 d 0;37 if disturbance_mode t(k) 1.038 d 2.0; % 2V 的负载突变干扰39 end4041 % 3. 控制器计算42 [u, ctrl_state] step_controller(algo_name, e, t(k), ctrl_state);4344 % 4. 被控对象响应 (带扰动)45 % 简单的一阶惯性加延迟模型模拟 DC-DC 或电机46 if k 147 y_out(k) 0;48 else49 % y(k) ay(k-1) bu(k-1) disturbance50 % 模拟参数时间常数约 0.05s51 a 0.9; b 0.1;52 y_out(k) a * y_out(k-1) b * u d * 0.5;53 % 限制输出范围54 y_out(k) max(0, min(24, y_out(k)));55 end5657 u_out(k) u;58 error_hist(k) e;59 end6061 % 保存结果62 results.(algo_name).y y_out;63 results.(algo_name).u u_out;64 results.(algo_name).e error_hist;65end6667% 4. 绘图对比 68figure(‘Name’, ‘控制效果对比’, ‘Color’, ‘w’);69hold on; grid on;70colors lines(length(algorithms));7172for i 1:length(algorithms)73 name algorithms{i};74 plot(t, results.(name).y, ‘LineWidth’, 1.5, ‘Color’, colors(i,:), ‘DisplayName’, name);75end7677% 参考线78plot(t, ones(size(t))*target_val, ‘k–’, ‘LineWidth’, 2, ‘DisplayName’, ‘Target’);79if disturbance_mode80 xline(1.0, ‘r:’, ‘Disturbance Start’, ‘LabelVerticalAlignment’, ‘bottom’);81end8283title([控制策略对比 (工况: ’ ifelse(disturbance_mode, ‘有扰动’, ‘无扰动’) ‘)’]);84xlabel(‘Time (s)’); ylabel(‘Output Value’);85legend(‘Location’, ‘best’);86ylim([0, 18]);8788% 辅助函数 (内嵌) 89function val ifelse(cond, a, b)90 if cond, val a; else, val b; end91end2. 控制器核心逻辑 (step_controller.m 占位1function [u, state] step_controller(name, e, t, state)2 switch name3 case ‘PID’4 % 标准增量式 PID5 if isempty(state), state struct(‘e_prev’, 0, ‘e_prev2’, 0, ‘u_prev’, 0, ‘sum_e’, 0); end6 Kp 1.5; Ki 0.5; Kd 0.1;7 state.sum_e state.sum_e e;8 de e - state.e_prev;9 u state.u_prev Kp*(e - state.e_prev) Kie Kdde;10 state.e_prev2 state.e_prev; state.e_prev e; state.u_prev u;1112 case ‘FuzzyPID’13 % 模糊自整定 PID: 根据 e 和 ec 在线调整 Kp, Ki, Kd14 % 这里简化为查表逻辑实际需 fuzzy logic toolbox 或自定义隶属度函数15 if isempty(state), state struct(‘e_prev’, 0, ‘Kp’, 1.0, ‘Ki’, 0.1, ‘Kd’, 0.05); end16 ec e - state.e_prev;1718 % — 模糊规则简化示例 —19 if abs(e) 2 % 误差大20 state.Kp 2.0; state.Ki 0.1; state.Kd 0.01;21 elseif abs(e) 0.5 % 误差中22 state.Kp 1.5; state.Ki 0.3; state.Kd 0.05;23 else % 误差小24 state.Kp 1.0; state.Ki 0.5; state.Kd 0.1;25 end2627 u state.Kpe state.Ki(state.e_prev e)0.001 state.Kd(e-state.e_prev)/0.001;28 state.e_prev e;2930 case ‘BP_NN’31 % BP 神经网络整定 PID (简化版)32 % 输入: e(k), e(k-1), e(k-2); 输出: Kp, Ki, Kd33 if isempty(state)34 state struct(‘W_io’, rand(3,4), ‘W_oh’, rand(3,3), …35 ‘e_hist’, [0;0;0], ‘Kp’, 1, ‘Ki’, 0.1, ‘Kd’, 0.05);36 end37 % 1. 输入层38 x_in [e; state.e_hist(1); state.e_hist(2); 1];39 % 2. 隐藏层 (Sigmoid)40 h_in state.W_io * x_in;41 h_out 1 ./ (1 exp(-h_in));42 h_out [h_out; 1];43 % 3. 输出层 (Sigmoid 映射到 PID 参数范围)44 o_in state.W_oh * h_out;45 o_out 1 ./ (1 exp(-o_in));4647 Kp o_out(1) * 3; Ki o_out(2) * 0.5; Kd o_out(3) * 0.5;4849 % 计算控制量50 u Kpe Kisum(state.e_hist)0.001 Kd(e-state.e_hist(1))/0.001;5152 % 反向传播更新权重 (简化省略详细梯度推导仅做演示)53 % 实际项目中需计算 dy/du 和 误差梯度54 % state.W_oh state.W_oh learning_rate * …5556 state.e_hist [e; state.e_hist(1); state.e_hist(2)];57 state.Kp Kp; state.Ki Ki; state.Kd Kd;5859 case ‘CascadePID’60 % 串级 PID: 外环电压内环电流 (此处用虚拟内环状态模拟)61 if isempty(state), state struct(‘e_outer’, 0, ‘i_inner’, 0, ‘u_inner’, 0); end62 % 外环63 Kp1 1.0; Ki1 0.2;64 state.i_inner state.i_inner Kp1*(e - state.e_outer) Ki1e; % 内环给定值65 state.e_outer e;66 % 内环 (假设内环响应极快直接输出)67 Kp2 2.0;68 u Kp2 * (state.i_inner - state.u_inner);69 state.u_inner u; % 简化反馈7071 case ‘FuzzyLogic’72 % 纯模糊控制 (非 PID 参数整定直接输出 u)73 % 需定义隶属度函数 NB, NM, NS, ZO, PS, PM, PB74 if isempty(state), state struct(‘e_prev’, 0); end75 ec e - state.e_prev;76 % 简化重心法解模糊逻辑77 u 0.5e 0.2ec;78 state.e_prev e;7980 case ‘PSO_PID’81 % PSO 通常用于离线优化这里模拟使用 PSO 优化后的固定参数82 % 假设 PSO 已经跑完得到了最优参数83 if isempty(state), state struct(‘used’, 1); end84 Kp 1.8; Ki 0.6; Kd 0.15; % PSO 优化后的结果85 % 复用 PID 公式86 if ~isfield(state, ‘sum_e’), state.sum_e 0; state.e_prev 0; state.u_prev 0; end87 state.sum_e state.sum_e e;88 u state.u_prev Kp(e - state.e_prev) Kie Kd(e-state.e_prev);89 state.e_prev e; state.u_prev u;90 end9192 % 执行器饱和限制93 u max(0, min(24, u));94end9596function state init_controller(name)97 state [];98end1%% generate_simulink.m2model_name ‘Advanced_Control_System’;3if exist(model_name, ‘model’), close_system(model_name, 0); delete_system(model_name); end4new_system(model_name); open_system(model_name);56% 添加模块7add_block(‘simulink/Sources/Step’, [model_name ‘/Target’], ‘Position’, [50 50 80 80]);8add_block(‘simulink/Sinks/Scope’, [model_name ‘/Scope’], ‘Position’, [400 50 430 80]);9add_block(‘simulink/User-Defined Functions/MATLAB Function’, [model_name ‘/Controller_Algo’], …10 ‘Position’, [150 50 200 80], ‘Script’, ‘function u fcn(e, t) %#codegen\n u e * 1.5;’); % 占位1112add_block(‘simulink/Continuous/Transfer Fcn’, [model_name ‘/Plant’], …13 ‘Numerator’, ‘[1]’, ‘Denominator’, ‘[1 2 1]’, ‘Position’, [250 50 300 80]);1415% 连线16add_line(model_name, ‘Target/1’, ‘Controller_Algo/1’);17add_line(model_name, ‘Controller_Algo/1’, ‘Plant/1’);18add_line(model_name, ‘Plant/1’, ‘Scope/1’); MATLAB 代码示例位置式 PIDmatlab编辑1% simple_pid.m2function u pid_controller(e, dt, state)3 % state: [sum_e, e_prev]4 if isempty(state)5 state [0; 0];6 end78 Kp 1.5; Ki 0.5; Kd 0.1;910 sum_e state(1) e * dt;11 de (e - state(2)) / dt;1213 u Kp * e Ki * sum_e Kd * de;1415 % 限幅16 u max(-10, min(10, u));1718 state [sum_e; e];19end2021% 测试脚本22Ts 0.01; t 0:Ts:5;23y zeros(size(t));24state [];25for i 2:length(t)26 e 1 - y(i-1); % 目标值127 [u, state] pid_controller(e, Ts, state);28 y(i) 0.9y(i-1) 0.1u; % 一阶惯性对象29end30plot(t, y); grid on; title(‘基础 PID 控制响应’);31xlabel(‘Time (s)’); ylabel(‘Output’); 第二部分嵌入式实战C语言实现✅ 对应文档《飞思卡尔智能车电磁循迹系统硬件设计》《ATMEGA328P自动避障小车程序源码原理图》《AN_SPMCT5_001(PID调节在电机控制中的应用)》 C 代码模板适用于 STM32 / Arduinoc编辑1// pid.h2#ifndefPID_H3#definePID_H45typedef struct {6 float Kp, Ki, Kd;7 float setpoint;8 float integral;9 float prev_error;10 float output_max;11 float output_min;12} PID_TypeDef;1314void PID_Init(PID_TypeDef* pid, float kp, float ki, float kd, float sp);15float PID_Calc(PID_TypeDef* pid, float feedback);1617#endif1819// pid.c20#include “pid.h”2122void PID_Init(PID_TypeDef* pid, float kp, float ki, float kd, float sp) {23 pid-Kp kp; pid-Ki ki; pid-Kd kd;24 pid-setpoint sp;25 pid-integral 0;26 pid-prev_error 0;27 pid-output_max 100;28 pid-output_min -100;29}3031float PID_Calc(PID_TypeDef* pid, float feedback) {32 float error pid-setpoint - feedback;33 pid-integral error;3435 // 抗积分饱和36 if (pid-integral 100) pid-integral 100;37 if (pid-integral -100) pid-integral -100;3839 float derivative error - pid-prev_error;40 float output pid-Kp * error pid-Ki * pid-integral pid-Kd * derivative;4142 // 输出限幅43 if (output pid-output_max) output pid-output_max;44 if (output pid-output_min) output pid-output_min;4546 pid-prev_error error;47 return output;48} 第三部分智能控制研究生/竞赛级✅ 对应文档《模糊PID》《BP神经网络控制》《粒子群算法PID参数优化》《串级PID控制》 示例PSO 优化 PID 参数MATLABmatlab编辑1% pso_pid_optimize.m2options optimoptions(‘particleswarm’, ‘SwarmSize’, 30, ‘MaxIterations’, 50);3lb [0.1, 0.01, 0.01]; % Kp, Ki, Kd 下界4ub [5.0, 1.0, 1.0]; % 上界56fitness_func (x) simulate_pid_performance(x(1), x(2), x(3));78[x_opt, fval] particleswarm(fitness_func, 3, lb, ub, options);910fprintf(‘最优参数: Kp%.3f, Ki%.3f, Kd%.3f\n’, x_opt(1), x_opt(2), x_opt(3));1112function cost simulate_pid_performance(Kp, Ki, Kd)13 Ts 0.01; T 5; t 0:Ts:T;14 y zeros(size(t)); e_sum 0; e_prev 0;15 for i 2:length(t)16 e 1 - y(i-1);17 e_sum e_sum eTs;18 de (e - e_prev)/Ts;19 u Kpe Kie_sum Kdde;20 y(i) 0.9y(i-1) 0.1u;21 e_prev e;22 end23 % ITAE 性能指标24 cost sum(abs(e) .* t);25end️ 第四部分Simulink GUI C 代码生成✅ 自动生成 Simulink 模型含模糊/PID/神经网络matlab编辑1% generate_advanced_model.m2model ‘Advanced_PID_Control’;3new_system(model); open_system(model);45% 添加模块6add_block(‘simulink/Sources/Step’, [model ‘/Reference’], ‘Position’, [50 50 80 80]);7add_block(‘simulink/User-Defined Functions/MATLAB Function’, [model ‘/Fuzzy_PID’], …8 ‘Position’, [150 50 200 80], ‘Script’, ‘function u fcn(e, ec) %#codegen\n u 0.5e 0.2ec;’);9add_block(‘simulink/Continuous/Transfer Fcn’, [model ‘/Plant’], …10 ‘Numerator’, ‘[1]’, ‘Denominator’, ‘[1 2 1]’, ‘Position’, [250 50 300 80]);11add_block(‘simulink/Sinks/Scope’, [model ‘/Scope’], ‘Position’, [350 50 380 80]);1213% 连线14add_line(model, ‘Reference/1’, ‘Fuzzy_PID/1’);15add_line(model, ‘Fuzzy_PID/1’, ‘Plant/1’);16add_line(model, ‘Plant/1’, ‘Scope/1’);1718disp(‘Simulink 模型已生成请双击 MATLAB Function 编辑模糊逻辑或神经网络代码。’); 第五部分GUI 调试界面交互式调参matlab编辑1% simple_gui_pid.m2f figure(‘Name’, ‘PID 参数调试台’, ‘Position’, [100 100 600 400]);34uicontrol(‘Style’, ‘text’, ‘String’, ‘Kp:’, ‘Position’, [20 300 30 20]);5h_kp uicontrol(‘Style’, ‘edit’, ‘String’, ‘1.5’, ‘Position’, [60 300 50 20]);67uicontrol(‘Style’, ‘text’, ‘String’, ‘Ki:’, ‘Position’, [20 270 30 20]);8h_ki uicontrol(‘Style’, ‘edit’, ‘String’, ‘0.5’, ‘Position’, [60 270 50 20]);910uicontrol(‘Style’, ‘text’, ‘String’, ‘Kd:’, ‘Position’, [20 240 30 20]);11h_kd uicontrol(‘Style’, ‘edit’, ‘String’, ‘0.1’, ‘Position’, [60 240 50 20]);1213uicontrol(‘Style’, ‘pushbutton’, ‘String’, ‘运行仿真’, ‘Position’, [20 200 80 30], ‘Callback’, run_sim);1415h_axes axes(‘Parent’, f, ‘Position’, [0.2 0.1 0.7 0.7]);1617 function run_sim(~, ~)18 Kp str2double(get(h_kp, ‘String’));19 Ki str2double(get(h_ki, ‘String’));20 Kd str2double(get(h_kd, ‘String’));2122 Ts 0.01; t 0:Ts:5;23 y zeros(size(t)); e_prev 0; sum_e 0;2425 for i 2:length(t)26 e 1 - y(i-1);27 sum_e sum_e eTs;28 de (e - e_prev)/Ts;29 u Kpe Kisum_e Kdde;30 y(i) 0.9y(i-1) 0.1u;31 e_prev e;32 end3334 cla(h_axes); plot(t, y); grid on;35 title([‘PID 响应 (Kp’ num2str(Kp) ‘, Ki’ num2str(Ki) ‘, Kd’ num2str(Kd) ‘)’]);36 end37end