Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Sunday, September 22, 2024

Using code from Simulink model in HIL

Steps of converting a Simulink model to code usable in Hardware-in-the-loop (HIL) simulation:
  1. Checkout/pull Simulink model from repository to your local.
  2. 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. 
  3. 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.
  4. 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.
  5. Copy code to real time Linux PC and verify code can be build there too.
  6. Commit code to its own repo.
  7. Run HIL with new code and verify HIL works as expected.

Wednesday, August 16, 2023

Compile s-function with C++17

To compile a Simulink s-function file with C++17 (assuming you have Visual Studio 2019 or higher) , use:
        mex COMPFLAGS='$COMPFLAGS /std:c++17' file.cpp

Note that this does NOT work: mex CXXFLAGS='$CXXFLAGS /std:c++17' file.cpp

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:

  1. Stabilize orientation so that the thruster points in downward direction.
  2. Control vertical speed to achieve a soft landing.
Orientation can be stabilized via passive fins or active side jets. Since there isn't much time for manufacturing and testing, using fins seems to be the easiest choice.
Assuming that the fins achieve close to zero yaw and pitch, speed can be controlled by changing the thrust via valve control using PID.  I wrote a Matlab script that can be used as a starting point:
I also wrote a simple open loop code that has no control, you can find it here.

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

When I read data from a file in Matlab, I sometimes read the wrong file because files have the same name but different paths and I want to read the latest file. If you print the file date/time to Matlab's command window, you will get a visual indication which can alert you if you read an older file with the same name. You can use the following two lines to print file date:
fileInfo = dir(fileName);
fprintf("File date: %s\n", fileInfo.date);

Tuesday, May 12, 2020

Applying derivative and integral on noisy data

Let's say you have a motion with sinusoidal acceleration, i.e. xddot(t) = sin(t). The analytic solution for speed and position will be xdot(t) = -cos(t)+1 (assuming zero initial speed) and x(t) = -sin(t) + t.  Let's say you don't know the analytical expressions of position, speed and acceleration and only have their values at a fixed sample rate. If you want to increase the samples, you can use Matlab's spline function.

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: