Products & Services Solutions Academia Support User Community Company

Learn more about xPC Target   

Parameter Tuning and Inlining Parameters

Introduction

By default, the xPC Target software lets you change parameters in your target application while it is running in real time.

You can also improve overall efficiency by inlining parameters. The xPC Target product supports the Real-Time Workshop® inline parameters functionality (see the Real-Time Workshop documentation for further details on inlined parameters). By default, this functionality makes all parameters nontunable. If you want to make some of the inlined parameters tunable, you can do so through the Model Parameter Configuration dialog box (see Inlined Parameters).

Parameter Tuning with xPC Target Explorer

The xPC Target software lets you change parameters in your target application while it is running in real time. With these functions, you do not need to set the Simulink interface to external mode, and you do not need to connect the Simulink interface with the target application.

You can download parameters to the target application while it is running or between runs. This feature lets you change parameters in your target application without rebuilding the Simulink model. You cannot use xPC Target Explorer to change tunable source block parameters while a simulation is running.

After you download a target application to the target PC, you can change block parameters using xPC Target Explorer. This procedure uses the Simulink model xpcosc.mdl as an example, and assumes you have created and downloaded the target application for that model.

  1. In xPC Target Explorer, right-click the downloaded target application node. For example, xpcosc.

  2. Select Start.

  3. To get the list of parameters in the target application, expand the Model Hierarchy node under the target application.

    The Model Hierarchy expands to show the elements in the Simulink model.

    The model hierarchy shows only those blocks that have tunable parameters.

  4. Select the parameter of the signal you want to edit. For example, select Gain.

    The right pane displays the block parameters dialog box for Gain. There is one parameter, Gain, for this block. The current value of the Gain parameter is displayed.

  5. Double-click the box that contains the gain value.

    The box becomes editable.

  6. Enter a new value for the gain.

  7. Press the Enter key.

    The box is updated and the Update Parameter button becomes active.

    If there is a scope, the plot frame then updates the signals after running the simulation with the new parameter value.

  8. Stop the target application. For example, to stop the target application xpcosc, right-click it and select Stop.

    The target application on the target PC stops running.

Parameter Tuning with the MATLAB Interface

You use the MATLAB functions to change block parameters. With these functions, you do not need to set the Simulink interface to external mode, and you do not need to connect the Simulink interface with the target application.

You can download parameters to the target application while it is running or between runs. This feature lets you change parameters in your target application without rebuilding the Simulink model.

After you download a target application to the target PC, you can change block parameters using xPC Target functions. This procedure uses the Simulink model xpcosc.mdl as an example, and assumes you have created and downloaded the target application for that model. It also assumes that you have assigned tg to the appropriate target PC.

  1. In the MATLAB window, type

    +tg

    or

    tg.start

    or

    start(tg)

    The target PC displays the following message:

    System: execution started (sample time: 0.001000)
  2. Display a list of parameters. Type either

    set(tg,'ShowParameters','on')

    or

    tg.ShowParameters='on'

    The latter command displays a list of properties for the target object.

    ShowParameters = on

    Parameters = 
    

    INDEX

    VALUE

    TYPE

    SIZE

    PARAMETER NAME

    BLOCK NAME

    0

    1000000

    DOUBLE

    Scalar

    Gain

    Gain

    1

    400

    DOUBLE

    Scalar

    Gain

    Gain1

    2

    1000000

    DOUBLE

    Scalar

    Gain

    Gain2

    3

    0

    DOUBLE

    Scalar

    Initial Condition

    Integrator

    4

    0

    DOUBLE

    Scalar

    Initial Condition

    Integrator1

    5

    4

    DOUBLE

    Scalar

    Amplitude

    Signal Generator

    6

    20

    DOUBLE

    Scalar

    Frequency

    Signal Generator

  3. Change the gain. For example, to change the Gain1 block, type either

    tg.setparam(1,800)

    or

    setparam(tg,1,800)

    As soon as you change parameters, the changed parameters in the target object are downloaded to the target application. The host PC displays the following message:

    ans = 
    parIndexVec: 1
    OldValues: 400
    NewValues: 800

    If there is a scope, the plot frame then updates the signals after running the simulation with the new parameters.

  4. Stop the target application. In the MATLAB window, type

    -tg

    or

    tg.stop

    or

    stop(tg)

    The target application on the target PC stops running, and the target PC displays messages like the following:

    minimal TET: 0.000023 at time 1313.789000
    maximal TET: 0.000034 at time 407.956000

Resetting Target Application Parameters to Previous Values

You can reset parameters to preceding target object property values by using xPC Target methods on the host PC. The setparam method returns a structure that stores the parameter index, the previous value, and the new value. If you expect to want to reset parameter values, set the setparam method to a variable. This variable points to a structure that stores the parameter index and the old and new parameter values for it.

  1. In the MATLAB window, type

    pt=tg.setparam(1,800)

    The setparam method returns a result like

    pt = 
    parIndexVec: 1
    OldValues: 400
    NewValues: 800
  2. To reset to the previous values, type

    setparam(tg,pt.parIndexVec,pt.OldValues)
    ans = 
    parIndexVec: 5
    OldValues: 800
    NewValues: 100

Parameter Tuning with Simulink External Mode

You use Simulink external mode to connect your Simulink block diagram to your target application. The block diagram becomes a graphical user interface to your target application. You set up the Simulink interface in external mode to establish a communication channel between your Simulink block window and your target application.

In Simulink external mode, wherever you change parameters in the Simulink block diagram, the Simulink software downloads those parameters to the target application while it is running. This feature lets you change parameters in your program without rebuilding the Simulink model to create a new target application.

After you download your target application to the target PC, you can connect your Simulink model to the target application. This procedure uses the Simulink model xpcosc.mdl as an example, and assumes you have created and downloaded the target application for that model.

  1. In the Simulink window, and from the Simulation menu, click External.

    A check mark appears next to the menu item External, and Simulink external mode is activated.

  2. In the Simulink block window, and from the Simulation menu, click Connect to target.

    All of the current Simulink model parameters are downloaded to your target application. This downloading guarantees the consistency of the parameters between the host model and the target application.

  3. From the Simulation menu, click Start Real-Time Code, or, in the MATLAB window, type

    +tg 

    or

    tg.start

    or

    start(tg)

    The target application begins running on the target PC, and the target PC displays the following message:

    System: execution started (sample time: 0.000250)
  4. From the Simulation block diagram, double-click the block labeled Gain1.

    The Block Parameters: Gain1 parameter dialog box opens.

  5. In the Gain text box, enter 800 and click OK.

    As soon as you change a model parameter and click OK, or you click the Apply button on the Block Parameters: Gain1 dialog box, all the changed parameters in the model are downloaded to the target application.

  6. From the Simulation menu, click Disconnect from Target.

    The Simulink model is disconnected from the target application. Now, if you change a block parameter in the Simulink model, there is no effect on the target application. Connecting and disconnecting the Simulink interface works regardless of whether the target application is running or not.

  7. In the MATLAB window, type either

    stop(tg)

    or

    -tg

    The target application on the target PC stops running, and the target PC displays the following messages:

    minimal TET: 0.000023 at time 1313.789000
    maximal TET: 0.000034 at time 407.956000

Parameter Tuning with a Web Browser

The Parameters pane displays a list of all the tunable parameters in your model. Row and column indices for vector/matrix parameters are also shown.

After you connect a Web browser to the target PC, you can use the Parameters page to change parameters in your target application while it is running in real time:

  1. In the left frame, click the Parameters button.

    The browser loads the Parameter List pane into the right frame.

    If the parameter is a scalar parameter, the current parameter value is shown in a box that you can edit.

    If the parameter is a vector or matrix, there is a button that takes you to another page that displays the vector or matrix (in the correct shape) and enables you to edit the parameter.

  2. Enter a new parameter value into one or more of the parameter boxes, and then click the Apply button.

    The new parameter values are uploaded to the target application.

Saving and Reloading Application Parameters with the MATLAB Interface

After you have a set of target application parameter values that you are satisfied with, you can save those values to a file on the target PC. You can then later reload these saved parameter values to the same target application. You can save parameters from your target application while the target application is running or between runs. This feature lets you save and restore parameters in your target application without rebuilding the Simulink model. You save and restore parameters with the target object methods saveparamset and loadparamset.

The procedures assume that

Saving the Current Set of Target Application Parameters

To save a set of parameters to a target application, use the saveparamset method. The target application can be stopped or running.

  1. Identify the set of parameter values you want to save.

  2. Select a descriptive filename to contain these values. For example, use the model name in the filename. You can only load parameter values to the same target application from which you saved the parameter values.

  3. In the MATLAB window, type either

    tg.saveparamset('xpc_osc4_param1') 

    or

    saveparamset(tg,'xpc_osc4_param1')

    The xPC Target software creates a file named xpcosc4_param1 in the current folder of the target PC, for example, C:\xpcosc4_param1.

For a description of how to restore parameter values to a target application, see Loading Saved Parameters to a Target Application. For a description of how to list the parameters and values stored in the parameter file, see Listing the Values of the Parameters Stored in a File.

Loading Saved Parameters to a Target Application

To load a set of saved parameters to a target application, use the loadparamset method. You must load parameters to the same model from which you save the parameter file. If you load a parameter file to a different model, the behavior is undefined.

This section assumes that you have a parameters file saved from an earlier run of saveparamset (see Saving the Current Set of Target Application Parameters).

  1. From the collection of parameter value files on the target PC, select the one that contains the parameter values you want to load.

  2. In the MATLAB window, type either

    tg.loadparamset('xpc_osc4_param1') 

    or

    loadparamset(tg,'xpc_osc4_param1')

    The xPC Target software loads the parameter values into the target application.

