Documentation Home Page RT-LAB Home Page
Pour la documentation en FRANÇAIS, utilisez l'outil de traduction de votre navigateur Chrome, Edge ou Safari. Voir un exemple.

Example on using API calls for Variables

Starting Project and Model Creation

RT-LAB gives the option to have a quick-project set up for testing right-away. Go to the File menu and click on Quick Start-up project

Follow the next few dialog boxes to have a project named QuickProject added to your Project Explorer window.

Now double-click on the project to show the model tree. Right click on the model and select Edit.

This will open up the MATLAB/Simulink version associated with the running RT-LAB version.


In the top-most level of the Simulink model, copy paste the MASTER sub-system named sm_computation and rename it to ss_computation.
Now we have to sub-systems other than the console i.e. sc_user_interface. This console should be updated to take signals of both the other subsystems.
One way is to increase the port-numbers for the OpCommn block to 6.


Then just copy the input ports of the model and the scopes; then connect them as shown below.


Refer to the figure above and make sure the flow of signals between the sub-systems is as shown there.

Now we will have two computation systems (sm* and ss*). Both have the same list of gain variable Kp, Ki and Kd.
These gain values can be tuned for the PID system the represent. 
We can add these variables and initialize them inside the model using the PreLoadFcn and InitFcn functions.

Now build the model. 

Verifying the MATLAB variables

Variables Kp, Ki, Kd will become available in the MATLAB Variables section in the project tree view.

Changing the value of the variable affects all blocks using that variable.

We will then use these variables and update their values via the API calls so that they can be simultaneously used in both the sub-systems.
Each variable has an id (defined in the [Variable] section).

0=Kd

1=Ki

2=Kp

This will be needed in the API call. 

Python API calls

Open the Python console and open the project using:
RtlabApi.OpenProject("QuickProject") 


Now check the variables we added for the gains Kp, Kd and Ki using:
RtlabApi.GetVariablesDescription() 

This call returns a tuple containing tuples for each variable. Tuple for each variable contains

  • Variable Id
  • Variable name (as defined in MATLAB)
  • Variable value
  • Tuple of paths of all parameters in blocks using the variable

More details can be found here.


Now we will set the variables. The command to use is RtlabApi.SetVariables(((Id, Value),(Id, Value),...)) 
More details on the command can be found here. For our example, enter the following command:
RtlabApi.SetVariable(((0,20.5),(1,5.5),(2,5.5))) 

The above command will update the Gain values of Kp, Kd and Ki; from 20,5,5 to 20.5,5.5,5.5 respectively.
More details on this function can be found here.

To verify this update, we can again use the RtlabApi.GetVariablesDescription() function and look in the 3rd element of each tuple.

OPAL-RT TECHNOLOGIES, Inc. | 1751, rue Richardson, bureau 1060 | Montréal, Québec Canada H3K 1G6 | opal-rt.com | +1 514-935-2323