OSQP Error

530 views
Skip to first unread message

Hoomaan Moradi

unread,
Dec 10, 2021, 5:08:51 AM12/10/21
to CasADi

Hello, I am trying to generate the C code to be used as a MPC controller in Simulink. I am using SQPmethod, but MATLAB returns this error:

 

Cannot open include file: 'osqp/osqp.h': No such file or directory

 

This is the code I have written:

clear all

clc

close all

 

import casadi.*

 

opti = casadi.Opti();

 

t_0 = 0;

N = 5;

t_s = 0.1;

gamma = 1;

 

x = opti.variable(2, N);

u = opti.variable(1, N);

 

Wm = opti.parameter(1, 1);

W1 = [5 0 0;0 5 0;0 0 0.1];

Wf = [1 0;0 1];

 

g = opti.variable(2*N, 1);

x_0 = opti.parameter(2, 1);

 

J = 0;

x_end = [10; 10];

 

jjj=1;

for iii = 1:N-1

 

    x_next = rk4_model((iii - 1) * t_s, x(:, iii), u(iii), t_s, Wm);

 

    g(jjj:jjj+1, 1) = (x(:, iii+1) - x_next);

    jjj=jjj+2;

    J = J + ((gamma^iii)* (sqrt([x(:, iii) - x_end;u(iii)]'*(W1'*W1)*[x(:, iii) - x_end;u(iii)])));

end

 

J = J + ((gamma^N)*(sqrt((x(:, (N)) - x_end)' * (Wf'* Wf) * (x(:, (N)) - x_end))));

 

gg = g==0;

opti.subject_to(gg);

opti.subject_to(x(:, 1) == x_0);

opti.subject_to(0.1 <= x(1, :) <= 30);

opti.subject_to(0.1 <= x(2, :) <= 30);

opti.subject_to(0 <= u <= 22);

 

opti.minimize(J);

 

opts = struct;

opts.qpsol = 'osqp';

 

opti.solver('sqpmethod', opts);

 

opti.set_initial(x, repmat([1;1], 1, N));

opti.set_initial(u, repmat([0], 1, N));

opti.set_value(x_0,[1;1]);

opti.set_value(Wm,3.3);

sol = opti.solve();

 

u_ = full(sol.value(u(1)));

 

f = opti.to_function('f',{Wm},{u_},'para','u_opt');

 

cg_options = struct;

cg_options.casadi_real = 'real_T';

cg_options.real_min    = 'real_T'; 

cg_options.casadi_int  = 'int_T';

cg_options.with_header = true;

cg = CodeGenerator('f',cg_options);

cg.add_include('simstruc.h');

cg.add(f);

cg.generate();

 

disp('CasADi codegen completed: created f.c')

 

pause(1)

disp('Compiling S function')

mex s_function.c f.c

 

For generating the C code, I used this post:

https://web.casadi.org/blog/s-function/

I also installed the osqp solver for MATLAB and C++ from its website, but the result did not change.

Marc-Simon Schäfer

unread,
Dec 14, 2021, 6:52:38 AM12/14/21
to CasADi
Hi,

try to add some arguments for your mex( ) call. Especially the include files and directories (for osqp). Here are some lines from another running example.

libPath = casadi.GlobalOptions.getCasadiPath();
incPath = casadi.GlobalOptions.getCasadiIncludePath();
mex('-v',...        % verbose mode
    ['-I' incPath],...      % includes
    ['-L' libPath],...      % used lib path
    ['-losqp'],...           % osqp lib for linking
    '-lcasadi',...
    '-Dcasadi_real_min',...
    's_function.c',...
    [filename '.c']...
    );
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages