-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d6c855
commit 3a3019e
Showing
4 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
%% jMPC Toolbox Model Predictive Control Examples | ||
% Embedded Version | ||
|
||
% Jonathan Currie (C) | ||
% Control Engineering 2023 | ||
|
||
%% Example 1: Oscillatory SISO Example | ||
% A simple SISO model to get started with for embedded MPC | ||
clc | ||
%Model | ||
Gs = tf(2,[0.7 0.2 1]); | ||
Ts = 0.1; | ||
%Convert to Discrete | ||
Gd = c2d(Gs,Ts); | ||
%Create a jSS Object | ||
Plant = jSS(Gd); | ||
%MPC Model | ||
Model = Plant; | ||
|
||
%Horizons & Time | ||
Np = 8; %Prediction Horizon | ||
Nc = [4 2 2]; %Control Horizon OR Blocking Moves | ||
T = 200; %Length of Simulation | ||
%Setpoint | ||
setp = ones(T,1); %Setpoint | ||
setp(75:150) = 0.5; | ||
%Constraints | ||
con = []; | ||
con.u = [-inf inf 0.2]; %in1 [umin umax delumax] | ||
con.y = [-inf 2]; %out1 [ymin ymax] | ||
%Weights | ||
uwt = 0.5; %U Weights (Larger = Penalize Delta U More) | ||
ywt = 0.5; %Y Weights (Larger = Penalize Setpoint-Y More) | ||
%Estimator Gain | ||
Kest = dlqe(Model); %Discrete Observer with W,V = eye() | ||
|
||
%-- Build MPC & Simulation --% | ||
MPC1 = jMPC(Model,Np,Nc,uwt,ywt,con,Kest) | ||
simopts = jSIM(MPC1,Plant,T,setp); | ||
%-- Simulate & Plot Result --% | ||
simresult = sim(MPC1,simopts) | ||
plot(MPC1,simresult); | ||
|
||
%% Embed the controller | ||
embed(MPC1,simopts) | ||
|
||
|
||
%% Embed controller as single precision | ||
clc | ||
embeddedopts = jMPCeset('precision','single'); | ||
embed(MPC1,simopts,embeddedopts) | ||
|
||
%% Generate MPC Testbench too (double vs double comparison) | ||
clc | ||
embeddedopts = jMPCeset('verifympc',1); | ||
embed(MPC1,simopts,embeddedopts) | ||
|
||
%% Embed controller as single precision (single vs single comparison) | ||
clc | ||
embeddedopts = jMPCeset('verifympc',1,'precision','single'); | ||
embed(MPC1,simopts,embeddedopts) | ||
|
||
%% Embedded Single Precision Verified against Reference Double Precision | ||
clc | ||
embeddedopts = jMPCeset('tbprecision','double','precision','single','verifympc',1); | ||
embed(MPC1,simopts,embeddedopts) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
%% jMPC Toolbox Model Predictive Control Examples | ||
% PIL Version | ||
|
||
% Jonathan Currie (C) | ||
% Control Engineering 2023 | ||
|
||
%% Dynamic System to Control via PIL | ||
clc | ||
clear | ||
Gs = tf(2,[0.7 0.2 1]); | ||
Gd = c2d(Gs,0.1); | ||
% Create jSS Objects (Model Format) | ||
Plant = jSS(Gd); | ||
Model = Plant; %No Model/Plant Mismatch | ||
|
||
%Tuning | ||
Np = 8; %Prediction Horizon | ||
Nc = [4 2 2]; %Blocking Moves | ||
uwt = 0.5; %DeltaU Weights | ||
ywt = 0.5; %Y Weights | ||
%Constraints | ||
con = []; | ||
con.u = [-inf inf 0.2]; %In1 [umin umax delumax] | ||
con.y = [-inf 2]; %Out1 [ymin ymax] | ||
%Estimator Gain | ||
Kest = dlqe(Model); %Discrete Observer with W,V = eye() | ||
|
||
%Simulation Setup | ||
T = 200; %Length of Simulation | ||
setp = ones(T,1); %Setpoint | ||
setp(75:150) = 0.5; | ||
|
||
%Single Precision | ||
opts = jMPCset('Single',1); | ||
|
||
%Build MPC & Simulation | ||
MPC1 = jMPC(Model,Np,Nc,uwt,ywt,con,Kest,opts); | ||
simopts = jSIM(MPC1,Plant,T,setp); | ||
|
||
%Assign Serial Device for PIL comms | ||
simopts.opts.serialdevice = serial('COM6','BaudRate',1250000); | ||
|
||
%% Generated Embedded MPC Controller | ||
eopts = jMPCeset('arch','c2000','verifympc',1); | ||
embed(MPC1,simopts,eopts); | ||
|
||
%% PIL Verification Run | ||
simpil = sim(MPC1,simopts,'pil') | ||
plot(MPC1,simpil,'timing'); | ||
|
||
%% Compare against MATLAB | ||
simMX = sim(MPC1,simopts,'mex'); | ||
compare(MPC1,simMX, simpil); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters