From 239b685feef129dba90d0945a756a4d4c6a57db4 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Wed, 27 Jan 2021 11:21:03 +0100 Subject: [PATCH] debug and testing with arduino --- .gitignore | 2 + .idea/Python-PS2000B.iml | 2 +- .idea/misc.xml | 2 +- User_Interface.py | 1 + cage_func.py | 92 ++++++++++++++++++++++++++-------------- main.py | 9 ++-- settings.py | 13 ++---- 7 files changed, 71 insertions(+), 50 deletions(-) diff --git a/.gitignore b/.gitignore index 37b8976..9c4f096 100644 --- a/.gitignore +++ b/.gitignore @@ -98,3 +98,5 @@ ENV/ .idea/misc.xml .idea/Python-PS2000B.iml .idea/Python-PS2000B.iml +.idea/Python-PS2000B.iml +.idea/misc.xml diff --git a/.idea/Python-PS2000B.iml b/.idea/Python-PS2000B.iml index 98105af..653cec9 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 6724169..5ea737b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/User_Interface.py b/User_Interface.py index 8c259f3..dbcbe75 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -228,6 +228,7 @@ class StatusDisplay(Frame): def update_labels(self, controller): # ToDo: do this with a dictionary + g.ARDUINO.update_status_info() i = 0 for axis in g.AXES: if axis.device is not None: diff --git a/cage_func.py b/cage_func.py index d246ccb..ab5651f 100644 --- a/cage_func.py +++ b/cage_func.py @@ -5,6 +5,7 @@ import pandas import time import numpy as np import serial +import traceback # ToDo: remove class Axis: @@ -69,31 +70,23 @@ class Axis: else: self.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" % (self.device.get_device_status_information(self.channel), self.device.get_voltage(self.channel), self.device.get_current(self.channel))) def power_down(self): # temporary powerdown, set outputs to 0 but keep connections enabled - self.target_current = 0 - self.target_field = 0 - self.target_field_comp = 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") + try: + self.target_current = 0 + self.target_field = 0 + self.target_field_comp = 0 + if self.device is not None: + 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") + except Exception as e: + print(e) # ToDo: more error handling here def set_signed_current(self, value): # sets current with correct polarity on this axis device = self.device @@ -101,21 +94,22 @@ class Axis: ardPin = self.ardPin # print("Attempting to set current", value, "A") self.target_current = value - if self.connected == "Connected": + 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 - pass # g.ARDUINO.digitalWrite(ardPin, "LOW") ToDo: reactivate and tie to arduino + g.ARDUINO.digitalWrite(ardPin, "LOW") # ToDo: reactivate and tie to arduino elif value < 0: - pass # g.ARDUINO.digitalWrite(ardPin, "HIGH") ToDo: reactivate + 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# # print("sending values to device: U =", maxVoltage, "I =", abs(value)) - device.set_current(abs(value), channel) - device.set_voltage(maxVoltage, channel) - device.enable_output(channel) + if self.connected == "Connected": # ToDo!: remove if, only for arduino testing! + device.set_current(abs(value), channel) + device.set_voltage(maxVoltage, channel) + device.enable_output(channel) else: print(self.name, "not connected, can't set current.") @@ -138,28 +132,60 @@ class ArduinoCtrl(Arduino): def __init__(self, pins): self.connected = "Unknown" self.pins = pins - print("Connecting to Arduino...") + print("\nConnecting 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: + self.pinMode(pin, "Output") + self.digitalWrite(pin, "LOW") + except Exception as e: print("Connection to Arduino failed.") + print(e) self.connected = "Not Connected" else: - g.arduino_connected = "Connected" + self.connected = "Connected" print("Arduino ready.") + def update_status_info(self): + if self.connected == "Connected": + try: + for axis in g.AXES: + if g.ARDUINO.digitalRead(axis.ardPin): # ToDo: Test if this actually works + axis.polarity_switched = "True" + else: + axis.polarity_switched = "False" + except Exception as e: + print("Error with Arduino:", e) + for axis in g.AXES: + axis.polarity_switched = "Unknown" + self.connected = "Connection Error" + else: + g.ARDUINO.connected = "Connected" + def safe(self): # sets output pins to low and closes serial connection for pin in self.pins: - g.ARDUINO.digitalWrite(pin, "LOW") + self.digitalWrite(pin, "LOW") def setup_axes(): # creates device objects for all PSUs and sets their values + # Connect to Arduino: + try: + if g.ARDUINO is not None: + # print("\nClosing arduino link") + try: + g.ARDUINO.close() # close serial link before attempting reconnection + except serial.serialutil.SerialException: + pass + # serial.flush() in Arduino.close() fails when reconnecting + # this ignores it and allows serial.close() to execute (I think) + g.ARDUINO = ArduinoCtrl(g.RELAY_PINS) + except Exception as e: + print("Arduino setup failed:", e) + print(traceback.print_exc()) + g.AXES = [] - print("Connecting to XY Device on %s..." % g.XY_PORT) + print("\nConnecting to XY Device on %s..." % g.XY_PORT) try: if g.XY_DEVICE is not None: print("closing serial connection on XY device") @@ -187,6 +213,7 @@ def setup_axes(): # creates device objects for all PSUs and sets their values g.AXES.append(g.Y_AXIS) g.AXES.append(g.Z_AXIS) + print("") # new line i = 0 for axis in g.AXES: # ToDo: move to axis init axis.resistance = g.RESISTANCES[i] @@ -199,7 +226,6 @@ def setup_axes(): # creates device objects for all PSUs and sets their values axis.ambient_field = g.AMBIENT_FIELD[i] i = i+1 - def activate_all(): # enables remote control and output on all PSUs and channels g.XY_DEVICE.enable_all() g.Z_DEVICE.enable_all() diff --git a/main.py b/main.py index f75a627..5b4e90f 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,10 @@ -import User_Interface as ui +from User_Interface import HelmholtzGUI import cage_func as func import traceback -import settings as g try: # start normal operations - # Connect to Arduino: - g.ARDUINO = func.ArduinoCtrl(g.RELAY_PINS) - print("Connecting to PSUs...") + print("Starting setup...") func.setup_axes() # initiate communication, set handles print("\nOpening User Interface...") @@ -16,7 +13,7 @@ try: # start normal operations #g.TestValuesZ = ui.TestValues() g.TestValues = [g.TestValuesX, g.TestValuesY]#, g.TestValuesZ]''' - application = ui.HelmholtzGUI() + application = HelmholtzGUI() application.mainloop() except BaseException as e: # if there is an error, print what happened diff --git a/settings.py b/settings.py index 4ef4052..5bf5932 100644 --- a/settings.py +++ b/settings.py @@ -12,24 +12,19 @@ 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([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 +RESISTANCES = np.array([4.5, 8, 4]) # resistance of [x,y,z] circuits +MAX_WATTS = np.array([8, 25, 8]) # 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 = "COM7" # placeholders +XY_PORT = "COM10" # placeholders Z_PORT = "COM11" AXIS_NAMES = ["X-Axis", "Y-Axis", "Z-Axis"] ports = [XY_PORT, XY_PORT, Z_PORT] -global ARDUINO +ARDUINO = None 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