This example shows how to use the MLToolkit API to connect to the SimWB real-time host, generate an RTDB and code for a model, create a test, start a test session, and get/set RTDB variable values as the session runs. This example also shows how to create test sessions that use different initial conditions. Use this technique, for example, when performing Monte Carlo simulations where a model is run using a variety of initial conditions.
rthosts = simwb.rthosts
rthostenv = simwb.rthostenv
rthostenv.Name = 'localhost' % Specify appropriate custom name for SimWB Real-Time host.
rthostenv.RTHostNameorIP = 'localhost' % Specify (DNS-resolved) host name or IP address for SimWB Real-Time host.
rtuser = simwb.rtuser('admin','nimda') % Specify relevant login credentials, username and password.
rthostenv.RTUser = rtuser
rthosts.add(rthostenv)
rtobj = simwb.rtobj('localhost')
Specify the name of the SimWB project to use. For this example, we will create a project on the Real-Time Host.
rtobj.createProject('Simulink/ABC')
rtobj.Project = 'Simulink/ABC' % The RTDB and model code will now reside in the specified project.
Next, set up the RTDB-generation options.
rtobj.RTWStruct % Inspect default option values. rtobj.RTWStruct.modelname = 'autotrans_demo' rtobj.RTWStruct.rtdb = 'autotrans_api' rtobj.RTWStruct.systemtargetfile = 'grt.tlc' rtobj.RTWStruct.SWprefix = 1 rtobj.RTWStruct.sourceregexp = '^SW' rtobj.RTWStruct.sinkregexp = '^SW'
rtobj.createRTDB
The createRTDB method generates a signal.db in the current working directory. View the contents of this file and validate the RTDB is as expected. Re-configure and re-generate if needed. Once the file looks right, upload it to the SimWB Real-Time Host.
type signal.db % Prints the signal.db file to the MATLAB Command Line. rtobj.uploadRTDB % Uploads the signal.db file to the server. rtobj.releaseRTDB % Releases this RTDB from the MLToolkit connection.
rtobj.rtwBuild
The generated code is located:
/usr/local/ccursim/projects/__project.Simulink/__project.ABC/RTW.Sources/autotrans_demo_ccurrt
opts.rtwname='autotrans_demo:cpu=4,freqdiv=1,runatcount=0,startorder=1,endorder=2,priority=99,schedulingpolicy=fifo,asyncexecchain=,options='
rtobj.createTest('test1',opts)
rtobj.startTestSession('test1')
The code below shows how to modify the value of RTDB variables. There are similar calls for modifying parameter values.
rtdb_vars = rtobj.getSharedMemRTDBVarList % This command lists the variables as they have been arranged in the shared memory. % When this example was written, the Speed variable was item 4 and the Throttle variable was item 5. Setting the throttle to 100. rtobj.getRTDBItemValue(4) rtobj.getRTDBItemValue(5) rtobj.setRTDBItemValue(5,100) rtobj.getRTDBItemValue(4) rtobj.getRTDBItemValue(5)
rtobj.stopTestSession
To generate variant initial conditions, you need to perform the following steps.
Default-models-RTDB folder from the InitialConditions folder for this test and rename the copied folder.rtdb.itemsIC.txt or the relevant parm*.txt file. Change the value=some_number key/value pair for the quantity to be varied.You can use whatever tool suits you best to accomplish this task. In this example, we invoke BASH scripts, which use SED to update a file.
throttle_50 - Creates a copy of the default initial conditions for 'test1' and sets the value of SWthrottle to 50.throttle_75 - Creates a copy of the default initial conditions for 'test1' and sets the value of SWthrottle to 75.type throttle_50 % View contents of the "throttle_50" bash script. cmd = fullfile(simwbroot,'examples','throttle_50'); [status,result] = system(cmd) % Executes the throttle_50 script. type throttle_75 % View contents of the "throttle_75" bash script. cmd = fullfile(simwbroot,'examples','throttle_75'); [status,result] = system(cmd) % Executes the throttle_75 script.
Create the test sessions that use the different initial conditions sets.
session_opts.initconditions = 'throttle_50'
rtobj.createTestSession('test1','throttle_50',session_opts);
session_opts.initconditions = 'throttle_75'
rtobj.createTestSession('test1','throttle_75',session_opts);
Run the throttle_50 test session and validate the initial value of SWthrottle variable using the commands shown in Get/Set RTDB Variable Values.
rtobj.startTestSession('test1','throttle_50')
Simulink®, developed by MathWorks, is a graphical modeling environment.