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:simulinkpress enter. NowSimulinkwill 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.slxand 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 followingblocksin the workspace window:Constant,DelayandOut1. -
Link the function blocks by means of connecting the arrows and rename the
Constantblock toMyRenamedConstant.
-
Now that you made a
simulink model, the next step is to generateCcode and compile that in a Motorcortex Application. SelectAPPSand pressSimulink Coder, this addsC CODEto top menu bar.
- Press
Generate Code, this creates amyModelEmpty_grt_rtwin yourworkspacefolder. Rename this folder tomyModelEmptyand 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-slxproject. -
Inside top menu bar, select
File, presssettings.... Go toBuild, Execution, Deployment, selectToolchainand check ifRemote Hostis connected, for more information go to Set-up Remote Host. -
Inside
Build, Execution, DeploymentselectCMake. This opensprofiles tab, pressto add a new profile, changeToolchain:toRemote HostandBuild type:toDebug, pressApplyandOK.
-
Right click on
motorcortex-slxproject and pressreload CMake project. -
On the left hand side, inside the folder
motorcortex-slx/test/slx-demo/openmain.cpp. -
Inside
Edit Run/Debug configuration dialogselectslx_demoandDebug-Remote Host.
-
Inside
Edit Run/Debug configuration dialogselectEdit Configurations..., selectslx_demoand fill in program argument:-c config/config. json-> pressApplyandOK.
Now you have to include the simulink model myModelEmpty to the main.cpp and CMakeList.txt.
- Delete
cmake-build-debugandcmake-build-debug-remote-host, you later generate new ones fromCMakeList.txt, with themyModelEmptyincluded. If you have done that you openmain.cppand include the following items:- Include
myModelEmpty.hthis is done with#include "myModelEmpty.h" myModelEmptyis the4thmodel, to construct this model under#endifyou include:auto model4 = constructSimulinkModel(myModelEmpty, simulink);- Inside
controlsTask2.add, you have to addmodel4like:controlsTask2.add({&model2, &model3, &model4});
- Include
You included the myModelEmpty to the main.cpp!
- Now you have to include
myModelEmptyto theCMakeList.txt, for this you have to include the following items to theCMakeList.txt:- Inside
target_include_directoriesyou include:${MODELS_LOCATION}/myModelEmpty. - Inside
target_link_librariesyou 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 changesthis will build newcmake-build-debugandcmake-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 therunbutton.
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/ValueandOutPutto the plot. - Check DESK-tool
settingsand putfrequency dividerto1.
- DelayBlock/DSTATE:
Congrats your simulink creation works with motorcortex!