STM32
直播中

张莉

7年用户 1249经验值
私信 关注
[问答]

萌新求助,求一个matlab例程

萌新求助,求一个matlab例程

回帖(1)

潘沪城

2021-11-22 09:36:56
构建思想






代码原理在word文档中:
逻辑控制器是对人主动开环控制及强时变控制的模拟,预测控制是对师傅经验的模拟,其共同特征是主动时变控制。两者的结合将是对人的控制思维特点的较全面模拟。本文提出的仿人逻辑控制器则是基于这样一种观察:有经验的师傅在系统超出极限时甚至处于危险时会毫不犹豫的切断某些重要通路,而在系统工作处于正常范围时则依照经验控制,使波动尽量小。这样人工处理的结果是:系统在正常区间运行平稳,突发事件瞬间切换,能够确保系统安全,模型失配时也能保证满意运行。虽然这样的处理也许不是全局最优的,但却是在确保强鲁棒性基础上的局部最优,这样一种策略对于某些工业控制至关重要。





控制器的工作原理如下:在系统工作时逻辑控制器和预测控制器同时工作,其控制输出量分别为K(t)和U(t)。逻辑控制设置较宽的误差控制带其作用是用最短的时间将系统带入正常状态,暂且不管小的波动。预测控制的作用是利用预测模型将系统稳定在正常状态,并使控制曲线最优。控制量切换要选择合适的时机对这两种控制信号进行切换。具体而言,当系统远离正常状态,即通过逻辑状态判断其运行状态处于K4+、K3+和K4-、K3-四种状态时,控制量切换到逻辑控制状态。而当运行状态在K+、K-和K时,控制量切换到预测控制器,此时相当于师傅的经验开始起作用。这样一种控制结构充分利用了逻辑控制对模型匹配要求极低的特点,在模型失配的情况下仍然能取得满意效果,而在模型匹配情况下取得最优效果。此控制器的本质是时变开环控制加闭环校正控制,其物理概念清晰,系数整定方便,是将人的思维控制方式融合到模型中的结果。
matlab simulink框图

此处运用matlab simulink模块构建:















MPC控制器参数:










结果展示






模型失配时 框图及结果











扰动后框图及结果






扰动值:





更改仿真时间到50s后的结果:





可以对结果分析:
在15秒添加扰动后,MPC+逻辑控制还是可以回到之前的顺滑轨迹,比之前回的慢了。

s函数代码
mpc2.m


Ts=.1;    % Sampling time
p=20;     % Prediction horizon
m=3;      % Control horizon


MPC2=mpc(tf(3,[2 3 4]),Ts,p,m);


NineState.m


%参数e0和de0是设定的相平面误差允许范围。
function [sys,x0,str,ts] = mfile(t,x,u,flag,e0,de0)


switch flag,
  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  % Initialize the states, sample times, and state ordering strings.
  case 0
    [sys,x0,str,ts]=mdlInitializeSizes;


  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  % Return the outputs of the S-function block.
  case 3
    sys=mdlOutputs(t,x,u,e0,de0);


  %%%%%%%%%%%%%%%%%%%
  % Unhandled flags %
  %%%%%%%%%%%%%%%%%%%
  % There are no termination tasks (flag=9) to be handled.
  % Also, there are no continuous or discrete states,
  % so flags 1,2, and 4 are not used, so return an emptyu
  % matrix
  case { 1, 2, 4, 9 }
    sys=[];


  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Unexpected flags (error handling)%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Return an error message for unhandled flag values.
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);


end


% end timestwo


%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes()


sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 1;  % dynamically sized
sizes.NumInputs      = 2;  % dynamically sized
sizes.DirFeedthrough = 1;   % has direct feedthrough
sizes.NumSampleTimes = 1;


sys = simsizes(sizes);
str = [];
x0  = [];
ts  = [-1 0];   % inherited sample time


% end mdlInitializeSizes


%
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%u(1,1)为输入控制量e
%u(2,1)为输入控制量de
function sys = mdlOutputs(t,x,u,e0,de0)


if (u(1,1)>=e0 & u(2,1)>=de0)
    sys=15;
elseif (u(1,1)>=e0 & abs(u(2,1))     sys=4;   
elseif (u(1,1)>=e0 & u(2,1)<=-de0)
    sys=2;
elseif abs((u(1,1))=de0)
    sys=1;
elseif abs(u(1,1))     sys=0;
elseif abs(u(1,1))     sys=-1;   
elseif (u(1,1)<=-e0 & u(2,1)>=de0)
    sys=-2;
elseif (u(1,1)<=-e0 &abs(u(2,1))     sys=-4;
elseif (u(1,1)<=-e0 & u(2,1)<-de0)
    sys=-15;
else
     error(['my error ']);
end
% end mdlOutputs


switchUK.m


%参数e0和de0是设定的相平面误差允许范围。
function [sys,x0,str,ts] = switchUK(t,x,u,flag,K1,K3,tempi)


switch flag,
  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  % Initialize the states, sample times, and state ordering strings.
  case 0
    [sys,x0,str,ts]=mdlInitializeSizes;


  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  % Return the outputs of the S-function block.
  case 3
    sys=mdlOutputs(t,x,u,K1,K3,tempi);


  %%%%%%%%%%%%%%%%%%%
  % Unhandled flags %
  %%%%%%%%%%%%%%%%%%%
  % There are no termination tasks (flag=9) to be handled.
  % Also, there are no continuous or discrete states,
  % so flags 1,2, and 4 are not used, so return an emptyu
  % matrix
  case { 1, 2, 4, 9 }
    sys=[];


  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Unexpected flags (error handling)%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % Return an error message for unhandled flag values.
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);


end


% end timestwo


%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes()


sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 1;  % dynamically sized
sizes.NumInputs      = 2;  % dynamically sized
sizes.DirFeedthrough = 1;   % has direct feedthrough
sizes.NumSampleTimes = 1;


sys = simsizes(sizes);
str = [];
x0  = [];
ts  = [-1 0];   % inherited sample time




% end mdlInitializeSizes


%
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%u(1,1)为输入控制量K(t)
%u(2,1)为输入控制量U(t)
function sys = mdlOutputs(t,x,u,K1,K3,tempi)


if abs(u(1,1))>K3
    sys=u(1,1);%如果输入控制量的绝对值比较大,即误差太大。则切换到K(t)逻辑控制
    tempi=1;
elseif abs(u(1,1))     sys=u(2,1);%如果输入控制量的绝对值比较小,则切换到U(t)预测控制
    tempi=0;
else
    if  tempi==0     %如果已经是预测控制,则保持,此处保持预测控制优先
        sys=u(2,1);
    else
       sys=u(1,1);%如果已经是逻辑控制,则保持
    end
end
% end mdlOutputs


注意

运行simulink前需先运行mpc2.m文件。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分