Clear Filters
Clear Filters

Multiple PID tuning in order to control all four states in the inverted pendulum model

16 views (last 30 days)
Hi!
I have to control the four states of the classical nonlinear inverted pendulum on a cart model (position and velocity of the cart, angle and angular velocity of the pendulum) in Simulink through PID control. Being on a cart it doesn't have to go through the swing up, the initial condition for the angle is π-0.5 (desired angle π with a small perturbation), so it's just the balancing problem.
This is my Simulink control scheme:
My implementation works as it should, but I had to manually tune the four PIDs because I wasn't able to obtain the same system with just one controller (if I give a vector of the four errors as input to a single PID block designed with a vector of four gains it messes up and creates a 4-dimensional output instead of executing the row-column product) and I tried everything but couldn't tune the multiple PIDs at the same time. Of course, the tuner app embedded in every PID block is useless in my case since the output of the system isn't just depending on a single PID's control input. I wonder if I can automatically find the optimal choices for the PID controllers, can someone help me?
  4 Comments
Claudio
Claudio on 18 Jun 2024
Sorry for the misunderstanding, here's the code of the model:
function eq_dot = InvPend(x,v,theta,omega,u)
% Parameters
m=1; % mass of the pendulum
M=5; % mass of the cart
L=2; % length of the pendulum
g=-9.80665; % gravitational acceleration
d=1; % friction coefficient
% Equations
D=m*L^2*(M+m*(1-cos(theta)^2));
x_dot=v;
v_dot=(1/D)*(-m^2*L^2*g*cos(theta)*sin(theta)+m*L^2*(m*L*omega^2*sin(theta)-d*v))+m*L^2*u/D;
theta_dot=omega;
omega_dot=(1/D)*((m+M)*m*g*L*sin(theta)-m*L*cos(theta)*(m*L*omega^2*sin(theta)-d*v))-m*L*cos(theta)*u/D;
eq_dot=[x_dot; v_dot; theta_dot; omega_dot];
end

Sign in to comment.

Answers (2)

Sam Chak
Sam Chak on 18 Jun 2024
Given that the initial angle is less than from the equilibrium inverted position, the sine of the angle () can be considered a relatively weak nonlinearity within that range. As a result, the inverted pendulum can be treated as a linear system and a full-state feedback control scheme can be confidently applied in this case:
.
The control gains can be tuned through a trial-and-error approach. For this simple demonstration, integer values were used.
x = pi-0.5:0.0001:pi+0.5;
y = sin(x);
plot(x, [pi - x; y]), grid on
legend('Linear', 'Nonlinear')
title('Nonlinearity of sin(\theta)')
xlabel('Angle / radian'), ylabel('sin(\theta)')
Simulation in MATLAB
%% Inverted Pendulum on a Cart
function EoM = InvPend(t, x)
% parameters
m = 1; % mass of the pendulum
M = 5; % mass of the cart
L = 2; % length of the pendulum
g = -9.80665; % gravitational acceleration
d = 1; % friction coefficient
D = m*L^2*(M + m*(1 - cos(x(3))^2));
% full-state feedback controller
K = [-1 -6 152 64]; % control gains
u = - K*[x(1);
x(2);
x(3) - pi;
x(4)];
% Equations of Motion
Dx = x(2);
Dv = (1/D)*(- m^2*L^2*g*cos(x(3))*sin(x(3)) + m*L^2*(m*L*x(4)^2*sin(x(3)) - d*x(2))) + m*L^2*u/D;
Dtheta = x(4);
Domega = (1/D)*((m + M)*m*g*L*sin(x(3)) - m*L*cos(x(3))*(m*L*x(4)^2*sin(x(3)) - d*x(2))) - m*L*cos(x(3))*u/D;
EoM = [Dx;
Dv;
Dtheta;
Domega];
end
tspan = [0 30];
x0 = [0; 0; pi-0.5; 0];
[t, x] = ode45(@InvPend, tspan, x0);
plot(t, x), grid on, xlabel('t'), title('Inverted Pendulum on a Cart')
legend({'$x$', '$v$', '$\theta$', '$\omega$'}, 'Interpreter', 'LaTeX', 'fontsize', 14)
%% Steady-state value of theta
ss_theta = x(end,3)
ss_theta = 3.1416
The pendulum's angular position (orange curve) is maintained at the desired equilibrium at steady-state.
  7 Comments
