/
acados_ocp.m
150 lines (115 loc) · 4.21 KB
/
acados_ocp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%
% Copyright 2019 Gianluca Frison, Dimitris Kouzoupis, Robin Verschueren,
% Andrea Zanelli, Niels van Duijkeren, Jonathan Frey, Tommaso Sartor,
% Branimir Novoselnik, Rien Quirynen, Rezart Qelibari, Dang Doan,
% Jonas Koenemann, Yutao Chen, Tobias Schöls, Jonas Schlagenhauf, Moritz Diehl
%
% This file is part of acados.
%
% The 2-Clause BSD License
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.;
%
classdef acados_ocp < handle
properties
C_ocp
C_ocp_ext_fun
model_struct
opts_struct
end % properties
methods
function obj = acados_ocp(model, opts)
obj.model_struct = model.model_struct;
obj.opts_struct = opts.opts_struct;
[~,~] = mkdir(obj.opts_struct.output_dir);
addpath(obj.opts_struct.output_dir);
% detect GNSF structure
if (strcmp(obj.opts_struct.sim_method, 'irk_gnsf'))
if (strcmp(obj.opts_struct.gnsf_detect_struct, 'true'))
obj.model_struct = detect_gnsf_structure(obj.model_struct);
generate_get_gnsf_structure(obj.model_struct, obj.opts_struct);
else
obj.model_struct = get_gnsf_structure(obj.model_struct);
end
end
% compile mex without model dependency
if (strcmp(obj.opts_struct.compile_mex, 'true'))
ocp_compile_mex(obj.opts_struct);
end
obj.C_ocp = ocp_create(obj.model_struct, obj.opts_struct);
% generate and compile casadi functions
if (strcmp(obj.opts_struct.codgen_model, 'true'))
ocp_generate_casadi_ext_fun(obj.model_struct, obj.opts_struct);
end
obj.C_ocp_ext_fun = ocp_create_ext_fun();
% compile mex with model dependency & set pointers for external functions in model
obj.C_ocp_ext_fun = ocp_set_ext_fun(obj.C_ocp, obj.C_ocp_ext_fun, obj.model_struct, obj.opts_struct);
% precompute
ocp_precompute(obj.C_ocp);
end
function solve(obj)
ocp_solve(obj.C_ocp);
end
function eval_param_sens(obj, field, stage, index)
ocp_eval_param_sens(obj.C_ocp, field, stage, index);
end
function set(varargin)
if nargin==3
obj = varargin{1};
field = varargin{2};
value = varargin{3};
ocp_set(obj.model_struct, obj.opts_struct, obj.C_ocp, obj.C_ocp_ext_fun, field, value);
elseif nargin==4
obj = varargin{1};
field = varargin{2};
value = varargin{3};
stage = varargin{4};
ocp_set(obj.model_struct, obj.opts_struct, obj.C_ocp, obj.C_ocp_ext_fun, field, value, stage);
else
disp('acados_ocp.set: wrong number of input arguments (2 or 3 allowed)');
end
end
function value = get(varargin)
if nargin==2
obj = varargin{1};
field = varargin{2};
value = ocp_get(obj.C_ocp, field);
elseif nargin==3
obj = varargin{1};
field = varargin{2};
stage = varargin{3};
value = ocp_get(obj.C_ocp, field, stage);
else
disp('acados_ocp.get: wrong number of input arguments (1 or 2 allowed)');
end
end
function delete(obj)
if ~isempty(obj.C_ocp_ext_fun)
ocp_destroy_ext_fun(obj.model_struct, obj.C_ocp, obj.C_ocp_ext_fun);
end
if ~isempty(obj.C_ocp)
ocp_destroy(obj.C_ocp);
end
end
end % methods
end % class