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;

Maybe an update just to avoid confusion: 1) you can use nonlinear least-squares when using the templates (simply they are not implemented in the MATLAB templating machinery - you can use NLSs from Python) 2) you can generate an S-Function from Python :slight_smile:

Hello,
I really like the code generation for s-function option for Matlab.

Is there a way to output all calculated states from the ocp block, instead of just the first one like in the wind_turbine_nx6 example?
temp

Hi @Freddy,

I am glad you like the S-function.

I think that the whole x trajectory in the OCP solver is not often needed, so I wont implement that.
However, please feel free to edit the template that is used for the S-function:

In particular, you would have to edit these sections:


Moreover, you will have to edit the Simulink block to adjust for the new outputs.

I hope that helps and isnt too involvedā€¦

Best,
Jonathan

1 Like

Hi @FreyJo,

is this info still up to date? I searched the acados directory for the error message and couldnā€™t find it like that anymore, just for external costs.
My problem is that I want to control not a state but a performance output based on the states. (PO is a torque, states are torsions and rotary speeds of a drivetrain). Doing the calculation based on the states and writing that into y / y_e would be my goto based on what I see in the examples (havenā€™t tested it yet), but for the linear cost function I would need to neglect the nonlinear aspects in order to squeeze it into the linear cost term.
My goal is to simulate/test it in Simulink and finally run it on an ECU to see how it performs.

Cheers,
Dennis

Hi @DennisWeir,

I added the nonlinear least squares cost type for this part of the interface last week, following C Code doesnt generate for nonlinear_ls cost type

It sounds like this is just what you need, or do you also need external cost type?

Cheers,
Jonathan

1 Like

Hi Jonathan,

nonlinear least squares should work just fine, thank you very much!

Cheers,
Dennis