forked from arminstr/reremeter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaceManualWithTransmission.py
More file actions
155 lines (137 loc) · 6.14 KB
/
interfaceManualWithTransmission.py
File metadata and controls
155 lines (137 loc) · 6.14 KB
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
import serial
import struct
import re
import numpy
import csv
import time
import os
dirname = "../data/training_scans/" + time.strftime("%Y%m%d")
filename = "../data/training_scans/" + time.strftime("%Y%m%d")+ "/data"+ time.strftime("%Y%m%d-%H%M%S") + ".csv"
filename_corrected = "../data/training_scans/" + time.strftime("%Y%m%d")+ "/data_corrected"+ time.strftime("%Y%m%d-%H%M%S") + ".csv"
toprow = ["PlasticType", "Color", "MeasurementType", "white", "nm850", "nm940", "nm1200", "nm1300", "nm1450", "nm1550", "nm1650"]
toprow_corrected = ["PlasticType", "wavelength1", "wavelength2", "wavelength3", "wavelength4", "wavelength5", "wavelength6", "wavelength7", "wavelength8","wavelength9","wavelength10","wavelength11","wavelength12","wavelength13","wavelength14"]
wavelength_correction = [10887, 162684, 224906, 134547, 113060, 135121, 101445, 78442]
# Create target directory & all intermediate directories if don't exists
if not os.path.exists(dirname):
os.makedirs(dirname)
# write top row #
with open(filename, 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(toprow)
csvFile.close()
# write top row #
with open(filename_corrected, 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(toprow_corrected)
csvFile.close()
ser = serial.Serial('/dev/tty.usbmodem205231794B411')
print(ser.name)
transmission_started = False
measurements = []
wavelengths = []
index = 1
cmd = 'n'
scan_again = 'n'
while 1:
if ser.is_open == False:
ser.open()
ser.flushInput()
ser.flush()
if(scan_again == 'n'):
plastic_type = input('Input plastic TYPE (PET, HDPE, PVC, LDPE, PP, PS, other): ')
plastic_color = input('Input plastic COLOR (green, white, blue, clear, other): ')
cmd = input('Do you want to measure ' + plastic_color + ' ' + plastic_type + ' (y/n)? ')
elif(scan_again == 'y'):
cmd = 'y'
else:
scan_again = 'n'
cmd = 'n'
if cmd == 'y':
start = time.time()
ser.write(b'CMD=1')
#byte_array = ser.readline()
string_array = str(ser.readline(), 'utf-8')
if string_array.startswith("CMD=OK"):
transmission_started = True
print('Measurement started...')
for x in range(0, 3):
string_array = str(ser.readline(), 'utf-8')
if string_array.startswith("MEASURE=PRE"):
measurement_type = "PRE"
if string_array.startswith("MEASURE=VALUE"):
measurement_type = "VALUE"
if string_array.startswith("MEASURE=POST"):
measurement_type = "POST"
num_array = re.findall('\d+', string_array) #find all integers
measurements.append([plastic_type, plastic_color, measurement_type, num_array[0], num_array[1], num_array[2], num_array[3], num_array[4], num_array[5], num_array[6], num_array[7]]) #store an array to write to the csv file
if len(measurements) > 3:
measurements = measurements[3:]
print(measurements)
string_array = str(ser.readline(), 'utf-8')
if string_array.startswith("MEASURE=FINISH"):
end = time.time()
plastic_id = -1
if(plastic_type == 'PET'):
plastic_id = 1
if(plastic_type == 'HDPE'):
plastic_id = 2
if(plastic_type == 'PVC'):
plastic_id = 3
if(plastic_type == 'LDPE'):
plastic_id = 4
if(plastic_type == 'PP'):
plastic_id = 5
if(plastic_type == 'PS'):
plastic_id = 6
if(plastic_type == 'OTHER'):
plastic_id = 7
if(plastic_type == 'none'):
plastic_id = 0
for y in range(0, 8):
j = y+3
wavelengths.insert(y, (int(round(int(measurements[1][j]) - (int(measurements[0][j]) + int(measurements[2][j]))/2))) - wavelength_correction[y])
measurements_corrected = [
plastic_id,
wavelengths[0],
wavelengths[1],
wavelengths[2],
wavelengths[3],
wavelengths[4],
wavelengths[5],
wavelengths[6],
wavelengths[7],
(wavelengths[2] - wavelengths[1])/(940-850),
(wavelengths[3] - wavelengths[2])/(1200-940),
(wavelengths[4] - wavelengths[3])/(1300-1200),
(wavelengths[5] - wavelengths[4])/(1450-1300),
(wavelengths[6] - wavelengths[5])/(1550-1450),
(wavelengths[7] - wavelengths[6])/(1650-1550)
]
print(measurements_corrected)
##### write lines #####
with open(filename_corrected, 'r') as readFile:
reader = csv.reader(readFile)
lines = list(reader)
lines.append(measurements_corrected)
with open(filename_corrected, 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)
readFile.close()
writeFile.close()
##### write lines #####
with open(filename, 'r') as readFile:
reader = csv.reader(readFile)
lines = list(reader)
for x in range(0, 3):
lines.append(measurements[x])
with open(filename, 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)
readFile.close()
writeFile.close()
time_ms = int(round((end - start)*1000))
print(str(time_ms) + ' ms')
print('Measurement successfully stored!')
cmd = 'n'
scan_again = input('Scan another sample (y/n)? ')
ser.close()