OCP implementation in Simulink

Hi all,

My team has the goal to set up the control unit of a Fuel-Cell-Electric-Truck. The hardware we use is a MicroAutoBox 2 from dSpace which is quite compatible with Simulink. Therefore, the controller is programmed using Simulink and ported to the AutoBox thereafter.

I’m responsible for the development and implementation of the vehicle’s energy management. Part of this task is the full horizon prediction of the energy consumption. Modeling and solving this OCP is no problem using Matlab. But I have problems implementing the OCP solver in Simulink. My plan is to call this function when riding the truck every five minutes which allows enough computational time (thinking of it as a function).

I tried calling the Matlab scripts as Matlab-Functions which was impossible when importing the casadi package. Transfering the solver to c-code and implementing it as a S-Function only returns x_0, not the entire horizon.

Therefore, I have these questions:

Is it possible to solve an OCP using Simulink?

If this is possible, are there any examples? (I didn’t find anything)

Thanks for your help. I hope you can understand my problem. If there are any questions, feel free to ask them.



Hi Max,

Did you checkout this example?

you can set xtraj and utraj as outputs, see https://github.com/acados/acados/blob/master/interfaces/acados_template/acados_template/simulink_default_opts.json

you already solved the OCP as far as I understand and just want to change how to interact with the solver in simulink as I understand.


Someone sent me your recent paper: SAE MOBILUS
Please feel encouraged to share something like this here Acados featured work collection

1 Like

Hi Jonathan,

Thanks for your quick answer. I had a look at the linked example but did not know about the xtraj and utraj output option. This option allowed me to solve my problem.

Thanks a lot and Greetings


1 Like

your model can run in MicroAutoBox 2 from dSpace ?

I also want to run my s-function in MicroAutoBox 2.

Hey @Gaofei,

currently this is our plan. We’ve ordered the MicroAutoBox2 but are waiting for the delivery.

I will share my expiriences when we are working on the box.

Greetings Max

@maxbayerlein thanks,Looking forward to your good news.