-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiMET_sampling.py
executable file
·109 lines (97 loc) · 3.16 KB
/
iMET_sampling.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
#!/usr/bin/python3
import os
import time
import serial
import sys
from gpiozero import LED, Button
#check if Arduino ADC is connected via USB
#if not used, program continues with only iMET recording
try:
ser_ADC = serial.Serial(
port='/dev/ARD',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2
)
use_ADC = 1
except: #error if no adc attached.
#print("No ADC attached. Not using.\n") #TODO
use_ADC = 0
#ser_xBee = serial.Serial(
# port='/dev/ttyS0',
# baudrate = 9600,
# parity=serial.PARITY_NONE,
# stopbits=serial.STOPBITS_ONE,
# bytesize=serial.EIGHTBITS,
# timeout=1
#)
#check if iMet attached
try:
ser_iMET = serial.Serial(
port='/dev/imet',
baudrate = 57600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
except:
print("no iMET attached, cannot continue. \n")
exit()
try:
collect_ADC = LED(27)
stop_button = Button(25, pull_up=False) #TODO: was 25, changed to 13/17
counter=0#potentially unneccessary
#open iMET file
#first get filename
data_path = "/mnt/sda1/iMET_data/IMETDATA"
#location = sys.argv[1]
#init_iMET_filename = "IMETDATA"
for i in range(100):
to_append = str(i)+".CSV" #TODO was i
data_file = data_path + to_append
file_exists = os.path.isfile(data_file)
if file_exists == 0: #if file doesn't exist exit loop and create it
#file is created in the following while loop
break
time.sleep(1)
#print("launching while loop \n") #TODO
temp = 1
while(True):
if use_ADC == 1:
iMET_file = open(data_file, "a+") #open new iMET file appending
data_iMET = ser_iMET.readline().decode()
collect_ADC.on() #tell arduino to grab data
collect_ADC.off()
data_ADC = ser_ADC.readline().decode()
#data_all = data_ADC + data_iMET
#ser_xBee.write(data_all)
iMET_file.write(data_iMET)
iMET_file.write("\n")
iMET_file.close()
else:
iMET_file = open(data_file, "a+") #open new iMET file appending
data_iMET = ser_iMET.readline().decode()
print(data_iMET) #TODO: remove
iMET_file.write(data_iMET)
iMET_file.write("\n")
iMET_file.close()
#stop_button.wait_for_press()
#print("aye bro the button was pressed")
"""
for x in range (10):
#iMET_file = open(data_file, "a+") #open new iMET file appending
data_iMET = ser_iMET.readline().decode()
print(data_iMET)
#iMET_file.write(data_iMET)
#iMET_file.write("\n")
#iMET_file.close()
"""
print("done\n")
except:
print("exception thrown")
if not iMET_file.closed:
print("closing file")
iMET_file.close()