Main Content

Change Parameter Values on Target Hardware

This example shows how to:

  • Configure a Simscape™ model to generate code that supports signal visualization and changes to Simscape run-time parameters.

  • Use Simulink® Real-Time™ and Simulink Coder™ to deploy an executable version of the model to a real-time target machine.

  • Use Simulink Real-Time Explorer on your development computer to change the value of a Simscape run-time parameter on the target machine and to see the effects of the parameter change.

Prerequisites

This example requires an active connection between your development computer and a real-time target machine. For information on configuring and connecting your development computer to target hardware, see Get Started with Simulink Real-Time (Simulink Real-Time).

Configure the Simscape Model for Deployment

To enable your development computer to change parameter values on a real-time target machine, configure the Simscape run-time and code generation parameters for your Simscape model.

  1. To open the reference model, at the MATLAB® command prompt, enter:

    ssc_resistive_ac_circuit

    The model opens and the PreLoadFcn loads parameters for the model to the MATLAB workspace. The peak voltage, A_peak_voltage_src, is 3 V, the resistance, R_resistor, is 10 Ohms, and the step size is 1e-5.

  2. To allot enough time to see the effects of parameter-tuning on the target machine, configure the application to run until you stop the simulation by setting the simulation stop time to inf.

  3. Adjust the step size for real-time simulation. At the MATLAB command prompt, enter:

    ts = 8e-5;

  4. Configure the model for code generation using Simulink Coder and Simulink Real-Time.

    1. Open the Configuration Parameters window. In the Simulink Editor, open the Modeling tab and click Model Settings. The Configuration Parameters window opens.

    2. In the Code Generation pane, to the right of System target file, click Browse and select a system target file (STF) for a Simulink Real-Time model.

    3. In the System Target File Browser window, click OK.

    4. Open the Code Generation > Report pane.

    5. To display a code generation report select Create code generation report and Open report automatically.

    6. Click OK.

  5. Enable signal logging on the signal that you want to view in the Simulation Data Inspector. Click the signal named Current and, from the action menu, select Enable Data Logging.

    Resistive AC circuit model demonstrating the Enable Data Logging option

Deploy the Model to the Real-Time Target Machine

Build an executable application to be deployed on the target machine.

  1. Check that you are connected to the real-time target machine:

    tg = slrealtime
    
  2. To build the code to be deployed, in the Simulink Editor, open the Real-Time tab and click Run on Target > Build Application.

    The code report opens after the code download.

  3. Verify that the generated code represents the Simscape run-time variables in a data structure.

    1. In the Code Generation Report, in the left pane, in the Data files node, open ssc_resistive_ac_circuit_data.cpp.

    2. Search for the section of the code that contains the parameter variables. In the Find box, enter Block parameters (default storage).

    3. Verify that the A_peak_voltage_src and the R_resistor variables are represented in the P_ssc_resistive_ac_circuit_T ssc_resistive_ac_circuit_P data structure.

Change Parameters and See Results Using Simulink Real-Time Explorer

Use Simulink Real-Time Explorer to change Simscape run-time parameters between runs of your real-time application on target hardware. Visualize the simulation results on a scope in the Explorer window.

  1. To open Simulink Real-Time Explorer, on your development computer, at the MATLAB command prompt, enter:

    slrtExplorer
    

  2. Select the target computer in the Targets Tree panel. To connect to the target computer, click Disconnected, to toggle it to Connected.

    Simulink Real-Time Explorer window with disconnected target computer.

  3. To load the real-time application built earlier, click Load Application. In the Application on host computer pane, click File Selector and select the ssc_resistive_ac_circuit.mldatx file. Click Load.

  4. To select the signals for streaming, on the Signals tab, select the signal Current, click the Add selected signals button to add the signal to the list in the right pane, and then click the Start Streaming button.

    Simulink Real-Time Explorer window with connected target computer.

  5. To view the Simscape run-time parameters in Simulink Real-Time Explorer, open the Signals and Parameters Parameters tab and click the Show contents of current system and below button .

    Simulink Real-Time Exporer window. The Parameters tab shows A_peak_voltage_src and R_resistor, their values, data type, and size.

  6. To run the application with the original peak amplitude value, click Start.

  7. To view the streaming signals, click Data Inspector.

    Simulation Data Inspector window showing a signal amplitude of plus or minus 0.3

    The streamed data shows that the current is approximately 0.3 A. The defining equation for the circuit in the model is I = V/R. The results are correct for the given voltage (10 V) and resistance (3 Ohms).

  8. Change the A_peak_voltage_src parameter, which represents the peak amplitude for the Voltage Source block. Because Simscape run-time parameters are run-time configurable, you cannot change the parameter value during simulation. Instead, you stop the simulation, change the value of the parameter, and apply the parameter change. Then, you restart the simulation to see how changing the parameter affects the results.

    1. To stop execution, in the Simulink Real-Time Explorer window, click Stop.

    2. Click Value box for the A_peak_voltage_src parameter and enter 50.

      Parameters tab showing the updated value of 50 for A_peak_voltage_src.

    3. Click the Start button to simulate with the modified peak amplitude value.

      Simulation Data Inspector window showing a signal amplitude of plus or minus 5.

      The streamed data shows that the current is approximately 5 A when the peak amplitude is 50 V. The results reflect the change in value for the voltage, given that the resistance is 10 Ohms.

See Also

(Simulink Real-Time) | (Simulink Real-Time)

Related Examples

More About