-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinematics.py
642 lines (533 loc) · 16.5 KB
/
kinematics.py
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import numpy as np
'''
Rotation method follows DCM for frame change.
Original propat follows DCM; a transpose of Rotation Matrix.
Normally, and without any comments, vectors are almost row vectors(1*3).
'''
def cross_matrix(w):
"""
skew matrix for cross product
input
w : 1*3 element radian array
output
cross_mat : 3*3 numpy array matrix
"""
cross_mat = np.array([[0, -w[2], w[1]],
[w[2], 0, -w[0]],
[-w[1], w[0], 0]])
return cross_mat
def rotmax(angle):
"""
rotation matrix for x-axis rotation DCM
input
angle : radian angle
output
rot_mat : 3*3 numpy array matrix
"""
cosine = np.cos(angle)
sine = np.sin(angle)
rot_mat = np.array([[1, 0, 0],
[0, cosine, sine],
[0, -sine, cosine]])
return rot_mat
def rotmay(angle):
"""
rotation matrix for y-axis rotation DCM
input
angle : radian angle
output
rot_mat : 3*3 numpy array matrix
"""
cosine = np.cos(angle)
sine = np.sin(angle)
rot_mat = np.array([[cosine, 0, -sine],
[0, 1, 0],
[sine, 0, cosine]])
return rot_mat
def rotmaz(angle):
"""
rotation matrix for y-axis rotation DCM
input
angle : radian angle
output
rot_mat : 3*3 numpy array matrix
"""
cosine = np.cos(angle)
sine = np.sin(angle)
rot_mat = np.array([[cosine, sine, 0],
[-sine, cosine, 0],
[0, 0, 1]])
return rot_mat
def rotmax_rx(angle):
"""
rotation matrix for x-axis rotation RM
input
angle : radian angle
output
rot_mat : 3*3 numpy array matrix
"""
cosine = np.cos(angle)
sine = np.sin(angle)
rot_mat = np.array([[1, 0, 0],
[0, cosine, -sine],
[0, sine, cosine]])
return rot_mat
def rotmay_rx(angle):
"""
rotation matrix for y-axis rotation RM
input
angle : radian angle
output
rot_mat : 3*3 numpy array matrix
"""
cosine = np.cos(angle)
sine = np.sin(angle)
rot_mat = np.array([[cosine, 0, sine],
[0, 1, 0],
[-sine, 0, cosine]])
return rot_mat
def rotmaz_rx(angle):
"""
rotation matrix for y-axis rotation RM
input
angle : radian angle
output
rot_mat : 3*3 numpy array matrix
"""
cosine = np.cos(angle)
sine = np.sin(angle)
rot_mat = np.array([[cosine, -sine, 0],
[sine, cosine, 0],
[0, 0, 1]])
return rot_mat
# =======================================================================
def eulerrmx(euler_angle, euler_vector):
"""
rodrigues rotation
input
euler_angle : radian angle
euler_vector : 1*3 element radian array
output
rot_mat : 3*3 numpy array matrix
"""
cosine_0 = np.cos(euler_angle)
sine = np.sin(euler_angle)
cosine_1 = 1 - cosine_0
euler_vector = np.reshape(euler_vector, (3, 1))
rot_mat = cosine_0 * np.eye(3) + cosine_1 * np.dot(euler_vector, euler_vector.T) + \
sine * cross_matrix(euler_vector.flatten())
return rot_mat
def rmxeuler(rot_mat):
"""
abstract Rodrigues rotation elements
input
rot_mat : 3*3 radian angle matrix
output
euler_angle : scalar radian angle
euler_vector : 1*3 numpy array
"""
trace = np.trace(rot_mat)
if trace == 3:
euler_angle = 0
euler_vector = np.array([1, 0, 0])
elif trace < -0.99999:
euler_angle = np.pi
w = np.diagonal(rot_mat)
euler_vector = np.sqrt((1 + w) / 2)
if euler_vector[0] > 0.5:
euler_vector[1] = np.sign(rot_mat[0, 1]) * euler_vector[1]
euler_vector[2] = np.sign(rot_mat[2, 0]) * euler_vector[2]
elif euler_vector[1] > 0.5:
euler_vector[0] = np.sign(rot_mat[0, 1]) * euler_vector[0]
euler_vector[2] = np.sign(rot_mat[1, 2]) * euler_vector[2]
else:
euler_vector[0] = np.sign(rot_mat[2, 0]) * euler_vector[0]
euler_vector[1] = np.sign(rot_mat[1, 2]) * euler_vector[1]
else:
euler_angle = np.arccos((trace - 1) / 2)
sine = np.sin(euler_angle)
euler_vector = np.array([
rot_mat[1, 2] - rot_mat[2, 1],
rot_mat[2, 0] - rot_mat[0, 2],
rot_mat[0, 1] - rot_mat[1, 0]
]) / (2 * sine)
return euler_angle, euler_vector
# =======================================================================
def rmxexyz(rot_mat):
"""
abstract X-Y-Z rotation angles from rotation matrix
input
rot_mat : 3*3 numpy array
output
euler_angles : 1*3 numpy array
"""
a11, a12, a21, a22, a31, a32, a33 = rot_mat[0, 0], rot_mat[0, 1], rot_mat[1, 0], rot_mat[1, 1], rot_mat[2, 0], \
rot_mat[2, 1], rot_mat[2, 2]
if abs(a31) <= 1:
eul2 = np.arcsin(a31)
elif a31 < 0:
eul2 = -np.pi / 2
else:
eul2 = np.pi / 2
if abs(a31) <= 0.99999:
if a33 != 0:
eul1 = np.arctan2(-a32, a33)
if eul1 > np.pi:
eul1 = eul1 - 2 * np.pi
else:
eul1 = np.pi / 2 * np.sign(-a32)
if a11 != 0:
eul3 = np.arctan2(-a21, a11)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = np.pi / 2 * np.sign(-a21)
else:
eul1 = 0
if a22 != 0:
eul3 = np.arctan2(a12, a22)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = np.pi / 2 * np.sign(a12)
euler_angles = np.array([eul1, eul2, eul3])
return euler_angles
def rmxezxy(rot_mat):
"""
abstract Z-X-Y rotation angles from rotation matrix
input
rot_mat : 3*3 numpy array
output
euler_angles : 1*3 numpy array
"""
spct = -rot_mat[0, 2]
ctsf = -rot_mat[1, 0]
ctcf = rot_mat[1, 1]
stet = rot_mat[1, 2]
cpct = rot_mat[2, 2]
if abs(stet) <= 1:
eul2 = np.arcsin(stet)
else:
eul2 = np.pi / 2 * np.sign(stet)
if abs(eul2) <= np.pi / 2 - 1e-5:
if abs(ctcf) != 0:
eul1 = np.arctan2(ctsf, ctcf)
if eul1 > np.pi:
eul1 = eul1 - 2 * np.pi
else:
eul1 = np.pi / 2 * np.sign(ctsf)
if abs(cpct) != 0:
eul3 = np.arctan2(spct, cpct)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = np.pi / 2 * np.sign(spct)
else:
capb = rot_mat[0, 0]
sapb = rot_mat[0, 1]
eul1 = 0.
if abs(capb) != 0:
eul3 = np.arctan2(sapb, capb)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = 0.
euler_angles = np.array([eul1, eul2, eul3])
return euler_angles
def rmxezxz(rot_mat):
"""
abstract Z-X-Z rotation angles from rotation matrix
input
rot_mat : 3*3 numpy array
output
euler_angles : 1*3 numpy array
"""
a11, a12, a13, a23, a31, a32, a33 = rot_mat[0, 0], rot_mat[0, 1], rot_mat[0, 2], rot_mat[1, 2], rot_mat[2, 0], \
rot_mat[2, 1], rot_mat[2, 2]
if abs(a33) <= 1:
eul2 = np.arccos(a33)
elif a33 < 0:
eul2 = np.pi
else:
eul2 = 0
if abs(eul2) >= 0.00001:
if a32 != 0:
eul1 = np.arctan2(a31, -a32)
else:
eul1 = np.pi / 2 * np.sign(a31)
if eul1 > np.pi:
eul1 = eul1 - 2 * np.pi
if a23 != 0:
eul3 = np.arctan2(a13, a23)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = np.pi / 2 * np.sign(a13)
else:
eul1 = 0
if a11 != 0:
eul3 = np.arctan2(a12, a11)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = np.pi / 2 * np.sign(a12)
euler_angles = np.array([eul1, eul2, eul3])
return euler_angles
def rmxezyx(rot_mat):
"""
abstract Z-Y-X rotation angles from rotation matrix
input
rot_mat : 3*3 numpy array
output
euler_angles : 1*3 numpy array
"""
stet = -rot_mat[0, 2]
ctsf = rot_mat[0, 1]
ctcf = rot_mat[0, 0]
spct = rot_mat[1, 2]
cpct = rot_mat[2, 2]
if abs(stet) <= 1.:
eul2 = np.arcsin(stet)
else:
eul2 = np.pi / 2 * np.sign(stet)
if abs(eul2) <= np.pi / 2 - 1e-5:
if abs(ctcf) != 0:
eul1 = np.arctan2(ctsf, ctcf)
if eul1 > np.pi:
eul1 = eul1 - 2 * np.pi
else:
eul1 = np.pi / 2 * np.sign(ctsf)
if abs(cpct) != 0:
eul3 = np.arctan2(spct, cpct)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = np.pi / 2 * np.sign(spct)
else:
capb = rot_mat[1, 1]
sapb = rot_mat[1, 0]
eul1 = 0.
if abs(capb) != 0:
eul3 = np.arctan2(sapb, capb)
if eul3 > np.pi:
eul3 = eul3 - 2 * np.pi
else:
eul3 = 0.
euler_angles = np.array([eul1, eul2, eul3])
return euler_angles
def rmxquat(rot_mat):
"""
Obtain the attitude quaternions from the attitude rotation matrix.
Parameters:
rot_mat : np.array
Rotation matrix (3x3).
Returns:
quaternions : np.array
Attitude quaternions.
"""
matra = np.trace(rot_mat)
auxi = 1 - matra
selec = np.array([1 + matra, auxi + 2 * rot_mat[0, 0], auxi + 2 * rot_mat[1, 1], auxi + 2 * rot_mat[2, 2]])
ites = np.argmax(selec)
auxi = 0.5 * np.sqrt(selec[ites])
if ites == 0:
quaternions = np.array([
(rot_mat[1, 2] - rot_mat[2, 1]) / (4 * auxi),
(rot_mat[2, 0] - rot_mat[0, 2]) / (4 * auxi),
(rot_mat[0, 1] - rot_mat[1, 0]) / (4 * auxi),
auxi
])
elif ites == 1:
quaternions = np.array([
auxi,
(rot_mat[0, 1] + rot_mat[1, 0]) / (4 * auxi),
(rot_mat[0, 2] + rot_mat[2, 0]) / (4 * auxi),
(rot_mat[1, 2] - rot_mat[2, 1]) / (4 * auxi)
])
elif ites == 2:
quaternions = np.array([
(rot_mat[0, 1] + rot_mat[1, 0]) / (4 * auxi),
auxi,
(rot_mat[1, 2] + rot_mat[2, 1]) / (4 * auxi),
(rot_mat[2, 0] - rot_mat[0, 2]) / (4 * auxi)
])
else: # ites == 3
quaternions = np.array([
(rot_mat[0, 2] + rot_mat[2, 0]) / (4 * auxi),
(rot_mat[1, 2] + rot_mat[2, 1]) / (4 * auxi),
auxi,
(rot_mat[0, 1] - rot_mat[1, 0]) / (4 * auxi)
])
return quaternions
def quat_matrix(q):
"""
q : quaternion [q_vec,q_meg]^T
Return : 4*4 quaternion matrix
"""
q_mat = [[[q[3], -q[2], q[1], q[0]],
[q[2], q[3], -q[0], q[1]],
[-q[1], q[0], q[3], q[2]],
[-q[0], -q[1], -q[2], q[3]]]]
return q_mat
def quat_inv(q):
"""
q : quaternion
Return : q_conj = [-q_vec,q_meg]^T
"""
q_conj = np.array([-q[0], -q[1], -q[2], q[3]])
return q_conj
def quat_norm(q):
"""
Normalize the quaternion to have a unit norm.
q : np.array
Input quaternion [qx, qy, qz, qw]
Returns:
np.array
Normalized quaternion [qx, qy, qz, qw]
"""
v = q[:3]
e = q[3]
e = min(max(e, -1), 1)
vnorm = np.dot(v, v)
enorm = e ** 2
if vnorm != 0:
enorm = np.sqrt((1 - enorm) / vnorm)
q_norm = np.concatenate((enorm * v, [e]))
else:
q_norm = np.array([0, 0, 0, 1])
return q_norm
def quat_prod(quat1, quat2):
"""
Compute the product of two quaternions.
quat1 : np.array
First quaternion [q1, q2, q3, q4] where Q = q1 i + q2 j + q3 k + q4.
quat2 : np.array
Second quaternion [p1, p2, p3, p4] where P = p1 i + p2 j + p3 k + p4.
Returns:
np.array
Quaternion product [r1, r2, r3, r4] where R = Q X P.
"""
v1 = quat1[:3]
v2 = quat2[:3]
q4 = quat1[3]
p4 = quat2[3]
# Quaternion product formula
real_part = q4 * p4 - np.dot(v1, v2)
imaginary_part = q4 * v2 + p4 * v1 + np.cross(v1, v2)
quat = np.concatenate((imaginary_part, [real_part]))
return quat
def quat_unity(q):
"""
q : np.array
Input quaternion [qx, qy, qz, qw]
Returns:
np.array
Normalized quaternion [qx, qy, qz, qw], such that the square of its norm equals 1.
"""
qnorm = np.sqrt(np.dot(q, q))
if (qnorm != 0):
q_unit = q / qnorm
else:
q_unit = np.array([0, 0, 0, 1])
return q_unit
def quatrmx(quaternion):
"""
Compute the rotation matrix from the quaternion.
Parameters:
quaternion : np.array
Attitude quaternion [q1, q2, q3, q4] where Q = q1 i + q2 j + q3 k + q4
Returns:
rot_mat : np.array
Rotation matrix (3, 3) as euler rotation matrix (DCM)
"""
q1q, q2q, q3q, q4q = quaternion[0] ** 2, quaternion[1] ** 2, quaternion[2] ** 2, quaternion[3] ** 2
q12, q13, q14 = 2 * quaternion[0] * quaternion[1], 2 * quaternion[0] * quaternion[2], 2 * quaternion[0] * \
quaternion[3]
q23, q24, q34 = 2 * quaternion[1] * quaternion[2], 2 * quaternion[1] * quaternion[3], 2 * quaternion[2] * \
quaternion[3]
rot_mat = np.array([
[q1q - q2q - q3q + q4q, q12 + q34, q13 - q24],
[q12 - q34, q2q - q1q + q4q - q3q, q23 + q14],
[q13 + q24, q23 - q14, q3q - q1q - q2q + q4q]
])
return rot_mat
def quatexyz(q):
"""
Parameters:
quaternion : np.array
Attitude quaternion [q1, q2, q3, q4] where Q = q1 i + q2 j + q3 k + q4
Returns:
rot_mat : np.array
Rotation matrix (3, 3) as a xyz sequence rotation
"""
if q.ndim == 1:
q = q.reshape(4, 1)
_, n = np.size(q)
euler_angle = []
for i in range(n):
rot_mat = quatrmx(q[:, i])
angle = rmxexyz(rot_mat)
euler_angle.append(angle)
euler_angle = np.array(euler_angle).T
return euler_angle
def quatezxz(q):
"""
Parameters:
quaternion : np.array
Attitude quaternion [q1, q2, q3, q4] where Q = q1 i + q2 j + q3 k + q4
Returns:
rot_mat : np.array
Rotation matrix (3, 3) as a zyz sequence rotation
"""
if q.ndim == 1:
q = q.reshape(4,1)
_, n = np.shape(q)
euler_angle = []
for i in range(n):
rot_mat = quatrmx(q[:, i])
angle = rmxezxz(rot_mat)
euler_angle.append(angle)
euler_angle = np.array(euler_angle).T
return euler_angle
def exyzrmx(euler_angles):
rot_mat = rotmaz(euler_angles[2]) @ rotmay(euler_angles[1]) @ rotmax(euler_angles[0])
return rot_mat
def ezxyrmx(euler_angles):
rot_mat = rotmay(euler_angles[2]) @ rotmax(euler_angles[1]) @ rotmaz(euler_angles[0])
return rot_mat
def ezxzrmx(euler_angles):
rot_mat = rotmaz(euler_angles[2]) @ rotmax(euler_angles[1]) @ rotmaz(euler_angles[0])
return rot_mat
def ezyxrmx(euler_angles):
rot_mat = rotmax(euler_angles[2]) @ rotmay(euler_angles[1]) @ rotmaz(euler_angles[0])
return rot_mat
def ezxzquat(euler_angles):
rot_mat = ezxzrmx(euler_angles)
quat = rmxquat(rot_mat)
return quat
def exyzquat(euler_angles):
rot_mat = exyzrmx(euler_angles)
quat = rmxquat(rot_mat)
return quat
def sangvel(w):
skew_ang_vel = np.array([
[0, w[2], -w[1], w[0]],
[-w[2], 0, w[0], w[1]],
[w[1], -w[0], 0, w[2]],
[-w[0], -w[1], -w[2], 0]
])
return skew_ang_vel
def proximus(angleinp, angleprox):
test = 2 * np.pi
angle = angleprox + np.mod((angleinp - angleprox + test / 2), test) - test / 2
return angle
def rectangular_to_spherical(geoc):
px = geoc[0]
py = geoc[1]
pz = geoc[2]
ws = px * px + py * py
rw = np.sqrt(ws + pz * pz)
lg = np.arctan2(py, px)
lt = np.arctan2(pz, np.sqrt(ws))
spherical = np.array([lg, lt, rw])
return spherical