如何在工作区图上运行Simulink仿真?

时间:2015-12-25 14:24:39

标签: matlab simulink

我正在图中绘制一个3d多边形,然后我想要一个Simulink四旋翼飞行器在同一个数字上飞行。

我试图"抓住",所以它在图的顶部保持相同的图形名称,但不显示多边形。

下图显示了我首先绘制的多边形,然后是模拟的四旋翼飞行。

Polygon

Quadrotor

Update-1:这是代码,为了测试我正在尝试一个非常简单的代码:

figure('Name','Test-1','NumberTitle','off')
grid on;
hold on;

voxel([0, 0, 0],[0.5, 0.5, 10],'r',0.4);

axis([0 5 0 5 0 15]);

%x values of the path
xVals = [1 -2; 5 -1; 10 0; 15 1];
%y values of the path
yVals = [1 2; 5 1; 10 0; 15 -1];

sl_quadrotorCR;

sim('sl_quadrotorCR');

这是我正在使用的Voxel代码的主要部分(http://www.mathworks.com/matlabcentral/fileexchange/3280-voxel):

function voxel(i,d,c,alpha);

%VOXEL function to draw a 3-D voxel in a 3-D plot
%
%Usage
%   voxel(start,size,color,alpha);
%
%   will draw a voxel at 'start' of size 'size' of color 'color' and
%   transparency alpha (1 for opaque, 0 for transparent)
%   Default size is 1
%   Default color is blue
%   Default alpha value is 1
%
%   start is a three element vector [x,y,z]
%   size the a three element vector [dx,dy,dz]
%   color is a character string to specify color 
%       (type 'help plot' to see list of valid colors)
%   
%
%   voxel([2 3 4],[1 2 3],'r',0.7);
%   axis([0 10 0 10 0 10]);
%

%   Suresh Joel Apr 15,2003
%           Updated Feb 25, 2004

switch(nargin),
case 0
    disp('Too few arguements for voxel');
    return;
case 1
    l=1;    %default length of side of voxel is 1
    c='b';  %default color of voxel is blue
case 2,
    c='b';
case 3,
    alpha=1;
case 4,
    %do nothing
otherwise
    disp('Too many arguements for voxel');
end;

x=[i(1)+[0 0 0 0 d(1) d(1) d(1) d(1)]; ...
        i(2)+[0 0 d(2) d(2) 0 0 d(2) d(2)]; ...
        i(3)+[0 d(3) 0 d(3) 0 d(3) 0 d(3)]]';



for n=1:3,

    if n==3,
        x=sortrows(x,[n,1]);
    else
        x=sortrows(x,[n n+1]);
    end;


    temp=x(3,:);
    x(3,:)=x(4,:);
    x(4,:)=temp;
    h=patch(x(1:4,1),x(1:4,2),x(1:4,3),c);
    set(h,'FaceAlpha',alpha);
    temp=x(7,:);
    x(7,:)=x(8,:);
    x(8,:)=temp;
    h=patch(x(5:8,1),x(5:8,2),x(5:8,3),c);
    set(h,'FaceAlpha',alpha);
end;   

更新-2:

以下是Simulink绘图代码:

% Copyright (C) 1993-2014, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for Matlab (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB.  If not, see <http://www.gnu.org/licenses/>.

function [sys,x0,str,ts] = quadrotor_plot(t,x,u,flag,s,plot,enable,vehicle)

% Flyer plot, lovingly coded by Paul Pounds, first coded 17/4/02
% version 2 2004 added scaling and ground display
% version 3 2010 improved rotor rendering and fixed mirroring bug
%
% Displays X-4 flyer position and attitude in a 3D plot.
% GREEN ROTOR POINTS NORTH
% BLUE ROTOR POINTS EAST

% PARAMETERS
% s defines the plot size in meters
% swi controls flyer attitude plot; 1 = on, otherwise off.

% INPUTS
% 1 Center X position
% 2 Center Y position
% 3 Center Z position
% 4 Yaw angle in rad
% 5 Pitch angle in rad
% 6 Roll angle in rad

% OUTPUTS
%   None
ts = [-1 0];

if ~isfield(vehicle, 'nrotors')
    vehicle.nrotors = 4;    % sensible default for quadrotor function
end

switch flag,
    case 0
        [sys,x0,str,ts] = mdlInitializeSizes(ts,plot,enable); % Initialization
    case 3
        sys = mdlOutputs(t,u,s,plot,enable, vehicle); % Calculate outputs
    case {1,2, 4, 9} % Unused flags
        sys = [];
    otherwise
        error(['unhandled flag = ',num2str(flag)]); % Error handling
end


% Initialize
function [sys,x0,str,ts] = mdlInitializeSizes(ts,plot,enable)
% Call simsizes for a sizes structure, fill it in, and convert it
% to a sizes array.
sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 0;
sizes.NumInputs      = 6;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0  = [];
str = [];          % Set str to an empty matrix.
ts = [0.05 0];

if enable == 1
    figure(plot);
    clf;
    %colordef(1,'none');
end
% End of mdlInitializeSizes.


function sys = mdlOutputs(t,u,s, plot, enable, quad)
global a1s b1s

% not quite sure what this is about -- PIC
if numel(a1s) == [0];
    a1s = zeros(1, quad.nrotors);
    b1s = zeros(1, quad.nrotors);
end

% vehicle dimensons
d = quad.d; %Hub displacement from COG
r = quad.r; %Rotor radius

for i = 1:quad.nrotors
    theta = (i-1)/quad.nrotors*2*pi;
    %   Di      Rotor hub displacements (1x3)
    % first rotor is on the x-axis, clockwise order looking down from above
    D(:,i) = [ d*cos(theta); d*sin(theta); 0];
    scal = s(1)/4;
    %Attitude center displacements
    C(:,i) = [ scal*cos(theta); scal*sin(theta); 0];
end

if enable == 1
    %draw ground
    figure(plot);
    clf;
    if length(s) == 1
        axis([-s s -s s 0 s]);
    else
        axis([-s(1) s(1) -s(1) s(1) 0 s(2)])
        s = s(1);
    end
    hold on;

    % plot the ground boundaries and the big cross
    plot3([-s -s],[s -s],[0 0],'-b')
    plot3([-s s],[s s],[0 0],'-b')
    plot3([s -s],[-s -s],[0 0],'-b')
    plot3([s s],[s -s],[0 0],'-b')
    plot3([s -s],[-s s],[0 0],'-b')
    plot3([-s s],[-s s],[0 0],'-b')

    %READ STATE
    z = [u(1);u(2);u(3)];
    n = [u(4);u(5);u(6)];

    %PREPROCESS ROTATION MATRIX
    phi = n(1);    %Euler angles
    the = n(2);
    psi = n(3);

    R = [cos(the)*cos(phi) sin(psi)*sin(the)*cos(phi)-cos(psi)*sin(phi) cos(psi)*sin(the)*cos(phi)+sin(psi)*sin(phi);   %BBF > Inertial rotation matrix
        cos(the)*sin(phi) sin(psi)*sin(the)*sin(phi)+cos(psi)*cos(phi) cos(psi)*sin(the)*sin(phi)-sin(psi)*cos(phi);
        -sin(the)         sin(psi)*cos(the)                            cos(psi)*cos(the)];

    %Manual Construction
    %Q3 = [cos(psi) -sin(psi) 0;sin(psi) cos(psi) 0;0 0 1];   %Rotation mappings
    %Q2 = [cos(the) 0 sin(the);0 1 0;-sin(the) 0 cos(the)];
    %Q1 = [1 0 0;0 cos(phi) -sin(phi);0 sin(phi) cos(phi)];
    %R = Q3*Q2*Q1;    %Rotation matrix

    %CALCULATE FLYER TIP POSITONS USING COORDINATE FRAME ROTATION
    F = [1 0 0;0 -1 0;0 0 -1];

    %Draw flyer rotors
    t = [0:pi/8:2*pi];
    for j = 1:length(t)
        circle(:,j) = [r*sin(t(j));r*cos(t(j));0];
    end

    for i = 1:quad.nrotors
        hub(:,i) = F*(z + R*D(:,i)); %points in the inertial frame

        q = 1; %Flapping angle scaling for output display - makes it easier to see what flapping is occurring
        Rr = [cos(q*a1s(i))  sin(q*b1s(i))*sin(q*a1s(i)) cos(q*b1s(i))*sin(q*a1s(i));   %Rotor > Plot frame
            0              cos(q*b1s(i))               -sin(q*b1s(i));
            -sin(q*a1s(i)) sin(q*b1s(i))*cos(q*a1s(i)) cos(q*b1s(i))*cos(q*a1s(i))];

        tippath(:,:,i) = F*R*Rr*circle;
        plot3([hub(1,i)+tippath(1,:,i)],[hub(2,i)+tippath(2,:,i)],[hub(3,i)+tippath(3,:,i)],'b-')
    end

    %Draw flyer
    hub0 = F*z;  % centre of vehicle
    for i = 1:quad.nrotors
        % line from hub to centre plot3([hub(1,N) hub(1,S)],[hub(2,N) hub(2,S)],[hub(3,N) hub(3,S)],'-b')
        plot3([hub(1,i) hub0(1)],[hub(2,i) hub0(2)],[hub(3,i) hub0(3)],'-b')

        % plot a circle at the hub itself
        plot3([hub(1,i)],[hub(2,i)],[hub(3,i)],'o')
    end

    % plot the vehicle's centroid on the ground plane
    plot3([z(1) 0],[-z(2) 0],[0 0],'--k')
    plot3([z(1)],[-z(2)],[0],'xk')

    % label the axes
    xlabel('x');
    ylabel('y');
    zlabel('z (height above ground)');
end

sys = [];
% End of mdlOutputs.

0 个答案:

没有答案