Read-Sensor-Resistances/test.py

115 lines
4.3 KiB
Python

from ui import Frame
from multiprocessing import *
from read_arduino import *
from serial_plotter import *
import os, json, wx, warnings
import serial.tools.list_ports
import numpy as np
warnings.filterwarnings("ignore", category=DeprecationWarning)
HEIGHT = 800
WIDTH = 800
global ani, t1
def main():
#################### USER INPUTS ###########################
# TODO: if there is already a setting.json exist, loads all inputs from there as 'default'
resistors = [float(frame.r_ref_1.GetValue()), float(frame.r_ref_2.GetValue()), float(frame.r_ref_3.GetValue()),
float(frame.r_ref_4.GetValue()),
0] # resisters for each An port, where n is an integer from 0-3. Use 0 if none. in Ohms
input_voltage = float(frame.input_voltage.GetValue())
bit_rate = float(frame.adjusted_volt.GetValue())
port = frame.dev_list.GetValue()
# typical window size: 480
window_size = int(frame.m_textCtrl26.GetValue())
#################### END USER INPUTS ########################
# filename, disable customize name but should inform the user the current name of the data file on the front end
# alternatively, could be default input if no other input given. But if there is might have problem...
dat_folder = "RecordedData"
os.makedirs(dat_folder, exist_ok=True)
filename = os.path.join(os.getcwd(), dat_folder, f"{datetime.now().strftime('%Y-%m-%d_%H%M%S')}.csv")
delay = 1000 # millisec pe r data, defined in the firmware
if not (len(resistors) == 5):
raise ValueError(f"expecting 5 resistor values, but got {len(resistors)}!!!")
gen_settings(resistors, input_voltage, bit_rate, port, filename, window_size, delay)
def gen_settings(resistors, input_voltage, bits, port, filename, window_size, delay):
"""
export all inputs from main() to a .json file
"""
name = "settings.json"
settings = {}
settings["refRes"] = resistors
settings["sensor_ports"] = np.where(np.array(resistors) > 0)[0].tolist()
settings["v_in"] = input_voltage
settings["port"] = port
settings["resolution"] = bits
settings["winSize"] = window_size
settings["file_name"] = filename
settings["delay"] = delay
open(name, 'w').write(json.dumps(settings, indent=4))
open(filename, "a", newline="", encoding="utf-8")
def get_devices():
"""
get all available devices connected, put them in the UI's dropdown menu
"""
ports = [comport.device for comport in serial.tools.list_ports.comports()] # get all available ports
frame.dev_list.AppendItems(ports)
def run(e):
"""
run the read_arduino.py and Serial Plotter in parallel
"""
global ani, t1 # the variables to call the plotter and the read_arduino.py, we want them to get tossed around between functions and even programs
main()
if 't1' in globals():
t1.terminate() # end the previous serial reads, if there is any
# place the read() function from read_arduino into another process to run it in background
t1 = Process(target=read, args=())
t1.start()
# run the plotter. Note that we should not put the plotter class, or function, in another process since
# matplot's FuncAnimation doesn't like that
plotter = SerialPlotter()
ani = plotter.plotting()
plotter.fig.show()
frame.btLaunch.SetLabelText("Plot") # change the text on the "launch" button to "plot"
if not frame.show_msg.GetValue():
frame.Hide()
if __name__ == '__main__':
global t1
app = wx.App()
frame = Frame(None)
get_devices()
frame.SetTitle("Cease your resistance! - alpha 0.1.1")
frame.btLaunch.Bind(wx.EVT_BUTTON, run)
if os.path.isfile("settings.json"):
print("Found existing settings.json, auto-fill previous inputs!")
settings = json.load(open('settings.json', 'r'))
frame.r_ref_1.SetValue(str(settings["refRes"][0]))
frame.r_ref_2.SetValue(str(settings["refRes"][1]))
frame.r_ref_3.SetValue(str(settings["refRes"][2]))
frame.r_ref_4.SetValue(str(settings["refRes"][3]))
frame.input_voltage.SetValue(str(settings["v_in"]))
frame.adjusted_volt.SetValue(str(settings["resolution"]))
frame.m_textCtrl26.SetValue(str(settings["winSize"]))
frame.Show()
app.MainLoop()
if 't1' in globals():
t1.terminate() # end the process if it has been started