Sam Chak
Sam Chak on 18 Jun 2024
I seldom use the Control System Tuner. That's why I posted the tutorial. As mentioned above, the gains are tuned through a trial-and-error, which I prefer to Search & Sort the gains (within a certain range) until the desired response is found. I believe Computer Scientists call this "brute-force search".
Claudio
Claudio on 19 Jun 2024
The trial-and-error works fine, but it's not the optimal solution nor necessarily something pretty close to it. My question is: is it possible to determine the best (or a very close one) gains combination in terms of overshoot, settling time and control input cost? I thought the control system tuner could do something like that

Sign in to comment.


Sam Chak
Sam Chak on 19 Jun 2024
Your new question in this comment is related to optimal control. I'm not sure if this topic is covered in your class (it wasn't in mine). However, it is possible to determine the optimal gains subject to a custom design cost function that you wish to optimize. One approach is to mentally linearize the nonlinear system (which I did below - quite straightforward with explanations), and then find the gains using the Linear Quadratic Regulator (LQR) algorithm.
Why LQR? Because it has a pre-designed, fixed cost function that is convex (quadratic) in nature. This means the control designers don't need to expend significant effort applying complicated mathematics to design a convex cost function.
Code to apply lqr() command to find the optimal gains (took me nearly two hours to search LQR info, links and type this out)
%% Parameters
m = 1; % mass of the pendulum
M = 5; % mass of the cart
L = 2; % length of the pendulum
g = -9.80665; % gravitational acceleration
d = 1; % friction coefficient
%% Linear State-Space
Dpi = m*L^2*(M + m*(1 - cos(pi)^2)); % Comes from "D" and theta is set to pi
% These 4 elements are determined from x_dot (1st state equation)
a11 = 0; % zero because no x
a12 = 1; % coefficient of v
a13 = 0; % zero because no theta
a14 = 0; % zero because no omega
% These 4 elements are determined from v_dot (2nd state equation)
a21 = 0; % zero because no x
a22 = - (m*L^2*d)/Dpi; % coefficient of v
a23 = - (m^2*L^2*g*cos(pi)^2)/Dpi; % coefficient of sin(theta), d/dθ sin(θ) = cos(θ)
a24 = 0; % zero because the term is multiplied by omega = 0
% These 4 elements are determined from theta_dot (3rd state equation)
a31 = 0; % zero because no x
a32 = 0; % zero because no v
a33 = 0; % zero because no theta
a34 = 1; % coefficient of omega
% These 4 elements are determined from omega_dot (4th state equation)
a41 = 0; % zero because no x
a42 = (m*L*cos(pi)*d)/Dpi; % coefficient of v
a43 = (m + M)*m*g*L*cos(pi)/Dpi; % coefficient of sin(theta), d/dθ sin(θ) = cos(θ)
a44 = 0; % zero because the term is multiplied by omega = 0
% State matrix
A = [a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44]
A = 4x4
0 1.0000 0 0 0 -0.2000 1.9613 0 0 0 0 1.0000 0 -0.1000 5.8840 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
b1 = 0; % zero because no u in x_dot
b2 = (m*L^2)/Dpi; % coefficient of u in v_dot
b3 = 0; % zero because no u in theta_dot
b4 = - m*L*cos(pi)/Dpi; % coefficient of u in omega_dot
% Input matrix
B = [b1
b2
b3
b4]
B = 4x1
0 0.2000 0 0.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% Linear Quadratic Regulator
Q = eye(size(A));
R = 1;
K = lqr(A, B, Q, R)
K = 1x4
-1.0000 -5.5777 151.7848 63.9705
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The optimal gains determined by LQR are nearly the same as the ones I brute-force searched by trial-and-error using integers. However, I'm uncertain how to obtain these LQR values using the Simulink-based Control System Tuner app.
If the MATLAB-based solution I've provided is still unacceptable, I would suggest consulting experts on the Control System Tuner tool.
  7 Comments
Sam Chak
Sam Chak on 21 Jun 2024
Since the 4-PID control architecture is desirable, your next step would to simplifying the 12-parameter control equation such that , , , and so on. If similar terms are grouped, you can cut the number of parameters by half, and it will generally improves the tuning efficiency of your chosen optimization strategy.

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!