From 0dfa01360690cf741bd9f3a539aa3217d0dc8958 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Fri, 1 Jan 2021 18:33:52 +0100 Subject: [PATCH] Added function for executing csv-files --- Test1.csv | 23 +++++++++++++++++++++++ cage_func.py | 47 +++++++++++++++++++++++++++++++++++++++++++---- requirements.txt | 3 ++- 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 Test1.csv diff --git a/Test1.csv b/Test1.csv new file mode 100644 index 0000000..ba50a1b --- /dev/null +++ b/Test1.csv @@ -0,0 +1,23 @@ +Time (s);xField (T);yField (T);zField (T) +0,5;0,000015;0,000025;0,00002 +1;0,0000155;0,0000245;0,0000205 +1,5;0,000016;0,000024;0,000021 +2;0,0000165;0,0000235;0,0000215 +2,5;0,000017;0,000023;0,000022 +3;0,0000175;0,0000225;0,0000225 +3,5;0,000018;0,000022;0,000023 +4;0,0000185;0,0000215;0,0000235 +4,5;0,000019;0,000021;0,000024 +5;0,0000195;0,0000205;0,0000245 +5,5;0,00002;0,00002;0,000025 +6;0,0000205;0,0000195;0,0000245 +6,5;0,000021;0,000019;0,000024 +7;0,0000215;0,0000185;0,0000235 +7,5;0,000022;0,000018;0,000023 +8;0,0000225;0,0000175;0,0000225 +8,5;0,000023;0,000017;0,000022 +9;0,0000235;0,0000165;0,0000215 +9,5;0,000024;0,000016;0,000021 +10;0,0000245;0,0000155;0,0000205 +10,5;0,000025;0,000015;0,00002 +11;0,000025;0,000015;0,00002 diff --git a/cage_func.py b/cage_func.py index 3254a0f..0816ca4 100644 --- a/cage_func.py +++ b/cage_func.py @@ -1,5 +1,7 @@ from pyps2000b import PS2000B import globals as g +import pandas +import time def set_devices(): # creates device objects for all PSUs @@ -29,10 +31,9 @@ def setup_arduino(): def safe_arduino(): # sets output pins to low and closes serial connection for pin in g.RELAY_PINS: g.ARDUINO.digitalWrite(pin, "LOW") - g.ARDUINO.close() -def print_status(axis): # axis control variable, stored in globals.py +def print_status(axis): # axis = axis control variable, stored in globals.py device = axis[0] # PSU channel = axis[1] # output channel on the PSU print("%s, %0.2f V, %0.2f A" @@ -48,11 +49,25 @@ def print_status_3(): print_status(g.Z_AXIS) -def set_to_zero(device): +def set_to_zero(device): # sets voltages and currents to 0 device.voltage1 = 0 device.current1 = 0 + device.voltage2 = 0 device.current2 = 0 - device.current2 = 0 + + +def power_down(): # temporary, set all outputs to 0 but keep connections enabled + set_to_zero(g.XY_DEVICE) + set_to_zero(g.Z_DEVICE) + safe_arduino() + + +def shut_down(): # shutdown at program end, set outputs to 0 and disable connections + set_to_zero(g.XY_DEVICE) + set_to_zero(g.Z_DEVICE) + deactivate_all() + safe_arduino() + g.ARDUINO.close() def set_field_simple(vector): # forms magnetic field as specified by vector, w/o cancelling ambient field @@ -91,3 +106,27 @@ def set_axis_current(axis, value): # sets current with correct polarity on one device.set_current(abs(value), channel) maxVoltage = min(max(1.1 * g.MAX_AMPS[axisIndex] * g.RESISTANCES[axisIndex], 12), g.MAX_VOLTS) # limit voltage device.set_voltage(maxVoltage) + + +def execute_csv(filepath, printing=0): # runs through csv file containing times and desired field vectors + # csv format: time (s); xField (T); yField (T); zField (T) + # decimal commas + print("Reading File:", filepath) + file = pandas.read_csv(filepath, sep=';', decimal=',', header=0) # read csv file + array = file.to_numpy() # convert csv to array + t_zero = time.time() + t_ref = t_zero + i = 0 + print("Starting Execution...") + while i < len(array): + t = time.time() - t_zero + if t >= array[i, 0]: + field_vec = array[i, 1:4] + print("t = %0.2f s, target field vector = " % (array[i, 0]), field_vec) + set_field(field_vec) + i = i + 1 + if t - t_ref >= 1 and printing == 1: # print status every second + print_status_3() + t_ref = t + print("File executed, powering down channels.") + power_down() # set currents and voltages to 0, set arduino pins to low diff --git a/requirements.txt b/requirements.txt index 0a4005f..c998aa7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ numpy==1.19.3 # bug in numpy 1.19.4, 1.19.3 used as workaround pyserial~=3.5 -future~=0.18.2 \ No newline at end of file +future~=0.18.2 +pandas~=1.1.5 \ No newline at end of file