Vladimir
Vladimir

Reputation: 13

Plotting the same figure in real time

So, I'm new to Simulink and have basic Matlab skills. I'm trying to plot an obstacle on the floor with a projector (which is connected as a 2nd monitor). Anyway, I'd like to plot on the same figure all the time but I'm having problems. Sometimes the figure opens when starting the simulation and sometimes it does not. I can not for the life of me, figure out why.

The basic idea of the plot is to plot and obstacle on the floor that moves towards the user at the same speed as a treadmill is moving so to make it feel like its actually on the floor. I've removed all the elements of the figure and only show a red bar as an obstacle and a black background.

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

Upvotes: 0

Views: 76

Answers (1)

m3tho
m3tho

Reputation: 602

The simulation was not executed the second time you called the function and the persistent figure opened in the previous call was left open. In short, if isempty(f) contained too much code. And drawnow helps with iterative updates of the plot in the window.

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

Upvotes: 2

Related Questions