matlab2018单摆动画程序,小车单摆的Matlab动画仿真S函数

程序的主体用了Matlab Central的博客Seth on

Simulink提供的函数,对其中的画图部分及参数设置做了修改,可接入Simulink模型仿真,后续给出例子。

a4c26d1e5885305701be709a3d33442f.png

function [sys,x0,str,ts] =

SimpPendAnimation(t,x,u,flag,pausetime)

%=====================================================================

%小车单摆 S-function.

%参考seth的倒立摆动画模型,做了一些修改。

%author:xianfa110

%http://blog.sina.com.cn/xianfa110

%=====================================================================

switch flag,

case 0,

[sys,x0,str,ts]=mdlInitializeSizes;

case 2,

sys=mdlUpdate(t,x,u,pausetime);

case { 1, 3, 4, 9 },

sys = [];

otherwise

error(['Unhandled flag =

',num2str(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 = 0;

sizes.NumDiscStates = 0;

sizes.NumOutputs = 0;

sizes.NumInputs = 2;

sizes.DirFeedthrough = 1;

sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

x0 = [];

str = [];

ts = [0.1 0];

LocalPendInit;

%=============================================================================

% mdlUpdate

% Update the pendulum animation.

%=============================================================================

function sys=mdlUpdate(t,x,u,pausetime)

fig = get_param(gcbh,'UserData');

if ishandle(fig),

if strcmp(get(fig,'Visible'),'on'),

ud = get(fig,'UserData');

LocalPendSets(t,ud,u,pausetime);

end

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 = 2;

XPendTop = u(1) + 10*sin(u(2));

YPendTop = -10*cos(u(2));

set(ud.Cart,...

'XData',ones(2,1)*[u(1)-XDelta u(1)+XDelta]);

set(ud.Pend,...

'XData',[u(1),XPendTop],...

'YData',[1,YPendTop]);

set(ud.Head,...

'XData',XPendTop,...

'YData',YPendTop);

set(ud.TimeField,...

'String',num2str(time));

% Force plot to be drawn

pause(pausetime);

drawnow;

% end LocalPendSets

%

%=============================================================================

% LocalPendInit

% Local function to initialize the pendulum animation. If the

animation

% window already exists, it is brought to the front. Otherwise, a new

% figure window is created.

%=============================================================================

function LocalPendInit

sys = get_param(gcs,'Parent');

TimeClock = 0;

RefSignal = 0;

XCart = 0;

Theta = 0;

XDelta = 2;

XPendTop = XCart + 10*sin(Theta);

YPendTop = -10*cos(Theta);

Fig = get_param(gcbh,'UserData');

if ishandle(Fig),

FigUD = get(Fig,'UserData');

set(FigUD.TimeField,...

'String',num2str(TimeClock));

set(FigUD.Cart,...

'XData',ones(2,1)*[XCart-XDelta

XCart+XDelta]); % bring it to the front

figure(Fig);

return

end

FigureName = 'Metronome Visualization';

Fig = figure(...

'Units', 'pixel',...

'Position', [100

100 500 300],...

'Name', FigureName,...

'NumberTitle', 'off',...

'IntegerHandle', 'off',...

'Resize', 'off');

AxesH = axes(...

'Parent', Fig,...

'Units', 'pixel',...

'Position',[50 50 400 200],...

'CLim', [1 64], ...

'Xlim', [-12 12],...

'Ylim', [-10 2],...

'Visible', 'off');

Cart = surface(...

'Parent', AxesH,...

'XData', ones(2,1)*[XCart-XDelta

XCart+XDelta],...

'YData', [0 0; 2 2],...

'ZData', zeros(2),...

'CData', ones(2),...

'EraseMode','xor');

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','.',...

'erasemode','xor','markersize',40);

uicontrol(...

'Parent', Fig,...

'Style', 'text',...

'Units', 'pixel',...

'Position', [150 0 100 25], ...

'HorizontalAlignment','right',...

'String', 'Time: ');

TimeField = uicontrol(...

'Parent', Fig,...

'Style', 'text',...

'Units', 'pixel', ...

'Position', [250 0 100 25],...

'HorizontalAlignment','left',...

'String',

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值