MATLAB & Simulink

Description how to use MATLAB & Simulink with motorcortex.

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.

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.

  1. Open MATLAB, inside the command window »> type: simulink press enter. Now Simulink will open.

  2. 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/ select myModelEmpty.slx and press open.

  3. Create a simulink function, for example a standard input to output function with a build in delay function. Select Library Browser, drag and drop the following blocks in the workspace window: Constant, Delay and Out1.

  4. Link the function blocks by means of connecting the arrows and rename the Constant block to MyRenamedConstant.

    image not found
  5. Now that you made a simulink model, the next step is to generate C code and compile that in a Motorcortex Application. Select APPS and press Simulink Coder, this adds C CODE to top menu bar.

image not found
  1. Press Generate Code, this creates a myModelEmpty_grt_rtw in your workspace folder. Rename this folder to myModelEmpty and copy paste this to the tempate project models folder: workspace/motorcortex-slx/models.
image not found

Now you added you Simulink model to you motorcortex project.

Go to Motorcortex.io, login with your account -> open path: projects/Simulink/sll-libs and deploy libraries to the controller.

image not found

Build Motorcortex-slx Application:

  1. Open Clion, inside top menu bar -> select File -> press open -> search for motorcortex-slx folder: /home/philippe/workspace/motorcortex-slx, select and press OK. This opens the motorcortex-slx project.

  2. Inside top menu bar, select File, press settings.... Go to Build, Execution, Deployment, select Toolchain and check if Remote Host is connected, for more information go to Set-up Remote Host.

  3. Inside Build, Execution, Deployment select CMake. This opens profiles tab, press to add a new profile, change Toolchain: to Remote Host and Build type: to Debug, press Apply and OK.

    image not found
  4. Right click on motorcortex-slx project and press reload CMake project.

  5. On the left hand side, inside the folder motorcortex-slx/test/slx-demo/ open main.cpp.

  6. Inside Edit Run/Debug configuration dialog select slx_demo and Debug-Remote Host.

    image not found
  7. Inside Edit Run/Debug configuration dialog select Edit Configurations..., select slx_demo and fill in program argument: -c config/config. json -> press Apply and OK.

    image not found

Now you have to include the simulink model myModelEmpty to the main.cpp and CMakeList.txt.

  1. Delete cmake-build-debug and cmake-build-debug-remote-host, you later generate new ones from CMakeList.txt, with the myModelEmpty included. If you have done that you open main.cpp and include the following items:
    • Include myModelEmpty.h this is done with #include "myModelEmpty.h"
    • myModelEmpty is the 4th model, to construct this model under #endif you include: auto model4 = constructSimulinkModel(myModelEmpty, simulink);
    • Inside controlsTask2.add, you have to add model4 like: controlsTask2.add({&model2, &model3, &model4});
image not found
image not found

You included the myModelEmpty to the main.cpp!

  1. Now you have to include myModelEmpty to the CMakeList.txt, for this you have to include the following items to the CMakeList.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 new cmake-build-debug and cmake-build-debug-remote-host.
image not found

Now you set main.cpp and Cmake such that it will include myModelEmpty to the application.

  1. 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 the run button.
image not found

If the build is successful your motorcortex application is running on your controller!

These steps will check the functionality of your Simulink model.

  1. Go to desk, and log-in, how to do this check out Motorcortex-DESK.

  2. Inside the three open path: /root/Simulink/myModelEmpty/ Set:

    • DelayBlock/DSTATE: 1
    • MyConstant/Value: 1
    • Include MyConstant/Value and OutPut to the plot.
    • Check DESK-tool settings and put frequency divider to 1.
image not found

Congrats your simulink creation works with motorcortex!