diff --git a/User_Interface.py b/User_Interface.py index 81de9be..0801e7d 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -1,7 +1,7 @@ from tkinter import * from tkinter import ttk -import settings -import cage_func +import settings as g +import cage_func as func import random as rand NORM_FONT = () @@ -19,7 +19,7 @@ class HelmholtzGUI(Tk): self.Menu = TopMenu(self) # displays menu bar at the top mainArea = Frame(self) - mainArea.pack(side="top", fill="both", expand=True) + mainArea.pack(side="top", fill="both", expand=False) mainArea.grid_rowconfigure(0, weight=1) mainArea.grid_columnconfigure(0, weight=1) @@ -81,25 +81,39 @@ class StatusDisplay(Frame): col = col + 1 # move to next column rowCounter = rowCounter + 1 # increase row counter to place future stuff below header - LabelTexts = ["Port:", "Channel:", "Output:"] # define content of row entries - rowNo = len(LabelTexts) # get number of label rows - columnNo = 3 # number of label columns - Labels = [[] for _ in range(columnNo)] - # prepare list of lists to contain all labels for row entries in all columns + TextLabels = ["Port:", "Channel:", "Output:"] # define content of row entries + self.rowNo = len(TextLabels) # get number of label rows - for i in range(0, rowNo): # create label objects for row entries - for j in range(columnNo): - Labels[j].append(Label(self, text=LabelTexts[i])) + self.columnNo = 6 # number of label columns + # prepare list of lists to contain all labels for row entries in all columns: + self.Labels = [[] for _ in range(self.columnNo)] + + self.label_dict = {} + for name in TextLabels: + self.label_dict[name] = [StringVar() for _ in range(int(self.columnNo/2))] + for col in range(int(self.columnNo/2)): + self.Labels[col*2].append(Label(self, text=name)) + self.Labels[col*2+1].append(Label(self, textvariable=self.label_dict[name][col])) col = 0 - for LabelCol in Labels: # place row entries in grid layout for all columns - for row in range(rowNo): # place row entries - LabelCol[row].grid(row=row+rowCounter, column=col*2, sticky="w") + for LabelCol in self.Labels: # place row entries in grid layout for all columns + for row in range(self.rowNo): # place row entries + LabelCol[row].grid(row=row+rowCounter, column=col, sticky="w") col = col + 1 - rowCounter = rowCounter + rowNo # increase row counter to place future stuff below this + rowCounter = rowCounter + self.rowNo # increase row counter to place future stuff below this - toBeRemoved = Label(self, text="Active TBD") - toBeRemoved.grid(row=1, column=1) + self.update_labels(controller) + + def update_labels(self, controller): + i = 0 + for axis in g.AXES: # ToDo: switch to proper axes when PSU connected + if axis.device is not None: + axis.update_values() + self.label_dict["Port:"][i].set(g.ports[i]) + self.label_dict["Channel:"][i].set(axis.channel) + self.label_dict["Output:"][i].set(axis.output_active) + i = i+1 + controller.after(2000, lambda: self.update_labels(controller)) def print_stuff(stuff): @@ -108,3 +122,15 @@ def print_stuff(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) diff --git a/cage_func.py b/cage_func.py index b0e8c50..d937105 100644 --- a/cage_func.py +++ b/cage_func.py @@ -1,12 +1,15 @@ from pyps2000b import PS2000B +from Arduino import Arduino import settings as g import pandas import time import numpy as np +import serial class Axis: - def __init__(self, device, PSU_channel, arduino_pin): + def __init__(self, index, device, PSU_channel, arduino_pin): + self.index = index self.device = device # power supply object (PS2000B class) self.channel = PSU_channel # power supply unit channel (1 or 2) self.ardPin = arduino_pin # output pin on the arduino for switching polarity on this axis @@ -21,6 +24,33 @@ class Axis: self.ambient_field = 0 # ambient field in this axis [T] # ToDo: get this info from settings file + self.output_active = 0 # power output on the PSU enabled? + self.remote_ctrl_active = 0 # remote control on the PSU enabled? + self.voltage_setpoint = 0 # target voltage on PSU [V] + self.voltage = 0 # actual voltage on PSU [V] + self.current_setpoint = 0 # target current on PSU [A] + self.current = 0 # actual current on PSU [A] + + self.polarity_switched = 0 # polarity switched on the Arduino? + + if self.device is not None: + self.update_status_info() + + self.name = g.AXIS_NAMES[index] + + def update_status_info(self): # Read out the values of the parameters stored in this class and update them + self.device.update_device_information(self.channel) + device_status = self.device.get_device_status_information(self.channel) + self.output_active = device_status.output_active + self.remote_ctrl_active = device_status.remote_control_active + + self.voltage = self.device.get_voltage(self.channel) + 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) + + self.polarity_switched = g.ARDUINO.digitalRead(self.ardPin) # ToDo: Test if this actually works + def print_status(self): # axis = axis control variable, stored in settings.py print("%s, %0.2f V, %0.2f A" % (self.device.get_device_status_information(self.channel), @@ -59,13 +89,35 @@ class Axis: def setup_axes(): # creates device objects for all PSUs and sets their values - g.XY_DEVICE = PS2000B.PS2000B(g.XY_PORT) - g.Z_DEVICE = PS2000B.PS2000B(g.Z_PORT) - g.X_AXIS = Axis(g.XY_DEVICE, 0, g.RELAY_PINS[0]) - g.Y_AXIS = Axis(g.XY_DEVICE, 1, g.RELAY_PINS[1]) - g.Z_AXIS = Axis(g.Z_DEVICE, 0, g.RELAY_PINS[2]) - g.axes = [g.X_AXIS, g.Y_AXIS, g.Z_AXIS] + print("Connecting to XY Device on %s..." % g.XY_PORT) + try: + g.XY_DEVICE = PS2000B.PS2000B(g.XY_PORT) # setup PSU + print("Connection established.") + g.X_AXIS = Axis(0, g.XY_DEVICE, 0, g.RELAY_PINS[0]) # create axis objects + g.Y_AXIS = Axis(1, g.XY_DEVICE, 1, g.RELAY_PINS[1]) + except serial.serialutil.SerialException: + g.X_AXIS = Axis(0, None, 0, g.RELAY_PINS[0]) # create axis objects + g.Y_AXIS = Axis(1, None, 1, g.RELAY_PINS[1]) + print("XY Device not connected or incorrect port set.") + + if g.Z_PORT == "NC": # check if device is connected + g.Z_AXIS = Axis(2, None, 0, g.RELAY_PINS[2]) + print("Z Device not connected or port not set.") + + print("Connecting to Z Device on %s..." % g.XY_PORT) + try: + g.Z_DEVICE = PS2000B.PS2000B(g.Z_PORT) + print("Connection established.") + g.Z_AXIS = Axis(2, g.Z_DEVICE, 0, g.RELAY_PINS[2]) + except serial.serialutil.SerialException: + g.Z_AXIS = Axis(2, None, 0, g.RELAY_PINS[2]) + print("Z Device not connected or incorrect port set.") + + g.AXES.append(g.X_AXIS) + g.AXES.append(g.Y_AXIS) + g.AXES.append(g.Z_AXIS) + i = 0 for axis in g.AXES: axis.resistance = g.RESISTANCES[i] @@ -89,9 +141,15 @@ def deactivate_all(): # disables remote control and output on all PSUs and chan def setup_arduino(): - for pin in g.RELAY_PINS: - g.ARDUINO.pinMode(pin, "Output") - g.ARDUINO.digitalWrite(pin, "LOW") + try: + g.ARDUINO = Arduino() # search for connected arduino and set handle + except Exception: + print("There seems to be no Arduino connected.") + else: + for pin in g.RELAY_PINS: + g.ARDUINO.pinMode(pin, "Output") + g.ARDUINO.digitalWrite(pin, "LOW") + print("Arduino ready.") def safe_arduino(): # sets output pins to low and closes serial connection diff --git a/main.py b/main.py index 5adf36c..c9376e8 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,22 @@ -import numpy as np -import settings as g -from tkinter import * -from Arduino import Arduino - -import cage_func as func import User_Interface as ui +import cage_func as func +import settings as g + + +print("Connecting to PSUs...") +func.setup_axes() # initiate communication, set handles + +print("Connecting to Arduino...") +#print("Arduino found, configuring pins...") +func.setup_arduino() + +print("Opening User Interface...") +'''g.TestValuesX = ui.TestValues() +g.TestValuesY = ui.TestValues() +#g.TestValuesZ = ui.TestValues() +g.TestValues = [g.TestValuesX, g.TestValuesY]#, g.TestValuesZ]''' application = ui.HelmholtzGUI() application.mainloop() + +# func.shut_down_all() diff --git a/pyps2000b/__pycache__/PS2000B.cpython-37.pyc b/pyps2000b/__pycache__/PS2000B.cpython-37.pyc deleted file mode 100644 index 77b78da..0000000 Binary files a/pyps2000b/__pycache__/PS2000B.cpython-37.pyc and /dev/null differ diff --git a/settings.py b/settings.py index d0ee122..12b9839 100644 --- a/settings.py +++ b/settings.py @@ -7,21 +7,29 @@ global X_AXIS # object structure: (device, channel, arduino pin, axis index) global Y_AXIS global Z_AXIS -global AXES # list containing [X_AXIS, Y_AXIS, Z_AXIS] +AXES = [] # list containing [X_AXIS, Y_AXIS, Z_AXIS] # Constants: COIL_CONST = np.array([38.6, 38.45, 37.9]) * 1e-9 # Coil constants [x,y,z] in T/A -AMBIENT_FIELD = np.array([80]) * 1e-6 # ambient magnetic field in measurement area, to be cancelled out +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 MAX_VOLTS = [16, 16, 16] # max. allowed voltage, limited to 16V by used diodes! # COM-Ports for power supply units: -XY_PORT = "COM1" # placeholders -Z_PORT = "COM2" +XY_PORT = "COM10" # placeholders +Z_PORT = "COM11" + +AXIS_NAMES = ["X-Axis", "Y-Axis", "Z-Axis"] +ports = [XY_PORT, XY_PORT, Z_PORT] global ARDUINO RELAY_PINS = [15, 16, 17] # pin on the Arduino for switching relay of each axis [x,y,z] # ToDo: make proper settings file to read from and write to + +global TestValuesX +global TestValuesY +global TestValuesZ +global TestValues