Files
Helmholtz_Test_Bench/csv_threading.py
T
2021-02-07 13:25:09 +01:00

66 lines
2.9 KiB
Python

import cage_func as func
import time
import pandas
from threading import *
import globals as g
class ExecCSVThread(Thread):
def __init__(self, threadID, array, parent, controller):
# ToDo: comments
Thread.__init__(self)
self.threadID = threadID
self.array = array # numpy array containing data from csv to be executed
self.parent = parent
self.controller = controller # object on which mainloop() is running, usually main window
def run(self):
func.ui_print("Starting Sequence Execution...")
# g.threadLock.acquire() # Get lock to synchronize threads
# ToDo: add locking/synchronization? Works without so far but might be more robust
execute_sequence(self.array, 0.1, self.controller)
self.parent.running = False
# reset buttons:
self.parent.select_file_button["state"] = "normal"
self.parent.execute_button["state"] = "normal"
self.parent.stop_button["state"] = "disabled"
def execute_sequence(array, delay, controller): # runs through array containing times and desired field vectors
# array format: [time (s), xField (T), yField (T), zField (T)]
# decimal commas
# all times in seconds
func.power_down_all() # sets outputs to 0 before starting
t_zero = time.time() # set reference time for start of run
i = 0
while i < len(array) and g.running: # while array is not finished and user has not cancelled
t = time.time() - t_zero # get relative time
if t >= array[i, 0]: # time for this row has come
field_vec = array[i, 1:4] # extract desired field vector
func.ui_print("%f s: t = %0.2f s, target field vector = "
% (time.time()-t_zero, array[i, 0]), field_vec*1e6, "\u03BCT")
func.set_field_simple(field_vec) # send field vector to test stand # ToDo!: reset to set_field()
func.ui_print(time.time()-t_zero)
controller.StatusDisplay.update_labels() # update status display after change
i = i + 1 # next row
elif t >= array[i, 0] - delay - 0.02: # next change time is close, not enough time to sleep
pass
else: # sleep to give other threads time to run
time.sleep(delay)
if g.running: # sequence ended without interruption
func.ui_print("Sequence executed, powering down channels.")
else: # interrupted by user
func.ui_print("Sequence cancelled, powering down channels.")
func.power_down_all() # set currents and voltages to 0, set arduino pins to low
def read_csv_to_array(filepath):
# csv format: time (s); xField (T); yField (T); zField (T)
# decimal commas
func.ui_print("Reading File:", filepath)
file = pandas.read_csv(filepath, sep=';', decimal=',', header=0) # read csv file
array = file.to_numpy() # convert csv to array
return array