convert the resistance value to scientific notation

This commit is contained in:
Ryan Xu 2022-12-04 14:40:46 -08:00
parent b1498cbde1
commit 3315eed1da
2 changed files with 11 additions and 8 deletions

View File

@ -31,9 +31,10 @@ def read():
dat_list = np.random.randint(0, v_in * 1000, SENSORS_MAX) # create a randomized voltage data dat_list = np.random.randint(0, v_in * 1000, SENSORS_MAX) # create a randomized voltage data
# 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) r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration
# write + export values as .csv format # write + export values as .csv format
dat = f", ".join(np.insert(r_arr.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) # 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')))
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')
@ -54,9 +55,10 @@ 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) r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration
# write + export values as .csv format # write + export values as .csv format
dat = f", ".join(np.insert(r_arr.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) # 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')))
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')

View File

@ -4,7 +4,7 @@ import os, json, traceback, wx
import numpy as np import numpy as np
import matplotlib import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import decimal
matplotlib.use("WXAgg") # for JetBrains IDE to force use wxPython as backend UI for plotting matplotlib.use("WXAgg") # for JetBrains IDE to force use wxPython as backend UI for plotting
@ -74,9 +74,10 @@ class SerialPlotter:
self.sensorsData[i].append(row[i]) self.sensorsData[i].append(row[i])
# plot a line # plot a line
# TODO: round the number to 1-2- decimal places # round the number to scientific notation
self.axs.plot(self.timeStamps[i],self.sensorsData[i], color=self.colors[i], self.axs.plot(self.timeStamps[i],self.sensorsData[i], color=self.colors[i],
label=f'sensor {i + 1}, latest: {np.floor(self.sensorsData[i][-1])} $\Omega$') label=f'sensor {i + 1}, latest: {np.format_float_scientific(self.sensorsData[i][-1], precision = 2)} $\Omega$')
self.axs.set_xlabel('Time (seconds)') self.axs.set_xlabel('Time (seconds)')
self.axs.set_ylabel(u'Resistance ($\Omega$)') self.axs.set_ylabel(u'Resistance ($\Omega$)')
i += 1 i += 1