In: Electrical Engineering
Develop a detailed model the inverted pendulum system in MatLab/Simulink (a feedback compensator to stabilise and control the dynamics of a rotating inverted pendulum, as described in Microchip Application Note AN964, “Software PID Control of an Inverted Pendulum Using the PIC16F684” by Charais and Lourens. T)
%--------------------------------------------------------------------------------------------
% trans_func_ip_comp.m
% Design and Development of Closed Loop Control for INVERTED
PENDULUM
% Closed Loop Compensated Transfer Function of the Inverted
Pendulum System
%--------------------------------------------------------------------------------------------
%
% E(s) U(s)
% Vpot --->O---[ C(s) ]--->[ G2(s) ]--->[ G1(s)
]---+---> Theta (s)
% - ^ |
% | | Theta = SYS * E
% +----------------[ H(s) ]<---------------+
% V viper
% func_ip_uc is a MAT File (MATLAB specific binary file),
% with variables G, GH, Gc, H, Th_U, U_E, num_G, den_G
load func_ip_uc
% C(s) = ( Kd * s^2 + Kp * s + Ki ) / s
% PID Controller to reshape the root locus.
Kp = 20;
Ki = 100;
Kd = 1;
Kc = 30;
num_PID = Kc * [Kd Kp Ki];
den_PID = [1 0];
disp ('The transfer function of the PID Controller is:')
PID = tf (num_PID, den_PID)
% G_comp(s) = PID(s) * G(s)
% Overall Forward Transfer Function.
num_Gcomp = conv (num_PID, num_G);
den_Gcomp = conv (den_PID, den_G);
G_comp = series (PID, G);
% Open Loop Transfer Function of the Compensated System
% G_comp_H(s) = G_comp(s) * H(s)
G_comp_H = series (G_comp, H);
% Closed-Loop Transfer Function of the Compensated System
% Gc_comp
disp 'Closed Loop Transfer Function of the Compensated Inverted
Pendulum System is:'
Gc_comp = feedback (G_comp, H)
% DC Gain
disp ('The DC Gain of the Closed Loop Compensated Inverted Pendulum
System is:')
disp (dcgain (Gc_comp))
%
% func_ip is a MAT File (MATLAB specific binary file),
% with variables G, GH, H, Th_U, U_E
load func_ip_uc
% Locations of Poles and Zeroes of Open-Loop Transfer Function
in Complex Plane
figure
pzmap (G)
title ('Pole-Zero Map of Open-Loop Uncompensated Inverted Pendulum
System')
% Impulse Response
figure
impulse (G)
title ('Impulse Response of Open Loop Uncompensated Inverted
Pendulum System')
% Step Response
figure
step (G)
title ('Step Response of Open Loop Uncompensated Inverted Pendulum
System')
% Locations of Poles and Zeroes of Closed-Loop Transfer Function
in Complex Plane
figure
pzmap (feedback (G, H))
title ('Unity Feedback Closed-Loop Uncompensated Inverted Pendulum
System')
% Root Locus Plot of Uncompensated System
figure
rlocus (GH)
title ('Root Locus of Uncompensated Inverted Pendulum
System')
% Experimental Data of the Inverted Pendulum System
%------------------------------------------------------------------------
% expdata is a MAT File (MATLAB specific binary file),
% with variables exp_data & tout
load expdata
plot (tout,exp_data);
xLabel ('Time (Seconds)');
yLabel('Pendulum Position From Vertical (Degrees)');
Title('Experimental Data');
axis ([0 0.15 -40 40]);
Grid;
% Closed Loop Compensated Transfer Function of the Inverted
Pendulum System
%--------------------------------------------------------------------------------------------
%
% E(s) U(s)
% Vpot --->O---[ C(s) ]--->[ G2(s) ]--->[ G1(s)
]---+---> Theta (s)
% - ^ |
% | | Theta = SYS * E
% +----------------[ H(s) ]<---------------+
% V viper
% func_ip_uc is a MAT File (MATLAB specific binary file),
% with variables G, GH, Gc, H, Th_U, U_E, num_G, den_G
load func_ip_uc
% C(s) = ( Kd * s^2 + Kp * s + Ki ) / s
% PID Controller to reshape the root locus.
Kp = 20;
Ki = 100;
Kd = 1;
Kc = 30;
num_PID = Kc * [Kd Kp Ki];
den_PID = [1 0];
disp ('The transfer function of the PID Controller is:')
PID = tf (num_PID, den_PID)
% G_comp(s) = PID(s) * G(s)
% Overall Forward Transfer Function.
num_Gcomp = conv (num_PID, num_G);
den_Gcomp = conv (den_PID, den_G);
G_comp = series (PID, G);
% Open Loop Transfer Function of the Compensated System
% G_comp_H(s) = G_comp(s) * H(s)
G_comp_H = series (G_comp, H);
% Closed-Loop Transfer Function of the Compensated System
% Gc_comp
disp 'Closed Loop Transfer Function of the Compensated Inverted
Pendulum System is:'
Gc_comp = feedback (G_comp, H)
% DC Gain
disp ('The DC Gain of the Closed Loop Compensated Inverted Pendulum
System is:')
disp (dcgain (Gc_comp))
% Open Loop & Closed Loop (Uncompensated) Transfer Function of
the Inverted Pendulum System
%--------------------------------------------------------------------------------------------
%
% E(s) U(s)
% Vpot --->O--->[ G2(s) ]--->[ G1(s) ]---+---> Theta
(s)
% - ^ |
% | | Theta = SYS * E
% +----------[ H(s) ]<----------+
% V viper
% ip_data is a MAT File (MATLAB specific binary file),
% with variables I, Kf, Km, Lp, M, b, g, l, m, r,
tau
load ip_data
Kp = 1 / ((M + m) * g);
K = Kf * Kp * Km * r * (M + m);
Ap = sqrt ((M + m) * m * g * l / ((M + m)*(I + (m * (l ^ 2)))- ((m
* l)^2)));
% G1(s) = Theta(s) / U(s)
% represents a small angle from the vertical upward
direction,
% u represents the input (impulse) force on the cart by pulley
chain mechanism.
num_Th_U = [0 0 Kp];
den_Th_U = [Ap^(-2) 0 -1];
Th_U = tf (num_Th_U, den_Th_U);
% G2(s) = U(s) / E(s)
% u represents the input force on the cart by the pulley chain
mechanism,
% e represents the input to the motor driving pulley-chain
mechanism.
num_U_E = [((Km * (M + m))*r) 0];
den_U_E = [tau 1];
U_E = tf (num_U_E, den_U_E);
disp ' '
% G(s) = Theta(s) / E(s)
% Forward Transfer Function (Open Loop Without Feedback)
num_G = conv (num_U_E, num_Th_U);
den_G = conv (den_U_E, den_Th_U);
disp 'Forward Path Transfer Function of Inverted Pendulum System
is:'
G = series (U_E, Th_U)
% H(s) (Feedback)
num_H = Kf;
den_H = 1;
H = tf (num_H, den_H);
% Closed Loop Transfer Function of Uncompensated System
% Gc(s) = G(s) / (1 + G(s) * H(s))
disp 'Closed Loop Transfer Function of Inverted Pendulum System
is:'
Gc = feedback (G, H)
% GH(s)
% Open Loop Transfer Function
GH = series (G, H);