-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
362 lines (283 loc) · 9.31 KB
/
test.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <iostream>
#include "math/gopt.hpp"
using namespace gopt;
#include <matplotlibcpp.h>
namespace plt = matplotlibcpp;
struct State
{
union {
struct {
Vec3 x;
Vec3 v;
Vec3 w;
Quat q;
};
Vector_t<double, 13> value;
};
State() : value(0) {}
};
struct Params
{
double m0;
double m_dot;
double Ixx;
double Iyy;
double Izz;
double Ixx_dot;
double Iyy_dot;
double Izz_dot;
double d_com_ct;
Vec2 tvc;
double tb;
double d_com_cp;
double Cd0;
double Cda;
double Cla;
double rho;
double Sref;
double T;
};
Vector_t<double, 13> dynamics(const Vector_t<double, 13>& x, const double& t, const Params& p)
{
State& s = *(State*)(&x[0]);
const double T_val = (t < p.tb) ? p.T : 0;
const double sx = std::sin(p.tvc[0]);
const double cx = std::cos(p.tvc[0]);
const double sy = std::sin(p.tvc[1]);
const double cy = std::cos(p.tvc[1]);
const Vec3 T = Vec3(sy, -sx * cy, cx * cy) * T_val;
const double m = p.m0 + p.m_dot * std::min(t, p.tb);
const double Ixx = p.Ixx + p.Ixx_dot * std::min(t, p.tb);
const double Iyy = p.Iyy + p.Iyy_dot * std::min(t, p.tb);
const double Izz = p.Izz + p.Izz_dot * std::min(t, p.tb);
// Aerodynamic forces
const double v_mag = s.v.length();
Vec3 F_ae;
double alpha = 0;
if (v_mag > 1e-6)
{
const Vec3 nz(0, 0, 1);
const Vec3 vu = rotate(s.q, s.v / v_mag);
const Vec3 nD = -vu;
Vec3 nL = nz - vu[2]*vu;
nL = (nL.length() > 1e-6) ? normalize(nL) : Vec3(0);
alpha = std::abs(std::acos(std::clamp(vu[2], -1.0, 1.0)));
const double rho = 1.225;
const Vec3 L = 0.5 * rho * v_mag * v_mag * p.Cla * alpha * p.Sref * nL;
const Vec3 D = 0.5 * rho * v_mag * v_mag * (p.Cd0 + p.Cda * alpha) * p.Sref * nD;
F_ae = L + D;
}
else
F_ae = Vec3(0);
const Vec3 forces = rotate(conjugate(s.q), T + F_ae) + Vec3(0, 0, -m * 9.81);
const Vec3 moments = cross(Vec3(0, 0, -p.d_com_ct), T) + cross(Vec3(0, 0, p.d_com_cp), F_ae);
const Mat3 I(Ixx, 0, 0, 0, Iyy, 0, 0, 0, Izz);
const Mat3 I_inv(1/Ixx, 0, 0, 0, 1/Iyy, 0, 0, 0, 1/Izz);
const Mat3 I_dot = (t < p.tb) ? Mat3(p.Ixx_dot, 0, 0, 0, p.Iyy_dot, 0, 0, 0, p.Izz_dot) : Mat3(0);
State res;
res.x = s.v;
res.v = forces / m;
res.w = I_inv * (moments - I_dot * s.w - cross(s.w, I * s.w));
res.q = -(Quat(0.0, s.w[0], s.w[1], s.w[2]) * normalize(s.q)) / 2;
return res.value;
}
Vec2 project(Quat q, Vec3 axis)
{
const Vec3 v = rotate(q, axis);
return { std::atan2(-v[1], v[2]), std::atan2(v[0], v[2]) };
}
struct Result
{
std::vector<double> x;
std::vector<Vector_t<double, 19>> y;
};
double objective(const Vector_t<double, 6>& K, const double phi, const double theta, const double psi, const double tvc_bound, const bool print = false, Result* result = nullptr)
{
double prev_t = 0;
const double dt = 0.01;
// This is right
State state;
Quat qx = Quat(phi, Vec3(1, 0, 0));
Quat qy = Quat(theta, Vec3(0, 1, 0));
Quat qz = Quat(psi, Vec3(0, 0, 1));
state.q = qx * qy * qz;
Params params;
params.m0 = 0.85; // kg
params.tb = 5; // s
params.m_dot = -0.2 * params.m0 / params.tb; // kg/s
params.Ixx = 0.05;
params.Iyy = 0.05;
params.Izz = 0.01;
params.Ixx_dot = -0.2 * params.Ixx / params.tb;
params.Iyy_dot = -0.2 * params.Iyy / params.tb;
params.Izz_dot = -0.2 * params.Izz / params.tb;
params.d_com_ct = 0.4; // m
params.tvc = 0; // rad
params.d_com_cp = 0.2; // m
params.Cd0 = 0.15;
params.Cda = 0.25;
params.Cla = 0.1;
params.rho = 1.225;
params.Sref = pi<> * 0.045 * 0.045;
params.T = 11; // N
const Mat2 Kp(K[0], 0, 0, K[1]);
const Mat2 Ki(K[2], 0, 0, K[3]);
const Mat2 Kd(K[4], 0, 0, K[5]);
Vec2 error = 0;
Vec2 prev_error = 0;
Vec2 prev2_error = 0;
Vec2 int_accum = 0;
double D = 0;
double t = 0;
while (true)
{
if (prev_t < params.tb)
{
// PID
const Vec2 set_point = 0;
error = set_point - project(state.q, Vec3(0, 0, 1));
// Proportional
Vec2 control = Kp * error;
// Integral
int_accum += error + prev_error;
control += Ki * int_accum * (dt/2);
// Derivative (2nd order aproximation)
control += Kd * (3 * error - 4 * prev_error + prev2_error) / (2 * dt);
// Transform forces into tvc angles
params.tvc[0] = std::clamp(control[0], -tvc_bound, tvc_bound);
params.tvc[1] = std::clamp(control[1], -tvc_bound, tvc_bound);
const double cx = std::cos(params.tvc[0]);
const double cy = std::cos(params.tvc[1]);
const double tz = cx * cy;
if (std::acos(tz) > tvc_bound)
{
const double sx = std::sin(params.tvc[0]);
const double sy = std::sin(params.tvc[1]);
const double tx = -sy;
const double ty = -sx * cy;
const double a = std::pow(std::tan(std::atan2(ty, tx)), 2);
const double b = std::pow(std::cos(tvc_bound), 2);
params.tvc[1] = copysign(std::asin(std::sqrt((1 - b) / (1 + a))), params.tvc[1]);
params.tvc[0] = copysign(std::acos(std::sqrt(b) / std::cos(params.tvc[1])), params.tvc[0]);
}
// Update previous variables
prev2_error = prev_error;
prev_error = error;
D += dt / 2 * (error.length() + prev_error.length());
}
else
params.tvc = 0;
// System propagation
t = prev_t + dt;
const auto fcn = [&](const Vector_t<double, 13> x, double t) { return dynamics(x, t, params); };
const auto res = DP45(fcn, state.value, prev_t, t, 1e-9, 1e-9);
state.value = res;
prev_t = t;
if (result)
{
result->x.push_back(t);
Vector_t<double, 19> v;
for (int i = 0; i < 13; i++)
v[i] = res[i];
for (int i = 0; i < 2; i++)
v[13 + i] = error[i];
v[15] = params.tvc[0];
v[16] = params.tvc[1];
// Dynamic pressure
v[17] = 0.5 * 1.225 * state.v.magnitude();
// Angle of attack
v[18] = state.v.magnitude() ? std::abs(std::acos(std::clamp(normalize(rotate(state.q, state.v))[2], -1.0, 1.0))) : 0;
result->y.push_back(v);
}
if ((t > params.tb) && state.x[2] <= 0 || state.v[2] <= 0)
break;
}
D -= t;
if (print)
printf("objective: %g, -> [Kp: %g %g, Ki: %g %g, Kd: %g %g], t_final: %g\n", D + t, K[0], K[1], K[2], K[3], K[4], K[5], t);
return D;
}
int main()
{
const double Kp = 10;
const double Ki = 1;
const double Kd = 1;
const Vector_t<double, 6> ub(Kp,Kp, Ki,Ki, Kd,Kd);
const Vector_t<double, 6> lb = -ub;
// Gravity vector
const double alpha = radians(245);
const double delta = radians(8.0);
const double R = std::tan(delta) / (1 + std::tan(delta) * std::tan(delta));
Vec3 grav = Vec3(R * std::cos(alpha), R * std::sin(alpha), std::sqrt(1 - R * R));
const double phi = std::atan2(grav[1], grav[2]);
const double theta = std::atan2(-grav[0], std::sqrt(grav[1] * grav[1] + grav[2] * grav[2]));
const double tvc_bound = radians(10.0);
const auto fcn = [&](const Vector_t<double, 6>& x)
{
return objective(x, phi, theta, 0.0, tvc_bound);
};
const auto best = particleswarm(fcn, lb, ub, 100, 10);
std::cout << "Best: " << best << ", objective value: " << std::endl;
Result result;
objective(best, phi, theta, 0, tvc_bound, true, &result);
std::vector<double> x(result.y.size());
std::vector<double> y(result.y.size());
std::vector<double> z(result.y.size());
std::vector<double> vel(result.y.size());
std::vector<double> err_x(result.y.size());
std::vector<double> err_y(result.y.size());
std::vector<double> theta_x(result.y.size());
std::vector<double> theta_y(result.y.size());
std::vector<double> dyn_pressure(result.y.size());
std::vector<double> AoA(result.y.size());
for (int i = 0; i < result.x.size(); i++)
{
x[i] = result.y[i][0];
y[i] = result.y[i][1];
z[i] = result.y[i][2];
vel[i] = std::sqrt(std::pow(result.y[i][3], 2) + std::pow(result.y[i][4], 2) + std::pow(result.y[i][5], 2));
err_x[i] = degrees(result.y[i][13]);
err_y[i] = degrees(result.y[i][14]);
theta_x[i] = degrees(result.y[i][15]);
theta_y[i] = degrees(result.y[i][16]);
dyn_pressure[i] = result.y[i][17];
AoA[i] = degrees(result.y[i][18]);
}
// Plot
plt::figure();
plt::named_plot("x", result.x, x, "-");
plt::named_plot("y", result.x, y, "-");
plt::named_plot("z", result.x, z, "-");
plt::title("Position");
plt::xlabel("t (s)");
plt::ylabel("position (m)");
plt::legend();
plt::xlim(0.0, result.x.back());
plt::figure();
plt::named_plot("x", result.x, err_x, "-");
plt::named_plot("y", result.x, err_y, "-");
plt::title("PID Controller error");
plt::xlabel("t (s)");
plt::ylabel("$Error\\:(\\circ)$");
plt::legend();
plt::xlim(0.0, result.x.back());
plt::figure();
plt::named_plot("x", result.x, theta_x, "-");
plt::named_plot("y", result.x, theta_y, "-");
plt::title("TVC");
plt::xlabel("t (s)");
plt::ylabel("$deflection\\:(\\circ)$");
plt::legend();
plt::xlim(0.0, result.x.back());
plt::show();
printf("Kp = [%.17e 0; 0 %.17e]\n", best[0], best[1]);
printf("Ki = [%.17e 0; 0 %.17e]\n", best[2], best[3]);
printf("Kd = [%.17e 0; 0 %.17e]\n", best[4], best[5]);
std::cout << std::endl << "At 15 deg: ";
objective(best, phi, theta, radians(15.0), tvc_bound, true);
std::cout << std::endl << "At 235 deg: ";
objective(best, phi, theta, radians(235.0), tvc_bound, true);
std::cout << std::endl << "At 300 deg: ";
objective(best, phi, theta, radians(300.0), tvc_bound, true);
return 0;
}