小车单摆的Matlab动画仿真S函数
(2008-10-08 23:52:49)
标签:
matlab单摆动画s函数it |
分类: Matlab实例 |
程序的主体用了Matlab Central的博客Seth on
Simulink提供的函数,对其中的画图部分及参数设置做了修改,可接入Simulink模型仿真,后续给出例子。
function [sys,x0,str,ts] =
SimpPendAnimation(t,x,u,flag,pausetime)
%=====================================================================
%小车单摆 S-function.
%参考seth的倒立摆动画模型,做了一些修改。
%author:xianfa110
%http://blog.sina.com.cn/xianfa110
%=====================================================================
switch flag,
end
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the
S-function.
%=============================================================================
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates
sizes.NumDiscStates
sizes.NumOutputs
sizes.NumInputs
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0
str = [];
ts
LocalPendInit;
%=============================================================================
% mdlUpdate
% Update the pendulum animation.
%=============================================================================
function sys=mdlUpdate(t,x,u,pausetime)
fig = get_param(gcbh,'UserData');
if ishandle(fig),
end;
sys = [];
% end mdlUpdate
%=============================================================================
% LocalPendSets
% Local function to set the position of the graphics objects in
the
% inverted pendulum animation window.
%=============================================================================
function LocalPendSets(time,ud,u,pausetime)
u = u([1 2]);
XDelta
XPendTop = u(1) + 10*sin(u(2));
YPendTop = -10*cos(u(2));
set(ud.Cart,...
set(ud.Pend,...
set(ud.Head,...
set(ud.TimeField,...
% Force plot to be drawn
pause(pausetime);
drawnow;
% end LocalPendSets
%
%=============================================================================
% LocalPendInit
% Local function to initialize the pendulum animation.
% window already exists, it is brought to the front.
% figure window is created.
%=============================================================================
function LocalPendInit
sys = get_param(gcs,'Parent');
TimeClock = 0;
RefSignal = 0;
XCart
Theta
XDelta
XPendTop
YPendTop
Fig = get_param(gcbh,'UserData');
if ishandle(Fig),
end
FigureName = 'Metronome Visualization';
Fig = figure(...
AxesH = axes(...
Cart = surface(...
line([-10,10],[0,0],'linewidth',3);
Pend =
line([XCart,XPendTop],[1,YPendTop],'color','b','linestyle','-','linewidth',2,'erasemode','xor');
Head = line(XPendTop,YPendTop,'color','r','linestyle','.',...
uicontrol(...
TimeField = uicontrol(...

加载中…