📄 doevent.m
字号:
'NewPhi = str2num(get(hUserInput3,''String'')); ', ...
'NewdPhi = str2num(get(hUserInput4,''String'')); ', ...
'if ~isempty(NewY), ', ...
' Y = max(-0.525, min(0.525, NewY)); ',...
' hSimIniPos = findobj(''Tag'',''txtSimIniPos'');',...
' dispinfo(''Y'',Y,hSimIniPos); ', ...
'end;',...
'if ~isempty(NewdY), ', ...
' dY = NewdY; ',...
' hSimIniVel = findobj(''Tag'',''txtSimIniVel'');',...
' dispinfo(''dY'',dY,hSimIniVel); ', ...
'end;',...
'if ~isempty(NewPhi), ', ...
' Phi = max(-12.6, min(12.6, NewPhi)); ',...
' hSimIniAng = findobj(''Tag'',''txtSimIniAng'');',...
' dispinfo(''Phi'',Phi,hSimIniAng); ', ...
'end;',...
'if ~isempty(NewdPhi), ', ...
' dPhi = NewdPhi; ',...
' hSimIniAVe = findobj(''Tag'',''txtSimIniAVe'');',...
' dispinfo(''dPhi'',dPhi,hSimIniAVe); ', ...
'end;',...
'delete(gcf); clear NewY NewdY NewPhi NewdPhi; draw(Y,Phi*pi/180);'];
cmdCancel = 'delete(gcf);';
KeyPressFcn = [''];
%
% Load and get a handle to the input window, command buttons and editbox.
%
h = figure('Visible','off','NumberTitle','off','MenuBar','none', ...
'Name',strTitle,'Position',posBox,'Resize','off','Color',[.75 .75 .75] ,'KeyPressFcn',KeyPressFcn);
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',strY,'Position',posYLbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','text','String',strdY,'Position',posdYLbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','text','String',strPhi,'Position',posPhiLbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','text','String',strdPhi,'Position',posdPhiLbl, ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','left');
uicontrol('Style','edit','Position',posYEdit,'String',num2str(Y), ...
'BackgroundColor','White','Callback','','Tag','hUserInput1');
uicontrol('Style','edit','Position',posdYEdit,'String',num2str(dY), ...
'BackgroundColor','White','Callback','','Tag','hUserInput2');
uicontrol('Style','edit','Position',posPhiEdit,'String',num2str(Phi), ...
'BackgroundColor','White','Callback','','Tag','hUserInput3');
uicontrol('Style','edit','Position',posdPhiEdit,'String',num2str(dPhi), ...
'BackgroundColor','White','Callback','','Tag','hUserInput4');
set(h,'Visible','on');
watchoff(fignumber);
elseif strcmp(strEvent,'SimIniPos')
cmdOK = [' watchon; global Y Phi;',...
'hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewY = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewY), ', ...
' NewY = max(-0.525, min(0.525, NewY)); ', ...
' Y = NewY; hSimIniPos = findobj(''Tag'',''txtSimIniPos'');', ...
' delete(gcf); dispinfo(''Y'',Y,hSimIniPos); draw(Y,Phi*pi/180); ', ...
' end; clear NewY; ' ];
inputbox('Initial Cart Position','New value: [m]',num2str(Y),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'SimIniVel')
cmdOK = [' watchon; global dY;',...
'hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewdY = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewdY), ', ...
' dY = NewdY; hSimIniVel = findobj(''Tag'',''txtSimIniVel'');', ...
' delete(gcf); dispinfo(''dY'',dY,hSimIniVel);', ...
' end; clear NewdY;' ];
inputbox('Initial Cart Velocity','New value: [m/s]',num2str(dY),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'SimIniAng')
cmdOK = [' watchon; global Y Phi;',...
'hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewPhi = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewPhi), ', ...
' NewPhi = max(-12.6, min(12.6, NewPhi)); ', ...
' Phi = NewPhi; hSimIniAng = findobj(''Tag'',''txtSimIniAng'');', ...
' delete(gcf); dispinfo(''Phi'',Phi,hSimIniAng); draw(Y,Phi*pi/180);', ...
' end; clear NewPhi; ' ];
inputbox('Initial Ball Angle','New value: [deg]',num2str(Phi),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'SimIniAVe')
cmdOK = [' watchon; global dPhi;',...
'hUserInput1 = findobj(''Tag'',''hUserInput1''); ',...
' NewdPhi = str2num(get(hUserInput1,''String'')); ', ...
' if ~isempty(NewdPhi), ', ...
' dPhi = NewdPhi; hSimIniAVe = findobj(''Tag'',''txtSimIniAVe'');', ...
' delete(gcf); dispinfo(''dPhi'',dPhi,hSimIniAVe);', ...
' end; clear NewdPhi;' ];
inputbox('Initial Ball Velocity','New value: [deg/s]',num2str(dPhi),cmdOK,'hUserInput1');
watchoff(fignumber);
elseif strcmp(strEvent,'SimIniDef')
%
% Set the initial values to their defaults.
%
Y = 0; dY = 0;
Phi = -12; dPhi = 0;
%
% Update the initial values displayed in the main window.
%
hSimIniPos = findobj('Tag','txtSimIniPos');
hSimIniVel = findobj('Tag','txtSimIniVel');
hSimIniAng = findobj('Tag','txtSimIniAng');
hSimIniAVe = findobj('Tag','txtSimIniAVe');
dispinfo('Y',Y,hSimIniPos);dispinfo('dY',dY,hSimIniVel);
dispinfo('Phi',Phi,hSimIniAng);dispinfo('dPhi',dPhi,hSimIniAVe);
%
% Redraw the cart and ball to reflect new initial values.
%
draw(Y,Phi/180*pi);
watchoff(fignumber);
elseif strcmp(strEvent,'SimStart'),
%
% Call the simulation routine and begin simulation/animation.
%
hbtnSimStart = findobj('Tag','btnSimStart');
set(hbtnSimStart,'String','Stop','Callback','doevent(''SimStop'')','Value',1);
Gains = [G1,G2,G3,G4,GU1,GU2];
simulate(Gains,Y,dY,Phi,dPhi,Ts,Ttot,SOCLR,SOCTC,SOCDP,ConType,CartRules,BallRules);
set(hbtnSimStart,'String','Start','Callback','doevent(''SimStart'')','Value',0);
%
% Now that we have some results, we can enable the Results menu and
% the Replay command button and menu command.
%
hmnuSimReplay = findobj('Tag','mnuSimReplay');
hReplayButton = findobj('Tag','btnSimReplay');
hmnuRes = findobj('Tag','mnuResults');
set(hmnuSimReplay,'Enable','on');
set(hReplayButton,'Enable','on');
set(hmnuRes,'Enable','on');
watchoff(fignumber);
elseif strcmp(strEvent,'SimStop'),
hbtnSimStart = findobj('Tag','btnSimStart');
set(hbtnSimStart,'Value',0);
elseif strcmp(strEvent,'SimReplay'),
%
% Declare which global variables need to be accessible and load
% simulation history.
%
load history;
hTime = findobj('Tag','txtTime');
hCart = findobj('Tag','objCart');
hBall = findobj('Tag','objBall');
set([hCart hBall],'EraseMode','background');
%
% Run through the animation again, using X1(t) and X3(t).
% Also, update the current time display in the main window.
%
for Pass=1:length(AllX1),
draw(AllX1(Pass),AllX3(Pass));
set(hTime,'String',sprintf(' Time: %5.2f [s]',Pass*Ts));
end;
set([hCart hBall],'EraseMode','normal');
watchoff(fignumber);
%##############################################################################
%
% RESULTS MENU EVENTS
%
elseif strcmp(strEvent,'ResCartPos'),
%
% Declare which global variables need to be accessible and load
% simulation history.
%
load history;
window('Cart Position Y','figure'); plot(t,AllX1,'k-'); grid;
title(['Cart Position Y, ' ConType ' Controller, IAE = ', num2str(sum(abs(AllX1))*Ts) ]);
xlabel('Time [s]'); ylabel('Y [m]');
watchoff(fignumber);
elseif strcmp(strEvent,'ResCartCtrl'),
load history;
window('Cart Control Signal U1','figure');
% plot(t,AllU1,'-k',t,AllX1*G1*GU1,'--r',t,AllX2*G2*GU1,'-.r');
plot(t,AllU1,'-k');grid;
% title([ConType ' Controller: - U1, -- Y稧1稧U1, -. dY稧2稧U1']);
title([ConType ' Controller: U1']);
xlabel('Time [s]'); ylabel('U1 [volts]');
watchoff(fignumber);
elseif strcmp(strEvent,'ResCartPosCtrl'),
load history;
window('Cart Position Y and Control Signal U1','figure');
subplot(211); plot(t,AllX1,'k'); grid;
title(['Cart Position Y, ' ConType ' Controller' ]); ylabel('Y [m]');
subplot(212); plot(t,AllU1,'k'); title('Control Signal U1');
xlabel('Time [s]'); ylabel('U1 [Volt]'); grid ;
watchoff(fignumber);
elseif strcmp(strEvent,'ResCartCtrlSurf'),
load fzytable; load history;
window('Cart States and Control Surface','figure');
meshz(-100:10:100,-100:10:100,CartTable',CartTable*0);
colormap([1 0 0]);
hold on;
% Correct for wrong drawing in Matlab 4 and earlier
s=version;
if (str2num(s(1))<5)
[Xxx,Yyy,Zzz] = frontpts(G1*AllX1,G2*AllX2,AllU1/GU1,.05) ;
else
Xxx = G1*AllX1;
Yyy = G2*AllX2;
Zzz = AllU1/GU1;
end;
plot3(Xxx,Yyy,Zzz,'k-') ;
text(Xxx(1),Yyy(1),Zzz(1)+10,'1') ;
text(Xxx(2),Yyy(2),Zzz(2)+10,'2') ;
text(Xxx(3),Yyy(3),Zzz(3)+10,'3') ;
% plot3(G1*AllX1,G2*AllX2,AllU1/GU1+5,'y-');
% hold on; plot3(G1*AllX1,G2*AllX2,AllU1/GU1+5,'y*');
title('Cart State and Control Surface');
xlabel('Position'); ylabel('Velocity'); zlabel('Control');
axis([-100 100 -100 100 -200 200]);view([1,-1,1])
clear Xxx Yyy Zzz;
watchoff(fignumber);
elseif strcmp(strEvent,'ResCartPhase'),
load history;
window('Cart Phase Plot','figure');
plot(AllX1*G1,AllX2*G2,'k');
text(AllX1(1)*G1,AllX2(1)*G2,'1') ;
text(AllX1(2)*G1,AllX2(2)*G2,'2') ;
text(AllX1(3)*G1,AllX2(3)*G2,'3') ;
title(['Cart Phase Plot, ' ConType ' Controller' ]);
xlabel('Position稧1'); ylabel('Velocity稧2'); grid;
axis([-100 100 -100 100]);
watchoff(fignumber);
elseif strcmp(strEvent,'ResBallAng'),
load history;
window('Ball Angle Phi','figure');
plot(t,180*AllX3/pi,'k');
IAEB = sum(abs(AllX3))*Ts*180/pi ;
title(['Ball Angle, ' ConType ' Controller, IAE = ', num2str(IAEB) ]);
grid; xlabel('Time [s]'); ylabel('Phi [deg]');
watchoff(fignumber);
elseif strcmp(strEvent,'ResBallCtrl'),
load history;
window('Ball Control Signal U2','figure');
% plot(t,AllU2,'-k',t,AllX3*G3*GU2,'--r',t,AllX4*G4*GU2,'-.r');
% title([ConType ' Controller: - U2, -- Y稧3稧U2, -. dY稧4稧U2']);
plot(t,AllU2,'-k');grid;
title([ConType ' Controller: U2']);
xlabel('Time [s]'); ylabel('U2 [volts]');
watchoff(fignumber);
elseif strcmp(strEvent,'ResBallAngCtrl'),
load history;
window('Ball Angle Phi and Control Signal U2','figure');
subplot(211); plot(t,AllX3,'k'); grid;
title(['Ball Angle, ' ConType ' Controller' ]); ylabel('Phi [rad]');
subplot(212); plot(t,AllU2,'k'); title('Control Signal U2');
xlabel('Time [s]'); ylabel('U2 [Volt]'); grid ;
watchoff(fignumber);
elseif strcmp(strEvent,'ResBallCtrlSurf'),
load fzytable; load history;
window('Ball States and Control Surface','figure');
meshz(-100:10:100,-100:10:100,BallTable',BallTable*0);
colormap([1 0 0]);
hold on;
% Correct for wrong drawing in Matlab 4 and earlier
s=version;
if (str2num(s(1))<5)
[Xxx,Yyy,Zzz] = frontpts(G3*AllX3,G4*AllX4,AllU2/GU2,.05) ;
else
Xxx = G3*AllX3;
Yyy = G4*AllX4;
Zzz = AllU2/GU2;
end;
plot3(Xxx,Yyy,Zzz,'k-') ;
text(Xxx(1),Yyy(1),Zzz(1)+10,'1') ;
text(Xxx(2),Yyy(2),Zzz(2)+10,'2') ;
text(Xxx(3),Yyy(3),Zzz(3)+10,'3') ;
% plot3(G3*AllX3,G4*AllX4,AllU2/GU2+5,'y-');
% hold on; plot3(G3*AllX3,G4*AllX4,AllU2/GU2+5,'y*');
title('Ball State and Control Surface');
xlabel('Angle'); ylabel('Angular Velocity'); zlabel('Control');
axis([-100 100 -100 100 -200 200]);view([1,-1,1])
clear Xxx Yyy Zzz;
watchoff(fignumber);
elseif strcmp(strEvent,'ResBallPhase'),
load history;
window('Ball Phase Plot','figure');
plot(AllX3*G3,AllX4*G4,'k');
text(AllX3(1)*G3,AllX4(1)*G4,'1') ;
text(AllX3(2)*G3,AllX4(2)*G4,'2') ;
text(AllX3(3)*G3,AllX4(3)*G4,'3') ;
title(['Ball Phase Plot, ' ConType ' Controller' ]);
xlabel('Angle稧3'); ylabel('Velocity稧4'); grid;
axis([-100 100 -100 100]);
watchoff(fignumber);
elseif strcmp(strEvent,'ResControl'),
load history;
window('Control Signal Components U1 + U2','figure');
plot(t,AllU,'-k',t,AllU1,'--r',t,AllU2,'-.r');grid;
title(['Control Signals: - Total U, -- Cart U1, -. Ball U2, ' ConType]);
xlabel('Time [s]'); ylabel('U, U1, U2 [volts]');
watchoff(fignumber);
elseif strcmp(strEvent,'ResStates'),
load history;
window('State Variables','figure');
subplot(221); plot(t,AllX1,'k'); grid;
%title(['Cart Position, ' ConType ' Controller']);
title([ConType ' Controller']);
ylabel('Y [m]');
subplot(222); plot(t,AllX2,'k'); grid;
% title('Cart Velocity');
ylabel('YDot [m/s]');
subplot(223); plot(t,AllX3,'k'); grid;
% title('Ball Position');
xlabel('Time [s]'); ylabel('Phi [rad]');
subplot(224); plot(t,AllX4,'k'); grid;
% title('Ball Velocity');
xlabel('Time [s]'); ylabel('PhiDot [rad/s]');
watchoff(fignumber);
else
delete(gcf);
error(['An unknow event occured. Event ''',strEvent,''' not recognized']);
end; % Phew!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -