now is able to work with multiple resistors

This commit is contained in:
Ryan Xu 2023-01-08 15:59:57 -08:00
parent 41e3b99e05
commit 438f2cc720
1 changed files with 30 additions and 4 deletions

View File

@ -32,7 +32,15 @@ def read():
# open the file and add a line of header to it, then close # open the file and add a line of header to it, then close
f = open(file_name, "a", newline="", encoding="utf-8") f = open(file_name, "a", newline="", encoding="utf-8")
writer = csv.writer(f) writer = csv.writer(f)
# headers based on number of ports used
if len(sensor_ports) == 1:
header = ['Time', 'Resistance'] header = ['Time', 'Resistance']
if len(sensor_ports)== 2:
header = ['Time', 'Resistance1', 'Resistance2']
if len(sensor_ports) == 3:
header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3']
if len(sensor_ports) == 4:
header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3', 'Resistance4']
writer.writerow(header) writer.writerow(header)
f.close() f.close()
while True: while True:
@ -40,9 +48,14 @@ def read():
# take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors
dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2 dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2
r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration
# created a new array to convert resistance values to sci notation
r_arr2 = np.empty(len(r_arr), dtype=object)
for i in range(len(r_arr)) :
a = np.format_float_scientific(r_arr[i], precision = 2)
r_arr2[i] = a
# write + export values as .csv format # write + export values as .csv format
# converted resistance values in array to scientific notation # converted resistance values in array to scientific notation
dat = f", ".join(np.insert(np.format_float_scientific(r_arr.astype(str)), 0, datetime.now().strftime('%H:%M:%S'))) dat = f", ".join(np.insert(r_arr2.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) #np.format_float_scientific()
print(dat) print(dat)
f = open(file_name, "a", newline="", encoding="utf-8") f = open(file_name, "a", newline="", encoding="utf-8")
f.write(dat + '\n') f.write(dat + '\n')
@ -55,7 +68,15 @@ def read():
# open the file and add a line of header to it, then close # open the file and add a line of header to it, then close
f = open(file_name, "a", newline="", encoding="utf-8") f = open(file_name, "a", newline="", encoding="utf-8")
writer = csv.writer(f) writer = csv.writer(f)
# headers based on number of ports used
if len(sensor_ports) == 1:
header = ['Time', 'Resistance'] header = ['Time', 'Resistance']
if len(sensor_ports)== 2:
header = ['Time', 'Resistance1', 'Resistance2']
if len(sensor_ports) == 3:
header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3']
if len(sensor_ports) == 4:
header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3', 'Resistance4']
writer.writerow(header) writer.writerow(header)
f.close() f.close()
while controller.isOpen(): while controller.isOpen():
@ -70,9 +91,14 @@ def read():
# take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors
dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2 dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2
r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration
# created a new array to convert resistance values to sci notation
r_arr2 = np.empty(len(r_arr), dtype=object)
for i in range(len(r_arr)) :
a = np.format_float_scientific(r_arr[i], precision = 2)
r_arr2[i] = a
# write + export values as .csv format # write + export values as .csv format
# converted resistance values in array to scientific notation # converted resistance values in array to scientific notation
dat = f", ".join(np.insert(np.format_float_scientific(r_arr.astype(str)), 0, datetime.now().strftime('%H:%M:%S'))) dat = f", ".join(np.insert(r_arr2.astype(str), 0, datetime.now().strftime('%H:%M:%S')))
print(dat) print(dat)
f = open(file_name, "a", newline="", encoding="utf-8") f = open(file_name, "a", newline="", encoding="utf-8")
f.write(dat + '\n') f.write(dat + '\n')