From 49083386727940db2975c2c20ff0eb79cf199bb6 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Tue, 26 Jan 2021 17:28:37 +0100 Subject: [PATCH] Testing and debug with PSU --- .gitignore | 3 + .idea/Python-PS2000B.iml | 2 +- .idea/misc.xml | 2 +- Arduino/__pycache__/__init__.cpython-37.pyc | Bin 256 -> 0 bytes One_Unit_Test.py | 28 +++--- User_Interface.py | 39 +++------ cage_func.py | 80 +++++++++++------- pyps2000b/PS2000B.py | 16 ++-- pyps2000b/__pycache__/__init__.cpython-37.pyc | Bin 230 -> 0 bytes settings.py | 18 ++-- 10 files changed, 99 insertions(+), 89 deletions(-) delete mode 100644 Arduino/__pycache__/__init__.cpython-37.pyc delete mode 100644 pyps2000b/__pycache__/__init__.cpython-37.pyc diff --git a/.gitignore b/.gitignore index 51044e5..37b8976 100644 --- a/.gitignore +++ b/.gitignore @@ -95,3 +95,6 @@ ENV/ # VScode .vscode/ *.pyc +.idea/misc.xml +.idea/Python-PS2000B.iml +.idea/Python-PS2000B.iml diff --git a/.idea/Python-PS2000B.iml b/.idea/Python-PS2000B.iml index 653cec9..98105af 100644 --- a/.idea/Python-PS2000B.iml +++ b/.idea/Python-PS2000B.iml @@ -2,7 +2,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 5ea737b..6724169 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Arduino/__pycache__/__init__.cpython-37.pyc b/Arduino/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 6d84313836f0da1c49a94fa67dd28ed7e32bbfe5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 256 zcmXv|F-`+95VU;N#oSz<>dai@@*ey;MR3fO#^dac1=t4hF_iO!NiCxew zCveH@y?yC%7|GHzI@Y@o852?phwMBA+)ZK%bJeF)lbZ=4Lw17aKo98gN_Q IizT_1znJGo5dZ)H diff --git a/One_Unit_Test.py b/One_Unit_Test.py index 5aa8a59..99d492a 100644 --- a/One_Unit_Test.py +++ b/One_Unit_Test.py @@ -7,7 +7,7 @@ from pyps2000b import PS2000B # User Inputs/Configuration---------------------------------- Test1 = 0 -Test2 = 0 +Test2 = 1 Test3 = 0 Test4 = 0 @@ -22,32 +22,38 @@ XY_PORT = "COM7" g.XY_DEVICE = PS2000B.PS2000B(XY_PORT) g.MAX_AMPS = np.sqrt(g.MAX_WATTS / g.RESISTANCES) -print(g.MAX_AMPS) +#print(g.MAX_AMPS) -def print_status(): +'''def print_status(): print("Output 1:") func.print_status(g.X_AXIS) print("Output 2:") - func.print_status(g.Y_AXIS) + func.print_status(g.Y_AXIS)''' -g.XY_DEVICE.enable_all() func.set_to_zero(g.XY_DEVICE) -print_status() +#print_status() t.sleep(3) if Test1 == 1: + print("setting") g.XY_DEVICE.voltage1 = 5 + g.XY_DEVICE.current1 = 1 + g.XY_DEVICE.enable_all() t.sleep(1) - print_status() + #print_status() t.sleep(5) + print("setting to zero") func.set_to_zero(g.XY_DEVICE) if Test2 == 1: - g.XY_DEVICE.current1 = 0.2 + print("setting current") + g.XY_DEVICE.set_current(0.2, 0) + g.XY_DEVICE.set_voltage(5, 0) + g.XY_DEVICE.enable_all() t.sleep(1) - print_status() + #print_status() t.sleep(5) func.set_to_zero(g.XY_DEVICE) @@ -59,8 +65,8 @@ if Test4 == 1: func.set_to_zero(g.XY_DEVICE) t.sleep(1) -print_status() +#print_status() g.XY_DEVICE.disable_all() -print_status() +#print_status() diff --git a/User_Interface.py b/User_Interface.py index f6fd8ae..8c259f3 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -2,7 +2,6 @@ from tkinter import * from tkinter import ttk import settings as g import cage_func as func -import random as rand import numpy as np NORM_FONT = () @@ -67,7 +66,7 @@ class TestFrame(Frame): # ToDo: remove one.pack(fill=X) two = Label(self, text="Two", bg="blue") two.pack() - button = ttk.Button(self, text="Print stuff", command=lambda: print_stuff("Hello")) + button = ttk.Button(self, text="Print stuff", command=lambda: print("Hello")) button.pack() @@ -149,17 +148,17 @@ class ManualMode(Frame): def execute(self): function_to_call = self.modes[self.input_mode.get()][0] # get function of appropriate mode - vector = np.array([0, 0, 0]) + vector = np.array([0, 0, 0], dtype=float) i = 0 for var in self.entry_vars: vector[i] = float(var.get()) i = i + 1 - print(vector) function_to_call(vector) # call function + # ToDo: update status display here @staticmethod def execute_field(vector): - print("field executing") + print("field executing", vector) try: func.set_field_simple(vector*1e-6) # ToDo: change to set_field except ValueError as e: @@ -167,7 +166,7 @@ class ManualMode(Frame): @staticmethod def execute_current(vector): - print("current executing") + print("current executing:", vector) try: func.set_current_vec(vector) except ValueError as e: @@ -220,6 +219,11 @@ class StatusDisplay(Frame): col = col + 1 # rowCounter = rowCounter + self.rowNo # increase row counter to place future stuff below this + # add button for reinitialization + reinit_button = Button(self, text="Reinitialize", command=func.setup_axes, + pady=5, padx=5, font=BIG_BUTTON_FONT) + reinit_button.grid(row=0, column=self.columnNo+1, rowspan=3) + self.update_labels(controller) def update_labels(self, controller): @@ -243,25 +247,4 @@ class StatusDisplay(Frame): self.label_dict["Target Current:"][i].set("%0.3f A" % axis.target_current) self.label_dict["Inverted:"][i].set(axis.polarity_switched) i = i + 1 - controller.after(2000, lambda: self.update_labels(controller)) - - -# ToDo: remove -def print_stuff(stuff): - print(stuff) - - -def random_no(): - return rand.uniform(0, 20) - - -class TestValues: - def __init__(self): - self.val1 = 0 - self.val2 = 0 - self.val3 = 0 - - def update_values(self): - self.val1 = rand.uniform(0, 20) - self.val2 = rand.uniform(0, 20) - self.val3 = rand.uniform(0, 20) + controller.after(500, lambda: self.update_labels(controller)) diff --git a/cage_func.py b/cage_func.py index f51f669..d246ccb 100644 --- a/cage_func.py +++ b/cage_func.py @@ -61,7 +61,7 @@ class Axis: self.voltage_setpoint = self.device.get_voltage_setpoint(self.channel) self.current = self.device.get_current(self.channel) self.current_setpoint = self.device.get_current_setpoint(self.channel) - except serial.serialutil.SerialException: + except (serial.serialutil.SerialException, IndexError): # print("Connection Error with %s PSU on %s" % (self.name, self.port)) self.connected = "Connection Error" self.output_active = "Unknown" @@ -69,16 +69,17 @@ class Axis: else: self.connected = "Connected" - try: - if g.ARDUINO.digitalRead(self.ardPin): # ToDo: Test if this actually works - self.polarity_switched = "True" - else: self.polarity_switched = "False" - except Exception: - print("Error with Arduino") - self.polarity_switched = "Unknown" - g.ARDUINO.connected = "Connection Error" - else: - g.ARDUINO.connected = "Connected" + if g.ARDUINO.connected == "Connected": + try: + if g.ARDUINO.digitalRead(self.ardPin): # ToDo: Test if this actually works + self.polarity_switched = "True" + else: self.polarity_switched = "False" + except Exception: + print("Error with Arduino") + self.polarity_switched = "Unknown" + g.ARDUINO.connected = "Connection Error" + else: + g.ARDUINO.connected = "Connected" def print_status(self): # axis = axis control variable, stored in settings.py print("%s, %0.2f V, %0.2f A" @@ -89,28 +90,32 @@ class Axis: self.target_current = 0 self.target_field = 0 self.target_field_comp = 0 - self.device.voltage1 = 0 - self.device.current1 = 0 + self.device.set_voltage(0, self.channel) + self.device.set_current(0, self.channel) + self.device.disable_output(self.channel) g.ARDUINO.digitalWrite(self.ardPin, "LOW") def set_signed_current(self, value): # sets current with correct polarity on this axis device = self.device channel = self.channel ardPin = self.ardPin + # print("Attempting to set current", value, "A") self.target_current = value if self.connected == "Connected": if abs(value) > self.max_amps: # prevent excessive currents - shut_down_all() # safe all devices # ToDo: safe this axis only, not full shutdown + self.power_down() # set output to 0 and deactivate raise ValueError("Invalid current value. Tried %0.2fA, max. %0.2fA allowed" % (value, self.max_amps)) elif value >= 0: # switch polarity as needed - g.ARDUINO.digitalWrite(ardPin, "LOW") + pass # g.ARDUINO.digitalWrite(ardPin, "LOW") ToDo: reactivate and tie to arduino elif value < 0: - g.ARDUINO.digitalWrite(ardPin, "HIGH") + pass # g.ARDUINO.digitalWrite(ardPin, "HIGH") ToDo: reactivate else: raise Exception("This should be impossible.") + maxVoltage = min(max(1.1 * self.max_amps * self.resistance, 8), self.max_volts) # limit voltage# + # print("sending values to device: U =", maxVoltage, "I =", abs(value)) device.set_current(abs(value), channel) - maxVoltage = min(max(1.1 * self.max_amps * self.resistance, 8), self.max_volts) # limit voltage - device.set_voltage(maxVoltage) + device.set_voltage(maxVoltage, channel) + device.enable_output(channel) else: print(self.name, "not connected, can't set current.") @@ -152,9 +157,14 @@ class ArduinoCtrl(Arduino): def setup_axes(): # creates device objects for all PSUs and sets their values + g.AXES = [] print("Connecting to XY Device on %s..." % g.XY_PORT) try: + if g.XY_DEVICE is not None: + print("closing serial connection on XY device") + g.XY_DEVICE.serial.close() + g.XY_DEVICE = None g.XY_DEVICE = PS2000B.PS2000B(g.XY_PORT) # setup PSU print("Connection established.") g.X_AXIS = Axis(0, g.XY_DEVICE, 0, g.ARDUINO.pins[0]) # create axis objects @@ -178,10 +188,11 @@ def setup_axes(): # creates device objects for all PSUs and sets their values g.AXES.append(g.Z_AXIS) i = 0 - for axis in g.AXES: + for axis in g.AXES: # ToDo: move to axis init axis.resistance = g.RESISTANCES[i] axis.max_watts = g.MAX_WATTS[i] axis.max_amps = np.sqrt(axis.max_watts / axis.resistance) + print(axis.name, "max Current:", axis.max_amps) axis.max_volts = g.MAX_VOLTS[i] axis.coil_constant = g.COIL_CONST[i] @@ -195,8 +206,19 @@ def activate_all(): # enables remote control and output on all PSUs and channel def deactivate_all(): # disables remote control and output on all PSUs and channels - g.XY_DEVICE.disable_all() - g.Z_DEVICE.disable_all() + # ToDo: add check if device is connected + try: + g.XY_DEVICE.disable_all() + except BaseException: + print("XY PSU deactivation unsuccessful.") + else: + print("XY PSU deactivated.") + try: + g.Z_DEVICE.disable_all() + except BaseException: + print("Z PSU deactivation unsuccessful.") + else: + print("Z PSU deactivated.") def print_status_3(): @@ -222,23 +244,19 @@ def power_down_all(): # temporary, set all outputs to 0 but keep connections en def shut_down_all(): # shutdown at program end or on error, set outputs to 0 and disable connections - # ToDo: better messages + # ToDo: better messages, check if things are connected first print("\nAttempting to safely shut down all devices. Check equipment to confirm.") try: set_to_zero(g.XY_DEVICE) except: - print("PSU XY set to 0 unsuccessful.") + print("XY PSU set to 0 unsuccessful.") else: - print("PSU XY currents and voltages set to 0.") + print("XY PSU currents and voltages set to 0.") try: set_to_zero(g.Z_DEVICE) except: - print("PSU Z set to 0 unsuccessful.") + print("Z PSU set to 0 unsuccessful.") else: - print("PSU Z currents and voltages set to 0.") - try: deactivate_all() - except: - print("PSU deactivation unsuccessful.") - else: - print("PSUs deactivated.") + print("Z PSU currents and voltages set to 0.") + deactivate_all() try: g.ARDUINO.safe() except: print("Arduino safing unsuccessful.") diff --git a/pyps2000b/PS2000B.py b/pyps2000b/PS2000B.py index 1df26fe..be9d496 100644 --- a/pyps2000b/PS2000B.py +++ b/pyps2000b/PS2000B.py @@ -212,16 +212,16 @@ class PS2000B: def __init__(self, serial_port): self.__device_status_information1 = None self.__device_status_information2 = None - self.__serial = serial.Serial(serial_port, - baudrate=Constants.CONNECTION_BAUD_RATE, - timeout=Constants.TIMEOUT_BETWEEN_COMMANDS * 2, - parity=serial.PARITY_ODD, - stopbits=Constants.CONNECTION_STOP_BITS) + self.serial = serial.Serial(serial_port, + baudrate=Constants.CONNECTION_BAUD_RATE, + timeout=Constants.TIMEOUT_BETWEEN_COMMANDS * 2, + parity=serial.PARITY_ODD, + stopbits=Constants.CONNECTION_STOP_BITS) self.__device_information = self.__read_device_information() def is_open(self): - return self.__serial.is_open + return self.serial.is_open def get_device_information(self): return self.__device_information @@ -247,8 +247,8 @@ class PS2000B: return result def __send_and_receive(self, raw_bytes): # sends request for info to device and reads reply - self.__serial.write(raw_bytes) - result = FromPowerSupply(self.__serial.read(Constants.MAX_LEN_IN_BYTES)) + self.serial.write(raw_bytes) + result = FromPowerSupply(self.serial.read(Constants.MAX_LEN_IN_BYTES)) return result def get_device_status_information(self, channel): # gets dynamic device information (e.g. current, voltage) diff --git a/pyps2000b/__pycache__/__init__.cpython-37.pyc b/pyps2000b/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 4d806eaf5345fb26605e0ff5d01cd9f2bac8f876..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 230 zcmXv|F=_)b5Y(9z=D>e&XWW=0WiZAzX_Mw4MGy$tTFZhvN$jo)*M5OtkUAgm2kr+c z-R2jmvJPZmW_Mr)W;>bGN^q&a%vtXD-Tb4HXDy_$h*R&%Rjrzj%1mrKToK~5%{W|# zkKm_EqKpZMf?xaIhHkM>{pxjI-D+cO;bTH$J{69n{3kze&rkZrB8RA3E-sW_Z|(jF zT|$dH2&^}e7z$vCUXnfnAE0v##Kbl_ollt_o2J=mV?qpU%p#v|<7J;*>0tG6-!4?Y D!TUvX diff --git a/settings.py b/settings.py index be21673..4ef4052 100644 --- a/settings.py +++ b/settings.py @@ -1,23 +1,23 @@ import numpy as np -global XY_DEVICE -global Z_DEVICE +XY_DEVICE = None +Z_DEVICE = None -global X_AXIS # object structure: (device, channel, arduino pin, axis index) -global Y_AXIS -global Z_AXIS +X_AXIS = None # object structure: (device, channel, arduino pin, axis index) +Y_AXIS = None +Z_AXIS = None -AXES = [] # list containing [X_AXIS, Y_AXIS, Z_AXIS] +AXES = None # list containing [X_AXIS, Y_AXIS, Z_AXIS] # Constants: COIL_CONST = np.array([38.6, 38.45, 37.9]) * 1e-6 # Coil constants [x,y,z] in T/A AMBIENT_FIELD = np.array([80, 80, 80]) * 1e-6 # ambient magnetic field in measurement area, to be cancelled out -RESISTANCES = np.array([3.9, 1, 1]) # resistance of [x,y,z] circuits -MAX_WATTS = np.array([8, 0, 0]) # max. allowed power for [x,y,z] circuits +RESISTANCES = np.array([4.5, 8, 1]) # resistance of [x,y,z] circuits +MAX_WATTS = np.array([8, 25, 0]) # max. allowed power for [x,y,z] circuits MAX_VOLTS = [16, 16, 16] # max. allowed voltage, limited to 16V by used diodes! # COM-Ports for power supply units: -XY_PORT = "COM10" # placeholders +XY_PORT = "COM7" # placeholders Z_PORT = "COM11" AXIS_NAMES = ["X-Axis", "Y-Axis", "Z-Axis"]