Main Content

External Mode Simulation by Using XCP Communication

In an external mode simulation, you can tune parameters in real time and monitor target application signals. Using the Run on Hardware Board app, you can set up and run external mode simulations that use an XCP communication channel.

For an external mode simulation, you:

  1. Build the target application on your development computer.

  2. Deploy the target application to the target hardware.

  3. Connect Simulink® to the target application that runs on the target hardware.

  4. Start execution of generated code on the target hardware.

Run XCP External Mode Simulation on Development Computer

Configure and run an external mode simulation that uses the XCP communication protocol. During the simulation:

  • Monitor a signal by using a Scope block, a Dashboard block, and the Simulation Data Inspector.

  • Tune a parameter by using a Dashboard block.

Configure Signal Monitoring and Parameter Tuning for XCP

  1. Create a folder for this example.

    mkdir ext_mode_xcp_example
    cd ext_mode_xcp_example

  2. Open Simulink and create a simple model, xcpExample, which contains these blocks:

    • Sine Wave

    • Scope

    • Half Gauge

    • Knob

  3. Double-click the Sine Wave block. Set Sample time to 0.1, and then click OK.

  4. Connect the Sine Wave block to the Scope block and name the connection, for example, Test Signal.

  5. Configure the signal for logging:

    1. Right-click Test Signal.

    2. From the context menu, select Log Selected Signals. If you do not enable signal logging, you cannot view the signal by using the Scope block or stream signal data to the Simulation Data Inspector.

  6. Configure the Half Gauge block to monitor the value of Test Signal:

    1. Double-click the Half Gauge block.

    2. In the Simulink Editor, select Test Signal.

    3. In the Block Parameters dialog box:

      • Connect the block to Test Signal.

      • In the Maximum field, enter a value, for example, 1.

    4. Click OK.

  7. Configure the Knob block to tune the Amplitude parameter of the Sine Wave block:

    1. Double-click the Knob block.

    2. In the Simulink Editor, select the Sine Wave block.

    3. In the Block Parameters dialog box:

      • Connect the block to the Amplitude parameter of the Sine Wave block.

      • In the Minimum and Maximum fields, enter values, for example, 0.1 and 1 respectively.

    4. Click OK.

  8. Save the model as xcpExample.

Specify Hardware and Prepare Model

  1. From the Apps tab on the Simulink toolstrip, click Run on Hardware Board.

  2. In the Run on Hardware Board pop-up, set Hardware board to a supported Intel® hardware board.

  3. On the Hardware tab, in the Prepare section, click Hardware Settings. The Configuration Parameters dialog box opens, displaying Hardware Implementation settings that are determined by the hardware board.

  4. On the Solver pane:

    1. In the Type field, specify Fixed-step.

    2. Under Solver details, in the Fixed-step size field, specify a value, for example, 0.1.

  5. On the Hardware Implementation > Hardware board settings > External Mode pane, set Communication interface to XCP on TCP/IP.

  6. Click OK. Then save the model.

Build, Deploy and Start Target Application

  1. Specify a nondefault value for the simulation stop time. On the Hardware tab, in the Run on Hardware section, set Stop Time field to Inf.

  2. Click .

    1. The software builds, deploys and starts the model as an application on the connected hardware board.

    2. Connects Simulink to the target application.

    3. Runs the generated model code.

    You can also perform the steps separately.

    1. On the Hardware tab, in the Deploy section, click Build, Deploy & Start .

    2. In the Run on Hardware section, expand the Monitor & Tune drop-down.

    3. Under Step By Step Commands, click Connect

    4. Click Start

Monitor Signal and Tune Parameter

You can monitor Test Signal through the:

  • Scope block –– Double-click the block.

  • Simulation Data Inspector –– Click the Simulation Data Inspector button. When the Simulation Data Inspector opens, select the Test Signal check box, which displays streamed data.

  • Half-Gauge block.