For a description of how to list the parameters and values stored in the parameter file, see Listing the Values of the Parameters Stored in a File.

Listing the Values of the Parameters Stored in a File

To list the parameters and their values, load the file for a target application, then turn on the ShowParameters target object property.

This section assumes that you have a parameters file saved from an earlier run of saveparamset (see Saving the Current Set of Target Application Parameters).

  1. Ensure that the target application is stopped. For example, type

    tg.stop
  2. Load the parameter file. For example, type

    tg.loadparamset('xpc_osc4_param1');
  3. Display a list of parameters. For example, type

    tg.ShowParameters='on';

    and then type

    tg

    The MATLAB window displays a list of parameters and their values for the target object.

Inlined Parameters

This procedure describes how you can globally inline parameters for a model, then specify which of these parameters you still want to be tunable. It assumes that you are familiar with how to build target applications (if you are not, read the xPC Target Getting Started Guide first). After you have performed this procedure, you will able to tune these parameters.

The following procedure uses the Simulink model xpcosc.mdl as an example.

  1. In the MATLAB Command Window, type

    xpcosc

    The model is displayed in the Simulink window.

  2. Select the blocks of the parameters you want to make tunable. For example, this procedure makes the signal generator's amplitude parameter tunable. Use the variable A to represent the amplitude.

  3. Double-click the Signal Generator block and enter A for the Amplitude parameter. Click OK.

  4. In the MATLAB Command Window, assign a constant to that variable. For example, type

    A = 4

    The value is displayed in the MATLAB workspace.

  5. In the Simulink window, from the Simulation menu, click Configuration Parameters.

    The Configuration Parameters dialog box for the model is displayed.

  6. Click the Optimization node.

  7. In the rightmost pane, select the Inline parameters check box.

    The Configure button is enabled.

  8. Click the Configure button.

    The Model Parameter Configuration dialog box is displayed. Note that the MATLAB workspace contains the constant you assigned to A.

  9. Select the line that contains your constant and click Add to table.

    The Model Parameter Configuration dialog box appears as follows.

    If you have more global parameters you want to be able to tune, add them also.

  10. Click Apply, then click OK.

  11. In the Configuration Parameters dialog, click Apply, then OK.

  12. If you want, increase the model stop time, or set it to inf.

  13. When you are finished, click Apply, then OK, and save the model. For example, save it as xpc_osc5.mdl.

  14. Build and download the model to your target PC.

You next can use xPC Target Explorer or the MATLAB interface to work with the tunable parameters.

Tuning Inlined Parameters with xPC Target Explorer

This procedure describes how you can tune inlined parameters through the xPC Target Explorer. It assumes that you have built and downloaded the model from the topic Inlined Parameters to the target PC. It also assumes that the model is running.

  1. If you have not yet started xPC Target Explorer, do so now. Be sure it is connected to the target PC to which you downloaded the xpc_osc5 target application.

  2. To get the list of tunable inlined parameters in the target application, expand the target application node, then expand the Model Hierarchy node under the target application node.

    Note that the Model Hierarchy node displays a list of signals and an object called Model Parameters. Model Parameters contains the list of tunable inlined parameters.

  3. To display the tunable parameters, select Model Parameters.

    The constant A and its value are shown in the right pane.

  4. Double-click the box that contains the tunable parameter A.

    The box becomes editable.

  5. Enter a new value for the parameter and press Enter.

    The box is updated and the Update Parameter button becomes active.

  6. To apply the new value, click the Update Parameter button.

  7. To verify the updated value, select the signal object associated with A. For example, select Signal Generator.

    The value of Signal Generator is shown in the right pane.

  8. Stop the target application.

Tuning Inlined Parameters with the MATLAB Interface

This procedure describes how you can tune inlined parameters through the MATLAB interface. It assumes that you have built and downloaded the model from the topic Inlined Parameters to the target PC. It also assumes that the model is running.

You can tune inlined parameters using a parameter ID as you would conventional parameters.

  1. Save the following code in an M-file. For example, change_inlineA.

    tg=xpc; %Create xPC Target object
    pid=tg.getparamid('','A'); %Get parameter ID of A
    if isempty(pid) %Check value of pid.
        error('Could not find A');
    end
    tg.setparam(pid,100); %If pid is valid, set parameter value.
  2. Execute that M-file. For example, type

    change_inlineA
  3. To see the new parameter value, type

    tg.showparameters='on'

    The tg object information is displayed, including the parameter lines:

NumParameters = 1

ShowParameters = on

Parameters = INDEX  VALUE  TYPE   SIZE  PARAMETER NAME  BLOCK NAME

              0      100  DOUBLE Scalar A

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS