Scheduling

This documentation show how to schedule tasks when running a MATLAB Function or Simulink Model directly on a desktop computer.

Contents

Introduction

These examples show how to schedule tasks while running a MATLAB Function or Simulink Model directly on a desktop computer.

The alternative to running MATLAB Functions and Simulink Models directly is to generate code or executables out of the MATLAB Functions and Simulink Models. These executables can be run on the desktop computer in or out of MATLAB. This executable deployment method is covered in the following module Deploying an Executable to a Desktop Computer. All scheduling methods listed here are supported for code generation except "Timer Callbacks".

The functions and blocks used to implement scheduling in MATLAB and Simulink are:

MATLAB Examples

MATLAB Free Run

The quickest way get started running algorithms in MATLAB is to run it as fast as possible in a loop.

Open Example

Using MATLAB, this example uses "while loops" to execute the tasks. The resulting algorithm executes as quickly as possible.

Etic and Etoc

The most basic form of scheduling is to use a tic and toc method to run a task at a certain frequency.

Open Example

Using MATLAB, this example uses the provided functions, etic and etoc, to execute tasks at a certain rate.

Timer Callbacks

A more advanced form of scheduling is to use a Timer Object. Timer Objects allows the user to parallelize tasks since each timer object runs on its own thread. Callbacks can be set up with each Timer Object to run a task at a certain rate.

Open Example

Using MATLAB, this example uses timer objects to execute tasks at a certain rate. This is the recommended way to time your tasks in MATLAB.

The only limitation is that timer objects is not supported for code generation, and therefore cannot be used when deploying MATLAB Functions to an executable.

Simulink Examples

Simulink is designed to enable Model Based Design workflows by allowing users to easily simulate and generate code using the same model. In doing so, users are able to verify that algorithms work before deploying to hardware.

By default, a model is set up to simulate a system as quickly as possible. We can use this simulation mode to run our algorithms.

Open Example

Using Simulink's Normal Simulation mode, this model prints to the screen as fast as possible. We could just as easily set this up to acquire video and view video, transmit and receive UDP data, and a variety of other robotics related tasks.

Soft Real Time block

There are a couple of reasons why we may not want to run an algorithm as often as possible. Some algorithms are time dependent and need to be run at a certain rate. The Soft Real Time block that has been provided by this getting started guide can be used to run a Simulink Model at a certain rate in Soft Real-Time.

The following example prints to the Command Window rate specified by the Digital Clock block.

Open Example