plotresults_Modelling_Lin_a_studentnumber.m
%% Clearing the workspace
clear;
close;
clc;
%% Setting up the Model parameters
setupsim_Modelling_studentnumber;
%% Running the model
sim('CP_Modelling_NLin_studentnumber');
%% Retrieving results
velocity = get(yout, 'velocity').Values.Data;
angular_velocity = get(yout, 'angular_velocity').Values.Data;
inclination = get(yout, 'inclination').Values.Data;
position = get(yout, 'position').Values.Data;
%% Plotting results
t = tiledlayout(5,1);
title(t, 'Time histories of the states using the nonlinear model');
nexttile
plot(tout, position)
xlabel('Time (s)');
ylabel('Cart Position [m]');
nexttile
plot(tout, inclination)
xlabel('Time (s)');
ylabel('Pendulum angle [deg]');
nexttile
plot(tout, velocity)
xlabel('Time (s)');
ylabel('Cart velocity [m/s]');
nexttile
plot(tout, angular_velocity)
xlabel('Time (s)');
ylabel('Pendulum rate [deg/s]');
nexttile
plot(tout, zeros(size(tout, 1),1))
xlabel('Time (s)');
ylabel('Input Force [N]');
setupsim_Modelling_Lin_b_studentnumber.m
function setupsim_Modelling_studentnumber
assignin('base', 'M', 0.4);
assignin('base', 'm', 0.15);
assignin('base', 'l', 0.2);
assignin('base', 'g', 9.81);
assignin('base', 'I', 0.006);
assignin('base', 'b', 0);
end
setupsim_Modelling_Lin_a_studentnumber.m
function setupsim_Modelling_studentnumber
assignin('base', 'M', 0.4);
assignin('base', 'm', 0.15);
assignin('base', 'l', 0.2);
assignin('base', 'g', 9.81);
assignin('base', 'I', 0.006);
assignin('base', 'b', 0);
end
plotresults_Modelling_Lin_b_studentnumber.m
%% Clearing the workspace
clear;
close;
clc;
%% Setting up the Model parameters
setupsim_Modelling_studentnumber;
%% Running the model
sim('CP_Modelling_NLin_studentnumber');
%% Retrieving results
velocity = get(yout, 'velocity').Values.Data;
angular_velocity = get(yout, 'angular_velocity').Values.Data;
inclination = get(yout, 'inclination').Values.Data;
position = get(yout, 'position').Values.Data;
%% Plotting results
t = tiledlayout(5,1);
title(t, 'Time histories of the states using the nonlinear model');
nexttile
plot(tout, position)
xlabel('Time (s)');
ylabel('Cart Position [m]');
nexttile
plot(tout, inclination)
xlabel('Time (s)');
ylabel('Pendulum angle [deg]');
nexttile
plot(tout, velocity)
xlabel('Time (s)');
ylabel('Cart velocity [m/s]');
nexttile
plot(tout, angular_velocity)
xlabel('Time (s)');
ylabel('Pendulum rate [deg/s]');
nexttile
plot(tout, zeros(size(tout, 1),1))
xlabel('Time (s)');
ylabel('Input Force [N]');
plotresults_Modelling_studentnumber.m
%% Clearing the workspace
clear;
close;
clc;
%% Setting up the Model parameters
setupsim_Modelling_studentnumber;
%% Running the model
sim('CP_Modelling_NLin_studentnumber');
%% Retrieving results
velocity = get(yout, 'velocity').Values.Data;
angular_velocity = get(yout, 'angular_velocity').Values.Data;
inclination = get(yout, 'inclination').Values.Data;
position = get(yout, 'position').Values.Data;
%% Plotting results
t = tiledlayout(5,1);
title(t, 'Time histories of the states using the nonlinear model');
nexttile
plot(tout, position)
xlabel('Time (s)');
ylabel('Cart Position [m]');
nexttile
plot(tout, inclination)
xlabel('Time (s)');
ylabel('Pendulum angle [deg]');
nexttile
plot(tout, velocity)
xlabel('Time (s)');
ylabel('Cart velocity [m/s]');
nexttile
plot(tout, angular_velocity)
xlabel('Time (s)');
ylabel('Pendulum rate [deg/s]');
nexttile
plot(tout, zeros(size(tout, 1),1))
xlabel('Time (s)');
ylabel('Input Force [N]');
cartpendulumanimation.m
%% This function makes an animation of the pendulum on the cart.
%{
This function was created by Dr Alejandro Donaire at UON
as part of ENGGH2440 - Modelling and Control S2 2019
To produce the anumation, the function requires the simulation variables:
'time' is the simulation time
'qa' is the position of the cart
'qu' is the angle of the pendulum
'qar' is the desired equilibrium position of the cart
'qur' is the desired equilibrium angle of the pendulum
%}
function output=Cart_Pendulum_Animation(time,qa,qu,qar,qur)
%%
clc;
l=0.2;
disp('Animation started');
%% Auxiliar variables
lc=0.06; % Cart length
hc=0.04; % Cart height
xp1plane=0.5; %
xp2plane=-0.5;
yp1plane=-1.5*hc;
%% Compute cartesian coordinates of the cart and pendulum
xm = qa + l*sin(qu); % x-coordinate of the pendulum
ym = l*cos(qu); % y-coordinate of the pendulum
xmr = qar; % x-coordinate reference position of the pendulum
ymr = l*cos(qur); % y-coordinate reference position of the pendulum
%% Allocation space movie
mov(1:length(time)) = struct('cdata',[],'colormap',[]);
%% Dimmension of the figure window for the movie
scrsz = get(0,'ScreenSize');
figmovie=figure('Name','Movie: Pendulum on the cart','Position',[0 0 scrsz(3)*2.5/3 scrsz(3)*1.5/2.9]);
%% Plot the current position of the cart-pendulum and the static ones from the previous declaration to store each frame in variable mov
for k=1:length(time)
%% Set the labels for each frame of the animation
figmovie;clf
axes('NextPlot','replacechildren','tag','plot_axes')
title('Pendulum on the cart','FontSize',18)
xlabel('x [m]','FontSize',18)
ylabel('y [m]','FontSize',18)
text(-0.05,-0.27,sprintf('Time %0.1f sec',time(k)),'BackgroundColor',[1 1 1],'EdgeColor','k','FontSize',18)
hold on;
%% Draw the suporting plane base for the cart
xplane = [xp2plane xp1plane];
yplane = [yp1plane yp1plane];
area(xplane,yplane,'basevalue',-hc,'facecolor',[0.5 .5 0.5]);
%% Initial position of the cart and pendulum
% Pendulum
line([qa(1),xm(1)],[0,ym(1)],'Color','k','LineStyle','--','LineWidth',1); % Pendulum link
plot(qa(1),0,'Marker','o','MarkerEdgeColor','k','LineStyle','--','MarkerFaceColor','w','MarkerSize',20); % Cart joint
plot(xm(1),ym(1),'Marker','o','MarkerEdgeColor','k','LineStyle','--','MarkerFaceColor','w','MarkerSize',30); % Pendulum mass
% Cart body
xp1i = qa(1)+lc;
xp2i = qa(1)-lc;
yp1i = 0;
yp2i = -hc;
area([xp2i;xp1i],[yp1i;yp1i],'basevalue',-hc,'facecolor','w','LineStyle','--');
% Cart wheels
plot(qa(1)+lc*2/3,-hc,'Marker','o','MarkerEdgeColor','k','MarkerFaceColor','w','MarkerSize',30);
plot(qa(1)-lc*2/3,-hc,'Marker','o','MarkerEdgeColor','k','MarkerFaceColor','w','MarkerSize',30);
%% Reference position of the cart and pendulum
% Pendulum
line([qar,xmr],[0,ymr],'Color',[0 .7 0],'LineStyle','--','LineWidth',1); % Pendulum link
plot(qar,0,'Marker','o','MarkerEdgeColor',[0 .7 0],'LineStyle','--','MarkerFaceColor','w','MarkerSize',20); % Cart joint
plot(xmr,ymr,'Marker','o','MarkerEdgeColor',[0 .7 0],'LineStyle','--','MarkerFaceColor','w','MarkerSize',30); % Pendulum mass
% Cart body
xp1r = lc+qar;
xp2r = -lc+qar;
yp1r = 0;
yp2r = -hc;
area([xp2r;xp1r],[yp1r;yp1r],'basevalue',-hc,'EdgeColor',[0 .7 0],'facecolor','w','LineStyle','--');
% Cart wheels
plot(lc*2/3+qar,-hc,'Marker','o','MarkerEdgeColor',[0 .7 0],'MarkerFaceColor','w','MarkerSize',30);
plot(-lc*2/3+qar,-hc,'Marker','o','MarkerEdgeColor',[0 .7 0],'MarkerFaceColor','w','MarkerSize',30);
%% Current position of the cart and pendulum
% Cart body
xp1 = qa(k)+lc;
xp2 = qa(k)-lc;
yp1 = 0;
yp2 = -hc;
area([xp2;xp1],[yp1;yp1],'basevalue',yp2,'facecolor',[0 0 .7]);
% cart wheels
plot(qa(k)+lc*2/3,-hc,'Marker','o','MarkerEdgeColor','k','MarkerFaceColor',[0 0 0],'MarkerSize',30);
plot(qa(k)-lc*2/3,-hc,'Marker','o','MarkerEdgeColor','k','MarkerFaceColor',[0 0 0],'MarkerSize',30);
% Pendulum
line([qa(k),xm(k)],[0,ym(k)],'Color','k','LineWidth',1); % Pendulum link
plot(qa(k),0,'Marker','o','MarkerEdgeColor','k','MarkerFaceColor',[0 0 .7],'MarkerSize',20); % Cart joint
plot(xm(k),ym(k),'Marker','o','MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',30); % Pendulum mass
%% legend
text(-0.32,0.25,{'{\color[rgb]{0,0,0} --- } Initial position','{\color[rgb]{0,0.7,0} --- } Target postion'},'BackgroundColor',[1 1 1],'EdgeColor','k','FontSize',18) ;
%% x-axis and y-axis limits
xlim([-2.5*l 2.5*l])
ylim([-1.5*l 1.5*l])
grid on
hold off
%% Record frame data
mov(k) = getframe(gcf);
end
%% Create AVI file (uncomment this section to create the video file)
vidObj = VideoWriter('Cart_Pendulum_Animation.avi'); % Create a video object
vidObj.FrameRate = 10; % Set frames per second in video object
open(vidObj); % Open video object
writeVideo(vidObj,mov); % Write the frames mov in video object
close(vidObj) % Close video object
disp('Animation finished - Saved in Cart_Pendulum_Animation.avi')
setupsim_Modelling_studentnumber.m
function setupsim_Modelling_studentnumber
assignin('base', 'M', 0.4);
assignin('base', 'm', 0.15);
assignin('base', 'l', 0.2);
assignin('base', 'g', 9.81);
assignin('base', 'I', 0.006);
assignin('base', 'b', 0);
end
MainFile_Modelling_Comparison_studentnumber.m
%% Clearing the workspace
clear;
close;
clc;
%% Setting up the Model parameters
setupsim_Modelling_studentnumber;
%% Running the model
sim('CP_Modelling_NLin_studentnumber');
CP_Modelling_Lin_a_studentnumber.slx
[Content_Types].xml
_rels/.rels
metadata/coreProperties.xml
model 2019-12-08T05:43:32Z Kanish Kanish 2019-12-08T05:43:48Z 1.0 R2019b
metadata/mwcoreProperties.xml
application/vnd.mathworks.simulink.model Simulink Model R2019b
metadata/mwcorePropertiesExtension.xml
9.7.0.1243256 7a3a96c2-6da9-459f-9358-afd0c970636a
metadata/mwcorePropertiesReleaseInfo.xml
9.7.0.1247435
R2019b
Update 2
Nov 07 2019
36752679
metadata/thumbnail.png
simulink/ScheduleCore.xml
false
Default
-2147483648
simulink/ScheduleEditor.xml
Default
graph.Graph
HighNumberFirst
Default
simulink/_rels/blockdiagram.xml.rels
simulink/_rels/configSetInfo.xml.rels
simulink/bddefaults.xml
landscape
auto
usletter
inches
[0.500000, 0.500000, 0.500000, 0.500000]
1
off
on
200
white
100
off
off
black
white
off
normal
Helvetica
10
normal
normal
on
on
0
off
left
top
black
white
off
Helvetica
10
normal
normal
model
off
note_annotation
off
off
off
Helvetica
9
normal
normal
simulink/blockdiagram.xml
windows-1252
on
Normal
0.035000
on
off
UseLocalSettings
AllNumericTypes
UseLocalSettings
Overwrite
Run 1
120
win64
on
UpdateHistoryNever
%
%
0
1.%
off
off
disabled
off
off
off
off
AliasTypeOnly
on
on
off
off
off
on
off
off
on
on
on
off
off
off
off
on
on
on
off
off
off
off
on
on
off
off
off
on
normal
automated
topmodel
normal
normal
normal
software-in-the-loop (sil)
5
1
10
10
0
off
1
none
off
MATLABWorkspace
accel.tlc
accel_default_tmf
make_rtw
off
$bdroot
0U
$bdroot
[]
off
on
manual
normal
1
any
1000
auto
0
0
rising
0
off
off
off
off
off
on
off
on
on
off
off
on
Ensure deterministic transfer (maximum delay)
Ensure data integrity only
Ensure deterministic transfer (minimum delay)
None
off
[23, 14, 1017, 767]
on
off
Deduce
simulink-default.rpt
Simulink
simulink/configSet0.xml
[]
[]
[]
0.0
10.0
auto
on
auto
auto
5
auto
10*128*eps
1000
4
1
auto
auto
1
1e-3
off
off
VariableStepAuto
VariableStepAuto
auto
DisableAll
UseLocalSettings
Nonadaptive
TrustRegion
off
off
Fast
off
off
Unconstrained
Whenever possible
[]
off
off
3
[]
[]
1
[t, u]
xFinal
xInitial
off
1000
off
off
off
off
Dataset
Dataset
on
off
on
on
off
on
off
streamout
on
on
xout
tout
yout
logsout
dsmout
RefineOutputTimes
[]
out
1
off
timeseries
out.mat
[-inf, inf]
BooleansAsBitfields
PassReuseOutputArgsAs
PassReuseOutputArgsThreshold
ZeroExternalMemoryAtStartup
ZeroInternalMemoryAtStartup
OptimizeModelRefInitCode
NoFixptDivByZeroProtection
UseSpecifiedMinMax
EfficientTunableParamExpr
[]
on
on
on
Tunable
off
off
off
double
off
off
on
on
off
off
on
off
on
off
uint_T
on
64
Structure reference
12
128
on
5
off
off
Native Integer
on
on
off
off
off
on
auto
Inherit from target
on
off
off
off
on
on
off
off
level2
Balanced
on
off
off
GradualUnderflow
off
UseOnlyExistingSharedCode
[]
error
none
none
none
error
none
UseLocalSettings
UseLocalSettings
UseLocalSettings
warning
warning
warning
warning
on
Simplified
error
off
off
UseLocalSettings
warning
warning
none
error
warning
warning
none
warning
error
error
error
none
warning
none
warning
none
warning
warning
error
error
none
warning
warning
none
none
none
none
none
none
error
error
...