-
Notifications
You must be signed in to change notification settings - Fork 0
/
acc.py
executable file
·329 lines (262 loc) · 8.95 KB
/
acc.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
####################################
# Libraries
####################################
#I2C library
import smbus
#GPIO
import RPi.GPIO as GPIO
#Requests
import requests
#JSON
import json
#Math
import math
#Other
import time, os
####################################
# User Parameters
# (Edit these as necessary)
####################################
#Set LIS331 address
addr = 0x19
#Set the acceleration range
maxScale = 400
#Set the LED GPIO pin
LED = 26
#Open file to save all data
#(creates new file in same folder if none
#and appends to existing file)
allData = open("AllSensorData.txt", "a")
#Open file to save alert data
#(creates new file in same folder if none
#and appends to existing file)
alrtData = open("AlertData.txt", "a")
#Open file to save unsent data
#(creates new file in same folder if none
#and appends to existing file)
unsentData = open("UnsentData.txt", "a")
#Open file to save testing data
#(creates new file in same folder if none
#and appends to existing file)
testingData = open("TestingData.txt", "a")
####################################
# Initializations & Functions
# (Leave as-is unless you are
# comfortable w/ code)
####################################
#LIS331 Constants (see Datasheet)
CTRL_REG1 = 0x20
CTRL_REG4 = 0x23
OUT_X_L = 0x28
OUT_X_H = 0x29
OUT_Y_L = 0x2A
OUT_Y_H = 0x2B
OUT_Z_L = 0x2C
OUT_Z_H = 0x2D
POWERMODE_NORMAL = 0x27
RANGE_100G = 0x00
RANGE_200G = 0x10
RANGE_400G = 0x30
# Create I2C bus
bus = smbus.SMBus(1)
#Initialize GPIO and turn GPIO 26 to low
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, GPIO.LOW)
#Initiliaze LIS331
def initialize(addr, maxScale):
scale = int(maxScale)
#Initialize accelerometer control register 1: Normal Power Mode and 50 Hz sample rate
bus.write_byte_data(addr, CTRL_REG1, POWERMODE_NORMAL)
#Initialize acceleromter scale selection (6g, 12 g, or 24g). This example uses 24g
if maxScale == 100:
bus.write_byte_data(addr, CTRL_REG4, RANGE_100G)
elif maxScale == 200:
bus.write_byte_data(addr, CTRL_REG4, RANGE_200G)
elif maxScale == 400:
bus.write_byte_data(addr, CTRL_REG4, RANGE_400G)
else:
print #"Error in the scale provided -- please enter 100, 200, or 400"
#Function to read the data from accelerometer
def readAxes(addr):
data0 = bus.read_byte_data(addr, OUT_X_L)
data1 = bus.read_byte_data(addr, OUT_X_H)
data2 = bus.read_byte_data(addr, OUT_Y_L)
data3 = bus.read_byte_data(addr, OUT_Y_H)
data4 = bus.read_byte_data(addr, OUT_Z_L)
data5 = bus.read_byte_data(addr, OUT_Z_H)
#Combine the two bytes and leftshit by 8
x = data0 | data1 << 8
y = data2 | data3 << 8
z = data4 | data5 << 8
#in case overflow
if x > 32767 :
x -= 65536
if y > 32767:
y -= 65536
if z > 32767 :
z -= 65536
#Calculate the two's complement as indicated in the datasheet
x = ~x
y = ~y
z = ~z
return x, y, z
#Function to calculate g-force from acceleration data
def convertToG(maxScale, xAccl, yAccl, zAccl):
#Caclulate "g" force based on the scale set by user
#Eqn: (2*range*reading)/totalBits (e.g. 48*reading/2^16)
X = (2*float(maxScale) * float(xAccl))/(2**16);
Y = (2*float(maxScale) * float(yAccl))/(2**16);
Z = (2*float(maxScale) * float(zAccl))/(2**16);
return X, Y, Z
#def sendToServer(timestamp, x, y, z):
def isDanger(timestamp, x, y, z):
counter = 0
x = long(x)
y = long(y)
z = long(z)
if abs(x) > 9 or abs(y) > 9 or abs(z) > 9:
alrtData.write(str(timestamp) + "\t" + "x: " + str(x) + "\t" + "y: " + str(y) + "\t" + "z: " + str(z) + "\n")
GPIO.output(LED, GPIO.HIGH)
elif abs(x) > 4 or abs(y) > 4 or abs(z) > 4:
while abs(x) > 4 or abs(y) > 4 or abs(z) > 4:
time_start = time.time()
counter = counter + 1
if counter > 4:
break
time_end = time.time()
if (counter > 4):
#sendToServer(x, y, z, timestamp)
alrtData.write(str(timestamp) + "\t" + "x: " + str(x) + "\t" + "y: " + str(y) + "\t" + "z: " + str(z) + "\n")
GPIO.output(LED, GPIO.HIGH)
def sendBatchToServer(xvalues, yvalues, zvalues, timestamps):
print "Sending data"
#data = 'data': [ {'x-axis': " + x_values[0] +", 'y-axis': " + y_values[0] +", 'z-axis': " + z_values[0] }"
data = {
"data": []
}
for i in range(0, 49):
data["data"].append({
"xAxis":x_values[i],
"yAxis":y_values[i],
"zAxis":z_values[i],
"timestamp":timestamps[i]
})
#url = "http://192.168.4.1:4000/receiveData"
#headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
#r = requests.post(url, json.dumps(data), headers=headers)
def checkForDanger(previousFive, timestamp):
totalVal = 0
avgVal = 0
maxVal = 0
for point in previousFive:
totalVal += point
if point > maxVal:
maxVal = point
avgVal = totalVal / 5
if (maxVal > 30) and (avgVal > 20):
print "calling send to server"
sendToServer(int(maxVal), int(avgVal), timestamp)
def sendToServer(maxVal, avgVal, timestamp):
print "sending to server"
print maxVal
print avgVal
data = {
"data": []
}
data["data"].append({
"max": maxVal,
"avg": avgVal,
"timestamp": timestamp
})
url = "http://192.168.4.1:4000/receiveData"
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, json.dumps(data), headers=headers)
print "Sent to server"
####################################
# Main Function
####################################
def main():
print ("Starting stream")
previous = [-1, -1, -1, -1, -1]
overThreshold = 2
belowFor = 0
while True:
#initialize LIS331 accelerometer
initialize(addr, 24)
#Start timestamp
ts = round(time.time()*1000);
#Write timestamp to AllSensorData file
allData.write(str(ts) + "\t")
#Get acceleration data for x, y, and z axes
xAccl, yAccl, zAccl = readAxes(addr)
#Calculate G force based on x, y, z acceleration data
x, y, z = convertToG(maxScale, xAccl, yAccl, zAccl)
#Determine if G force is dangerous to human body & take proper action
isDanger(ts, x, y, z)
#Write all sensor data to file AllSensorData (as you probably guessed :) )
allData.write("x: " + str(x) + "\t" + "y: " + str(y) + "\t" + "z: " + str(z) + "\n")
#calculate total acceleration
total = math.sqrt(math.pow(x,2) + math.pow(y,2) + math.pow(z,2))
#print G values (don't need for full installation)
print "Acceleration in X-Axis : %d" %x
print "Acceleration in Y-Axis : %d" %y
print "Acceleration in Z-Axis : %d" %z
print "Total Acceleration : %d" %total
print "\n"
#uncomment if testing
#testingData.write(str(ts) + "\t" + "x: " + str(x) + "\t" + "y: " + str(y) + "\t" + "z: " + str(z)+ "\t" + "total: " + str(total) + "\n")
#update previousFive array
#previous[0] = previous[1]
#previous[1] = previous[2]
#previous[2] = previous[3]
#previous[3] = previous[4]
#previous[4] = total
if total > 40 and overThreshold == 2:
overThreshold = 0
belowFor = 0
elif total < 40 and overThreshold == 0:
belowFor += 1
if belowFor > 1:
print "calling check for danger"
checkForDanger(previous, ts)
overThreshold = 2
belowFor = 0
previous[0] = previous[1]
previous[1] = previous[2]
previous[2] = previous[3]
previous[3] = previous[4]
previous[4] = total
#data filtering
#checkForDanger(previous, ts)
#Short delay to prevent overclocking computer
time.sleep(0.05)
#data filtering
#if the data point has a value above 70G's, enter this block
#if(total > 70):
#increasing = true
#if(increasing):
#if(total > currentMax):
#currentMax = total
#else:
#increasing = false
#saveNextTwo = true
#if(saveNextTwo):
#counter++
#if(counter == 2):
#sendToServer(previousFive)
#Run this program unless there is a keyboard interrupt
try:
while True:
pass
except KeyboardInterrupt:
myprocess.kill()
allData.close()
alrtData.close()
GPIO.cleanup()
if __name__ =="__main__":
main()
allData.close()
alrtData.close()
unsentData.close()
GPIO.cleanup()