To change the amplitude of the sine wave, rotate the pointer on the Knob block to the required value.

To tune tunable block parameters during a simulation, you can also use these methods:

  • If a block parameter is a variable in the MATLAB® workspace, in the Command Window, assign a new value to the variable. Then, in the Simulink Editor, update the diagram by pressing Ctrl+D. Simulink downloads the new value to the target application.

  • Open the Block Parameter dialog box. In the parameter field, specify the required value. When you click Apply or OK, Simulink downloads the new parameter value to the target application.

To change multiple tunable parameters simultaneously:

  1. In the Prepare section, click Batch Mode.

  2. In your model, modify the required parameters.

  3. Click Update All Parameters. The software modifies the parameter values on the target application simultaneously.

For more information about parameter tuning with generated code, see Create Tunable Calibration Parameter in the Generated Code.

Stop Target Application

To stop the execution of generated model code before StopTime is reached and disconnect the target application, on the Simulink Editor toolbar, click the Stop button .

If you want to disconnect the target application from Simulink without stopping code execution, click . Then, under Step By Step Commands, click Disconnect.

Graphical Controls for XCP External Mode Simulations

You can control an XCP external mode simulation through:

  • The Hardware tab on the Simulink Editor toolbar. To display the Hardware tab, from the Apps tab on the Simulink toolstrip, click Run on Hardware Board.

  • The External Mode Control Panel. To open this dialog box, on the Hardware tab, in the Prepare section, click . Then, under Signal Monitoring & Tracing, click Control Panel.

This table lists the controls that you can use for an XCP external mode simulation.

External Mode ActionToolstripExternal Mode Control Panel

Build target application and run on hardware.

Run on Hardware > Build, Deploy & Start

N/A

Connect Simulink to a waiting or running target application.

Run on Hardware > Connect

When Simulink is connected to the target application, Connect appears dimmed and Disconnect is active.

Connect

When Simulink is connected to the target application, Connect changes to Disconnect.

Start real-time execution of generated code in the target environment.

Run on Hardware > Start

Start Real-Time Code

When the generated code starts executing, the button changes to Stop Real-Time Code.

Disconnect Simulink from the target environment, but do not stop real-time execution of code.

Run on Hardware > Disconnect

When Simulink is disconnected from the target application, Disconnect appears dimmed and Connect is active.

Disconnect

Stop target application execution and disconnect Simulink from the target environment.

In Run on Hardware section, Stop button .

Stop Real-Time Code

Tune batch of block parameters.

In Prepare section, Batch Mode and Update All Parameters buttons.

Batch download

To tune a batch of block parameters:

  1. In the External Mode Control Panel, select Batch download.

    Batch download is equivalent to the Simulink toolstrip control Batch Mode.

  2. In the Simulink Editor, modify the required block parameters.

  3. When you modify parameters, the External Mode Control Panel displays this message next to Download:

    Parameter changes pending... 
    Simulink stores the modified parameters locally.

    Download is equivalent to the Simulink toolstrip control Update All Parameters.

  4. Click Download. Simulink downloads the batch of modified parameters to the target application.

Run XCP External Mode Simulation from Command Line

You can use commands or scripts to run XCP external mode simulations. To retrieve and set the values of model parameters, use the get_param and set_param commands.

To run these commands, you must have a Simulink model open and a target application running.

  1. Set the model simulation mode to external mode.

    set_param(gcs,'SimulationMode','external');

  2. Connect Simulink to the target application.

    set_param(gcs,'SimulationCommand','connect')

  3. Run generated model code.

    set_param(gcs,'SimulationCommand','start');

  4. To tune a parameter, change its workspace variable value through a line command. For example, if a block parameter value is specified as a Simulink.Parameter object, assign the new value to the Value property.

    myParamObj.Value = 5.23;

  5. To download the new value to the target application, update the model.

    set_param(gcs,'SimulationCommand','update');

  6. Stop the target application and disconnect Simulink from the target environment.

    set_param(gcs,'SimulationCommand','stop');

    To disconnect Simulink from the target application without stopping execution of generated code, use this command:

    set_param(gcs,'SimulationCommand','disconnect');

