Search code examples
matlabsimulationsimulink

Access to CPU time during simulink?


I need to run simulink and set a timeout and a penalty on time out. Hence, I need a block to give me the CPU time (real world time).

Real world time

The clock block gives the simulation time:

Simulink Clock block

The CAN timeout detection is discrete and it does not work with my continuous solver.

Simulink CAN timeout

The Matlab function blocks make simulation so slow.

Is there any alternative?


Solution

  • Typically using a real world clock in an (acceleralted) simulation is not a good idea. Here is a simple example using a m-script to set a timeout. It's primarily based on this documentation page

    model='untitled';
    timeout=10;
    
    set_param(model,'SimulationCommand','start')
    tic;
    while(true)
        if not(strcmpi(get_param(model,'SimulationStatus'),'running'))
            disp('simulation exited')
            break;
        end
        if toc>=timeout
            disp('timout reached')
            set_param(model,'SimulationCommand','stop')
            break;
        end
        pause(1);
    end
    

    If you don't like polling, this could also be implemented using callbacks, but this requires inserting a callback into your model.