Change Parameter Values on Target Hardware

This example shows how to:

  • Configure a Simscape™ plant 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 plant 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.


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 Simulink Real-Time Setup and Configuration (Simulink Real-Time).

Configure the Simscape Model for Deployment

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

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


    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, set the simulation stop time to inf.

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

    ts = 4e-5;

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

    1. Open the model configuration parameters.

    2. To set the System Target File, on the Code Generation pane, click Browse and select slrt.tlc.

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

    4. Open the Report pane.

    5. To display a code generation report, on the Report pane, select Create code generation report and Open report automatically.

    6. Click OK.

  5. Add a Simulink Real-Time Scope block to the Simscape plant model.

    1. In the Simulink library browser, navigate to the Simulink-Real Time > Displays and Logging library.

    2. Add a Scope block to the Simscape model.

    3. Connect the scope to the output signal from the PS-Simulink Converter block.

  6. Configure Simulink Real-Time Scope block as a host scope.

    1. Specify the Scope type parameter as Host.

    2. Set the Number of samples to 100000.

    3. Click OK.

Deploy the Model to the Real-Time Target Machine

Build an executable application and download it to real-time hardware.

  1. Check that you are connected to the real-time target machine, in this case, the target name is SLRTLABTG2:

    tg = slrt
    Target: SLRTLABTGT2
    Connected            = Yes
    Application          = loader
  2. To build and download the code to the target machine, in the model window, click the Build button .

    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.c.

    2. Search for the section of the code that contains the parameter variables. In the Find box, enter Block parameters (auto 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:


  2. View the Simscape run-time parameters in Simulink Real-Time Explorer.

    1. In the Applications pane, expand the real-time application node, in this case, SLRTLABTGT2/ssc_resistive_ac_circuit.

    2. Under the real-time application node, expand the Model Hierarchy node.

    3. Select ssc_resistive_ac_circuit and click the View Parameters button .

      The Parameters workspace opens, showing a table of parameters with properties and actions.

  3. View the host scope in Simulink Real-Time Explorer.

    1. In the Scopes pane, expand the real-time application node (SLRTLABTGT2/ssc_resistive_ac_circuit).

    2. Under the real-time application node, expand the Host Scopes node.

    3. To open the Scopes workspace, select Scope 1 and click the View Host Scope button .

    4. To make both the parameter and scope workspaces visible at the same time, click the scope workspace tab and drag the tab down until the Pane Move button  appears in the middle of the dialog box. Release the tab when the cursor reaches the lower quadrant of the Pane Move button.

  4. To see the value for the peak amplitude of the voltage source, click the arrow next to the A_peak_voltage_src parameter.

    The Values text box opens, containing the original value of 3 V for the peak amplitude.

  5. Simulate with the original peak amplitude value. To start execution, click the real-time application (SLRTLABTGT2/ssc_resistive_ac_circuit) and then click the Start button  on the Applications toolbar.

    The scope 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).

  6. Change the parameter that represents the peak amplitude for the Simscape Voltage Source block. Because Simscape run-time parameters are run-time configurable, 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, click the real-time application in the Applications workspace, and then click the Stop button .

    2. Specify 50 in the Values text box for the peak amplitude parameter and press Enter.

    3. Click the Apply parameter value changes button .

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

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

  7. Modify and revert the value for the resistance of the Resistor block.

    1. Stop the execution.

    2. Click the arrow next to the R_resistor parameter.

      The Values text box opens, containing the original value of 10 Ohms for the resistance.

    3. Specify 2 in the text box, and then press Enter.

    4. To revert the resistance to its previous value, click the Revert button .

See Also

| | |

Related Examples

More About