First, the establishment of linear first order inverted pendulum model
The one-stage inverted pendulum system is an unstable system, and its mechanism needs to be modeled. In the research process, air friction, etc., should be ignored, and then the inverted pendulum system can be abstracted, which is considered to be composed of trolley and homogeneous rigid bar, and the force analysis of these two parts is as shown in the figure:Where is the mass of the trolley and the mass of the pendulum; B, F and X are respectively the friction coefficient of the trolley, the force applied to the trolley and the position of the trolley [8]. I and are the inertia of the pendulum rod and the length from the rotation axis of the pendulum rod to the center of mass respectively; And are the included Angle between the pendulum rod and the vertical upward direction and the vertical downward direction respectively; N and P are the horizontal and vertical components of the pendulum force respectively. Resultant force of trolley in horizontal direction:The resultant force in the horizontal direction of the pendulum rod:
Horizontal motion equation of pendulum rod:Pendulum moment balance equation:The motion equation of the pendulum in the vertical direction can be obtained:Vertical motion equation of the pendulum rod:Replace the force F with u, and linearize at the same time, namely:
Two, some source code
Function varargout = DLB (varargin) % gui_Singleton =1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @dlb_OpeningFcn, ...
'gui_OutputFcn', @dlb_OutputFcn, ...
'gui_LayoutFcn', [],...'gui_Callback'[]);if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:}); End % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- open interface -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - %function dlb_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
global h1 h2 h3;
axes(handles.sys1);
axis equal;
axis([0 5 - 20 20 0 20]);
grid on;
view(80.30);
reall=15;
ll=4+reall;
v=[0 0 0;0 5 0;3 5 0;3 0 0;0 0 3;0 5 3;3 5 3;3 0 3];
f=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8];
g_v=[1 2 3;1 3 3;2 3 3;2 2 3;1 2 ll;1 3 ll;2 3 ll;2 2 ll];
g_f=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8];
h1=patch('Faces',f,'Vertices',v,'FaceColor'.'b');
h2=patch('Faces',g_f,'Vertices',g_v,'FaceColor'.'r');
hold on;
[ballx,bally,ballz]=sphere(8);
ballo=[1.5 2.5 ll];
h3=surf(ballx*1.1+ballo(1),bally*1.1+ballo(2),ballz*1.1+ballo(3));
colormap(autumn);
axes(handles.sys3);
sys3data=imread('model.jpg'); % open inverted pendulum schematic image(sys3data);set(gca,'Xtick'[],'Ytick'[],'box'.'on'); Function varargout = dlb_OutputFcn(hObject, eventData, handles) %1} = handles.output;
function normal(hObject, eventdata, handles)% public call functionif ispc
set(hObject,'BackgroundColor'.'white');
else
set(hObject,'BackgroundColor',get(0.'defaultUicontrolBackgroundColor')); End % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- inverted pendulum model parameters to create -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- %function mc_CreateFcn(hObject, eventdata, handles)%M, car massnormal(hObject, eventdata, handles);
function mc_Callback(hObject, eventdata, handles)
function l_CreateFcn(hObject, eventdata, handles)%L, length of swing rodnormal(hObject, eventdata, handles);
function l_Callback(hObject, eventdata, handles)
function mq_CreateFcn(hObject, eventdata, handles)%m, pendulum massnormal(hObject, eventdata, handles);
function mq_Callback(hObject, eventdata, handles)% -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- create LQR parameters -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % functionq1_CreateFcn(hObject, eventdata, handles) %q1
normal(hObject, eventdata, handles);
function q1_Callback(hObject, eventdata, handles)
function q2_CreateFcn(hObject, eventdata, handles) %q2
normal(hObject, eventdata, handles);
function q2_Callback(hObject, eventdata, handles)
function q3_CreateFcn(hObject, eventdata, handles) %q3
normal(hObject, eventdata, handles);
function q3_Callback(hObject, eventdata, handles)
function q4_CreateFcn(hObject, eventdata, handles) %q4
normal(hObject, eventdata, handles);
function q4_Callback(hObject, eventdata, handles)
function r_CreateFcn(hObject, eventdata, handles) %R
normal(hObject, eventdata, handles);
function r_Callback(hObject, eventdata, handles)% -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- LQR theory is adopted to calculate the gain K -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % functionlqrok_Callback(hObject, eventdata, handles)
global A B C D K;
M_str=get(handles.mc,'string'); M=str2double(M_str); M m_str=get(handles. Mq,'string'); m=str2double(m_str); M L_str=get(handles. L,'string'); L=str2double(L_str); Q1_str =get(handles. Q1,'string');
q1=str2double(q1_str);
q2_str=get(handles.q2,'string');
q2=str2double(q2_str);
q3_str=get(handles.q3,'string');
q3=str2double(q3_str);
q4_str=get(handles.q4,'string');
q4=str2double(q4_str);
R_str=get(handles.r,'string');
R=str2double(R_str);
Q=[q1,0.0.0;0,q2,0.0;0.0,q3,0;0.0.0,q4];
%Inverted Pendulum Model
g=9.8;
I=1/12*m*L^2;
l=1/2*L;
t1=m*(M+m)*g*l/[(M+m)*I+M*m*l^2];
t2=-m^2*g*l^2/[(m+M)*I+M*m*l^2];
t3=-m*l/[(M+m)*I+M*m*l^2];
t4=(I+m*l^2)/[(m+M)*I+M*m*l^2];
A=[0.1.0.0; t1,0.0.0;0.0.0.1; t2,0.0.0];
B=[0; t3;0; t4]; C=[0.0.1.0];
D=[0]; [K,sz,ez]=lqr(A,B,Q,R); % Calculate K value using LQR methodset(handles.k1,'string',K(1));
set(handles.k2,'string',K(2));
set(handles.k3,'string',K(3));
set(handles.k4,'string',K(4)); %--------------------K creation -----------------------%function k1_CreateFcn(hObject, eventdata, handles) %K1
normal(hObject, eventdata, handles);
function k1_Callback(hObject, eventdata, handles)
function k2_CreateFcn(hObject, eventdata, handles) %K2
normal(hObject, eventdata, handles);
function k2_Callback(hObject, eventdata, handles)
function k3_CreateFcn(hObject, eventdata, handles) %K3
normal(hObject, eventdata, handles);
function k3_Callback(hObject, eventdata, handles)
function k4_CreateFcn(hObject, eventdata, handles) %K4
normal(hObject, eventdata, handles);
function k4_Callback(hObject, eventdata, handles)% -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- pendulum Angle and cart position, the initial value -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % functionin_po_CreateFcn(hObject, eventdata, handles)% Car horizontal position creatednormal(hObject, eventdata, handles);
function in_po_Callback(hObject, eventdata, handles)% Car horizontal position callback Current_Val=str2num(get(hObject,'string'));
set(handles.slider_po,'Value',Current_Val);
function slider_po_CreateFcn(hObject, eventdata, handles)% Drag the bar horizontally to create usewhiteBG= 1;
if usewhitebg
set(hObject,'BackgroundColor'[9. 9. 9.]);
else
set(hObject,'BackgroundColor',get(0.'defaultUicontrolBackgroundColor'));
end
function slider_po_Callback(hObject, eventdata, handles)% Car horizontal drag bar callback global H1 H2 H3;
Value=get(hObject,'Value');
set(handles.in_po,'String',Value);
axes(handles.sys1);
axis equal;
axis([0 5 - 20 20 0 20]);
reall=15;
ll=4+reall;
v=[0 0 0;0 5 0;3 5 0;3 0 0;0 0 3;0 5 3;3 5 3;3 0 3];
f=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8];
g_v=[1 2 3;1 3 3;2 3 3;2 2 3;1 2 ll;1 3 ll;2 3 ll;2 2 ll];
g_f=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8];
delete(h1);delete(h2);delete(h3);
i=get(handles.slider_po,'Value') *10;
bbb=[0 i 0;0 i 0;0 i 0;0 i 0;0 i 0;0 i 0;0 i 0;0 i 0; ] ; v1=v+bbb; af=get(handles.slider_ag,'Value');
gt1=0.5*cos(-af)0.5;
gt2=0.5*sin(-af)0;
gt3=0.5/cos(af)+(ll0.5*tan(af))*sin(af)-cos(af)+0.5;
gt4=(ll0.5*tan(af))*cos(af)+sin(af)-ll;
gt5=0.5/cos(af)+(ll0.5*tan(af))*sin(af)0.5;
gt6=(ll0.5*tan(af))*cos(af)-ll;
jiao=[0 -gt1 -gt2;0 gt1 gt2;0 gt1 gt2;0 -gt1 -gt2;0 gt3 gt4;0 gt5 gt6;0 gt5 gt6;0 gt3 gt4];
g_v1=g_v+jiao+bbb;
h1=patch('Faces',f,'Vertices',v1,'FaceColor'.'b');
h2=patch('Faces',g_f,'Vertices',g_v1,'FaceColor'.'r');
hold on;
[ballx,bally,ballz]=sphere(8);
ballo=[1.5 2.5 ll];
bally_1=bally*1.1+i+ballo(2)+ll*sin(af);
ballz_1=ballz*1.1+ll*cos(af);
h3=surf(ballx*1.1+ballo(1),bally_1,ballz_1);
colormap(autumn);
function in_ag_CreateFcn(hObject, eventdata, handles)% Swing bar Angle creatednormal(hObject, eventdata, handles);
function in_ag_Callback(hObject, eventdata, handles)% swing rod Angle callback Current_Val=str2num(get(hObject,'string'));
set(handles.slider_ag,'Value',Current_Val);
function slider_ag_CreateFcn(hObject, eventdata, handles)Swing bar Angle drag bar to create usewhiteBG= 1;
if usewhitebg
set(hObject,'BackgroundColor'[9. 9. 9.]);
else
set(hObject,'BackgroundColor',get(0.'defaultUicontrolBackgroundColor'));
end
function slider_ag_Callback(hObject, eventdata, handles)% Swing bar Angle drag bar callback global H1 H2 H3;
Value=get(hObject,'Value');
set(handles.in_ag,'String',Value);
axes(handles.sys1);
axis equal;
axis([0 5 - 20 20 0 20]);
v=[0 0 0;0 5 0;3 5 0;3 0 0;0 0 3;0 5 3;3 5 3;3 0 3];
f=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8];
reall=15;
ll=4+reall;
g_v=[1 2 3;1 3 3;2 3 3;2 2 3;1 2 ll;1 3 ll;2 3 ll;2 2 ll];
g_f=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8];
delete(h1);delete(h2);delete(h3);
i=get(handles.slider_po,'Value') *10;
bbb=[0 i 0;0 i 0;0 i 0;0 i 0;0 i 0;0 i 0;0 i 0;0 i 0; ] ; v1=v+bbb; af=get(handles.slider_ag,'Value');
gt1=0.5*cos(-af)0.5;
gt2=0.5*sin(-af)0;
gt3=0.5/cos(af)+(ll0.5*tan(af))*sin(af)-cos(af)+0.5;
gt4=(ll0.5*tan(af))*cos(af)+sin(af)-ll;
gt5=0.5/cos(af)+(ll0.5*tan(af))*sin(af)0.5;
gt6=(ll0.5*tan(af))*cos(af)-ll;
jiao=[0 -gt1 -gt2;0 gt1 gt2;0 gt1 gt2;0 -gt1 -gt2;0 gt3 gt4;0 gt5 gt6;0 gt5 gt6;0 gt3 gt4];
g_v1=g_v+jiao+bbb;
h1=patch('Faces',f,'Vertices',v1,'FaceColor'.'b');
h2=patch('Faces',g_f,'Vertices',g_v1,'FaceColor'.'r');
hold on;
Copy the code
3. Operation results
Matlab version and references
1 matlab version 2014A
[1] Yang Baoyang, YU Jizhou, Yang Shan. Intelligent Optimization Algorithm and Its MATLAB Example (2nd Edition) [M]. Publishing House of Electronics Industry, 2016. [2] ZHANG Yan, WU Shuigen. MATLAB Optimization Algorithm source code [M]. Tsinghua University Press, 2017. [3] Zhou Pin. Design and Application of MATLAB Neural Network [M]. Tsinghua University Press, 2013. [4] Chen Ming. MATLAB Neural Network Principle and Examples of Fine Solution [M]. Tsinghua University Press, 2013. [5] FANG Qingcheng. MATLAB R2016a Neural Network Design and Application of 28 Cases Analysis [M]. Tsinghua University Press, 2018.