-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_front.c
253 lines (227 loc) · 7.14 KB
/
main_front.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright (c) The acados authors.
*
* 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.;
*/
// standard
#include <stdio.h>
#include <stdlib.h>
// acados
#include "acados/utils/print.h"
#include "acados/utils/math.h"
#include "acados_c/ocp_nlp_interface.h"
#include "acados_c/external_function_interface.h"
#include "acados_solver_front.h"
// blasfeo
#include "blasfeo/include/blasfeo_d_aux_ext_dep.h"
#define NX FRONT_NX
#define NP FRONT_NP
#define NU FRONT_NU
#define NBX0 FRONT_NBX0
int main()
{
front_solver_capsule *acados_ocp_capsule = front_acados_create_capsule();
// there is an opportunity to change the number of shooting intervals in C without new code generation
int N = FRONT_N;
// allocate the array and fill it accordingly
double* new_time_steps = NULL;
int status = front_acados_create_with_discretization(acados_ocp_capsule, N, new_time_steps);
if (status)
{
printf("front_acados_create() returned status %d. Exiting.\n", status);
exit(1);
}
ocp_nlp_config *nlp_config = front_acados_get_nlp_config(acados_ocp_capsule);
ocp_nlp_dims *nlp_dims = front_acados_get_nlp_dims(acados_ocp_capsule);
ocp_nlp_in *nlp_in = front_acados_get_nlp_in(acados_ocp_capsule);
ocp_nlp_out *nlp_out = front_acados_get_nlp_out(acados_ocp_capsule);
ocp_nlp_solver *nlp_solver = front_acados_get_nlp_solver(acados_ocp_capsule);
void *nlp_opts = front_acados_get_nlp_opts(acados_ocp_capsule);
// initial condition
double lbx0[NBX0];
double ubx0[NBX0];
lbx0[0] = 0;
ubx0[0] = 0;
lbx0[1] = 0;
ubx0[1] = 0;
lbx0[2] = 0;
ubx0[2] = 0;
lbx0[3] = 0;
ubx0[3] = 0;
lbx0[4] = 0;
ubx0[4] = 0;
lbx0[5] = 0;
ubx0[5] = 0;
lbx0[6] = 0;
ubx0[6] = 0;
lbx0[7] = 0;
ubx0[7] = 0;
lbx0[8] = 0;
ubx0[8] = 0;
lbx0[9] = 0;
ubx0[9] = 0;
lbx0[10] = 0;
ubx0[10] = 0;
lbx0[11] = 0;
ubx0[11] = 0;
lbx0[12] = 0;
ubx0[12] = 0;
lbx0[13] = 0;
ubx0[13] = 0;
lbx0[14] = 0;
ubx0[14] = 0;
lbx0[15] = 0;
ubx0[15] = 0;
lbx0[16] = 0;
ubx0[16] = 0;
lbx0[17] = 0;
ubx0[17] = 0;
lbx0[18] = 0;
ubx0[18] = 0;
lbx0[19] = 0;
ubx0[19] = 0;
lbx0[20] = 0;
ubx0[20] = 0;
lbx0[21] = 0;
ubx0[21] = 0;
lbx0[22] = 0;
ubx0[22] = 0;
lbx0[23] = 0;
ubx0[23] = 0;
lbx0[24] = 0;
ubx0[24] = 0;
ocp_nlp_constraints_model_set(nlp_config, nlp_dims, nlp_in, 0, "lbx", lbx0);
ocp_nlp_constraints_model_set(nlp_config, nlp_dims, nlp_in, 0, "ubx", ubx0);
// initialization for state values
double x_init[NX];
x_init[0] = 0.0;
x_init[1] = 0.0;
x_init[2] = 0.0;
x_init[3] = 0.0;
x_init[4] = 0.0;
x_init[5] = 0.0;
x_init[6] = 0.0;
x_init[7] = 0.0;
x_init[8] = 0.0;
x_init[9] = 0.0;
x_init[10] = 0.0;
x_init[11] = 0.0;
x_init[12] = 0.0;
x_init[13] = 0.0;
x_init[14] = 0.0;
x_init[15] = 0.0;
x_init[16] = 0.0;
x_init[17] = 0.0;
x_init[18] = 0.0;
x_init[19] = 0.0;
x_init[20] = 0.0;
x_init[21] = 0.0;
x_init[22] = 0.0;
x_init[23] = 0.0;
x_init[24] = 0.0;
// initial value for control input
double u0[NU];
u0[0] = 0.0;
u0[1] = 0.0;
u0[2] = 0.0;
u0[3] = 0.0;
u0[4] = 0.0;
u0[5] = 0.0;
u0[6] = 0.0;
u0[7] = 0.0;
u0[8] = 0.0;
u0[9] = 0.0;
u0[10] = 0.0;
u0[11] = 0.0;
u0[12] = 0.0;
u0[13] = 0.0;
u0[14] = 0.0;
u0[15] = 0.0;
u0[16] = 0.0;
u0[17] = 0.0;
// prepare evaluation
int NTIMINGS = 1;
double min_time = 1e12;
double kkt_norm_inf;
double elapsed_time;
int sqp_iter;
double xtraj[NX * (N+1)];
double utraj[NU * N];
// solve ocp in loop
int rti_phase = 0;
for (int ii = 0; ii < NTIMINGS; ii++)
{
// initialize solution
for (int i = 0; i < N; i++)
{
ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, i, "x", x_init);
ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, i, "u", u0);
}
ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, N, "x", x_init);
ocp_nlp_solver_opts_set(nlp_config, nlp_opts, "rti_phase", &rti_phase);
status = front_acados_solve(acados_ocp_capsule);
ocp_nlp_get(nlp_config, nlp_solver, "time_tot", &elapsed_time);
min_time = MIN(elapsed_time, min_time);
}
/* print solution and statistics */
for (int ii = 0; ii <= nlp_dims->N; ii++)
ocp_nlp_out_get(nlp_config, nlp_dims, nlp_out, ii, "x", &xtraj[ii*NX]);
for (int ii = 0; ii < nlp_dims->N; ii++)
ocp_nlp_out_get(nlp_config, nlp_dims, nlp_out, ii, "u", &utraj[ii*NU]);
printf("\n--- xtraj ---\n");
d_print_exp_tran_mat( NX, N+1, xtraj, NX);
printf("\n--- utraj ---\n");
d_print_exp_tran_mat( NU, N, utraj, NU );
// ocp_nlp_out_print(nlp_solver->dims, nlp_out);
printf("\nsolved ocp %d times, solution printed above\n\n", NTIMINGS);
if (status == ACADOS_SUCCESS)
{
printf("front_acados_solve(): SUCCESS!\n");
}
else
{
printf("front_acados_solve() failed with status %d.\n", status);
}
// get solution
ocp_nlp_out_get(nlp_config, nlp_dims, nlp_out, 0, "kkt_norm_inf", &kkt_norm_inf);
ocp_nlp_get(nlp_config, nlp_solver, "sqp_iter", &sqp_iter);
front_acados_print_stats(acados_ocp_capsule);
printf("\nSolver info:\n");
printf(" SQP iterations %2d\n minimum time for %d solve %f [ms]\n KKT %e\n",
sqp_iter, NTIMINGS, min_time*1000, kkt_norm_inf);
// free solver
status = front_acados_free(acados_ocp_capsule);
if (status) {
printf("front_acados_free() returned status %d. \n", status);
}
// free solver capsule
status = front_acados_free_capsule(acados_ocp_capsule);
if (status) {
printf("front_acados_free_capsule() returned status %d. \n", status);
}
return status;
}