-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
179 lines (138 loc) · 4.06 KB
/
Main.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
from serial import Serial
from datetime import datetime
import os, time, warnings, kbhit, math
import pandas as pd
import numpy as np
warnings.filterwarnings("ignore")
import time
import board
import adafruit_icm20x
tmpCalX= -1.9303944992140032
tmpCalY= -1.5171074844610022
hCalX = 16.322921
hCalY = -7.212200
hCalZ = -22.263491
sCal1= [1.103134, -0.014659, 0.025388]
sCal2= [-0.014659, 1.040778, 0.014565]
sCal3= [0.025388, 0.014565, 0.979180]
def getCalibarion(x,y,z):
x -= hCalX
y -= hCalY
A = np.array([sCal1, sCal2, sCal3])
B = np.array([[x],[y],[z]])
Cal = np.matmul(A, B)
x = Cal[0][0]
y = Cal[1][0]
z = Cal[2][0]
return x,y
def getHeading(magnetic):
x = magnetic[0]
y = magnetic[1]
z = magnetic[2]
x, y = getCalibarion(x,y,z)
x -= tmpCalX
y -= tmpCalY
compassHeading = -1
if y == 0:
if x >0:
compassHeading = 90.0
else:
compassHeading = 270.0
if x < 0:
compassHeading = 360+math.atan2(x, y)*180/math.pi
else:
compassHeading = math.atan2(x, y)*180/math.pi
return compassHeading
def beforeDataColection(icm):
lx = kbhit.lxTerm()
lx.start()
xList=[]
yList=[]
zList=[]
print("Calibration Start")
while True:
if lx.kbhit():
c = lx.getch()
c_ord = ord(c)
if c_ord == 32: # Spacebar
print("\nCalibration Stop")
break
x = icm.magnetic[0]
y = icm.magnetic[1]
z = icm.magnetic[2]
x, y = getCalibarion(x,y,z)
xList.append(x)
yList.append(y)
zList.append(z)
lx.reset()
comp_df = pd.DataFrame({"x": xList, "y": yList, "z": zList})
offsetX = (comp_df.x.max()+comp_df.x.min())/2
offsetY = (comp_df.y.max()+comp_df.y.min())/2
return offsetX, offsetY
port = "/dev/ttyACM0"
distIndex = 7
def getData():
global tmpCalX, tmpCalY
ser = Serial(port, 115200)
i2c = board.I2C()
icm = adafruit_icm20x.ICM20948(i2c)
lx = kbhit.lxTerm()
lx.start()
dateList=[]
distList = []
timeCnt = 0
compassHeading = getHeading(icm.magnetic)
filteredHeading = compassHeading
headingList=[]
filteredHeadingList=[]
sensitivity = 0.5
res = ser.readline()
dist= res.decode()[:len(res)-1].split(",")[distIndex]
filteredDist = dist
distList=[]
filteredDistList=[]
dSensitivity = 0.5
tmpCalX, tmpCalY = beforeDataColection(icm)
print(tmpCalX, tmpCalY)
while True:
compassHeading = getHeading(icm.magnetic)
filteredHeading = filteredHeading * (1-sensitivity) + compassHeading * sensitivity
headingList.append(compassHeading)
filteredHeadingList.append(filteredHeading)
res = ser.readline()
dist= res.decode()[:len(res)-1].split(",")[distIndex]
filteredDist = filteredDist * (1-dSensitivity) + dist * dSensitivity
distList.append(dist)
filteredDistList.append(filteredDist)
date = datetime.now()
print("[%s] %.2f %s" %(str(date), compassHeading, dist))
distList.append(dist[:len(dist)-1])
dateList.append(date)
if stopLogging(lx):
break
if timeCnt > 0:
print("Stop Logging Enabled")
if stopLogging(lx):
break
lx.reset()
toDistCSV(dateList, distList, filteredHeadingList)
def toDistCSV(dateList, distList, angleList):
dwmDf = pd.DataFrame({'time': dateList, 'distance': distList, 'angle': angleList})
now = datetime.now()
fileName = str(now.strftime('%Y-%m-%d %Hd%M%S'))+".csv"
os.chdir("final_csv/")
dwmDf.to_csv(fileName, index=False)
#os.chdir("../../")
def changeDir():
os.chdir("/home/pi/Documents/Python-RPI-RTLS")
def stopLogging(lx):
if lx.kbhit():
c = lx.getch()
c_ord = ord(c)
if c_ord == 32: # Spacebar
print("\nData Collection Stop")
return True
def runDataCollection():
changeDir()
getData()
runDataCollection()