📄 doevent.m
字号:
%DOEVENT Event handler for the pendulum ball balancing simulator
%
% Syntax: doevent(strEvent)
%
% Inputs: strEvent is a string describing which item on the PENDUL2 menu
% the user has selected. See 'pendul2.m' for strEvent definitions.
%
% Result: Appropriate program action has been triggered. Since this function
% processes all possible menu selections, refer to the function body
% for a complete description.
%
% Remark: This function is the event handler of the PENDULUM ball-balancing
% simulator. All menu events are handled in a consistent manner.
% Note that set/variable/parameter editing events display new windows
% and that eventual editing does not occur until those windows are closed.
%
% Petur Snaeland, 25.04.1995
% Revised: Hordur Kvaran, 20.10.97
function doevent(strEvent)
% For Matlab 5 compatibility all global definitions are now declared here in the
% beginning instead of individually depending on strEvent.
% Hordur Kvaran, 14.10.97
global G1 G2 G3 G4 GU1 GU2 Y dY Phi dPhi Ts Ttot SOCLR SOCTC SOCDP ConType;
global BallRules CartRules;
% Set the pointer to a busy hourglass. Turned off once doevent finishes execution.
fignumber=watchon;
%##############################################################################
%
% MAIN MENU EVENTS
%
whitebg('w') ; % The Matlab in Bari, Italy, does not like this;
if strcmp(strEvent,'MainExit'),
%
% Delete the main window. Do not delete the figures that may be
% open - the user might want to look at them.
%
delete(gcf);
fignumber=NaN;
elseif strcmp(strEvent,'MainSave'),
%
% Save gains, initial values, SOC parameters and controller type to a .MAT file:
%
save settings G1 G2 G3 G4 GU1 GU2 Y dY Phi dPhi Ts Ttot SOCLR SOCTC SOCDP ...
ConType CartRules BallRules;
load cartsets ;
save setscart ISL ISM ISR MVL SST MVR PHL PSL OOO PSR PHR;
load ballsets ;
save setsball ISL ISM ISR MVL SST MVR PHL PSL OOO PSR PHR;
watchoff(fignumber);
elseif strcmp(strEvent,'MainLoad'),
load settings;
load setscart ;
save cartsets ISL ISM ISR MVL SST MVR PHL PSL OOO PSR PHR;
load setsball ;
save ballsets ISL ISM ISR MVL SST MVR PHL PSL OOO PSR PHR;
%
% Update the displayed information in the lower half of the main screen.
%
hCartPos = findobj('Tag','txtCartPos');
hCartVel = findobj('Tag','txtCartVel');
hCartOut = findobj('Tag','txtCartOut');
hBallPos = findobj('Tag','txtBallPos');
hBallVel = findobj('Tag','txtBallVel');
hBallOut = findobj('Tag','txtBallOut');
hSimIniPos = findobj('Tag','txtSimIniPos');
hSimIniVel = findobj('Tag','txtSimIniVel');
hSimIniAng = findobj('Tag','txtSimIniAng');
hSimIniAVe = findobj('Tag','txtSimIniAVe');
hStep = findobj('Tag','txtStep');
hTtot = findobj('Tag','txtTtot');
hConType = findobj('Tag','txtConType');
dispinfo('G1',G1,hCartPos); dispinfo('G2',G2,hCartVel); dispinfo('GU1',GU1,hCartOut);
dispinfo('G3',G3,hBallPos); dispinfo('G4',G4,hBallVel); dispinfo('GU2',GU2,hBallOut);
dispinfo('Y',Y,hSimIniPos); dispinfo('dY',dY,hSimIniVel); dispinfo('Phi',Phi,hSimIniAng); dispinfo('dPhi',dPhi,hSimIniAVe);
dispinfo('Ts',Ts,hStep); dispinfo('Ttot',Ttot,hTtot);
dispinfo('ConType',ConType,hConType);
%
% Set controller type and check submenus as usually.
%
if strcmp(ConType,'Linear Summation'),
ConType = setctrlr('SimConLin',hConType);
elseif strcmp(ConType,'Rule Based Fuzzy'),
ConType = setctrlr('SimConRul',hConType);
elseif strcmp(ConType,'Table Based Fuzzy'),
ConType = setctrlr('SimConTbl',hConType);
elseif strcmp(ConType,'SOC on Cart'),
ConType = setctrlr('SimConSOC',hConType);
end;
%
% Redraw the cart and ball to reflect new initial values.
%
draw(Y,Phi*pi/180);
watchoff(fignumber);
elseif strcmp(strEvent,'MainDefault'),
doevent('CartGainDef'); %Reset cart defaults
doevent('BallGainDef'); %Reset ball defaults
doevent('SimIniDef'); %Reset simulation defaults
hStep = findobj('Tag','txtStep');
hTtot = findobj('Tag','txtTtot');
Ts = 0.1; dispinfo('Ts',Ts,hStep); % Time step size
Ttot = 5 ; dispinfo('Ttot',Ttot,hTtot); % Total simulation period.
SOCLR = 10; SOCTC = 0.5; SOCDP = 0.1; % SOC cart controller parameters.
%
% Revert to the default fuzzy rule base
%
RuleBase=['ISLMVLPHLISMMVLPSLISRMVLOOOISLSSTPSLISMSSTOOOISRSSTPSRISLMVROOOISMMVRPSRISRMVRPHR'];
savebase('cartbase.txt',RuleBase);
savebase('ballbase.txt',RuleBase);
BallRules = ones(1,9); CartRules = ones(1,9);
clear RuleBase;
%
% Revert to the default fuzzy sets
%
load decarset ; %Load default cart-sets
save cartsets ISL ISM ISR MVL SST MVR PHL PSL OOO PSR PHR;
load debalset ; %Load default ball-sets
save ballsets ISL ISM ISR MVL SST MVR PHL PSL OOO PSR PHR;
%Rebuild the control tables
CartTable = carttabl(CartRules);
BallTable = balltabl(BallRules);
save fzytable CartTable BallTable ;
%
% Set controller type and check submenus as usually.
%
hConType = findobj('Tag','txtConType');
ConType = setctrlr('SimConLin',hConType);
%
% Redraw the cart and ball to reflect new initial values.
%
draw(Y,Phi*pi/180);
watchoff(fignumber);
%##############################################################################
%
% CART MENU EVENTS
%
elseif strcmp(strEvent,'CartGains'),
MainPos = get(gcf,'Position');
MainX = MainPos(1); MainY = MainPos(2); MainW = MainPos(3); MainH = MainPos(4);
%
% Define spacing, width and height of buttons and input window:
%
numEdit=3;
gap=10;
BtnW=100; BtnH=25;
LabelW = 100;
EditW=50; EditH=20;
BoxW=350; BoxH=(4+numEdit-1)*gap+numEdit*EditH;
%
% Set up position matrices for the window, editbox and buttons:
%
posBox = [MainX+(MainW-BoxW)/2 MainY+(MainH-BoxH)/2 BoxW BoxH];
posFrame = [gap gap BoxW-BtnW-3*gap BoxH-2*gap];
posOK = [BoxW-gap-BtnW BoxH-gap-BtnH BtnW BtnH];
posCancel = [BoxW-gap-BtnW BoxH-2*(gap+BtnH) BtnW BtnH];
posG1Lbl = [2*gap BoxH-2*gap-EditH LabelW EditH-gap/2];
posG2Lbl = [2*gap BoxH-3*gap-2*EditH LabelW EditH-gap/2];
posGU1Lbl = [2*gap BoxH-4*gap-3*EditH LabelW EditH-gap/2];
posG1Edit = [BoxW-BtnW-EditW-3*gap BoxH-2*gap-EditH EditW EditH];
posG2Edit = [BoxW-BtnW-EditW-3*gap BoxH-3*gap-2*EditH EditW EditH];
posGU1Edit = [BoxW-BtnW-EditW-3*gap BoxH-4*gap-3*EditH EditW EditH];
strTitle = 'Edit Cart Gains';
strG1 = 'Cart Position Gain';
strG2 = 'Cart Velocity Gain';
strGU1 = 'Cart Output Gain ';
%
% Define callback strings for the command buttons. These strings
% specify how to respond when the user clicks the buttons.
%
cmdOK = [ ' watchon; global G1 G2 GU1;',...
' hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' hUserInput2 = findobj(''Tag'',''hUserInput2''); ',...
' hUserInput3 = findobj(''Tag'',''hUserInput3''); ',...
' NewG1 = str2num(get(hUserInput1,''String'')); ', ...
' NewG2 = str2num(get(hUserInput2,''String'')); ', ...
' NewGU1 = str2num(get(hUserInput3,''String'')); ', ...
' if ~isempty(NewG1), ', ...
' G1 = NewG1; hCartPos = findobj(''Tag'',''txtCartPos'');', ...
' dispinfo(''G1'',G1,hCartPos); ', ...
' end;',...
' if ~isempty(NewG2), ', ...
' G2 = NewG2; hCartVel = findobj(''Tag'',''txtCartVel'');', ...
' dispinfo(''G2'',G2,hCartVel); ', ...
' end;',...
' if ~isempty(NewG1), ', ...
' GU1 = NewGU1; hCartOut = findobj(''Tag'',''txtCartOut'');', ...
' dispinfo(''GU1'',GU1,hCartOut); ', ...
' end;',...
' delete(gcf); clear NewG1 NewG2 NewGU1; watchoff;'];
cmdCancel = 'delete(gcf);';
%
% Load the input windows, command buttons and editboxes.
%
h = figure('Visible','off','NumberTitle','off','MenuBar','none', ...
'Name',strTitle,'Position',posBox,'Resize','off','Color',[.75 .75 .75]);
uicontrol('Style','frame','String','','Position',posFrame,'BackgroundColor',[.75 .75 .75]);
uicontrol('Style','pushbutton','Units','pixels','Enable','on', ...
'Position',posOK,'String','OK','Callback',cmdOK );
uicontrol('Style','pushbutton','Units','pixels','Enable','on', ...
'Position',posCancel,'String','Cancel','Callback',cmdCancel );
uicontrol('Style','text','String',strG1,'Position',posG1Lbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','text','String',strG2,'Position',posG2Lbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','text','String',strGU1,'Position',posGU1Lbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','edit','Position',posG1Edit,'String',num2str(G1), ...
'BackgroundColor','White','Callback','','Tag','hUserInput1');
uicontrol('Style','edit','Position',posG2Edit,'String',num2str(G2), ...
'BackgroundColor','White','Callback','','Tag','hUserInput2');
uicontrol('Style','edit','Position',posGU1Edit,'String',num2str(GU1), ...
'BackgroundColor','White','Callback','','Tag','hUserInput3');
set(h,'Visible','on');
watchoff(fignumber);
elseif strcmp(strEvent,'CartGainPos'),
%
% Define the command string that will be executed when the user presses the
% OK button after editing. Check for illegal input (if so, do nothing).
% Else, close the box, clear its variables and display the new information.
%
cmdOK = [' watchon; global G1;',...
' hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewG1 = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewG1), ', ...
' G1 = NewG1; hCartPos=findobj(''Tag'',''txtCartPos'');', ...
' delete(gcf); dispinfo(''G1'',G1,hCartPos); ', ...
' end; clear NewG1; watchoff;' ];
inputbox('Cart Position Gain','New value: []',num2str(G1),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'CartGainVel'),
cmdOK = [' watchon; global G2;',...
' hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewG2 = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewG2), ', ...
' G2 = NewG2; hCartVel = findobj(''Tag'',''txtCartVel'');', ...
' delete(gcf); dispinfo(''G2'',G2,hCartVel); ', ...
' end; clear NewG2; watchoff;' ];
inputbox('Cart Velocity Gain','New value: []',num2str(G2),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'CartGainOut'),
cmdOK = [' watchon; global GU1;',...
' hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewGU1 = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewGU1), ', ...
' GU1 = NewGU1; hCartOut = findobj(''Tag'',''txtCartOut'');', ...
' delete(gcf); dispinfo(''GU1'',GU1,hCartOut); ', ...
' end; clear NewGU1; watchoff;' ];
inputbox('Cart Output Gain','New value: []',num2str(GU1),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'CartGainDef'),
%
% Set the cart gains to their default values.
%
G1 = 65; G2 = 65; GU1 = 0.37;
%
% Update the gains displayed in the main window.
%
hCartPos = findobj('Tag','txtCartPos');
hCartVel = findobj('Tag','txtCartVel');
hCartOut = findobj('Tag','txtCartOut');
dispinfo('G1',G1,hCartPos);
dispinfo('G2',G2,hCartVel);
dispinfo('GU1',GU1,hCartOut);
watchoff(fignumber);
elseif strcmp(strEvent,'CartSets'),
%
% Open the fuzzy set editing window.
%
editsets('cartsets');
watchoff(fignumber);
elseif strcmp(strEvent,'CartBase'),
%
% Open the fuzzy rule base editing window.
%
editbase('cartbase.txt');
watchoff(fignumber);
elseif strcmp(strEvent,'CartTable'),
%
% Load both of the fuzzy control tables. Recompute the cart
% control table, and then save both tables again.
%
load fzytable;
CartTable = carttabl(CartRules);
save fzytable CartTable BallTable ;
watchoff(fignumber);
elseif strcmp(strEvent,'CartSurface'),
%
% Load the control tables. Display a filled surface plot of
% the cart table. Set appropriate titles and axis limits.
%
load fzytable;
window('Cart Control Surface','figure');
surfl(-100:10:100,-100:10:100,CartTable');
title('Cart Control Surface');
xlabel('Position'); ylabel('Velocity');
axis([-100 100 -100 100 -200 200]);view([1,-1,1]);
watchoff(fignumber);
elseif strcmp(strEvent,'TuneSOC'),
MainPos = get(gcf,'Position');
MainX = MainPos(1); MainY = MainPos(2); MainW = MainPos(3); MainH = MainPos(4);
%
% Define spacing, width and height of buttons and input window:
%
numEdit=3;
gap=10;
BtnW=100; BtnH=25;
LabelW = 100;
EditW=50; EditH=20;
BoxW=350; BoxH=(4+numEdit-1)*gap+numEdit*EditH;
%
% Set up position matrices for the window, editbox and buttons:
%
posBox = [MainX+(MainW-BoxW)/2 MainY+(MainH-BoxH)/2 BoxW BoxH];
posFrame = [gap gap BoxW-BtnW-3*gap BoxH-2*gap];
posOK = [BoxW-gap-BtnW BoxH-gap-BtnH BtnW BtnH];
posCancel = [BoxW-gap-BtnW BoxH-2*(gap+BtnH) BtnW BtnH];
posSOCLRLbl = [2*gap BoxH-2*gap-EditH LabelW EditH-gap/2];
posSOCTCLbl = [2*gap BoxH-3*gap-2*EditH LabelW EditH-gap/2];
posSOCDPLbl = [2*gap BoxH-4*gap-3*EditH LabelW EditH-gap/2];
posSOCLREdit = [BoxW-BtnW-EditW-3*gap BoxH-2*gap-EditH EditW EditH];
posSOCTCEdit = [BoxW-BtnW-EditW-3*gap BoxH-3*gap-2*EditH EditW EditH];
posSOCDPEdit = [BoxW-BtnW-EditW-3*gap BoxH-4*gap-3*EditH EditW EditH];
strTitle = 'Edit SOC Parameters';
strSOCLR = 'SOC Learning Rate';
strSOCTC = 'SOC Time Constant';
strSOCDP = 'SOC Penalty Delay';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -