Generate Code for Simulink Model Using MLToolkit

This example shows how to generate code for a Simulink® model using the MLToolkit.

The MLToolkit allows you to easily generate SimWB-compliant code from a Simulink model. It provides you the convenience of generating this code without modifying the Simulink model to include hardware-specific S-Function blocks. Instead, the software automatically inserts RTBD-access-specific blocks during code generation. Use the generated code to simulate the model in real time using SimWB, which supports a wide variety of I/Os for model simulation.

Contents

Prerequisites

Install SimWB and MLToolkit

This example requires the MLToolkit and SimWB Real-Time core. To confirm that this software is installed, open any Simulink®model. The SimWB option must be visible in the menu bar.
For MATLAB® versions R2019a and earlier:

For MATLAB® versions R2019b and later:

SimWB Target menu item 2019 and later

Start Configuration Server Process

Before using the MLToolkit, you must start the Real-Time core process, cfgsrv, on the real-time host. The cfgsrv process is an interface required to configure, set up, and run a Simulink model in real time. For more information about this process, see Configuration Server (cfgsrv).

Automotive Suspension Model

This example uses a copy of the sldemo_suspn Simulink® model, which describes the suspension of an automotive. For more information about this model, see Automotive Suspension in Simulink documentation.

Open the model. Type sldemo_suspn at the MATLAB command prompt.

Create a copy of this model. Save this copy model as my_sldemo_suspn.mdl.

The remainder of this tutorial will use the copy model. So, close the sldemo_suspn model, and open the copy, my_sldemo_suspn, instead.

Specify Fixed-Step Solver for Model

To generate code for a Simulink® model, it must use a fixed-step solver. To specify a fixed-step solver in the Simulink editor, click Simulation > Model Configuration Parameters. In the Select pane (left-hand side) of the Configuration Parameters dialog box, select Solver. In the Solver Options pane (right-hand side), select Fixed-step in the Type menu. In the Fixed-step size box, enter 0.01.

Step 1: Prepare Signals of Interest in Simulink® Model

To access signals in a Simulink model in real time using external hardware, ensure that these signals are connected to source (inputs) or sink (outputs) blocks. These blocks are mapped to RTDB inputs/outputs.

If necessary, add the relevant blocks to the model. Specify names for these source/sink blocks that are valid C variable names. Block names that contain spaces cannot be included in the real-time database. So, you must eliminate spaces from the names of blocks of interest.

For example, consider the following signals:

Input Signals

Output Signals

The modified model is as follows:

Step 2: Connect Model to Real-Time Host

Open the MLToolkit GUI. In the menu bar of the Simulink Editor for the model, click SimWB > Toolkit.

In the Simulator Access tab, specify the following:

Click Connect.

When the model connects to the real-time host successfully, the connection icon status changes as follows:

In the Project Properties pane, use the default values for Access Group and Project Folder.

Step 3: Create RTDB for Simulink Model

Click theRTDB Creator tab. Specify the following:

Click Create and Upload RTDB. The RTDB Append/Overwrite dialog box opens.

Click Overwrite. This option overwrites any existing RTDB on the real-time host that has the same name as specified in the RTDB box.

A text file, signal.db, is created in the current folder. This file contains the RTDB variable definitions. The software uploads this file to the real-time host. So, when you use the SIMulation Workbench Control Center after generating the code, this model is available in the GUI.

Step 4: Generate SimWB-Compliant Code

Click theCode Generator tab. Specify the following:

Click Generate, Export, and Make. The software generates SimWB-compliant code and a real-time executable, and uploads the same to the real-time host. The following image shows a snippet of the generated output.

The software checks if the model is compliant with the selected RTDB before it generates SimWB-compliant code. The check ensures that source and sink blocks in the model uniquely map to variables in the RTDB. A block maps to an RTDB variable when they have the same name, data type, and dimension. The model must pass the compliance check for code generation.

Note: You cannot use the Simulink Coder keyboard shortcut, Ctrl + B, to generate SimWB-compliant code. You can use only the MLToolkit GUI to generate this code.

Step 5: Launch SIMulation Workbench Control Center

Launch the SIMulation Workbench Control Center. Use this tool to set up tests, configure the RTDB, run/playback the model, and create custom HMIs for testing the model.

Related Links

Videos

Targeting SimWB real-time host from Simulink