10.3. dSPACE MicroAutoBox III

10.3.1. Code Generation

The steps to deploy a FORCES PRO controller on a dSPACE MicroAutoBox III are detailed below.

  1. Set the code generation options (see Figure 10.28):

codeoptions.platform = 'dSPACE-MABXIII'; % to generate code for the MicroAutoBox III
codeoptions.printlevel = 0; % printing is not supported for the MicroAutoBox III
codeoptions.cleanup = 0; % to keep necessary files for target compile


When generating code for the MicroAutoBox III, codeoptions.optlevel can take values 0-4 instead of 0-3 where

  • 0: no optimization

  • 1: optimize for size

  • 2: optimize for speed

  • 3: optimize for size and speed

  • 4: optimize for size and speed with more precise numerics


Figure 10.28 Set the appropriate code generation options.

  1. Create a new Simulink model (henceforth referred to as dSPACE-MABXIII.slx) using the dSPACE Run-Time Target template provided by dSPACE and save it in the BasicExample folder (see Figure 10.29).

  2. Populate the Simulink model with the system you want to control (see Figure 10.30).


Figure 10.29 Create a Simulink model.


Figure 10.30 Populate the Simulink model.

  1. Run the BasicExample.m script to perform code generation for your solver (henceforth referred to as FORCESNLPsolver, placed in the folder “BasicExample”). This will create the necessary files for your building (see Figure 10.31 , Figure 10.32 and Figure 10.33).

  2. The FORCESNLPsolver_simulinkBlock.<mex_extension> file (created during code generation) needs to be in the same path as your model (see Figure 10.34).


Figure 10.31 Generated files.


Figure 10.32 Solver interface files.


Figure 10.33 Solver libraries.


Figure 10.34 The .<mex_extension> solver file is in the same path as the model.

  1. Open the FORCESNLPsolver_lib.mdl Simulink model file, contained in the interface folder of the FORCESNLPsolver folder created during code generation (see Figure 10.35).

  2. Copy-paste the FORCES PRO Simulink block into your simulation model and connect its inputs and outputs appropriately (see Figure 10.36).


Figure 10.35 Open the generated Simulink solver model.


Figure 10.36 Copy-paste and connect the FORCES PRO block.

  1. Access the Simulink model’s options. In the “Solver” tab, set the options (see Figure 10.37):

  • Simulation start/stop time: Depending on the simulation wanted.

  • Solver type: Discrete or fixed-step.

  • Fixed-step size: Needs to be higher than the execution time of the solver.

  1. In the “Code Generation” tab, set the options (see Figure 10.38):

  • System target file: dsrt.tlc

  • Language: C

  • Generate makefile: Checked

  • Template makefile: dsrt_default_tmf

  • Make command: make_dsrt

  1. In the “Code Generation/Custom Code” tab, include the directories (see Figure 10.39):

  • .\FORCESNLPsolver\include

  • .\FORCESNLPsolver\interface

  • .\FORCESNLPsolver\lib_target

  1. In the “Code Generation/Custom Code” tab, add the source files (see Figure 10.40):

  • FORCESNLPsolver_simulinkBlock.c

  • FORCESNLPsolver_casadi2forces.c

  • FORCESNLPsolver_model_1.c

  • FORCESNLPsolver_model_11.c

  1. In the “Code Generation/Custom Code” tab, add the library files (see Figure 10.41):

  • libFORCESNLPsolver.a


Figure 10.37 Set the Simulink solver options.


Figure 10.38 Set the Simulink code generation options.


Figure 10.39 Add the directories included for the code generation.


Figure 10.40 Add the source files used for the code generation.


Figure 10.41 Add the libraries used for the code generation.

  1. Access the FORCES PRO block’s parameters (see Figure 10.42).

  2. Remove the “FORCESNLPsolver” prefix from the S-function module (see Figure 10.43).


Figure 10.42 Open the FORCES PRO block’s parameters.


Figure 10.43 Remove the leading solver name from the S-function module.

  1. Create a new Project and Application in ConfigurationDesk. Select directory of project, name of project and application, the model dSPACE_MABXIII.slx as the application process and connected MicroAutoBox III to deploy to (see Figure 10.44).

  2. Go to the tasks tab and make sure the period of the Periodic Task matches the fixed step size selected in the Simulink model options (see Figure 10.45).

  3. Go to the build tab and start the building process. After building is complete the application will be loaded automatically in the MicroAutoBox III (see Figure 10.46).


Figure 10.44 Create project and application in ConfigurationDesk.


Figure 10.45 Set period of Periodic Task.


Figure 10.46 Build the project.

10.3.2. Solver Execution

The steps to simulate a FORCES PRO controller on a dSPACE MicroAutoBox III are detailed below.

  1. After code generation with FORCES PRO and building with the ConfigurationDesk, the ConfigurationDesk project will have generated files to use to run your model on the MicroAutoBox III (see Figure 10.47 and Figure 10.48).


Figure 10.47 The generated files from the ConfigurationDesk building.


Figure 10.48 The files necessary for the simulation of the FORCES PRO controller.

  1. Open dSpace Control Desk and select create new project and name it (see Figure 10.49).

  2. Name the experiment to execute (see Figure 10.50).

  3. Select the platform to which you will deploy the generated executable (see Figure 10.51).

  4. Import the variable description file BasicExample.sdf in order to have access to the model variables and see the results of the execution (see Figure 10.52).


Figure 10.49 Start a new project and name it.


Figure 10.50 Name your experiment.


Figure 10.51 Select the MicroAutoBox III platform.


Figure 10.52 Import the variable description file.

  1. On the project layout select the tab Variables and on the BasicExample.sdf category expand Model Root.

  2. Select U OUTPUT and X OUTPUT and Drag & Drop all the input variables together to the Layout. In the opened menu select Time Plotter (see Figure 10.53 and Figure 10.54).

  3. To see all the plots concurrently right-click on the left of the Y-axis and select YAxes-view> Horizontal stacked (see Figure 10.55).


Figure 10.53 Add the inputs of U OUTPUT in a Time Plotter.


Figure 10.54 Add the inputs of X OUTPUT in the same Time Plotter.


Figure 10.55 Select to show all the signals on the same plot with their own Y-axes

  1. Application should have already been loaded from the building of ConfigurationDesk. Otherwise, select the Platforms/Devices tab. Right-Click on your platform and select Real-Time Application> Load. Choose the executable file BasicExample.rta (see Figure 10.56 and Figure 10.57).

  2. Select Go Online and Start Measuring to see the results. (see Figure 10.58 and Figure 10.59).


Figure 10.56 Load the application on the dSPACE MicroAutoBox III.


Figure 10.57 Select BasicExample.rta from the ConfigurationDesk project folder.


Figure 10.58 Buttons Go Online and Start Measuring to receive execution results.


Figure 10.59 Plots and results from experiment on dSPACE MicroAutoBox III.