MATLAB 动画仿真

MATLAB 动画仿真


1 利用MATLAB进行图形处理

  matlab调用画图plot函数族,返回该函数的句柄,通过句柄可以修改图形的属性。
t = 0:20;
plot_handle = plot(t, sin(t));

这里写图片描述

// 修改图形属性
set(plot_handle, 'YData', cos(t));

这里写图片描述

plot_handle1 = plot(t,sin(t));
hold on;
plot_handle2 = plot(t, cos(t));
set(plot_handle2,'YData', cos(2*t));
总结:fill命令来模拟倒立摆

查看plot函数的属性:
get(plot_handle)
AlignVertexCenters: ‘off’
Annotation: [1x1 matlab.graphics.eventdata.Annotation]
BeingDeleted: ‘off’
BusyAction: ‘queue’
ButtonDownFcn: ”
Children: []
Clipping: ‘on’
Color: [0 0.4470 0.7410]
CreateFcn: ”
DeleteFcn: ”
DisplayName: ”
HandleVisibility: ‘on’
HitTest: ‘on’
Interruptible: ‘on’
LineStyle: ‘-’
LineWidth: 0.5000
Marker: ‘none’
MarkerEdgeColor: ‘auto’
MarkerFaceColor: ‘none’
MarkerSize: 6
Parent: [1x1 Axes]
PickableParts: ‘visible’
Selected: ‘off’
SelectionHighlight: ‘on’
Tag: ”
Type: ‘line’
UIContextMenu: []
UserData: []
Visible: ‘on’
XData: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
XDataMode: ‘manual’
XDataSource: ”
YData: [1x21 double]
YDataSource: ”
ZData: [1x0 double]
ZDataSource: ”


2 动画举例:倒立摆

对于倒立摆的数学建模参考建模方程
这里写图片描述
倒立摆的四个角:

( y + w / 2 , g ) , ( y + w / 2 , g + h ) , ( y w / 2 , g + h ) ( y w / 2 , g )

杆的两端:
( y , g + h ) y + L sin θ , g + h + L cos θ )

matlab画出图形:
利用drawBase画图形,画杆和板子(几座)的图形

function handle = drawBase(y, width, height, gap, handle, mode)
    X = [y - width/2, y + width/2, y + width/2; y - width/2];
    Y = [gap, gap, gap+height, gap+height];

    if isempty(handle)
        handle = fill(X,Y,'m','EraseMode', mode);
    else
        set(handle,'XData',X,'YData',Y);
end

handle既做输入也做输出

绘制杆的代码:

function handle = drawRod(y, theta, L, gap, height, handle, mode)
   X = [y, y+L*sin(theta)];
   Y = [gap+height, gap + height + L*cos(theta)];

   if isempty(handle)
       handle = plot(X, Y, 'g', 'EraseMode', mode);
   else
       set(handle,'XData',X,'YData',Y);
end

EraseMode擦出模式:normal、non、或者background

function drawPendulum(u,L,gap,width,height)

    % process inputs to function
    y        = u(1);
    theta    = u(2);
    t        = u(3);

    % define persistent variables 
    persistent base_handle
    persistent rod_handle

    % first time function is called, initialize plot and persistent vars
    if t==0,
        figure(1), clf
        track_width=3;
        plot([-track_width,track_width],[0,0],'k'); % plot track
        hold on
        base_handle = drawBase(y, width, height, gap, [], 'normal');
        rod_handle  = drawRod(y, theta, L, gap, height, [], 'normal');
        axis([-track_width, track_width, -L, 2*track_width-L]);


    % at every other time step, redraw base and rod
    else 
        drawBase(y, width, height, gap, base_handle);
        drawRod(y, theta, L, gap, height, rod_handle);
    end
end


%
%=======================================================================
% drawBase
% draw the base of the pendulum
% return handle if 3rd argument is empty, otherwise use 3rd arg as handle
%=======================================================================
%
function handle = drawBase(y, width, height, gap, handle, mode)

  X = [y-width/2, y+width/2, y+width/2, y-width/2];
  Y = [gap, gap, gap+height, gap+height];

  if isempty(handle),
    handle = fill(X,Y,'m', 'EraseMode', mode);
    %handle = plot(X,Y,'m', 'EraseMode', mode);
  else
    set(handle,'XData',X,'YData',Y);
    drawnow
  end
end

%
%=======================================================================
% drawRod
% draw the pendulum rod
% return handle if 3rd argument is empty, otherwise use 3rd arg as handle
%=======================================================================
%
function handle = drawRod(y, theta, L, gap, height, handle, mode)


  X = [y, y+L*sin(theta)];
  Y = [gap+height, gap + height + L*cos(theta)];

  if isempty(handle),
    handle = plot(X, Y, 'g', 'EraseMode', mode);
  else
    set(handle,'XData',X,'YData',Y);
    drawnow
  end
end

3 动画举例:绘制航天器

function drawSpacecraft(uu,pts)

    % process inputs to function
    pn       = uu(1);       % inertial North position     
    pe       = uu(2);       % inertial East position
    pd       = uu(3);       % inertial Down position
    u        = uu(4);       % body frame velocities
    v        = uu(5);       
    w        = uu(6);       
    phi      = uu(7);       % roll angle         
    theta    = uu(8);       % pitch angle     
    psi      = uu(9);       % yaw angle     
    p        = uu(10);      % roll rate
    q        = uu(11);      % pitch rate     
    r        = uu(12);      % yaw rate    
    t        = uu(13);      % time

    % define persistent variables 
    persistent spacecraft_handle;

    % first time function is called, initialize plot and persistent vars
    if t<=0.1,
        figure(1), clf
        spacecraft_handle = drawSpacecraftBody(pts,pn,pe,pd,phi,theta,psi, [], 'normal');
        title('Spacecraft')
        xlabel('East')
        ylabel('North')
        zlabel('-Down')
        view(32,47)  % set the vieew angle for figure
        axis([-10,10,-10,10,-10,10]);
        hold on

    % at every other time step, redraw base and rod
    else 
        drawSpacecraftBody(pts,pn,pe,pd,phi,theta,psi, spacecraft_handle);
    end