XCP External Mode Limitations

This table describes limitations that apply to external mode simulations that use XCP communication.

FeatureDetails

Parameter updates that change model structure

You cannot change:

  • The number of states, inputs, or outputs of a block

  • The sample time or the number of sample times

  • The integration algorithm for continuous systems

  • The name of the model or of a block

If you make parameter updates that change the model structure, you must rebuild the target application.

You can change numerator and denominator polynomial parameters for the Transfer Fcn, Discrete Transfer Fcn, and Discrete Filter blocks if the number of states does not change.

You cannot change zero entries in the State-Space, Zero-Pole, and Discrete Zero-Pole blocks in the user-specified or computed parameters, that is, the A, B, C, and D matrices obtained by a zero-pole to state-space transformation.

In the State-Space block, if you specify the matrices in the controllable canonical realization, then changes to the A, B, C, and D matrices that preserve this realization and the dimensions of the matrices are allowed.

If the Simulink block diagram does not match the target application, Simulink produces an error stating that the checksums do not match. The checksums take into account the top models, but not referenced models. To rebuild the target application, use the updated block diagram.

Signal value display

The graphical display of signal values during a simulation is not supported. For example, you cannot use the Data Display in Simulation menu items Show Value Labels When Hovering, Toggle Value Labels When Clicked, and Show Value Label of Selected Port. For more information, see Display Signal Values in Model Diagrams.

Signal triggering and data archiving

The Signal & Triggering, Arm Trigger, Cancel Trigger, and Data Archiving features available on the External Mode Control Panel are not supported.

Signal streaming

Dynamic selection of signals for streaming is not supported. To select different signals for streaming, rebuild the model.

Overriding signal logging settingsOverriding signal logging settings by using the Signal Logging Selector is not supported.

Compiler debug symbol format

Your toolchain must generate debug information in one of these formats:

  • DWARF

  • PDB

Inlined parameters

If you set DefaultParameterBehavior to 'Inlined', the code generator embeds numeric model parameter values instead of symbolic parameter names in the generated code. You can use Simulink.Parameter objects to remove parameters from inlining and declare the parameters tunable. However, when you connect Simulink to the target application, the numeric values of the tunable parameters are not automatically uploaded to the model. Simulink produces a warning.

Global variables

Signals, parameters, and states must be specified as global variables. The target memory locations where the variables are stored must lie in the range 0 – 4294967295.

Parameter structures

You cannot tune parameters that are structures.

Pure integer code

Pure integer code is supported. For code generation, if 'PurelyIntegerCode' is 'on', specify 'FixedStep' with a resolution that is greater than or equal to 1 microsecond. For example, specify 1.000001, but not 1.0000001.

If you do not specify -tf finalTime in the execution command, the target application runs the generated model code indefinitely, ignoring StopTime.

If you specify -tf finalTime in the execution command:

  • The finalTime value represents base rate clock ticks, not seconds.

  • The maximum value for finalTime, in ticks, is MAX_int32_T.

Variable-size signals

Uploading of variable-size signals is not supported.

Scope and Floating Scope blocks, and Scope Viewer

Some signal data types are not supported. The simulation produces a warning.

Scopes in referenced models

In a model hierarchy, if the top model runs in external mode and a referenced model runs in normal or accelerator mode, scopes in the referenced model are not displayed.

Nonzero simulation start time

Nonzero simulation start times are not supported. Use the default value for Solver > Start time, 0.0.

File-scoped data

File-scoped data is not supported. For example, data items to which you apply the built-in custom storage class FileScope. The simulation produces a warning.

Row-major code generation

Code generated with the row-major format is not supported.

Related Topics

External Websites