forked from Hear-Me-Out/Hear-Me-Out
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_sensor_data_wireless.py
142 lines (120 loc) · 4.46 KB
/
collect_sensor_data_wireless.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
'''
Collect sensor data from the glove at sampling_rate
'''
import Adafruit_BluefruitLE
from utils import SensorDataStream
from Adafruit_BluefruitLE.services import UART
import time
import datetime
import csv
import numpy as np
ble = Adafruit_BluefruitLE.get_provider()
rate = 0.02
def connectDevice():
'''
Connects to the Adafruit Bluefruit Module and return the object for that device
'''
#clear any cached data
ble.clear_cached_data()
#get BLE adapter
adapter = ble.get_default_adapter()
adapter.power_on()
print("[ Using adapter: {0} ]".format(adapter.name))
#disconnet any connected UART devices
UART.disconnect_devices()
#scan for UART devices
print("Scanning for UART devices...")
try:
adapter.start_scan()
device = UART.find_device()
if device is None:
raise RuntimeError("Failed to find any UART device!")
finally:
adapter.stop_scan()
print("Connecting to device...")
device.connect()
return device
def readData():
'''
Reads sensor data from the Bluefruit module at the sampling rate
'''
device = connectDevice()
#offset for the IMU
offset = [588, 228, -370, -270, 104, -50, 0,0,0,0,0]
#offset = [0,0,0,0,0,0,0,0,0,0,0]
try:
sd = SensorDataStream(UART,device)
sd.start()
labels = [ "good", "morning", "afternoon", "we_are", "from",
"R", "I", "T", "kottayam", "thank_you", "this_is",
"our", "project", "coffee", "idli", "sambhar",
"water", "how_are", "you", "i_am", "please",
"sorry", "want", "where_is", "railway_station", "dosa" ]
#get sample size
print "Enter sample size: ",
sample_size = input()
for label in labels:
s = sample_size
print label
print "Start collecting data(y/n) ",
a = raw_input().lower()
if a == 'y':
time.sleep(2)
#get data from device
while s:
print "[START ACTION]"
count = 50
row = []
while count:
received = sd.read()
data = []
if received is not None:
#split into the three segments
received = received.split('#')
#don't know why but splitting the string adds null character to the ends, so remove those too
received = list(map(lambda x: x.rstrip('\x00'), received))
#convert from string to list of values
for item in received:
data.extend(item[1:].split(','))
#convert each element in the list from str to int
data = list(map(int,data))
#adjust the offset
data = list(np.subtract(data,offset))
row.append(data)
print(data)
#writer.writerow(row)
else:
print("Received no data!")
time.sleep(rate)
count -= 1
#Uncomment For getting Offsets for Calibration--
import pandas as pd
df = pd.DataFrame(row, columns=np.arange(11))
print(df.mean())
print "Save Data?(y/n)",
#print(row)
a = raw_input().lower()
if a == 'y':
s -= 1
tm = datetime.datetime.now()
tm = '-'.join(str(tm).split())
fname = "Training_Data/"+label+"-"+tm+".csv"
try:
with open(fname, 'w') as f:
writer = csv.writer(f)
for r in row:
writer.writerow(r)
f.close()
except KeyError:
print("[Exit]")
break
finally:
device.disconnect()
def main():
'''
Main function
'''
ble.initialize()
ble.run_mainloop_with(readData)
if __name__ == "__main__":
main()