Skip to content

How to generate code from a Simulink model

Diego Ferigo edited this page Sep 28, 2018 · 1 revision

This guide briefly describes how to generate, compile, and run C++ sources from a Simulink model.

Resources:

Prerequisites

Generate the C++ code

  1. Clone robotology/whole-body-controllers
  2. Configure the project:
    mkdir build
    cd build
    cmake ..
  3. Open the controller
  4. Plug the robot (i.e. open Gazebo or turn on the real robot)
  5. Press Ctrl+D
  6. Press the Build button

The code generation process will now start. If there are no errors, a folder <nameofthemdl>_grt_rtw should be generated in the matlab current folder.

Deploy the model

# Clone the repository
git clone https://github.com/robotology-playground/autogenerated-whole-body-controllers.git

# Check the branches
# At the time of writing there are master for iCubGazeboV2_5, and robot/iCubGenova04
cd autogenerated-whole-body-controllers
git branch -a

# Checkout the right branch
git checkout <selected branch>

# Configure robotology/whole-body-controllers
cd <dir>/whole-body-controllers
mkdir build
cd build
cmake -DAUTOGENERATED_WBC_SOURCE_DIR="<dir of autogenerated-whole-body-controllers>"

# Copy the models to the autogenerated-whole-body-controllers repository
make copy-autogenerated-models

# Commit the models
cd <dir>/autogenerated-whole-body-controllers
git add autogenerated/
git commit -a

# Deploy the new models
git push

# Wait the tests to pass. Currently we test Ubuntu and OSX.

Compile and run the autogenerated sources

At the current stage the sources can be compiled only if Simulink and Simulink Coder are installed due to headers that are not exported during the code generation process. The executable can than be deployed to the target platform.

cd <dir>/autogenerated-whole-body-controllers
cd build
cmake ..
cmake --build .

At this point the executables are generated in the build/bin folder and they can be executed with ./CoderMain_<nameofthemdl>. The simulation can be stopped gracefully pressing Ctrl+C.