I am trying to generate a C code from a MATLAB code (MPC) that uses CasADi. I initially tried to use MATLAB Coder to do it, but it couldn't generate code from casadi.Function() because it contains some statements that are not supported by MATLAB Coder.
Therefore I am trying to use the code generation feature of Casadi itself, but from the user guide and the examples, what I understand is that it only generates C code from a function, is it correct? Does it mean that I should generate C code from the casadi.Function(), and use MATLAB Coder for the rest?
That's why I am confused about how to generate a C code. The rough structure of the MATLAB code is the following (only the parts that use Casadi are shown):
ocp.vars.E_BSPb = casadi.SX.sym('E_BSPb');
ocp.vars.E_BSLi = casadi.SX.sym('E_BSLi');
..... and some more symbolic variable declerations
..... (some more code)
ocp.sys.f = casadi.Function('f', {ocp.sys.x, ocp.sys.u}, {ocp.sys.rhs});
..... (some more code)
for k = 1:ocp.N
ocp.lftsys.X(:, k+1) = ocp.sys.f(ocp.lftsys.X(:, k), ocp.lftsys.U(:, k));
end
.....
ocp.lp_prob = struct('f', ocp.J, 'x', ocp.lftsys.U, 'g', ocp.cnstr.g, 'p', ocp.lftsys.P);
ocp.solver = casadi.nlpsol('solver', 'ipopt', ocp.lp_prob, ocp.solver_opts);
.....
for k = 1:prms.sim.n_days * 24 / prms.sim.dt
.....
ocp.sol = ocp.solver('x0', ocp.sol_guess, 'lbx', ocp.cnstr.lbu, 'ubx', ocp.cnstr.ubu,...
'lbg', ocp.cnstr.lbg, 'ubg', ocp.cnstr.ubg, 'p', ocp.prmvals);
.....
end
I thought of generating code for the casadi.Function(), and then somehow assigning that generated function to ocp.sys.f, and then using MATLAB Coder to generate C code. But I dont even know if such an assignment is feasible.
Does someone know what would be the best way to generate C code from this code?
Thanks very much :)