-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibration.cpp
More file actions
242 lines (231 loc) · 8.21 KB
/
Copy pathcalibration.cpp
File metadata and controls
242 lines (231 loc) · 8.21 KB
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
#include <Eigen/Geometry>
#include <string>
#include <tweeny-3.2.0.h>
#include <XPLMDataAccess.h>
#include <XPLMUtilities.h>
#include <array>
#include <cmath>
#include <format>
#include "calibration.hpp"
#include "util.hpp"
#include "ui.hpp"
namespace Calibration {
namespace DataRef {
XPLMDataRef physics = XPLMFindDataRef("sim/operation/override/override_planepath");
XPLMDataRef x = XPLMFindDataRef("sim/flightmodel/position/local_x");
XPLMDataRef y = XPLMFindDataRef("sim/flightmodel/position/local_y");
XPLMDataRef z = XPLMFindDataRef("sim/flightmodel/position/local_z");
XPLMDataRef vx = XPLMFindDataRef("sim/flightmodel/position/local_vx");
XPLMDataRef vy = XPLMFindDataRef("sim/flightmodel/position/local_vy");
XPLMDataRef vz = XPLMFindDataRef("sim/flightmodel/position/local_vz");
XPLMDataRef yaw = XPLMFindDataRef("sim/flightmodel/position/psi");
XPLMDataRef quat = XPLMFindDataRef("sim/flightmodel/position/q");
XPLMDataRef P = XPLMFindDataRef("sim/flightmodel/position/P");
XPLMDataRef Q = XPLMFindDataRef("sim/flightmodel/position/Q");
XPLMDataRef R = XPLMFindDataRef("sim/flightmodel/position/R");
XPLMDataRef semilen = XPLMFindDataRef("sim/aircraft/parts/acf_semilen_JND");
}
namespace Saved {
Eigen::Vector3d pos;
float yaw;
Eigen::Quaternionf rot;
Eigen::Vector3f vel;
Eigen::Vector3f rates;
}
namespace Anim {
int step = 0;
bool rotate = false;
int millis = 0;
float semilen = 0;
Eigen::Vector3d start_pos;
Eigen::Quaternionf start_rot;
Eigen::Vector3d last_pos;
Eigen::Quaternionf last_rot;
Eigen::Vector3d dest_pos;
Eigen::Quaternionf dest_rot;
tweeny::tween<float> tween;
tweeny::tween<float> tween_rot;
void Start();
}
int enabled = 0;
bool IsEnabled() { return enabled; };
void Save();
void Restore();
constexpr int animation_time = 1500; //ms
}
bool Calibration::Toggle() {
if (!enabled) {
Save();
float s[56];
XPLMGetDatavf(DataRef::semilen, s, 0, 56);
Anim::semilen = *std::max_element(s, s + 56);
Anim::last_pos = Saved::pos;
Anim::last_rot = Saved::rot;
Anim::step = 0;
Anim::Start();
enabled = 1;
return true;
} else {
if (Anim::step >= 0) {
if (Anim::step == 0) {
Anim::step = -2;
} else if (Anim::step > 0) {
Anim::step = -1;
};
Anim::rotate = false;
Anim::Start();
};
return false;
}
}
void Calibration::ToggleRotation() {
if (!enabled || Anim::step < 0) { return; };
Anim::rotate = !Anim::rotate;
}
void Calibration::NextCalibrationStep() {
if (!enabled || Anim::step < 0) { return; };
Anim::step = (Anim::step + 1) % steps.size();
Anim::Start();
};
void Calibration::PreviousCalibrationStep() {
if (!enabled || Anim::step < 0) { return; };
Anim::step = Anim::step > 0 ? Anim::step - 1 : steps.size() - 1;
Anim::Start();
};
void Calibration::Save() {
// Save plane position and velocity for later
Saved::pos = {
XPLMGetDatad(DataRef::x),
XPLMGetDatad(DataRef::y),
XPLMGetDatad(DataRef::z)
};
Saved::yaw = XPLMGetDataf(DataRef::yaw);
XPLMGetDatavf(DataRef::quat, &Saved::rot.w(), 0, 1);
XPLMGetDatavf(DataRef::quat, Saved::rot.vec().data(), 1, 3);
Saved::vel = {
XPLMGetDataf(DataRef::vx),
XPLMGetDataf(DataRef::vy),
XPLMGetDataf(DataRef::vz)
};
Saved::rates = {
XPLMGetDataf(DataRef::P),
XPLMGetDataf(DataRef::Q),
XPLMGetDataf(DataRef::R)
};
};
void Calibration::Restore() {
// Restore saved position and velocity variables
XPLMSetDatad(DataRef::x, Saved::pos.x());
// keep altitude from animation
// XPLMSetDatad(DataRef::y, Saved::pos.y());
XPLMSetDatad(DataRef::z, Saved::pos.z());
XPLMSetDataf(DataRef::vx, Saved::vel.x());
XPLMSetDataf(DataRef::vy, Saved::vel.y());
XPLMSetDataf(DataRef::vz, Saved::vel.z());
XPLMSetDataf(DataRef::P, Saved::rates.x());
XPLMSetDataf(DataRef::Q, Saved::rates.y());
XPLMSetDataf(DataRef::R, Saved::rates.z());
XPLMSetDatavf(DataRef::quat, Saved::rot.vec().data(), 1, 3);
XPLMSetDatavf(DataRef::quat, &Saved::rot.w(), 0, 1);
}
void Calibration::Anim::Start() {
millis = 0;
Eigen::Vector3d base_pos = Saved::pos + Eigen::Vector3d{ 0, semilen, 0 };
Eigen::Quaternionf base_rot = Eigen::Quaternionf(1, 0, 0, 0) *
Eigen::AngleAxisf(Saved::yaw * deg_to_rad, Eigen::Vector3f::UnitZ());
switch (step) {
case -2: // Return to saved
// slightly above the saved position, otherwise the landing gear gets struck
dest_pos = Saved::pos + Eigen::Vector3d(0.0, 0.1, 0.0);
dest_rot = Saved::rot;
break;
case -1:
case 0: // Accelerometer - Front
dest_pos = base_pos;
dest_rot = base_rot;
break;
case 1: // Accelerometer - Roll left
dest_pos = base_pos;
dest_rot = base_rot * Eigen::AngleAxisf(-M_PI / 2, Eigen::Vector3f::UnitX());
break;
case 2: // Accelerometer - Roll right
dest_pos = base_pos;
dest_rot = base_rot * Eigen::AngleAxisf(M_PI / 2, Eigen::Vector3f::UnitX());
break;
case 3: // Accelerometer - Nose down
// if the plane is perfectly nose down/up it glitches
// a small offset prevents it
dest_pos = base_pos;
dest_rot = base_rot * Eigen::AngleAxisf(-M_PI / 2 + 0.0001f, Eigen::Vector3f::UnitY());
break;
case 4: // Accelerometer - Nose up
dest_pos = base_pos;
dest_rot = base_rot * Eigen::AngleAxisf(M_PI / 2 + 0.0001f, Eigen::Vector3f::UnitY());
break;
case 5: // Accelerometer - Upside down
dest_pos = base_pos;
dest_rot = base_rot * Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitX());
break;
}
tween = tweeny::from(0.0f).to(1.0f).during(animation_time)
.via(tweeny::easing::quadraticInOutEasing());
start_pos = last_pos;
start_rot = last_rot;
if (step >= 0) {
UI::Window::LabelCalibration::SetText(steps[step]);
} else {
UI::Window::LabelCalibration::SetText("None");
}
}
void Calibration::Loop(float dt) {
using namespace Anim;
// update animation timer
if (millis < animation_time || rotate) {
millis += dt * 1000;
if (millis > animation_time && !rotate) {
millis = animation_time;
}
}
float t = tween.seek(millis);
// tween position and rotation
Eigen::Vector3d curr_pos = {
start_pos.x() + (dest_pos.x() - start_pos.x()) * double(t),
start_pos.y() + (dest_pos.y() - start_pos.y()) * double(t),
start_pos.z() + (dest_pos.z() - start_pos.z()) * double(t),
};
Eigen::Quaternionf curr_rot = start_rot.slerp(t, dest_rot);
if (millis > animation_time) {
float angle = 2 * M_PI / 10000.0f * (millis - animation_time);
Eigen::Vector3f up = { 0, 0, 1 };
Eigen::AngleAxisf yaw_rotate = { angle, curr_rot.conjugate() * up };
curr_rot = dest_rot * yaw_rotate;
}
// update data refs
XPLMSetDatad(DataRef::x, curr_pos.x());
XPLMSetDatad(DataRef::y, curr_pos.y());
XPLMSetDatad(DataRef::z, curr_pos.z());
XPLMSetDataf(DataRef::vx, (curr_pos.x() - last_pos.x()) / dt);
XPLMSetDataf(DataRef::vy, (curr_pos.y() - last_pos.y()) / dt);
XPLMSetDataf(DataRef::vz, (curr_pos.z() - last_pos.z()) / dt);
XPLMSetDatavf(DataRef::quat, curr_rot.vec().data(), 1, 3);
XPLMSetDatavf(DataRef::quat, &curr_rot.w(), 0, 1);
Eigen::Vector3f rates = angularVelocity(last_rot, curr_rot, dt);
XPLMSetDataf(DataRef::P, rates.x());
XPLMSetDataf(DataRef::Q, rates.y());
XPLMSetDataf(DataRef::R, rates.z());
// update last position for next loop iteration
last_pos = curr_pos;
last_rot = curr_rot;
// end calibration
if (step < 0) {
if (millis >= animation_time) {
step--;
if (step == -3) {
Restore();
enabled = 0;
} else {
Anim::Start();
}
}
}
}