Multiple PID tuning in order to control all four states in the inverted pendulum model
    19 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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 件のコメント
  Sam Chak
      
      
 2024 年 6 月 18 日
				Thanks for showing the "Code". But I cannot copy the code from the image and test it.
回答 (2 件)
  Sam Chak
      
      
 2024 年 6 月 18 日
        Given that the initial angle is less than  from the equilibrium inverted position, the sine of the angle (
 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:
) 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:
 from the equilibrium inverted position, the sine of the angle (
 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:
) 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)
The pendulum's angular position (orange curve) is maintained at the desired equilibrium  at steady-state.
 at steady-state.
 at steady-state.
 at steady-state.7 件のコメント
  Sam Chak
      
      
 2024 年 6 月 18 日
				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".
  Sam Chak
      
      
 2024 年 6 月 19 日
        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]
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]
%% Linear Quadratic Regulator
Q   = eye(size(A));
R   = 1;
K   = lqr(A, B, Q, R)
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.
8 件のコメント
  Sam Chak
      
      
 2024 年 6 月 21 日
				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.
 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.
 ,
,  ,
,  ,
,  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.
 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.
  Oren
 2025 年 3 月 4 日
				The original question asked about PID control, but this answer only provides the proportional gain. How does one find the integral and differential gain? The PID Tuner app provided in matlab reccomends splitting the 4 states into 4 separate signals for 4 different PID blocks and tuning them separately. But tuning just assesses its response to a unit step function, and the velocity states don't converge to one (because that would be unstable), so the tuner app concludes they are broken.
参考
カテゴリ
				Help Center および File Exchange で Classical Control Design についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


















