实时绘制相同的数字

时间:2017-11-09 10:04:26

标签: matlab simulink

所以,我是Simulink的新手并拥有基本的Matlab技能​​。我试图用投影仪(作为第二个显示器连接)在地板上绘制障碍物。 无论如何,我想一直在同一个人物上画,但我有问题。有时,在开始模拟时图形会打开,有时则不会。我不能为我的生活找出原因。

情节的基本思想是在地板上绘制和障碍物,以与跑步机移动时相同的速度朝向用户移动,以使其感觉它实际上在地板上。我删除了图中的所有元素,只显示红色条形作为障碍物和黑色背景。

function plot_fcn(x)

persistent f 

    projectionArea=3; %3m - arbitrary, will change later
    barLength=0.35; %0.35m - arbitrary, will change later
    no_contact=true; % contact indicator
    treadmillSpeed=10/9; %4km/h = 10/9 m/s
    refreshRate= 100; % 100Hz
    obstacleIncrement=treadmillSpeed/refreshRate; % eye noticeable increment


if isempty(f)
        target=1; 
        beforeBar=1;
        p=[10;901;1680;1027.5];
        f = figure;
        set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
        set(0, 'DefaultFigurePosition', p);
        % target=x;
        while (no_contact) 

            afterBar=projectionArea-barLength-beforeBar;

            Y = [beforeBar, barLength, afterBar;
                 beforeBar, barLength, afterBar;
                 beforeBar, barLength, afterBar];

            f=area(Y);

            set(f,'EdgeColor','red');
            f(1).FaceColor = [0 0 0];
            f(2).FaceColor = [1 0 0];
            f(3).FaceColor = [0 0 0];

            if beforeBar>=projectionArea-(target+barLength/2)
                no_contact=false
            else 
                beforeBar=beforeBar+treadmillSpeed*obstacleIncrement;
                pause(obstacleIncrement)
            end
        end

        end
end

1 个答案:

答案 0 :(得分:2)

第二次调用函数时未执行模拟,并且上一次调用中打开的持久图形保持打开状态。简而言之,if isempty(f)包含太多代码。 drawnow有助于窗口中绘图的迭代更新。

function plot_fcn()

persistent f

projectionArea=3; %3m - arbitrary, will change later
barLength=0.35; %0.35m - arbitrary, will change later
no_contact=true; % contact indicator
treadmillSpeed=10/9; %4km/h = 10/9 m/s
refreshRate= 100; % 100Hz
obstacleIncrement=treadmillSpeed/refreshRate; % eye noticeable increment

target=1;
beforeBar=1;
if isempty(f) || ~ishandle(f) || ~isa(f,'matlab.ui.Figure')
    f = figure;
    set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
else
    delete(findobj(f,'type','Area'))
end
figure(f); %focus window
while (no_contact)
    afterBar=projectionArea-barLength-beforeBar;

    Y = [beforeBar, barLength, afterBar;
        beforeBar, barLength, afterBar;
        beforeBar, barLength, afterBar];

    aH=area(Y);

    set(aH,'EdgeColor','red');
    aH(1).FaceColor = [0 0 0];
    aH(2).FaceColor = [1 0 0];
    aH(3).FaceColor = [0 0 0];

    if beforeBar>=projectionArea-(target+barLength/2)
        no_contact=false;
    else
        beforeBar=beforeBar+treadmillSpeed*obstacleIncrement;
        pause(obstacleIncrement)
    end

    if isempty(f) || ~ishandle(f) || ~isa(f,'matlab.ui.Figure')
        f = figure;
        set(f, 'MenuBar', 'none', 'ToolBar', 'none', 'Color','black');
    else
        figure(f); %focus window
        drawnow
    end

end