end


%=======================================================================
% drawSpacecraftBody
% return handle if 7th argument is empty, otherwise use 7th arg as handle
%=======================================================================
%
function handle = drawSpacecraftBody(NED,pn,pe,pd,phi,theta,psi, handle, mode)

  NED = rotate(NED,phi,theta,psi); % rotate spacecraft by phi, theta, psi
  NED = translate(NED,pn,pe,pd); % translate spacecraft
  % transform vertices from NED to XYZ (for matlab rendering)
   R = [...
       0, 1, 0;...
       1, 0, 0;...
       0, 0, -1;...
       ];
   XYZ = R*NED;

  % plot spacecraft
  if isempty(handle),
    handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'EraseMode', mode);
  else
    set(handle,'XData',XYZ(1,:),'YData',XYZ(2,:),'ZData',XYZ(3,:));
    drawnow
  end
end


%%%%%%%%%%%%%%%%%%%%%%%
function XYZ=rotate(XYZ,phi,theta,psi)
  % define rotation matrix
  R_roll = [...
          1, 0, 0;...
          0, cos(phi), -sin(phi);...
          0, sin(phi), cos(phi)];
  R_pitch = [...
          cos(theta), 0, sin(theta);...
          0, 1, 0;...
          -sin(theta), 0, cos(theta)];
  R_yaw = [...
          cos(psi), -sin(psi), 0;...
          sin(psi), cos(psi), 0;...
          0, 0, 1];
  R = R_yaw*R_pitch*R_roll;

  % rotate vertices
  XYZ = R*XYZ;

end
% end rotateVert

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% translate vertices by pn, pe, pd
function XYZ = translate(XYZ,pn,pe,pd)

  XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));

end

% end translateXYZ

这里写图片描述


4 动画举例:使用顶点和面的航天器

function drawSpacecraft(uu,V,F,patchcolors)

    % process inputs to function
    pn       = uu(1);       % inertial North position     
    pe       = uu(2);       % inertial East position
    pd       = uu(3);           
    u        = uu(4);       
    v        = uu(5);       
    w        = uu(6);       
    phi      = uu(7);       % roll angle         
    theta    = uu(8);       % pitch angle     
    psi      = uu(9);       % yaw angle     
    p        = uu(10);       % roll rate
    q        = uu(11);       % pitch rate     
    r        = uu(12);       % yaw rate    
    t        = uu(13);       % time

    % define persistent variables 
    persistent spacecraft_handle;

    % first time function is called, initialize plot and persistent vars
    if t==0,
        figure(1), clf
        spacecraft_handle = drawSpacecraftBody(V,F,patchcolors,...
                                               pn,pe,pd,phi,theta,psi,...
                                               [],'normal');
        title('Spacecraft')
        xlabel('East')
        ylabel('North')
        zlabel('-Down')
        view(32,47)  % set the vieew angle for figure
        axis([-10,10,-10,10,-10,10]);
        hold on

    % at every other time step, redraw base and rod
    else 
        drawSpacecraftBody(V,F,patchcolors,...
                           pn,pe,pd,phi,theta,psi,...
                           spacecraft_handle);
    end
end


%=======================================================================
% drawSpacecraft
% return handle if 3rd argument is empty, otherwise use 3rd arg as handle
%=======================================================================
%
function handle = drawSpacecraftBody(V,F,patchcolors,...
                                     pn,pe,pd,phi,theta,psi,...
                                     handle,mode)
  V = rotate(V', phi, theta, psi)';  % rotate spacecraft
  V = translate(V', pn, pe, pd)';  % translate spacecraft
  % transform vertices from NED to XYZ (for matlab rendering)
  R = [...
      0, 1, 0;...
      1, 0, 0;...
      0, 0, -1;...
      ];
  V = V*R;

  if isempty(handle),
  handle = patch('Vertices', V, 'Faces', F,...
                 'FaceVertexCData',patchcolors,...
                 'FaceColor','flat',...
                 'EraseMode', mode);
  else
    set(handle,'Vertices',V,'Faces',F);
    drawnow
  end
end

%%%%%%%%%%%%%%%%%%%%%%%
function XYZ=rotate(XYZ,phi,theta,psi);
  % define rotation matrix
  R_roll = [...
          1, 0, 0;...
          0, cos(phi), -sin(phi);...
          0, sin(phi), cos(phi)];
  R_pitch = [...
          cos(theta), 0, sin(theta);...
          0, 1, 0;...
          -sin(theta), 0, cos(theta)];
  R_yaw = [...
          cos(psi), -sin(psi), 0;...
          sin(psi), cos(psi), 0;...
          0, 0, 1];
  R = R_yaw*R_pitch*R_roll;
  % rotate vertices
  XYZ = R*XYZ;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% translate vertices by pn, pe, pd
function XYZ = translate(XYZ,pn,pe,pd)
  XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
end

这里写图片描述

猜你喜欢

转载自blog.csdn.net/CCDreamOldBoys/article/details/80732652
今日推荐