-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_sim_back.c
244 lines (215 loc) · 5.89 KB
/
main_sim_back.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
/*
* 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/sim_interface.h"
#include "acados_sim_solver_back.h"
#define NX BACK_NX
#define NZ BACK_NZ
#define NU BACK_NU
#define NP BACK_NP
int main()
{
int status = 0;
back_sim_solver_capsule *capsule = back_acados_sim_solver_create_capsule();
status = back_acados_sim_create(capsule);
if (status)
{
printf("acados_create() returned status %d. Exiting.\n", status);
exit(1);
}
sim_config *acados_sim_config = back_acados_get_sim_config(capsule);
sim_in *acados_sim_in = back_acados_get_sim_in(capsule);
sim_out *acados_sim_out = back_acados_get_sim_out(capsule);
void *acados_sim_dims = back_acados_get_sim_dims(capsule);
// initial condition
double x_current[NX];
x_current[0] = 0.0;
x_current[1] = 0.0;
x_current[2] = 0.0;
x_current[3] = 0.0;
x_current[4] = 0.0;
x_current[5] = 0.0;
x_current[6] = 0.0;
x_current[7] = 0.0;
x_current[8] = 0.0;
x_current[9] = 0.0;
x_current[10] = 0.0;
x_current[11] = 0.0;
x_current[12] = 0.0;
x_current[13] = 0.0;
x_current[14] = 0.0;
x_current[15] = 0.0;
x_current[16] = 0.0;
x_current[17] = 0.0;
x_current[18] = 0.0;
x_current[19] = 0.0;
x_current[20] = 0.0;
x_current[21] = 0.0;
x_current[22] = 0.0;
x_current[23] = 0.0;
x_current[24] = 0.0;
x_current[0] = [0];
x_current[1] = [0];
x_current[2] = [0];
x_current[3] = [0];
x_current[4] = [0];
x_current[5] = [0];
x_current[6] = [0];
x_current[7] = [0];
x_current[8] = [0];
x_current[9] = [0];
x_current[10] = [0];
x_current[11] = [0];
x_current[12] = [0];
x_current[13] = [0];
x_current[14] = [0];
x_current[15] = [0];
x_current[16] = [0];
x_current[17] = [0];
x_current[18] = [0];
x_current[19] = [0];
x_current[20] = [0];
x_current[21] = [0];
x_current[22] = [0];
x_current[23] = [0];
x_current[24] = [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;
// set parameters
double p[NP];
p[0] = [0];
p[1] = [0];
p[2] = [0];
p[3] = [0];
p[4] = [0];
p[5] = [0];
p[6] = [0];
p[7] = [0];
p[8] = [0];
p[9] = [0];
p[10] = [0];
p[11] = [0];
p[12] = [0];
p[13] = [0];
p[14] = [0];
p[15] = [0];
p[16] = [0];
p[17] = [0];
p[18] = [0];
p[19] = [0];
p[20] = [0];
p[21] = [0];
p[22] = [0];
p[23] = [0];
p[24] = [0];
p[25] = [0];
p[26] = [0];
p[27] = [0];
p[28] = [0];
p[29] = [0];
p[30] = [0];
p[31] = [0];
p[32] = [0];
p[33] = [0];
p[34] = [0];
p[35] = [0];
p[36] = [0];
p[37] = [0];
p[38] = [0];
p[39] = [0];
p[40] = [0];
p[41] = [0];
p[42] = [0];
p[43] = [0];
p[44] = [0];
p[45] = [0];
p[46] = [0];
p[47] = [0];
p[48] = [0];
p[49] = [0];
p[50] = [0];
back_acados_sim_update_params(capsule, p, NP);
int n_sim_steps = 3;
// solve ocp in loop
for (int ii = 0; ii < n_sim_steps; ii++)
{
// set inputs
sim_in_set(acados_sim_config, acados_sim_dims,
acados_sim_in, "x", x_current);
sim_in_set(acados_sim_config, acados_sim_dims,
acados_sim_in, "u", u0);
// solve
status = back_acados_sim_solve(capsule);
if (status != ACADOS_SUCCESS)
{
printf("acados_solve() failed with status %d.\n", status);
}
// get outputs
sim_out_get(acados_sim_config, acados_sim_dims,
acados_sim_out, "x", x_current);
// print solution
printf("\nx_current, %d\n", ii);
for (int jj = 0; jj < NX; jj++)
{
printf("%e\n", x_current[jj]);
}
}
printf("\nPerformed %d simulation steps with acados integrator successfully.\n\n", n_sim_steps);
// free solver
status = back_acados_sim_free(capsule);
if (status) {
printf("back_acados_sim_free() returned status %d. \n", status);
}
back_acados_sim_solver_free_capsule(capsule);
return status;
}