diff --git a/Arduino/__pycache__/arduino.cpython-37.pyc b/Arduino/__pycache__/arduino.cpython-37.pyc deleted file mode 100644 index ce96862..0000000 Binary files a/Arduino/__pycache__/arduino.cpython-37.pyc and /dev/null differ diff --git a/Arduino/arduino.py b/Arduino/arduino.py index e76a5b5..597c58b 100644 --- a/Arduino/arduino.py +++ b/Arduino/arduino.py @@ -103,6 +103,7 @@ def find_port(baud, timeout): return sr return None + def get_version(sr): cmd_str = build_cmd_str("version") try: @@ -115,7 +116,6 @@ def get_version(sr): class Arduino(object): - def __init__(self, baud=115200, port=None, timeout=2, sr=None): """ Initializes serial communication with Arduino if no connection is diff --git a/User_Interface.py b/User_Interface.py index d028c1d..a5811fe 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -90,7 +90,7 @@ class StatusDisplay(Frame): rowCounter = rowCounter + 1 # increase row counter to place future stuff below header # define content of row entries - TextLabels = ["PSU Serial Port:", "PSU Channel:", "Connection Status:", "", "Output:", "Remote Control:", + TextLabels = ["PSU Serial Port:", "PSU Channel:", "PSU Status:", "Arduino Status:", "", "Output:", "Remote Control:", "Voltage Setpoint:", "Actual Voltage:", "Current Setpoint:", "Actual Current:", "", "Target Field:", "Trgt. Field Raw:", "Target Current:", "Inverted:"] self.rowNo = len(TextLabels) # get number of label rows @@ -121,10 +121,11 @@ class StatusDisplay(Frame): i = 0 for axis in g.AXES: if axis.device is not None: - axis.update_values() + axis.update_status_info() self.label_dict["PSU Serial Port:"][i].set(g.ports[i]) self.label_dict["PSU Channel:"][i].set(axis.channel) - self.label_dict["Connection Status:"][i].set(axis.connected) + self.label_dict["PSU Status:"][i].set(axis.connected) + self.label_dict["Arduino Status:"][i].set(g.ARDUINO.connected) # ToDo (optional): make this multicolumn self.label_dict["Output:"][i].set(axis.output_active) self.label_dict["Remote Control:"][i].set(axis.remote_ctrl_active) self.label_dict["Voltage Setpoint:"][i].set("%0.3f V" % axis.voltage_setpoint) diff --git a/cage_func.py b/cage_func.py index da98df8..f0802fd 100644 --- a/cage_func.py +++ b/cage_func.py @@ -39,8 +39,8 @@ class Axis: self.polarity_switched = "Unknown" # polarity switched on the Arduino? - self.target_field_comp = 0.0 # field to be created by coil pair (this is sent to the coils) [T] - self.target_field = 0.0 # field that should occur in measurement area (ambient still needs to be compensated) [T] + self.target_field_comp = 0 # field to be created by coil pair (this is sent to the coils) [T] + self.target_field = 0 # field that should occur in measurement area (ambient still needs to be compensated) [T] self.target_current = 0 # signed current that should pass through coil pair [A] if self.device is not None: @@ -73,9 +73,12 @@ class Axis: if g.ARDUINO.digitalRead(self.ardPin): # ToDo: Test if this actually works self.polarity_switched = "True" else: self.polarity_switched = "False" - except serial.serialutil.SerialException: - # print("Connection Error with Arduino") + 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" @@ -122,30 +125,49 @@ class Axis: self.set_signed_current(current) +class ArduinoCtrl(Arduino): + + def __init__(self, pins): + self.connected = "Unknown" + self.pins = pins + print("Connecting to Arduino...") + try: + Arduino.__init__(self) # search for connected arduino and connect + for pin in self.pins: + g.ARDUINO.pinMode(pin, "Output") + g.ARDUINO.digitalWrite(pin, "LOW") + except Exception: + print("Connection to Arduino failed.") + self.connected = "Not Connected" + else: + g.arduino_connected = "Connected" + print("Arduino ready.") + + def safe(self): # sets output pins to low and closes serial connection + for pin in self.pins: + g.ARDUINO.digitalWrite(pin, "LOW") + + def setup_axes(): # creates device objects for all PSUs and sets their values 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]) + g.X_AXIS = Axis(0, g.XY_DEVICE, 0, g.ARDUINO.pins[0]) # create axis objects + g.Y_AXIS = Axis(1, g.XY_DEVICE, 1, g.ARDUINO.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]) + g.X_AXIS = Axis(0, None, 0, g.ARDUINO.pins[0]) # create axis objects + g.Y_AXIS = Axis(1, None, 1, g.ARDUINO.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]) + g.Z_AXIS = Axis(2, g.Z_DEVICE, 0, g.ARDUINO.pins[2]) except serial.serialutil.SerialException: - g.Z_AXIS = Axis(2, None, 0, g.RELAY_PINS[2]) + g.Z_AXIS = Axis(2, None, 0, g.ARDUINO.pins[2]) print("Z Device not connected or incorrect port set.") g.AXES.append(g.X_AXIS) @@ -174,23 +196,6 @@ def deactivate_all(): # disables remote control and output on all PSUs and chan g.Z_DEVICE.disable_all() -def setup_arduino(): - 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 - for pin in g.RELAY_PINS: - g.ARDUINO.digitalWrite(pin, "LOW") - - def print_status_3(): print("X-Axis:") g.X_AXIS.print_status() @@ -210,7 +215,7 @@ def set_to_zero(device): # sets voltages and currents to 0 def power_down_all(): # temporary, set all outputs to 0 but keep connections enabled set_to_zero(g.XY_DEVICE) set_to_zero(g.Z_DEVICE) - safe_arduino() + g.ARDUINO.safe() def shut_down_all(): # shutdown at program end or on error, set outputs to 0 and disable connections @@ -219,18 +224,29 @@ def shut_down_all(): # shutdown at program end or on error, set outputs to 0 an try: set_to_zero(g.XY_DEVICE) except: print("PSU XY set to 0 unsuccessful.") + else: + print("PSU XY currents and voltages set to 0.") try: set_to_zero(g.Z_DEVICE) except: print("PSU Z set to 0 unsuccessful.") + else: + print("PSU Z currents and voltages set to 0.") try: deactivate_all() except: print("PSU deactivation unsuccessful.") - try: safe_arduino() + else: + print("PSUs deactivated.") + try: g.ARDUINO.safe() except: print("Arduino safing unsuccessful.") + # else: # commented out bc this throws no exception, even when arduino is not connected + # print("Arduino pins set to LOW.") # ToDo: figure out error handling for this try: g.ARDUINO.close() except: print("Closing Arduino connection failed.") + else: + print("Serial connection to Arduino closed.") + def set_field_simple(vector): # forms magnetic field as specified by vector, w/o cancelling ambient field for i in [0, 1, 2]: diff --git a/main.py b/main.py index f64f2a0..f75a627 100644 --- a/main.py +++ b/main.py @@ -4,12 +4,12 @@ import traceback import settings as g try: # start normal operations + # Connect to Arduino: + g.ARDUINO = func.ArduinoCtrl(g.RELAY_PINS) + print("Connecting to PSUs...") func.setup_axes() # initiate communication, set handles - print("Connecting to Arduino...") - func.setup_arduino() - print("\nOpening User Interface...") '''g.TestValuesX = ui.TestValues() g.TestValuesY = ui.TestValues()