From ef914ff6fc6f8dceaf1fe1785a1b642dd8c5738a Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Thu, 4 Feb 2021 11:00:33 +0100 Subject: [PATCH] small stuff --- User_Interface.py | 23 ++++------ cage_func.py | 105 +++++++++++++++++++++++++--------------------- globals.py | 3 +- main.py | 6 +-- 4 files changed, 71 insertions(+), 66 deletions(-) diff --git a/User_Interface.py b/User_Interface.py index c89986c..3ccb434 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -62,7 +62,7 @@ class TopMenu: ModeSelector = Menu(menu) menu.add_cascade(label="Mode", menu=ModeSelector) ModeSelector.add_command(label="Static Manual Input", command=lambda: self.manual_mode(window)) - ModeSelector.add_command(label="Configuration...", command=lambda: self.configuration(window)) + ModeSelector.add_command(label="Settings...", command=lambda: self.configuration(window)) @staticmethod def manual_mode(window): @@ -214,20 +214,16 @@ class ManualMode(Frame): vector[i] = float(var.get()) i = i + 1 function_to_call(vector) # call function - # ToDo: update status display here def execute_field(self, vector): func.ui_print("field executing", vector) - try: - comp = self.compensate.get() - if comp == 0: - func.set_field(vector * 1e-6) - elif comp == 1: - func.set_field_simple(vector * 1e-6) - else: - func.ui_print("Unexpected value encountered: compensate =", comp) - except ValueError as e: - func.ui_print(e) + comp = self.compensate.get() + if comp == 1: + func.set_field(vector * 1e-6) + elif comp == 0: + func.set_field_simple(vector * 1e-6) + else: + func.ui_print("Unexpected value encountered: compensate =", comp) @staticmethod def execute_current(vector): @@ -288,7 +284,7 @@ class Configuration(Frame): # {Key: [[x-value,y-value,z-value], unit, description, config file key, unit conversion factor]} self.entries = { "Coil Constants:": [[DoubleVar() for _ in range(3)], "\u03BCT/A", "", "coil_const", 1e6], - "Ambient Field:": [[DoubleVar() for _ in range(3)], "\u03BCT/A", + "Ambient Field:": [[DoubleVar() for _ in range(3)], "\u03BCT", "Field to be compensated", "ambient_field", 1e6], "Resistances:": [[DoubleVar() for _ in range(3)], "\u03A9", "Resistance of coils + equipment", "resistance", 1], @@ -480,7 +476,6 @@ class StatusDisplay(Frame): self.update_labels(controller) def update_labels(self, controller): - # ToDo (optional): do this with a dictionary g.ARDUINO.update_status_info() i = 0 for axis in g.AXES: diff --git a/cage_func.py b/cage_func.py index b47510a..e1f3499 100644 --- a/cage_func.py +++ b/cage_func.py @@ -95,7 +95,7 @@ class Axis: self.device.disable_output(self.channel) g.ARDUINO.digitalWrite(self.ardPin, "LOW") except Exception as e: - ui_print(e) # ToDo: more error handling here + ui_print("Error while powering down %s: %s" % (self.name, e)) def set_signed_current(self, value): # sets current with correct polarity on this axis device = self.device @@ -104,18 +104,21 @@ class Axis: # ui_print("Attempting to set current", value, "A") self.target_current = value if self.connected == "Connected" or True: # ToDo!: remove True, only for arduino testing! + if abs(value) > self.max_amps: # prevent excessive currents 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") # ToDo: reactivate and tie to arduino + g.ARDUINO.digitalWrite(ardPin, "LOW") # ToDo: tie to arduino? elif value < 0: - g.ARDUINO.digitalWrite(ardPin, "HIGH") # ToDo: tie to arduino + g.ARDUINO.digitalWrite(ardPin, "HIGH") # ToDo: tie to arduino? else: raise Exception("This should be impossible.") - maxVoltage = min(max(1.1 * self.max_amps * self.resistance, 8), self.max_volts) # limit voltage# + + maxVoltage = min(max(1.1 * self.max_amps * self.resistance, 8), self.max_volts) # limit voltage # ui_print("sending values to device: U =", maxVoltage, "I =", abs(value)) - if self.connected == "Connected": # ToDo!: remove if, only for arduino testing! + if self.connected == "Connected": # ToDo!: remove if clause, only for arduino testing! device.set_current(abs(value), channel) device.set_voltage(maxVoltage, channel) device.enable_output(channel) @@ -170,7 +173,7 @@ class ArduinoCtrl(Arduino): axis.polarity_switched = "Unknown" self.connected = "Connection Error" else: - g.ARDUINO.connected = "Connected" + self.connected = "Connected" def safe(self): # sets output pins to low and closes serial connection for pin in self.pins: @@ -282,7 +285,6 @@ def ui_print(*content): # prints text to built in console def value_in_limits(axis, key, value): # Check if value is within safe limits (set in globals.py) - # ToDo: replace checks everywhere with this max_value = g.default_arrays[key][1][g.AXIS_NAMES.index(axis)] # get max value min_value = g.default_arrays[key][2][g.AXIS_NAMES.index(axis)] # get min value @@ -360,22 +362,6 @@ def activate_all(): # enables remote control and output on all PSUs and channel g.Z_DEVICE.enable_all() -def deactivate_all(): # disables remote control and output on all PSUs and channels - # ToDo: add check if device is connected - try: - g.XY_DEVICE.disable_all() - except BaseException: - ui_print("XY PSU deactivation unsuccessful.") - else: - ui_print("XY PSU deactivated.") - try: - g.Z_DEVICE.disable_all() - except BaseException: - ui_print("Z PSU deactivation unsuccessful.") - else: - ui_print("Z PSU deactivated.") - - def print_status_3(): ui_print("X-Axis:") g.X_AXIS.print_status() @@ -399,55 +385,78 @@ 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, check if things are connected first + # ToDo: remove checks if connected or make them only for printing ui_print("\nAttempting to safely shut down all devices. Check equipment to confirm.") - try: - set_to_zero(g.XY_DEVICE) - except: - ui_print("XY PSU set to 0 unsuccessful.") + if g.XY_DEVICE is not None: + try: + set_to_zero(g.XY_DEVICE) + g.XY_DEVICE.disable_all() + except BaseException as e: + ui_print("Error while deactivating XY PSU:", e) + else: + ui_print("XY PSU deactivated.") else: - ui_print("XY PSU currents and voltages set to 0.") - try: - set_to_zero(g.Z_DEVICE) - except: - ui_print("Z PSU set to 0 unsuccessful.") + ui_print("XY PSU not connected, can't deactivate.") + if g.Z_DEVICE is not None: + try: + set_to_zero(g.Z_DEVICE) + g.Z_DEVICE.disable_all() + except BaseException as e: + ui_print("Error while deactivating Z PSU:", e) + else: + ui_print("Z PSU deactivated.") else: - ui_print("Z PSU currents and voltages set to 0.") - deactivate_all() + ui_print("Z PSU not connected, can't deactivate.") + try: g.ARDUINO.safe() - except: - ui_print("Arduino safing unsuccessful.") - # else: # commented out bc this throws no exception, even when arduino is not connected - # ui_print("Arduino pins set to LOW.") # ToDo: figure out error handling for this - try: - g.ARDUINO.close() - except: - ui_print("Closing Arduino connection failed.") + except BaseException as e: + ui_print("Arduino safing unsuccessful:", e) + # this throws no exception, even when arduino is not connected + # ToDo (optional): figure out error handling for this + if g.ARDUINO.connected == "Connected": + try: + g.ARDUINO.close() + except BaseException as e: + ui_print("Closing Arduino connection failed:", e) + else: + ui_print("Serial connection to Arduino closed.") else: - ui_print("Serial connection to Arduino closed.") + ui_print("Arduino not connected, can't close connection.") def set_field_simple(vector): # forms magnetic field as specified by vector, w/o cancelling ambient field for i in [0, 1, 2]: - g.AXES[i].set_field_simple(vector[i]) + try: + g.AXES[i].set_field_simple(vector[i]) + except ValueError as e: + ui_print(e) def set_field(vector): # forms magnetic field as specified by vector, corrected for ambient field for i in [0, 1, 2]: - g.AXES[i].set_field(vector[i]) + try: + g.AXES[i].set_field(vector[i]) + except ValueError as e: + ui_print(e) def set_current_vec(vector): # sets needed currents on each axis for given vector i = 0 for axis in g.AXES: - axis.set_signed_current(vector[i]) - i = i + 1 + try: + axis.target_field = 0 + axis.target_field_comp = 0 + axis.set_signed_current(vector[i]) + except ValueError as e: + ui_print(e) + i += 1 def execute_csv(filepath): # runs through csv file containing times and desired field vectors # csv format: time (s); xField (T); yField (T); zField (T) # decimal commas + # ToDo: set to zero before start 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 diff --git a/globals.py b/globals.py index 95dfcbc..7530983 100644 --- a/globals.py +++ b/globals.py @@ -5,7 +5,7 @@ XY_DEVICE = None Z_DEVICE = None ARDUINO = None -X_AXIS = None # object structure: (device, channel, arduino pin, axis index) +X_AXIS = None Y_AXIS = None Z_AXIS = None @@ -26,6 +26,7 @@ global PORTS # format: [[default values], [maximum values], [minimum values]] # ToDo: check actual maximum ratings # ToDo: Add maximum current: 5A (BA Blessing page 30), remove max_watts (there for testing with resistors) +# ToDo: put this into a config file default_arrays = { "coil_const": np.array([[38.6, 38.45, 37.9], [50, 50, 50], [0, 0, 0]]) * 1e-6, # Coil constants [x,y,z] [T/A] "ambient_field": np.array([[30, 30, 30], [200, 200, 200], [0, 0, 0]]) * 1e-6, # background magnetic field [T] diff --git a/main.py b/main.py index ab8f0cf..268a471 100644 --- a/main.py +++ b/main.py @@ -28,9 +28,9 @@ try: # start normal operations except BaseException as e: # if there is an error, print what happened - func.ui_print("\nAn error occurred, Shutting down.") - func.ui_print(e) - func.ui_print(traceback.print_exc()) + print("\nAn error occurred, Shutting down.") + print(e) + print(traceback.print_exc()) finally: # safely shut everything down at the end func.shut_down_all()