In this section, we will, explain how to generate and build a standalone ros node of a Simulink model. We will focus on the generation of proc_control and assume you've set up your environment to work with control library.
Our repo is open source and you can clone it here
Start by opening the simulink model proc_control "proc_control.slx". Trough "Apps" tabs, Select embeded coder in the drop list to enable the code generation feature as shown in the picture.
We can remark that there is two way to generate code.
The Generate Code fonction will generate the C++ files of the project without adding the matlab dependencies files.
The package code & artifact will generate the C++ files of the project and add all the dependencies in a zip file.
Basicly, when you change the matlab version, you execute package code & artifact first to ensure that all the matlab dependecy our up to date. After, you can execute Generate Code and only replace the modified files. If matlab generate the code succesfuly, it will appear in a new simulink tab.
If you have error durring code generation phase, please check the next section to validate that you respect the code generaion limitation before checking up online.
Keep in mind that somes simulink blocs or matlab fonctions are not compatible with code generation. You have to ensure that none of these features are use in the project. It is realy easy to check in the matlab docmentation. For each fonction or block, check in the matlab documentation is there a section named Extended Capabilities with the C/C++ Code Generation check. If the section don't exist, it means that its not support code generation.
Something that I don't notice at first, is that some fonctions support code genrations with limitations and these are not shown by default. When it's the case, there will be an left arrow and you have to click on on it display them. Here's an exemple with the non linear mpc block.
Also, we use the discrete solver to improve performences of the generated code. That means that you can't continious bloc like integrator or transfert fonction. Instead use discrete time integrator and discrete transfert function.
For more information, please visit the matlab documentation about Embedded Coder
Before building de generated code, be sure to have clone proc_control. Basicly you just copy and replaceyour generated code in proc_control/src/ . Be sure to don't copy any cmakelist.txt or a package.xml as theses files are allready in the repo.
If you have upgrade your matlab version, delete all the files in the src folder to ensure to add the newer dependency.
All the file must be in the src folder without subfolder. I know when using package code & artifact, matlab generate a lot of subfolders in the zip file. You can unzip the zip file in the src folder, use this command to extract all the files in subfolder:
$ find ./ -type f -exec mv --backup=numbered -t ./ {} +
than delete the empty subfolders.
Use catkin_make
to build your code.
If you are reading this section, probably that you tried catkin_make
and saw a bunch of error in your terminal. Don't worry it is normal to not succesfully build the code the first time. This is why i am writting this pages.
These error occurs when you update your matlab dependecies. Mainly when you update your matlab version. When you succesffully build the code onces, you only have to change the C++ files of the project to keep the dependencies intact. This way you shoudn't have more troubles. See "Generate C++ Code section" above
Don't forget to delete your build and devel folder between each
catkin_make
command.
After working with matlab 2020a, 2020b, 2021a, 2021b and 2022a, I realise that it's always the same two errors that show up.
Missing References | Undefine reference for rtIOStream |
---|---|
This error means that matlab forgot import some dependencies when using package code & artifact. You can find the dependencies file in your matlabroot folder. By default it's located :
usr/local/MATLAB/R{your version}/
Simply seach for the missing files and copy them in your src folder.
This error look's more subtiles, but it is still a missing libraries. Basicly its means that you have rtiostream_utils.h in your project and the rtiostream_utils.c is missing. So Simply seach it in your matlabroot folder and copy it to your project.
If you are looking for rtiostream_utils.h, you will notice that there are 2 files with the same name. You want the one from /toolbox/coder/rtiostream/utils. Not the one from utils_host
If you found a new error, Great ! You will be able to document it here when you have found the solution.
One tip i can give you is to read carefully the error in your terminal, and use vs code to check where the compiler break and trace it back to the missing libraries.
You can also open an issue in a Mathworks forum.