Hi @chris,
The S-function was indeed never tested with an MHE problem, more specifically, the first input is always the initial state and sets lbx0, ubx0 internally, which is assumed there to be of size nx.
One would have to generalize the templated S-function Code with respect to MHE problems, such that x0 is not an input of the S-Function, if nbx0 != nbx see these lines:
https://github.com/acados/acados/blob/da58ccce733f03e6cd6b2447899c57a0abfc9db0/interfaces/acados_template/acados_template/c_templates_tera/acados_solver_sfun.in.c#L91-L94 https://github.com/acados/acados/blob/da58ccce733f03e6cd6b2447899c57a0abfc9db0/interfaces/acados_template/acados_template/c_templates_tera/acados_solver_sfun.in.c#L212-L217
Moreover, one should change the Outputs of the Sfunction.
These are hard coded here for now https://github.com/acados/acados/blob/da58ccce733f03e6cd6b2447899c57a0abfc9db0/interfaces/acados_template/acados_template/c_templates_tera/acados_solver_sfun.in.c#L358-L363
I guess the following outputs are also relevant in the MHE case:
out_status, out_KKT_res, out_cpu_time, out_sqp_iter.
Additionally, one would at the very least want the last value of x I guess.
What do you think?
I think, at least what I stated above should be added in the Sfunction template logic.
I would like to encourage you to try this yourself and make a pull request.
I am not sure when I will find time to do this myself.
General disclaimer: the S-function is not as flexible in terms of interacting with the solver. One might have to adapt the templated S-function code in order to add the inputs / outputs one wants for the specific application. For example, what was requested here: Code Generation for S-function - Matlab Interface
Best,
Jonathan