Read-Sensor-Resistances/test.py

119 lines
4.8 KiB
Python
Raw Normal View History

2022-05-15 19:48:22 +00:00
from ui import Frame
2022-08-12 07:33:27 +00:00
from multiprocessing import *
from read_arduino import *
2022-08-08 22:51:36 +00:00
from serial_plotter import *
import os, sys, json, wx,importlib, warnings
2022-05-15 19:48:22 +00:00
import serial.tools.list_ports
import numpy as np
warnings.filterwarnings("ignore", category=DeprecationWarning)
# acknowledgement: https://stackoverflow.com/a/68666505. handles splash screens
if '_PYIBoot_SPLASH' in os.environ and importlib.util.find_spec("pyi_splash"):
import pyi_splash
pyi_splash.update_text('UI Loaded ...')
pyi_splash.close()
2022-05-15 19:48:22 +00:00
HEIGHT = 800
WIDTH = 800
2022-08-12 07:33:27 +00:00
global ani, t1
2022-08-08 22:51:36 +00:00
2022-05-15 19:48:22 +00:00
def main():
#################### USER INPUTS ###########################
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())
2022-05-15 19:48:22 +00:00
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)
2022-05-15 19:48:22 +00:00
def gen_settings(resistors, input_voltage, bits, port, filename, window_size, delay):
2022-05-15 19:48:22 +00:00
"""
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
2022-05-15 19:48:22 +00:00
settings["port"] = port
settings["resolution"] = bits
2022-05-15 19:48:22 +00:00
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")
2022-08-12 07:33:27 +00:00
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
2022-08-12 07:33:27 +00:00
# matplot's FuncAnimation doesn't like that
2022-08-08 22:51:36 +00:00
plotter = SerialPlotter()
ani = plotter.plotting()
plotter.fig.show()
2022-05-15 19:48:22 +00:00
2022-08-12 07:33:27 +00:00
frame.btLaunch.SetLabelText("Plot") # change the text on the "launch" button to "plot"
2022-05-15 19:48:22 +00:00
if not frame.show_msg.GetValue():
frame.Hide()
2022-07-17 20:29:44 +00:00
2022-05-15 19:48:22 +00:00
if __name__ == '__main__':
sys.stdout.write("this is an alpha version of the design. This debug terminal will be gone on official release\n")
# Acknowledgement: https://stackoverflow.com/a/27694505
if sys.platform.startswith('win'):
# On Windows calling this function is necessary.
freeze_support()
2022-08-12 07:33:27 +00:00
global t1
2022-05-15 19:48:22 +00:00
app = wx.App()
frame = Frame(None)
ports = [comport.device for comport in serial.tools.list_ports.comports()] # get all available ports
frame.dev_list.AppendItems(ports)
frame.SetTitle("Cease your resistance! - alpha 0.2.0")
2022-05-15 19:48:22 +00:00
frame.btLaunch.Bind(wx.EVT_BUTTON, run)
if os.path.isfile("settings.json"):
sys.stdout.write("Found existing settings.json, auto-fill previous inputs!\n")
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"]))
if settings["port"] in ports: # auto-select device port if exist
frame.dev_list.SetValue(settings["port"])
2022-05-15 19:48:22 +00:00
frame.Show()
app.MainLoop()
2022-08-12 07:33:27 +00:00
if 't1' in globals():
t1.terminate() # gracefully end the read_arduino process if it has been started