模型預測控制(MPC)+邏輯控制(相平面分區控制)--matlab例程介紹

模型預測控制(MPC)+邏輯控制(相平面分區控制)–matlab例程介紹
MATLAB-模型模糊神經網絡預測控制demo(訓練數據用):鏈接:https://blog.csdn.net/answerMack/article/details/103628559

構建思想

在這裏插入圖片描述
代碼原理在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))<de0)
    sys=4;    
elseif (u(1,1)>=e0 & u(2,1)<=-de0)
    sys=2;
elseif abs((u(1,1))<e0 & u(2,1)>=de0)
    sys=1;
elseif abs(u(1,1))<e0 & abs(u(2,1))<de0
    sys=0;
 elseif abs(u(1,1))<e0 & u(2,1)<-de0
    sys=-1;   
elseif (u(1,1)<=-e0 & u(2,1)>=de0)
    sys=-2;
elseif (u(1,1)<=-e0 &abs(u(2,1))<de0)
    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))<K1
    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文件。

參考

參考書籍:《matlab控制系統應用與實例》清華大學出版社 樊京、劉叔軍、蓋曉華、崔世林編輯。
源碼鏈接:http://www.tupwk.com.cn/downpage/(此爲書籍代碼鏈接)
參考文章:https://www.cnblogs.com/kui-sdu/p/9026796.html

發佈了43 篇原創文章 · 獲贊 23 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章