-
Notifications
You must be signed in to change notification settings - Fork 11
/
11_EulerGyro.py
71 lines (60 loc) · 1.61 KB
/
11_EulerGyro.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
'''
Filename: 11_EulerGyro.py
Created on: April,3, 2021
Author: dhpark
'''
import numpy as np
import matplotlib.pyplot as plt
from math import sin, cos, tan, pi
from scipy import io
prevPhi, prevTheta, prevPsi = None,None,None
input_mat = io.loadmat('./11_ArsGyro.mat')
def GetGyro(i):
p = input_mat['wx'][i][0] # (41500, 1)
q = input_mat['wy'][i][0] # (41500, 1)
r = input_mat['wz'][i][0] # (41500, 1)
return p, q, r
def EulerGyro(p,q,r,dt):
global prevPhi, prevTheta, prevPsi
if prevPhi is None:
prevPhi = 0
prevTheta = 0
prevPsi = 0
sinPhi = sin(prevPhi)
cosPhi = cos(prevPhi)
cosTheta = cos(prevTheta)
tanTheta = tan(prevTheta)
phi = prevPhi + dt*(p + q*sinPhi*tanTheta + r*cosPhi*tanTheta)
theta = prevTheta + dt*(q*cosPhi - r*sinPhi)
psi = prevPsi + dt*(q*sinPhi/cosTheta + r*cosPhi/cosTheta)
prevPsi = psi
prevPhi = phi
prevTheta = theta
return phi, theta, psi
Nsamples = 41500
EulerSaved = np.zeros([Nsamples,3])
dt = 0.01
for k in range(Nsamples):
p,q,r = GetGyro(k)
phi, theta, psi = EulerGyro(p,q,r,dt)
EulerSaved[k] = [phi, theta, psi]
'''
roll -> phi
pitch -> theta
yaw -> psi
'''
t = np.arange(0, Nsamples * dt ,dt)
PhiSaved = EulerSaved[:,0] * 180/pi
ThetaSaved = EulerSaved[:,1] * 180/pi
PsiSaved = EulerSaved[:,2] * 180/pi
plt.figure()
plt.plot(t, PhiSaved)
plt.xlabel('Time [Sec]')
plt.ylabel('Roll angle [deg]')
plt.savefig('result/11_EulerGyro_roll.png')
plt.figure()
plt.plot(t, ThetaSaved)
plt.xlabel('Time [Sec]')
plt.ylabel('Pitch angle [deg]')
plt.savefig('result/11_EulerGyro_pitch.png')
plt.show()