Code Generation for S-function - Matlab Interface

Hi guys,

I am trying to use acados.

My goals are:

  1. generate the code through acados Matlab Interfaces
  2. run it from MATLAB

Afterwords I would like to port it to an embedded platform by using S-function.
This will in theory allow me to directly deploy the code onto my embedded platforms.
Therefore:
3) Generate the code for integration into a S-function
4) Run a Simulink model using the S-function

I need advices on where to start looking at.

For the points 1) and 2) I am able to run the examples in the folder
…/acados/examples/acados_matlab_octave/

For points 3) and 4) I don’t even know where to start :slight_smile:

Thanks a lot,

BilloBallo

Hi BilloBallo,

the S-function generation is happening through the template interface.
If you were able to set up an acados ocp solver ocp, you can run ocp.generate_c_code.
It will generate C code with the ocp formulation and options specified.
Moreover, it generates a main function, Makefile, Headers and an S-function.
To go embedded, you can also use the Code without the S-function, which might be easier depending on the plattform.
For the S-function, navigate into c_generated_code and run make_sfun to obtain the compiled S-function.
You can put this S-function into the S-function block of Simulink.
Note, that not all settings are supported by the interfaces yet, but feel free to post again, if you need something specific.
There is an ongoing Pull Request in which aims to improve the S-function generation, see https://github.com/acados/acados/pull/518.

Cheers,
Jonathan

1 Like

Hi Jonathan,
thanks a lot for your reply.
I was able to set up the acados ocp solver with matlab interface, but did not generate the code successfully.

I have added to the example file:
“ACADOS_ROOT_PATH/examples/acados_matlab_octave/linear_mass_spring_model/example_ocp.m”
ocp.generate_c_code() at the end of the script and gave me back this error:

#######################################################################
Error using ocp_generate_c_code (line 15)
mex templating does only support linear_ls cost for now. Got cost_type: ext_cost, cost_type_e: ext_cost.
Notice that it might still be possible to solve the OCP from MATLAB.

Error in acados_ocp/generate_c_code (line 144)
ocp_generate_c_code(obj)

Error in example_ocp (line 338)
ocp.generate_c_code()
#######################################################################

Since the error is saying “…still be possible to solve the OCP from MATLAB…” and not generate code I guess Matlab interface does not support code generation yet, am I right?

Do I need to use python only “acados_template” interface for this?

I have installed the python package from the folder
"ACADOS_ROOT_PATH/interfaces/acados_template/
running python3 install .

Then, I tried to run from the folder
“ACADOS_ROOT_PATH/examples/acados_python/test”
python3 main_test.py
but it gives me back the following error

#######################################################################
sh: 1: python: not found
Traceback (most recent call last):
File “main_test.py”, line 58, in
raise Exception(“acados status = {} on test {}. Exiting\n”.format(status, parameters))
Exception: acados status = 32512 on test (‘LS’, ‘ERK’, ‘PARTIAL_CONDENSING_HPIPM’, ‘SQP’). Exiting
#######################################################################

I have also added the env variable

export LD_LIBRARY_PATH=ACADOS_ROOT_PATH/lib

I hope I explained clearly what I did,
Thanks a lot.

BilloBallo

Dear BilloBallo,

sorry I missed your reply for a while.

mex templating does only support linear_ls cost for now. Got cost_type: ext_cost, cost_type_e: ext_cost.
Notice that it might still be possible to solve the OCP from MATLAB.

this message is meant to say that it is supported by the MEX interface, but not by the templates.
This being sad, the templates do not (yet) support all possible options/ modules of acados, as the error says, it only supports linear least squares (linear_ls) as a cost module.
Here, you can find the problem description handled by acados, and the modules that can be used for cost, constraints and dynamics.
For the examples in <acados_root>/examples/acados_matlab_octave/getting_started, the code generation works, (at least on the GUC branch, which is to be merged soon), since they use more basic modules.
I hope you will give these another shot.

Just to note, the templates are the bottleneck for using more advanced acados modules.
I am not sure about the Python error.
But especially, if you want to use an S-function eventually, I would stick to Matlab.

Cheers,
Jonathan

Short addition to the linear mass spring example in Matlab, you can switch to using the modules supported by the templates easily.
Just use
cost_type = 'auto'
and
this constraint formulation

    % bounds on x and u
    	nbx = nx/2;
    	nbu = nu;
    	ng = 0;
    	nh = 0;