pendul2.m
来自「为了下载东西」· M 代码 · 共 157 行
M
157 行
%PENDUL2 Second setup file for the inverted pendulum simulator
%
%
% PENDULUM 3.0 - A BALL BALANCING SIMULATOR
%
%
% Simulates linear and fuzzy control strategies for a ball balancing
% system. Version 1.0 written by prof. Jan Jantzen, Danmarks Tekniske
% Universitet. Version 2.0, to run under Matlab SE 4.0, written by
% Petur Snaeland, M.Sc. student, May 1995.
%
% Program execution is similar to that of event-driven programs. Control
% flow is therefore not continuous, since user-interface objects trigger
% command execution as they are activated by the user. However, the same
% subroutine, doevent(strEvent), is always called when events are fired.
% There, any response to user interaction is determined.
%
% Revised Hordur Kvaran, 20.10.97
%
%##############################################################################
global ConType Ts Ttot Y dY Phi dPhi G1 G2 G3 G4 GU1 GU2 SOCLR SOCTC SOCDP BallRules CartRules;
pendmenu; %Load the menu
%
% Assign initial values to simulation parameters.
%
Ts = 0.1; Ttot = 5 ; % Time step size and total simulation period.
Y = 0 ; dY = 0; % Cart position/velocity at t=0.
Phi = -12.0; dPhi = 0; % Ball angle/angular velocity at t=0.
% G1 = 65; G2 = 65; GU1 = 0.37; % Cart controller input/output gains.
% G3 = 332; G4 = 86; GU2 = 0.5; % Ball controller input/output gains.
G1 = 24; G2 = 24; GU1 = 1; % Cart controller input/output gains.
G3 = 166; G4 = 43; GU2 = 1; % Ball controller input/output gains.
SOCLR = 10; SOCTC = 0.5; SOCDP = 0.1; % SOC cart controller parameters.
BallRules = ones(1,9); % Uses all ball-rules as default
CartRules = ones(1,9); % Uses all cart-rules as default
ltGray = [0.75 0.75 0.75]; % RGB description of light gray.
dkGray = [0.5 0.5 0.5]; % RGB description of dark gray.
%##############################################################################
%
% INITIALIZE OBJECTS IN ANIMATION AREA
%
hCart = patch([-0.1 -0.1 0.1 0.1],[0 0.1 0.1 0],[0 0 0.7]);
hBall = patch([-0.1 -0.1 0.1 0.1],[0 0.1 0.1 0],ltGray);
set(hCart,'Tag','objCart');
set(hBall,'Tag','objBall');
frame('',[60 110 240 15],ltGray); % Left side of track
frame('',[300 110 240 15],ltGray); % Right side of track
frame('',[50 110 10 30],ltGray); % Left hand stopper
frame('',[540 110 10 30],ltGray); % Right hand stopper
draw(Y,Phi/180*pi);
%##############################################################################
%
% INITIALIZE MAIN SCREEN INFORMATION PANELS AND COMMAND BUTTONS
%
frame('',[0 0 600 110],ltGray);
%
% Select the linear controller to begin with and check menus accordingly (see setctrlr.m).
%
ConType = 'Linear Summation';
hConType = label([ ConType ' Controller'],[200 80 200 20],ltGray);
set(hConType,'HorizontalAlignment','center','Tag','txtConType');
ConType=setctrlr('SimConLin',hConType);
%
% Display initial values within a frame:
%
frame('',[5 5 115 100],ltGray);
label('Initial Conditions:', [10 80 109 18],ltGray);
uicontrol('Style','text','Position',[10 55 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Y: ' num2str(Y) ' [m]'],...
'ButtonDownFcn','doevent(''SimIniPos'');','Tag','txtSimIniPos');
uicontrol('Style','text','Position',[10 40 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' dY: ' num2str(dY) ' [m/s]'],...
'ButtonDownFcn','doevent(''SimIniVel'');','Tag','txtSimIniVel');
uicontrol('Style','text','Position',[10 25 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Phi: ' num2str(Phi) ' [deg]'],...
'ButtonDownFcn','doevent(''SimIniAng'');','Tag','txtSimIniAng');
uicontrol('Style','text','Position',[10 10 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' dPhi: ' num2str(dPhi) ' [deg/s]'],...
'ButtonDownFcn','doevent(''SimIniAVe'');','Tag','txtSimIniAVe');
%
% Display cart gains within a frame:
%
frame('',[125 5 115 70],ltGray);
label('Cart Gains:', [130 55 109 18],ltGray);
uicontrol('Style','text','Position',[130 40 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Position: ' num2str(G1) ],...
'ButtonDownFcn','doevent(''CartGainPos'');','Tag','txtCartPos');
uicontrol('Style','text','Position',[130 25 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Velocity: ' num2str(G2) ],...
'ButtonDownFcn','doevent(''CartGainVel'');','Tag','txtCartVel');
uicontrol('Style','text','Position',[130 10 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Output: ' num2str(GU1)],...
'ButtonDownFcn','doevent(''CartGainOut'');','Tag','txtCartOut');
%
% Display ball gains within a frame:
%
frame('',[245 5 115 70],ltGray);
label('Ball Gains:', [250 55 109 18],ltGray);
uicontrol('Style','text','Position',[250 40 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Angle: ' num2str(G3) ],...
'ButtonDownFcn','doevent(''BallGainAng'');','Tag','txtBallPos');
uicontrol('Style','text','Position',[250 25 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Velocity: ' num2str(G4) ],...
'ButtonDownFcn','doevent(''BallGainVel'');','Tag','txtBallVel');
uicontrol('Style','text','Position',[250 10 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Output: ' num2str(GU2)],...
'ButtonDownFcn','doevent(''BallGainOut'');','Tag','txtBallOut');
%
% Display current time, step size and total simulation time within a frame:
%
frame('',[365 5 115 70],ltGray);
label('Simulation:', [370 55 109 18],ltGray);
uicontrol('Style','text','Position',[370 40 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',' Time: 0.00 [s]','Tag','txtTime');
uicontrol('Style','text','Position',[370 25 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Tstep: ' num2str(Ts) ' [s]'],...
'ButtonDownFcn','doevent(''SimInt'');','Tag','txtStep');
uicontrol('Style','text','Position',[370 10 109 15],'HorizontalAlignment','left',...
'BackgroundColor',ltGray,'String',[' Tmax: ' num2str(Ttot) ' [s]'],...
'ButtonDownFcn','doevent(''SimPer'');','Tag','txtTtot');
%
% Display buttons to start and replay a simulation, and to exit.
%
frame('',[485 5 110 100],ltGray);
uicontrol('Style','pushbutton','Units','pixels','Enable','on', ...
'Position',[490 75 100 25],'String','Start','Callback',...
'doevent(''SimStart'');','Tag','btnSimStart');
uicontrol('Style','pushbutton','Units','pixels','Enable','on', ...
'Position',[490 45 100 25],'String','Replay','Callback',...
'doevent(''SimReplay'');','Tag','btnSimReplay','Enable','off');
uicontrol('Style','pushbutton','Units','pixels','Enable','on', ...
'Position',[490 10 100 25],'String','Exit','Callback',...
'doevent(''MainExit'');','Tag','btnMainExit');
uicontrol('Style','checkbox','Units','pixels','Background','w',...
'Position',[510 230 100 25],'String','Animation', ...
'Enable','on','Value',1,'Callback', '','Tag','chkAnimation');
%##############################################################################
%
% CLEAR UNNECESSARY LOCAL VARIABLES
%
clear dkGray ltGray;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?