📄 s_anm81s.m
字号:
function [sys,x0,str,ts] = s_anm81s(t,x,u,flag)% S-file animation example 1% This example demonstrates buidling an animation % using a single S-file with no callbacks.%% Based on sfuntmpl.m, supplied with SIMULINK% Copyright (c) 1990-96 by The MathWorks, Inc.%switch flag, case 0, % Initialization [sys,x0,str,ts]=mdlInitializeSizes; case 1, % Derivatives sys=mdlDerivatives(t,x,u); case 2, sys=mdlUpdate(t,x,u); case 3, sys=mdlOutputs(t,x,u); % Compute output vector case 4, % Compute time of next sample sys=mdlGetTimeOfNextVarHit(t,x,u); case 9, % Finished. Do any needed sys=mdlTerminate(t,x,u); otherwise % Invalid input error(['Unhandled flag = ',num2str(flag)]);end%********************************************************%* mdlInitializeSizes *%********************************************************function [sys,x0,str,ts]=mdlInitializeSizes()% Return the sizes of the system vectors, initial % conditions, and the sample times and offets.sizes = simsizes; % Create the sizes structuresizes.NumContStates = 0;sizes.NumDiscStates = 0;sizes.NumOutputs = 0;sizes.NumInputs = 1;sizes.DirFeedthrough = 0;sizes.NumSampleTimes = 1;sys = simsizes(sizes); x0 = []; % There are no statesstr = []; % str is always an empty matrix % Update the figure every 0.25 sects = [0.25 0]; %initialize the array of sample times. % Initialize the figure% The handles of the disk and index mark are stored % in the block's UserData. if(findobj('UserData',gcb)) % Figure is open, do nothingelse h_fig = figure('Position',[200 200 400 300], ... 'MenuBar','none','NumberTitle','off', ... 'Resize','off', ... 'Name',[gcs,' Rolling Disk']) ; set(h_fig,'UserData',gcb) ; % Save name of current block % in the figure's UserData. % This is used to detect % that a rolling disk figure % is already open for the % current block, so that % only one instance of the % figure is open at a time % for a given instance of the % block. r = 2 ; R = 12 ; q = r ; thp = 0:0.2:pi ; xp = R*cos(thp); yp = -R*sin(thp) ; xp = [xp,-R,-(R+q),-(R+q),(R+q), (R+q),R] ; yp = [yp,0,0,-(R+q),-(R+q),0,0] ; cl_x = [0,0] ; cl_y = [0,-R] ; % Make the disk thp = 0:0.3:2.3*pi ; xd = r*cos(thp); yd = r*sin(thp) ; hd = fill(xp,yp,[0.85,0.85,0.85]); % Draw trough hold on ; % So it won't get erased set(hd,'erasemode','none'); axis('equal');axis('off'); hd0 = plot(cl_x,cl_y,'k--'); % Draw the centerline set(hd0,'erasemode','none'); % During this initialization pass, create the disk % (hd2) and the index mark (hd3). theta = 0 ; xc = (R-r)*sin(theta); % Find center of disk yc = - (R-r)*cos(theta) ; psi = theta*(R-r)/r ; xm_c = r*sin(psi) ; % Relative position of index mark ym_c = r*cos(psi) ; xm = xc + xm_c ; % Translate mark ym = yc + ym_c ; hd2 = fill(xd+xc, yd+yc, ... [0.85,0.85,0.85]) ; % Draw disk and mark hd3 = plot([xc,xm],[yc,ym],'k-'); set_param(gcb,'UserData',[hd2,hd3]) ;end%********************************************************%* mdlDerivatives *%********************************************************function sys=mdlDerivatives(t,x,u)% Compute derivatives of continuous statessys = []; % Empty since no continuous states%********************************************************%* mdlUpdate *%*******************************************************function sys=mdlUpdate(t,x,u)% Compute update for discrete states.sys = []; % Empty since this model has no states.% Update the figurer = 2 ; R = 12 ;q = r ;userdat = get_param(gcb,'UserData') ;hd2 = userdat(1) ;hd3 = userdat(2) ;theta = u(1) ; % The sole input is thetaxc = (R-r)*sin(theta); % Find center of diskyc = - (R-r)*cos(theta) ;psi = theta*(R-r)/r ;xm_c = r*sin(psi) ; % Find relative position of indexym_c = r*cos(psi) ;xm = xc + xm_c ; % Translate markym = yc + ym_c ;thp = 0:0.3:2*pi ;xd = r*cos(thp);yd = r*sin(thp) ;% Move the disk and index marks to new positionsset(hd2,'XData',xd+xc); set(hd2,'YData',yd+yc);set(hd3,'XData',[xc,xm]);set(hd3,'YData',[yc,ym]);%********************************************************%* mdlOutputs *%********************************************************function sys=mdlOutputs(t,x,u)% Compute output vector sys = [];%********************************************************%* mdlGetTimeOfNextVarHit *%********************************************************function sys=mdlGetTimeOfNextVarHit(t,x,u)sys = [];%********************************************************%* mdlTerminate *%********************************************************function sys=mdlTerminate(t,x,u)% Perform any necessary tasks at the end of the simulationsys = [];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -