Using SQP-RTI in Simulink / on dSpace

Hi guys,

my goal is to apply an MPC to an embedded system. Therefore it will be necessary to run the code on a dSpace RT system, which I’ve never done before. My first approach will be to integrate my controller into a Simulink model and try to make it run on the dSpace.

I’ve successfully used the matlab interface to generate an S-Function of my controller, and my simulation results are satisfying. As I’m using the SQP-RTI algorithm, I would like to exploit the possibility of spliting my OCP to (1) the preparation phase and (2) the feedback phase. My question is, how can one implement this in a simulink model? Is this even possible when using the acados S-Function generation? Or would you suggest integrating my controller into Simulink differently, and what would your suggestion be?

Thanks in advance for your help!

Greets

Hi,

the split into preparation and feedback phase is not done yet for the S-function.

Sounds good, I would focus on that first.
If you find that your controller then is not fast enough, one can think about how to adapt the S-function with the RTI phase split.

Note that it was recently reported that the timings saved through that are lower than expected.


Maybe you can first time the different phases to see how much time you would gain.

Cheers

Hi @FreyJo,

thanks for your fast reply, I’ll first focus on that approach! I’m right now struggling to build my Simulink model containing the acados S-Function using the Simulink Coder. It seems like there is some problem including all the different header/ source files, leading to error messages like e.g. " undefined reference to `ocp_nlp_plan_create’ ". I have already tried to include some directories and acados_solver_(…).c as a source file, but now I don’t know how to continue. Is this a known issue and do you have any suggestion on how to solve it?

Thanks a lot!

Hey @bissingen,

if you rebuild the examples like wind_turbine_nx6 fitting to your problem you should be able to implement it in a simulink model without any source files. You just need the acados_solver_sfunction_ocp_model. c and -.mexw64 files.

In your code you should create the ocp first, then use ocp.generate_c_code and make_s_fun.m. In make_s_fun.m you will see the needed sources and libraries being included automatically in the build:

SOURCES = [ ...
        'ocp_model_model/ocp_model_impl_dae_fun.c ', ...
        'ocp_model_model/ocp_model_impl_dae_fun_jac_x_xdot_z.c ', ...
        'ocp_model_model/ocp_model_impl_dae_jac_x_xdot_u_z.c ', ...
        'ocp_model_constraints/ocp_model_constr_h_fun.c ', ...
        'ocp_model_constraints/ocp_model_constr_h_fun_jac_uxt_hess.c ', ...
        'ocp_model_constraints/ocp_model_constr_h_fun_jac_uxt_zt.c ', ...
        'acados_solver_sfunction_ocp_model.c ', ...
        'acados_solver_ocp_model.c '
      ];

INC_PATH = 'D:\ACADOS_C_Code_Test\ConfigDesk\Models\acados\examples\acados_matlab_octave\..\../include';

INCS = [ ' -I', fullfile(INC_PATH, 'blasfeo', 'include'), ...
         ' -I', fullfile(INC_PATH, 'hpipm', 'include'), ...
        ' -I', INC_PATH, ' -I', fullfile(INC_PATH, 'acados'), ' '];

CFLAGS  = ' -O';

LIB_PATH = 'D:\ACADOS_C_Code_Test\ConfigDesk\Models\acados\examples\acados_matlab_octave\..\../lib';

LIBS = '-lacados -lhpipm -lblasfeo';

eval( [ 'mex -v -output  acados_solver_sfunction_ocp_model ', ...
    CFLAGS, INCS, ' ', SOURCES, ' -L', LIB_PATH, ' ', LIBS ]);

Edit: Note that this depends on your problem formulation.

If you want to use the simulink coder to go on another target system like dspace rt you will have to compile the libraries and source files again fitting to dspace, e.g. with toolchain-files. (see my post here or the issue un github).

If you have any questions dont hesitate to ask.

greets, Alex

Hi Alex,

thanks for your reply! In my simulation the S-Function works fine. But when I try to build my model on my windows pc using simulink coder, there are occuring many errors. I’ve never worked with dspace or the simulink coder before, so what do you mean with toolchain files? Do you maybe have an example for that?

I’ve googled a lot and found out that S-Functions need to be inclined (there needs to exist a .tlc file for them) to be compiled by the simulink coder. Is this somehow connected to those toolchain files you mentioned? Unfortunately, I’m not really into C programming / the whole compiling process :frowning:

Greets
Kelvin

I meanwhile managed to compile my simulink model using the simulink compiler on my windows pc. I forgot to include all sources/libs in the simulink configuration parameters/s-function modules according to make_sfun.m, now it’s working fine. I’ll let you know about my progress with compiling erverything for the dspace system in the next days :slight_smile:

Nice, good to hear!
I had to challenge the same problems. Which dSpace System are you using? Do you have access to the software dspace configuration desk?
There might be problems with the compilers, dSpace Scalexio uses QNX 6.5, the autoboxes partly use “power Compiler” MPCC, etc.
Depending on the compiler the syntax of the binary code might change (binary incompatibility) and thus produce some errors running the software on the target dspace system.
The toolchain files provide this information (which compiler, libs, etc to use) to an user-friendly compilation software like cmake. Cmake has a gui and e.g. acados and all the libs are completely prepared to easily being compiled by those.

I dont know if you know this information but I had to learn it the hard way. :smiley:

Btw, the dSpace Support did a good joob, maybe worth a try if you have a valid license.

greets, Alex

Hi Alex,

I’m using dSpace PX10, which also uses QNX 6.5. So it seems that using your toolchain file could solve my problem as well. Can you help me on how to use the toolchain files?
Right now, when using the simulink coder (with the QNX 6.5, the “dSpace-Compiler”), the acados.lib/ hpipm.lib/… files are not being recognized due to their file format.

Thanks a lot!

Hi @bissingen,

i went through exactly the same, funny. :wink:
My toolchain file is online in my git: https://github.com/alexwin9478/Exchange_MPC/blob/master/Toolchain-dSpace_v3.cmake

You need Cmake installed on your system, where the dspace compiler lays etc. you start the cmake gui in the acados_root_folder, choose “Unix Makefiles” and the toolchain file. Now you have to press “configure” and change the settings, the most important is to choose “Target=generic” for alle availalbe Target-Options. Another important one is “ext_dependencies = 0”.
Dont forget to set the Environment variables in windows “QNX_Host” and “QNX_TARGET” to the regarding paths of the QNX Compiler in the dspace folders.

I got help from @giaf in this post: S-Function fails Simulink Compile Check

Keep me updated about your progress. :+1:
greets, Alex

Nice, I’m trying it out right now!

Hi guys,

I’ve been trying to cross-compile the Acados-libraries for my target system (dS1007) using the approach from @AlexWin. It helped me a lot, but its still not working for me…

The code generation for my simulink model is working, but an error occurs when compiling the code for my taget system

STARTED Linking…

libacados.a(ocp_nlp_common.c.o): Relocations in generic ELF (EM: 3)

LINKING FAILED

It seems, that cross-compiled libraries still don’t fit my hardware. Does any of you guys have some suggestions? Is it even possible to use Cmake to cross-compile Acados for the dS1007?

Thanks a lot, I really appreciate your help!

You got am PM, lets have a look at this together. Alex

1 Like