- Checkout/pull Simulink model from repository to your local.
- Run Simulink model and confirm it finishes as expected. If not, inform the model maintainer and ask them to commit/push the model with correct settings to repo.
- Confirm that C/C++ code can be generated from model. Sometimes an s-function build file (mexw64) exists but its source code is missing, which allows the model to run but prevents code generation.
- Copy code to Visual Studio and verify that you can build and run it. There are cases where Simulink is more forgiving of errors like uninitialized variables, or missing #include <cmath> but Visual Studio cannot build the code.
- Copy code to real time Linux PC and verify code can be build there too.
- Commit code to its own repo.
- Run HIL with new code and verify HIL works as expected.
C++ and MATLAB Simulink tips for HWIL simulation software engineers
Sunday, September 22, 2024
Using code from Simulink model in HIL
Wednesday, August 16, 2023
Compile s-function with C++17
Wednesday, February 9, 2022
Vertical Landing Rocket Competition
This year's Teknofest has a Vertical Landing Rocket Competition. We can divide the control problem into two:
- Stabilize orientation so that the thruster points in downward direction.
- Control vertical speed to achieve a soft landing.
Tuesday, November 30, 2021
Generate C/C++ Code From Matlab Function
Use MATLAB Coder to generate C/C++ code from an m file (must be a function). At the end of the process, click on "View Report" and on the Report Viewer window, click on Package Code to package all the dependencies (e.g. tmwtypes.h) into a single zip file.
Friday, November 26, 2021
Check if the MATLAB instance you created has exited
When you run MATLAB from a Windows bat file and want to know if it has finished its job and exited (without considering other already open MATLAB instance), you can use the following:
start matlab.exe -nosplash -nodesktop -noFigureWindows
set pid=not found
for /f "tokens=2" %%a in ('tasklist^|find /i "matlab") do (set pid=%%a)
echo matlab pid = %pid%
:waitForMatlabToExit
timeout /t /5 /nobreak
tasklist /fi "pid eq %pid%" | find ":">nul
if errorlevel 1 goto waitForMatlabToExit
echo Matlab exited
exit /b
Thursday, November 25, 2021
Calling a Matlab function from Windows command line (cmd)
Calling a Matlab function from a Windows cmd batch file is easy if the function has no string arguments. However if the function takes a string argument, you have to use two single quotes around the argument. Example:
start matlab -nosplash -nodesktop -noFigureWindows -r eval("'myFunction(''fileName.txt'')'")
Monday, September 14, 2020
Tip when reading data from file
fileInfo = dir(fileName);fprintf("File date: %s\n", fileInfo.date);
Tuesday, May 12, 2020
Applying derivative and integral on noisy data
If you have position as input and calculate speed and acceleration, if there is any noise in the signal, it will be amplified. If you have acceleration as input and calculate speed and position via integration, noise will be attenuated. Another advantage of using integration instead of derivation is that we can use linear interpolation since when integrating, we don't need smooth second derivatives. This both decreases computational load and lag due to two points in linear interpolation compared to four points in spline. See below Matlab code for an example:
