ACADOS - Simulink

Hi :wave:

I have developed a NMPC controller for a simple wind turbine model using the Matlab interface. And I been able to simulate a close loop simulation:

% simulation loop
for i_sim=1:n_sim  
    
    % select current state 
    x_ThisStep                      = x_sim(:,i_sim);
  
    % solve ocp in every step if n_stage=n_step=1 or after each n_step*n_stage steps, starting at 1
    if n_stage*n_step == 1 || mod(i_sim,n_step*n_stage) == 1 
       
        % init ocp
        x_ocp_0                     = x_ThisStep; % current state
        if i_nmpc>1 % warm start      
            x_ocp_init              = [x_ocp_opt(:,n_stage+1:end),repmat(x_ocp_opt(:,end),1,n_stage)];
            u_ocp_init              = [u_ocp_opt(:,n_stage+1:end),repmat(u_ocp_opt(:,end),1,n_stage)];
        end
        p_ocp                       = p_nmpc     (:,:,i_nmpc);

        % solve ocp
        [x_ocp_opt,u_ocp_opt,ocp]   = SolveOcp(x_ocp_0,x_ocp_init,u_ocp_init,p_ocp,x_cond,u_cond,ocp);

        % store nmpc results
        x_nmpc(:,:,i_nmpc)          = x_ocp_opt;
        u_nmpc(:,:,i_nmpc)          = u_ocp_opt;    

        % update counters
        i_nmpc                      = i_nmpc+1;
        i_stage                     = 0;
    end
    
    % Update i_stage in every step if n_step=1 or after each n_step, starting at 1  
    if n_step == 1 ||  mod(i_sim,n_step) == 1        
        i_stage                     = i_stage+1;
    end

    % select current input and parameter
    u_ThisStep                      = u_ocp_opt(:,i_stage);
    p_ThisStep                      = p_sim(:,i_sim); 
    
    % solve sim
    [x_NextStep]                    = SolveSim(x_ThisStep,u_ThisStep,p_ThisStep,x_cond,u_cond,sim);
 
    % store simulation results
    u_sim(:,i_sim)                  = u_ThisStep;
    x_sim(:,i_sim+1)                = x_NextStep;

With the expected results. And I would like to do the same using Simulink.

To do so, I have checked the example located here acados\examples\acados_matlab_octave\getting_started\simulink_example.m

After checking the example I have compiled the code using the following commands:

%% Simulation model and OCP objects
sim             = acados_sim(sim_model, sim_opts);
ocp             = acados_ocp(ocp_model, ocp_opts);

%% Render templated Code for the model contained in ocp object
%
ocp.generate_c_code;

%% Compile Sfunctions
cd c_generated_code

make_sfun; % ocp solver
make_sfun_sim; % integrator

Then I just call the generated code like it is done on the example.

However, when I use the same inputs from my script code the Simulink is not performing as expected. And I cannot find any reason to it…

Does anyone know what could be the issue? Any help would be welcomed :pray:

Thanks in advance :handshake:

Javier

Hi Javier,

there can be small differences between the template based solver and the normal Maltab solver, since it uses different backends as showed here:
https://docs.acados.org/matlab_octave_interface/index.html#interface-structure

You could set up a test in this fashion and try to set up a minimal example where they differ.acados/test_template_lin_mass_ocp.m at master · acados/acados · GitHub
Note that this doesnt work on Windows AFAIK

Cheers!