This repository has been archived by the owner on Apr 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccel.py
80 lines (65 loc) · 2.35 KB
/
accel.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
import numpy as np
import math
#ACCX_CALIB = -20
#ACCY_CALIB = -10
#ACCZ_CALIB = 13
cum_rot = np.matrix([[1,0,0],[0,1,0],[0,0,1]])
#GYRX_CALIB = -80.83
#GYRY_CALIB = 64.81
#GYRZ_CALIB = -52.35
#rot
def rotationMatrixToEulerAngles(R) :
sy = math.sqrt(R[0,0] * R[0,0] + R[1,0] * R[1,0])
singular = sy < 1e-6
if not singular :
x = math.atan2(R[2,1] , R[2,2])
y = math.atan2(-R[2,0], sy)
z = math.atan2(R[1,0], R[0,0])
else :
x = math.atan2(-R[1,2], R[1,1])
y = math.atan2(-R[2,0], sy)
z = 0
return np.array([x, y, z])
def gyrotoeuler(gyro,pos,dt):
sinPhi = math.sin(pos[0])
cosPhi = math.cos(pos[0])
cosTheta = math.cos(pos[1])
tanTheta = math.tan(pos[1])
d= np.matrix([
[1,math.sin(pos[0])*math.tan(pos[1]),math.cos(pos[0])*math.tan(pos[1])],
[0,math.cos(pos[0]),-math.sin(pos[0])],
[0,math.sin(pos[0])/math.cos(pos[1]),math.cos(pos[0])/math.cos(pos[1])]
])
cat = d*np.matrix(gyro).T
return np.matrix(pos)+np.matrix(cat)*dt
def findInertialFrameAccel(accX, accY, accZ, gyrX, gyrY, gyrZ, dt,inital_g):
global cum_rot
g_norm = [[accX],[accY],[accZ]]
for i in range(0,3):
g_norm[i]= g_norm[i][0]
g_norm = np.matrix(g_norm)
holder = [gyrX,gyrY,gyrZ]
for i in range(0,3):
if abs(holder[i]) <0.05:
holder[i]=0
holder = gyrotoeuler(holder,rotationMatrixToEulerAngles(cum_rot),dt).T
z_rot = np.matrix([
[ math.cos(math.radians(holder.item(2))) , -math.sin(math.radians(holder.item(2))) , 0 ],
[ math.sin(math.radians(holder.item(2))) , math.cos(math.radians(holder.item(2))) , 0 ],
[ 0 , 0 , 1 ]
])
y_rot = np.matrix([
[ math.cos(math.radians(holder.item(1))) , 0 , math.sin(math.radians(holder.item(1))) ],
[ 0 , 1 , 0 ],
[ -math.sin(math.radians(holder.item(1))) , 0 , math.cos(math.radians(holder.item(1))) ]
])
x_rot = np.matrix([
[ 1 , 0 , 0 ],
[ 0 , math.cos(math.radians(holder.item(0))) , -math.sin(math.radians(holder.item(0))) ],
[ 0 , math.sin(math.radians(holder.item(0))) , math.cos(math.radians(holder.item(0))) ]
])
total_rot = z_rot*y_rot*x_rot
cum_rot = cum_rot *total_rot
inertal_acc = cum_rot*g_norm.T
inertal_acc = inertal_acc - np.matrix(inital_g).T
return inertal_acc