Skip to content

Commit

Permalink
Final updates from Wiki!
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathancurrie committed Mar 15, 2023
1 parent 8d6c855 commit 3a3019e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 7 deletions.
67 changes: 67 additions & 0 deletions Examples/Embedded_Examples.m
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)

5 changes: 5 additions & 0 deletions Examples/Linear_Examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
simresult = sim(MPC1,simopts)
plot(MPC1,simresult);

%% Compare Simulation Methods
simresultML = sim(MPC1,simopts,'matlab');
simresultMX = sim(MPC1,simopts,'mex');
compare(MPC1,simresultML,simresultMX)

%% Rossiter MIMO Model
% Ref: J Rossiter (2004), Model-Based Predictive Control, A Practical
% Approach, CRC Press (Accompanying Software Example).
Expand Down
53 changes: 53 additions & 0 deletions Examples/PIL_Examples.m
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);
22 changes: 15 additions & 7 deletions Examples/Quanser 3DOF Helicopter/main_Q3DOF.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
Q = 2*eye(6); R = 0.1*eye(3); Q(4,4) = 1; Q(5,5) = 1; Q(6,6) = 0.1;
Kest = dlqe(Model,Q,R);

%Single precision, lower QP tol for speed and set initial control at
%linearization point
opts = jMPCset('Single',1,'QPTol',1e-4,'InitialU',Vop);

%-- Build MPC & Simulation --%
Expand All @@ -53,13 +55,14 @@
%Add serial port connected to 3-DOF acquisition system
sd = serial('COM6','BaudRate',1250000);
simopts.opts.serialdevice = sd;

%-- Simulate & Plot Result --%
simresult = sim(MPC1,simopts)
plot(MPC1,simresult,'timing');


%% Embed The Controller (assumes we are in Matlab\jMPC\Examples\Quanser 3DOF Helicopter\ folder)
eopts = jMPCeset('arch','c2000','accelqp',1,'dir','..\..\..\Testing\EmbedMPC\');
eopts = jMPCeset('arch','c2000');
embed(MPC1,simopts,eopts);


Expand All @@ -69,14 +72,16 @@
simhelipil = sim(MPC1,simopts,'PIL')
plot(MPC1,simhelipil,'timing');

%% Compare to MATLAB Sim
compare(MPC1,simresult,simhelipil);

%% Run Embedded Controller and Record [USE TI Project HeliMPC]
% IMPORTANT: Start the controller FIRST before running this code.
clc
fclose(sd);

%Number of samples to collect (time = n*Ts)
n = 5000;
n = 2000;

%Preallocate
u = zeros(2,n);
Expand Down Expand Up @@ -112,6 +117,9 @@
%% Optionally Save Data
% save helimpc u xm ym yp setp time iter status MPC1 simopts

%% Optionally Load Data
load helimpc;
n = 2000;

%% Plot Results
Ts = MPC1.Model.Ts;
Expand All @@ -122,7 +130,7 @@
clf;
subplot(321);
plot(k,yp(1,idx)*180/pi,'r'); hold on; plot(k,setp(1,idx)*180/pi,'--','color',jcolour('spgrey')); hold off;
set(gca,'XTickLabel',[],'ylim',[0 18],'xlim',[0 (T-1)*Ts]);
set(gca,'XTickLabel',[],'ylim',[0 20],'xlim',[0 (T-1)*Ts]);
ylabel('Elevation [\circ]');

subplot(322);
Expand All @@ -134,7 +142,7 @@
subplot(323);
plot(k,yp(3,idx)*180/pi,'r'); hold on; plot(k,setp(2,idx)*180/pi,'--','color',jcolour('spgrey')); hold off;
% h = hline(-1.1*180/pi,'r:'); set(h,'color',jcolour('softgrey'));
set(gca,'XTickLabel',[],'ylim',[-50 230],'xlim',[0 (T-1)*Ts]);
set(gca,'XTickLabel',[],'ylim',[-150 150],'xlim',[0 (T-1)*Ts]);
ylabel('Rotation [\circ]');

subplot(324);
Expand All @@ -158,7 +166,7 @@
axes('Position',[0 0 1 1],'Visible','off','Tag','CommonTitle');
text(0.3,0.98,'Embedded MPC Control of the 3DOF Helicopter')

squeezefigs(3,3,-0.1,[],false)
boldfigs(2,13)
set(gcf,'position',jsize('32'));
% squeezefigs(3,3,-0.1,[],false)
% boldfigs(2,13)
% set(gcf,'position',jsize('32'));
% printplot('../figs/embedmpc/helimpcsp');

0 comments on commit 3a3019e

Please sign in to comment.