MATLAB & Simulink
4 minute read
Motorcortex SLX Simulink extension library
, is used as a module for Motorcortex. For example the Simulink function block input
can be linked to a control module parameter as the function block output
can be linked to an etherCAT device input.
Refer to simulink extension library for the git repository.
Git clone motorcortex-slx:
Clone the motorcortex-slx git repository, to your computer, for example in your ~/workspace$:
folder:
git clone git@git.vectioneer.com:vectioneer-core/motorcortex-slx.git
If git-clone was successful the motorcotex-slx folder is visible inside the workspace
folder.
Generate C Code from MATLAB & Simulink Model:
Below is an explanation how to build a simple Simulink model and generate C code from it that can be compiled into a Motorcortex Application.
-
Open
MATLAB
, inside the command window »> type:simulink
press enter. NowSimulink
will open. -
Open the motorcortex-slx template: in the side bar on the left press
open...
, select the folder in which you stored motorcortex-slx for example:workspace/motorcortex-slx/templates/
selectmyModelEmpty.slx
and press open. -
Create a
simulink function
, for example a standard input to output function with a build in delay function. SelectLibrary Browser
, drag and drop the followingblocks
in the workspace window:Constant
,Delay
andOut1
. -
Link the function blocks by means of connecting the arrows and rename the
Constant
block toMyRenamedConstant
. -
Now that you made a
simulink model
, the next step is to generateC
code and compile that in a Motorcortex Application. SelectAPPS
and pressSimulink Coder
, this addsC CODE
to top menu bar.

- Press
Generate Code
, this creates amyModelEmpty_grt_rtw
in yourworkspace
folder. Rename this folder tomyModelEmpty
and copy paste this to the tempate project models folder:workspace/motorcortex-slx/models
.

Now you added you Simulink model to you motorcortex project.
Install Simulink libraries:
Go to Motorcortex.io, login with your account -> open path: projects/Simulink/sll-libs
and deploy
libraries to the controller.

Build Motorcortex-slx Application:
-
Open
Clion
, inside top menu bar -> selectFile
-> pressopen
-> search for motorcortex-slx folder:/home/philippe/workspace/motorcortex-slx
, select and pressOK
. This opens themotorcortex-slx
project. -
Inside top menu bar, select
File
, presssettings...
. Go toBuild, Execution, Deployment
, selectToolchain
and check ifRemote Host
is connected, for more information go to Set-up Remote Host. -
Inside
Build, Execution, Deployment
selectCMake
. This opensprofiles tab
, pressToolchain:
toRemote Host
andBuild type:
toDebug
, pressApply
andOK
. -
Right click on
motorcortex-slx
project and pressreload CMake project
. -
On the left hand side, inside the folder
motorcortex-slx/test/slx-demo/
openmain.cpp
. -
Inside
Edit Run/Debug configuration dialog
selectslx_demo
andDebug-Remote Host
. -
Inside
Edit Run/Debug configuration dialog
selectEdit Configurations...
, selectslx_demo
and fill in program argument:-c config/config. json
-> pressApply
andOK
.
Now you have to include the simulink model myModelEmpty
to the main.cpp
and CMakeList.txt
.
- Delete
cmake-build-debug
andcmake-build-debug-remote-host
, you later generate new ones fromCMakeList.txt
, with themyModelEmpty
included. If you have done that you openmain.cpp
and include the following items:- Include
myModelEmpty.h
this is done with#include "myModelEmpty.h"
myModelEmpty
is the4th
model, to construct this model under#endif
you include:auto model4 = constructSimulinkModel(myModelEmpty, simulink);
- Inside
controlsTask2.add
, you have to addmodel4
like:controlsTask2.add({&model2, &model3, &model4});
- Include


You included the myModelEmpty
to the main.cpp!
- Now you have to include
myModelEmpty
to theCMakeList.txt
, for this you have to include the following items to theCMakeList.txt
:- Inside
target_include_directories
you include:${MODELS_LOCATION}/myModelEmpty
. - Inside
target_link_libraries
you include:mcx-slx-myModelEmpty
, like this:target_link_libraries(slx_demo mcx-sll mcx-slx-myModel mcx-slx-hisModel mcx-slx-gantry mcx-slx-myModelEmpty mcx-core)
- Press
Reload changes
this will build newcmake-build-debug
andcmake-build-debug-remote-host
.
- Inside

Now you set main.cpp
and Cmake
such that it will include myModelEmpty
to the application.
- Before you run the application, make sure there is no application running on your controller (
sudo motorcortex stop
). Now you can build your application on your controller by means of pressing therun
button.

If the build is successful your motorcortex application is running on your controller!
DESK - Simulink Functional Test
These steps will check the functionality of your Simulink model.
-
Go to desk, and log-in, how to do this check out Motorcortex-DESK.
-
Inside the three open path:
/root/Simulink/myModelEmpty/
Set:- DelayBlock/DSTATE:
1
- MyConstant/Value:
1
- Include
MyConstant/Value
andOutPut
to the plot. - Check DESK-tool
settings
and putfrequency divider
to1
.
- DelayBlock/DSTATE:

Congrats your simulink creation works with motorcortex!