Repository navigation
Expand file tree
/
Copy pathtransforms.cpp
More file actions
402 lines (371 loc) · 12 KB
/
Copy pathtransforms.cpp
File metadata and controls
402 lines (371 loc) · 12 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "transforms.h"
/**
* @brief dcm2quat - converts DCM to quaternion, where q[0] is scalar element
* @param R - input DCM
* @param q - output Quaternion
*/
void dcm2quat(float R[3][3], float q[4])
{
float q0, q1, q2, q3;
q0 = sqrt(0.25*(1 + R[0][0] + R[1][1] + R[2][2]));
q1 = sqrt(0.25*(1 + R[0][0] - R[1][1] - R[2][2]));
q2 = sqrt(0.25*(1 - R[0][0] + R[1][1] - R[2][2]));
q3 = sqrt(0.25*(1 - R[0][0] - R[1][1] + R[2][2]));
if ((q0 > q1) && (q0 > q2) && (q0 > q3)){
q[0] = q0;
q[1] = (R[1][2] - R[2][1])/(4*q0);
q[2] = (R[2][0] - R[0][2])/(4*q0);
q[3] = (R[0][1] - R[1][0])/(4*q0);
} else if ((q1 > q0) && (q1 > q2) && (q1 > q3)){
q[1] = q1;
q[0] = (R[1][2] - R[2][1])/(4*q1);
q[2] = (R[0][1] + R[1][0])/(4*q1);
q[3] = (R[2][0] + R[0][2])/(4*q1);
} else if ((q2 > q0) && (q2 > q1) && (q2 > q3)){
q[2] = q2;
q[0] = (R[2][0] - R[0][2])/(4*q2);
q[1] = (R[0][1] + R[1][0])/(4*q2);
q[3] = (R[1][2] + R[2][1])/(4*q2);
} else {
q[3] = q3;
q[0] = (R[0][1] - R[1][0])/(4*q3);
q[1] = (R[2][0] + R[0][2])/(4*q3);
q[2] = (R[1][2] + R[2][1])/(4*q3);
}
}
/**
* @brief euler2dcm321 - converts Euler angles roll, pitch, yaw to a DCM via a
* 321 rotation (or rotation in yaw, followed by rotation in pitch, then roll)
* @param phi - input roll angle (rad)
* @param theta - input pitch angle (rad)
* @param psi - input yaw angle (rad)
* @param R - output DCM
*/
void euler2dcm321(float phi, float theta, float psi, float R[3][3])
{
R[0][0] = cos(theta)*cos(psi); R[0][1] = cos(theta)*sin(psi); R[0][2] = -sin(theta);
R[1][0] = -cos(phi)*sin(psi) + sin(phi)*sin(theta)*cos(psi); R[1][1] = cos(phi)*cos(psi) + sin(phi)*sin(theta)*sin(psi); R[1][2] = sin(phi)*cos(theta);
R[2][0] = sin(phi)*sin(psi) + cos(phi)*sin(theta)*cos(psi); R[2][1] = -sin(phi)*cos(psi) + cos(phi)*sin(theta)*sin(psi); R[2][2] = cos(phi)*cos(theta);
}
/**
* @brief euler2quat321 - converts Euler angles roll, pitch, yaw to a quaternion via a
* 321 rotation (or rotation in yaw, followed by rotation in pitch, then roll)
* @param phi - input roll angle (rad)
* @param theta - input pitch angle (rad)
* @param psi - input yaw angle (rad)
* @param q - output quaternion (q[0] is scalar element)
*/
void euler2quat321(float phi, float theta, float psi, float q[4])
{
float R[3][3];
euler2dcm321(phi, theta, psi, R);
dcm2quat(R, q);
}
/**
* @brief quat2dcm - converts a quaternion to a DCM
* @param q - input quaternion (q[0] is scalar element)
* @param R - output DCM
*/
void quat2dcm(float q[4], float R[3][3])
{
R[0][0] = q[0]*q[0] + q[1]*q[1] - q[2]*q[2] - q[3]*q[3];
R[0][1] = 2*(q[1]*q[2] + q[0]*q[3]);
R[0][2] = 2*(q[1]*q[3] - q[0]*q[2]);
R[1][0] = 2*(q[1]*q[2] - q[0]*q[3]);
R[1][1] = q[0]*q[0] - q[1]*q[1] + q[2]*q[2] - q[3]*q[3];
R[1][2] = 2*(q[2]*q[3] + q[0]*q[1]);
R[2][0] = 2*(q[1]*q[3] + q[0]*q[2]);
R[2][1] = 2*(q[2]*q[3] - q[0]*q[1]);
R[2][2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
}
/**
* @brief dcm2euler321 - converts a DCM to Euler angles via a 321 rotation (or
* rotation in yaw, followed by rotation in pitch, then roll)
* @param R - input DCM
* @param phi - output roll angle (rad)
* @param theta - output pitch angle (rad)
* @param psi - output yaw angle (rad)
*/
void dcm2euler321(float R[3][3], float &phi, float &theta, float &psi)
{
phi = atan2(R[1][2], R[2][2]);
theta = -asin(R[0][2]);
psi = atan2(R[0][1], R[0][0]);
}
/**
* @brief quat2euler321 - converts a quaternion to euler angles via a 321 rotation
* (or a rotation in yaw, followed by rotation in pitch, then roll)
* @param q - input quaternion (q[0] scalar part)
* @param phi - output roll (rad)
* @param theta - output pitch (rad)
* @param psi - output yaw (rad)
*/
void quat2euler321(float q[4], float &phi, float &theta, float &psi)
{
float R[3][3];
quat2dcm(q,R);
dcm2euler321(R, phi, theta, psi);
}
void posTrans(std::string orientation, float pos[3], float corrpos[3])
{
float qrot[4];
if (orientation == "forward") {
qrot[0] = 0.5;
qrot[1] = 0.5;
qrot[2] = 0.5;
qrot[3] = -0.5;
}else if (orientation == "backward"){
qrot[0] = 0.5;
qrot[1] = 0.5;
qrot[2] = -0.5;
qrot[3] = 0.5;
}else if (orientation == "left"){
qrot[0] = 0.5*sqrt(2);
qrot[1] = 0.5*sqrt(2);
qrot[2] = 0.;
qrot[3] = 0.;
}else if (orientation == "right"){
qrot[0] = 0.;
qrot[1] = 0.;
qrot[2] = 0.5*sqrt(2);
qrot[3] = -0.5*sqrt(2);
}else {
//case "up":
//case "down":
qrot[0] = 0.5;
qrot[1] = 0.5;
qrot[2] = 0.5;
qrot[3] = -0.5;
}
float qrotinv[4], X[4];
X[0] = 0;
X[1] = pos[0];
X[2] = pos[1];
X[3] = pos[2];
quatinv(qrot, qrotinv);
float qtemp[4];
quatmult(qrotinv, X, qtemp);
quatmult(qtemp, qrot, X);
corrpos[0] = X[1];
corrpos[1] = X[2];
corrpos[2] = X[3];
}
/**
* @brief coodTrans - transform input quaternion from body frame B1 to
* body frame B2 (where B2 is FRD: x is forward, y is right, z is down)
* @param orientation - the original body frame:
* forward - x is right, y is up, z is backward
* backward - x is left, y is up, z is forward
* right - x is backward, y is up, z is left
* left - x is forward, y is up, z is right
* @param q1 - input attitude quaternion from local inertial frame to body frame B1
* @param q - output attitude quaternion from local inertial frame to body frame B2 (FRD)
*/
void attTrans(std::string orientation, float q1[4], float q[4])
{
float qrot[4];
if (orientation == "forward") {
qrot[0] = 0.5;
qrot[1] = -0.5;
qrot[2] = -0.5;
qrot[3] = 0.5;
}else if (orientation == "backward"){
qrot[0] = 0.5;
qrot[1] = -0.5;
qrot[2] = 0.5;
qrot[3] = -0.5;
}else if (orientation == "left"){
qrot[0] = 0.5*sqrt(2);
qrot[1] = -0.5*sqrt(2);
qrot[2] = 0.;
qrot[3] = 0.;
}else if (orientation == "right"){
qrot[0] = 0.;
qrot[1] = 0.;
qrot[2] = -0.5*sqrt(2);
qrot[3] = 0.5*sqrt(2);
}else {
//case "up":
//case "down":
qrot[0] = 0.5;
qrot[1] = -0.5;
qrot[2] = -0.5;
qrot[3] = 0.5;
}
float qrotinv[4];
quatinv(qrot,qrotinv);
float qtemp[4];
quatmult(qrot,q1,qtemp);
quatmult(qtemp,qrotinv,q);
}
/**
* @brief coorTransCorr -transform input quaternion from body frame B1 to
* body frame B2 (where B2 is FRD: x is forward, y is right, z is down), and adjust
* for slight body axis orientation error
* @param orientation - the original body frame:
* forward - x is right, y is up, z is backward
* backward - x is left, y is up, z is forward
* right - x is backward, y is up, z is left
* left - x is forward, y is up, z is right
* @param q1 - input attitude quaternion from local inertial frame to body frame B1
* @param dpsi - input yaw error (rad)
* @param q - output attitude quaternion from local inertial frame to body frame B2 (FRD)
*/
void attTransCorr(std::string orientation, float q1[4], float &dpsi, float q[4])
{
float dq[4];
float dphi = 0; float dtheta = 0;
euler2quat321(dphi, dtheta, dpsi, dq);
float qrot[4];
if (orientation == "forward") {
qrot[0] = 0.5;
qrot[1] = -0.5;
qrot[2] = -0.5;
qrot[3] = 0.5;
}else if (orientation == "backward"){
qrot[0] = 0.5;
qrot[1] = -0.5;
qrot[2] = 0.5;
qrot[3] = -0.5;
}else if (orientation == "left"){
qrot[0] = 0.5*sqrt(2);
qrot[1] = -0.5*sqrt(2);
qrot[2] = 0.;
qrot[3] = 0.;
}else if (orientation == "right"){
qrot[0] = 0.;
qrot[1] = 0.;
qrot[2] = -0.5*sqrt(2);
qrot[3] = 0.5*sqrt(2);
}else {
//case "up":
//case "down":
qrot[0] = 0.5;
qrot[1] = -0.5;
qrot[2] = -0.5;
qrot[3] = 0.5;
}
float qrotinv[4];
quatinv(qrot,qrotinv);
float dqinv[4];
quatinv(dq,dqinv);
float qtemp1[4], qtemp2[4], qtemp3[4];
quatmult(qrot,dq,qtemp1);
quatmult(qtemp1, q1, qtemp2);
quatmult(qtemp2,dqinv, qtemp3);
quatmult(qtemp3, qrotinv, q);
}
/**
* @brief quatmult - multiplies quaternion q1 and quaternion q2
* @param q1 - first input quaternion (q[0] scalar part)
* @param q2 - second input quaternion (q[0] scalar part)
* @param q - output quaternion (q[0] scalar part)
*/
void quatmult(float q1[4], float q2[4], float q[4])
{
q[0] = q1[0]*q2[0] - q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3];
q[1] = q1[1]*q2[0] + q1[0]*q2[1] - q1[3]*q2[2] + q1[2]*q2[3];
q[2] = q1[2]*q2[0] + q1[3]*q2[1] + q1[0]*q2[2] - q1[1]*q2[3];
q[3] = q1[3]*q2[0] - q1[2]*q2[1] + q1[1]*q2[2] + q1[0]*q2[3];
}
void quatinv(float q1[4], float q[4])
{
float qnorm = q1[0]*q1[0] + q1[1]*q1[1] + q1[2]*q1[2] + q1[3]*q1[3];
if (qnorm > 0.01 && qnorm < 100.0){
q[0] = q1[0]/qnorm;
q[1] = -q1[1]/qnorm;
q[2] = -q1[2]/qnorm;
q[3] = -q1[3]/qnorm;
} else {
q[0] = 1;
q[1] = 0;
q[2] = 0;
q[3] = 0;
}
}
/**
* @brief poseTransArb -transform input quaternion from body frame B1 to
* body frame B2 (where B2 is FRD: x is forward, y is right, z is down), and adjust
* for slight body axis orientation error. Also adjust position coordinate.
* @param orientation - the original body frame:
* forward - x is right, y is up, z is backward
* backward - x is left, y is up, z is forward
* right - x is backward, y is up, z is left
* left - x is forward, y is up, z is right
* @param q1 - input attitude quaternion from local inertial frame to body frame B1
* @param X - input position estimate
* @param dA - input angle error (rad) - error from prescribed orientation
* @param corrq - output attitude quaternion from local inertial frame to body frame B2 (FRD)
* @param corrX - output position in NED coordinates
*/
void poseTransArb(std::string orientation, float q1[4], float X[3], float dA[3], float corrq[4], float corrX[3])
{
/*double phi, theta, psi;
quat2euler321(q1, &phi, &theta, &psi);
phi -= dA[0];
theta -= dA[1];
euler2quat321(&phi, &theta, &psi, q1);
*/
float dphi = 0; float dtheta = 0; float dpsi = dA[2];
float dQ[4];
euler2quat321(dphi, dtheta, dpsi, dQ);
float qrot[4];
if (orientation == "forward") {
qrot[0] = 0.5;
qrot[1] = 0.5;
qrot[2] = 0.5;
qrot[3] = -0.5;
}else if (orientation == "backward"){
qrot[0] = 0.5;
qrot[1] = 0.5;
qrot[2] = -0.5;
qrot[3] = 0.5;
}else if (orientation == "left"){
qrot[0] = 0.5*sqrt(2);
qrot[1] = 0.5*sqrt(2);
qrot[2] = 0.;
qrot[3] = 0.;
}else if (orientation == "right"){
qrot[0] = 0.;
qrot[1] = 0.;
qrot[2] = 0.5*sqrt(2);
qrot[3] = -0.5*sqrt(2);
}else {
//case "up":
//case "down":
qrot[0] = 0.5;
qrot[1] = 0.5;
qrot[2] = 0.5;
qrot[3] = -0.5;
}
float qrotinv[4];
quatinv(qrot,qrotinv);
float dQinv[4];
quatinv(dQ,dQinv);
float X4[4];
X4[0] = 0;
X4[1] = X[0];
X4[2] = X[1];
X4[3] = X[2];
float qtemp1[4], qtemp1inv[4], temp1[4];
quatmult(qrot,dQinv,qtemp1);
quatinv(qtemp1,qtemp1inv);
quatmult(qtemp1inv,X4,temp1);
quatmult(temp1, qtemp1,X4);
corrX[0] = X4[1];
corrX[1] = X4[2];
corrX[2] = X4[3];
float corrqtemp[4];
quatmult(qtemp1inv, q1, temp1);
quatmult(temp1,qtemp1, corrqtemp);
float phi, theta, psi;
quat2euler321(corrqtemp, phi, theta, psi);
phi -= dA[0];
theta -= dA[1];
euler2quat321(phi, theta, psi, corrq);
/*double phi, theta, psi;
quat2euler321(q1, &phi, &theta, &psi);
phi -= dA[0];
theta -= dA[1];
euler2quat321(&phi, &theta, &psi, corrq);*/
}