Simulink

From robotics


Simulink logo by Mathworks

Simulink is a graphical programming version of MATLAB and can be opened by typing in ‘simulink’ into the MATLAB command window.

Simulink Interface

This opens the Simulink Library Browser which contains all the blocks for Simulink programming. To create a new Simulink model, go to File->New->Model in the Simulink Library Browser. The blocks are separated into different categories. If you aren’t sure which category a desired block is in, type it into the search bar (circled above).

Model Window

The model window contains a variety of options for modifying the simulation. The library browser button (blue circle below) opens the library browser for adding blocks. The play button starts the simulation. The bar next to it (red circle) indicates how many seconds the simulation will represent. When the simulation is running, it can be paused or stopped by the user. Note: If the simulation is set to a certain number of seconds, that is not the number of seconds it will run for, just how many it will represent. The time it takes a simulation to run depends on the complexity of the system.

In the Library Browser, the commonly used blocks contain a variety of blocks with simple functions such as constants, summers, products, etc. To add blocks, simply drag and drop onto the model, or double-click.

To connect blocks, click on a triangle next to one block and connect it to a triangle on the block you want to connect to (triangles are circled below). Triangles pointing into blocks are inputs and triangles pointing out are outputs. Inputs can only connect to outputs and outputs can only connect to inputs. Each arrow can only have one input or output, but outputs can be separated later (See Mux and Demux).

Blocks!

Reading Outputs

Once the simulation is created, values may have to be read at different points. A common block to help with this is the “scope” block, which shows the value of an input over time. To see these values, double-click the scope and a window will appear with the same name as the scope.

Scope Interface

In this example, the input to the scope is always 1*1+1=2. A common way to make the scope easier to read is pressing the “autoscale” button (circled above). This will adjust the y-axis based on the values. Specific parameters of blocks, such as constants, can be changed by double-clicking the block.

Mux and Dexmux

Inputs and outputs are not necessarily single values. The Mux and Demux blocks can combine variables into vectors and separate vectors into variables respectively.

Mux and Demux example

In the model above, two values are muxed together and sent to scope “scope.” The variables are then demuxed and sent to “scope1” and “scope2.”

Adding Matlab functions

MATLAB functions can be added into simulations. This is sometimes easier than creating a series of blocks. Use the MATLAB function block in the library and double-click it, causing an empty function to appear in a text editor. The number of inputs and outputs can easily be customized, as well as the name of the function. Remember: the name of the function should match the file name.

Function block example 2

The MATLAB function block can be treated as any other command block within the simulation. Note: MATLAB functions blocks can slow the program more than command blocks, so this method is not always recommended.

Organizing your model

Eventually a Simulink model could become too complicated to read easily. To solve this, there are three suggestions:

  • Move the blocks
Moving the blocks based on factors such as the order of the program can make the code easier to read. The blocks can also be resized.
  • Adjust the arrows
The arrows connecting the blocks must always have the same start and endpoints, but the actual shape of the arrow is easily adjusted. Move the mouse over an arrow, then click and drag the arrow into the desired shape.
  • Group blocks into simulations
Blocks can be grouped into sub-functions so they only appear as a single block on the main model. To do this, highlight the blocks and arrows you want in the subsystem and press “Ctrl-G.” This will create a subsystem with the same functions as that combination of blocks. You can also double-click the sub-function to open it and edit it directly.
Mux and Demux example