Software-in-the-Loop Execution from Command Line - MATLAB & Simulink (original) (raw)
Main Content
Use software-in-the-loop (SIL) execution to verify the numerical behavior of the generated C/C++ code with reference to your original MATLAB® functions.
To set up and start a SIL execution from the command line:
- Create a coder.EmbeddedCodeConfig object.
- Configure the object for SIL.
- Use the codegen function to generate library code for your MATLAB function and the SIL interface.
- Use the coder.runTest function to run the test file for your original MATLAB function.
To terminate the SIL execution, use the clear _`function`__sil
or clear mex
command.
The following example shows how you can set up and run a SIL execution from the command line.
Tutorial Files: Kalman Filter
Open this example to obtain the files for this tutorial:
kalman01.m
— MATLAB function for the Kalman estimatortest01_ui.m
— MATLAB file to testkalman01.m
plot_trajectory.m
— File that plots actual target trajectory and Kalman estimator outputposition.mat
— Input data
SIL Execution of Code Generated for a Kalman Estimator
Configure SIL execution
- From your working folder, create a coder.EmbeddedCodeConfig object.
config = coder.config('lib');
config.GenerateReport = true; % Optional, documents code in HTML report - Configure the object for SIL.
config.VerificationMode = 'SIL';
% Check that production hardware setting is the default
% i.e. 'Generic->MATLAB Host Computer'
disp(config.HardwareImplementation.ProdHWDeviceType); - If required, enable the Microsoft® Visual Studio® debugger for SIL execution:
config.SILPILDebugging = true;
- From your working folder, create a coder.EmbeddedCodeConfig object.
Generate code and run SIL execution
Generate library code for thekalman01
MATLAB function and the SIL interface, and run the MATLAB test file,test01_ui
. The test file useskalman01_sil
, the generated SIL interface forkalman01
.
codegen -config config -args {zeros(2,1)} kalman01 -test test01_ui
The software creates the following output folders:codegen\lib\kalman01
— Standalone code forkalman01
.codegen\lib\kalman01\sil
— SIL interface code forkalman01
.
Verify that the output of this run matches the output from the originalkalman01.m
function.
Note
On a Windows® operating system, the Windows Firewall can potentially block a SIL or PIL execution. To allow the execution, use the Windows Security Alert dialog box. For example, in Windows 7, click Allow access.
Debug code during SIL execution
If you enable the Microsoft Visual Studio debugger, then running the test file opens the Microsoft Visual Studio IDE with debugger breakpoints at the start of thekalman01_initialize
andkalman01
functions.
You can use the debugger features to observe code behavior. For example, you can step through code and examine variables.
To end the debugging session:- Remove all breakpoints.
- Click the Continue button (F5).
The SIL execution runs to completion.
Terminate SIL execution
Terminate the SIL execution process.
You can also use the commandclear mex
, which clears MEX functions from memory.
Note
Suppose that the MATLAB current working directory (the folder that pwd displays) is a UNC path (for example, \\server\a\b\c
) on a Windows platform. If you launch a SIL executable from this path, the location where the SIL executable is launched can be unpredictable and different from the MATLAB current working directory (for example,C:\Windows
).
To fix this issue, use a mapped network drive for the UNC path as your MATLAB current working directory.