changed all print() to ui_print()

This commit is contained in:
Martin Zietz
2021-01-27 14:11:30 +01:00
parent 4c7b9edd94
commit 89a8a59377
3 changed files with 72 additions and 72 deletions
+68 -55
View File
@@ -54,17 +54,19 @@ class Axis:
device_status = self.device.get_device_status_information(self.channel)
if device_status.output_active:
self.output_active = "Active"
else: self.output_active = "Inactive"
else:
self.output_active = "Inactive"
if device_status.remote_control_active:
self.remote_ctrl_active = "Active"
else: self.remote_ctrl_active = "Inactive"
else:
self.remote_ctrl_active = "Inactive"
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)
except (serial.serialutil.SerialException, IndexError):
# print("Connection Error with %s PSU on %s" % (self.name, self.port))
# ui_print("Connection Error with %s PSU on %s" % (self.name, self.port))
self.connected = "Connection Error"
self.output_active = "Unknown"
self.remote_ctrl_active = "Unknown"
@@ -72,9 +74,9 @@ class Axis:
self.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)))
ui_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
try:
@@ -87,13 +89,13 @@ class Axis:
self.device.disable_output(self.channel)
g.ARDUINO.digitalWrite(self.ardPin, "LOW")
except Exception as e:
print(e) # ToDo: more error handling here
ui_print(e) # ToDo: more error handling here
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")
# 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
@@ -106,13 +108,13 @@ class Axis:
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))
# ui_print("sending values to device: U =", maxVoltage, "I =", abs(value))
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.")
ui_print(self.name, "not connected, can't set current.")
def set_field_simple(self, value): # forms magnetic field as specified by value, w/o cancelling ambient field
self.target_field = value
@@ -133,19 +135,18 @@ class ArduinoCtrl(Arduino):
def __init__(self, pins):
self.connected = "Unknown"
self.pins = pins
print("\nConnecting to Arduino...")
ui_print("\nConnecting to Arduino...")
try:
Arduino.__init__(self) # search for connected arduino and connect
for pin in self.pins:
self.pinMode(pin, "Output")
self.digitalWrite(pin, "LOW")
except Exception as e:
print("Connection to Arduino failed.")
print(e)
ui_print("Connection to Arduino failed:", e)
self.connected = "Not Connected"
else:
self.connected = "Connected"
print("Arduino ready.")
ui_print("Arduino ready.")
def update_status_info(self):
if self.connected == "Connected":
@@ -156,7 +157,7 @@ class ArduinoCtrl(Arduino):
else:
axis.polarity_switched = "False"
except Exception as e:
print("Error with Arduino:", e)
ui_print("Error with Arduino:", e)
for axis in g.AXES:
axis.polarity_switched = "Unknown"
self.connected = "Connection Error"
@@ -173,8 +174,10 @@ def ui_print(*content): # prints text to built in console
for text in content:
output = " ".join((output, str(text)))
if g.app is not None:
g.app.OutputConsole.console.insert(END, output)
else:
output = "".join(("\n", output)) # begin new line each time
g.app.OutputConsole.console.insert(END, output) # print to console
g.app.OutputConsole.console.see(END) # scroll to bottom
else: # if window is not open, do normal print
print(output)
@@ -182,60 +185,66 @@ 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")
# ui_print("\nClosing arduino link")
try:
g.ARDUINO.close() # close serial link before attempting reconnection
except serial.serialutil.SerialException:
pass
pass
# serial.flush() in Arduino.close() fails when reconnecting
# this ignores it and allows serial.close() to execute (I think)
except AttributeError:
pass
# when no Arduino is connected but g.ARDUINO has been initialized then there is nothing to close
# this throws exception, which can be ignored
g.ARDUINO = ArduinoCtrl(g.RELAY_PINS)
except Exception as e:
print("Arduino setup failed:", e)
print(traceback.print_exc())
ui_print("Arduino setup failed:", e)
ui_print(traceback.print_exc())
g.AXES = []
print("\nConnecting to XY Device on %s..." % g.XY_PORT)
ui_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")
ui_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.")
ui_print("Connection established.")
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.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.")
ui_print("XY Device not connected or incorrect port set.")
print("Connecting to Z Device on %s..." % g.XY_PORT)
ui_print("Connecting to Z Device on %s..." % g.XY_PORT)
try:
g.Z_DEVICE = PS2000B.PS2000B(g.Z_PORT)
print("Connection established.")
ui_print("Connection established.")
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.ARDUINO.pins[2])
print("Z Device not connected or incorrect port set.")
ui_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)
print("") # new line
ui_print("") # new line
i = 0
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)
ui_print(axis.name, "max Current:", axis.max_amps)
axis.max_volts = g.MAX_VOLTS[i]
axis.coil_constant = g.COIL_CONST[i]
axis.ambient_field = g.AMBIENT_FIELD[i]
i = i+1
i = i + 1
ui_print("")
def activate_all(): # enables remote control and output on all PSUs and channels
g.XY_DEVICE.enable_all()
@@ -247,23 +256,23 @@ def deactivate_all(): # disables remote control and output on all PSUs and chan
try:
g.XY_DEVICE.disable_all()
except BaseException:
print("XY PSU deactivation unsuccessful.")
ui_print("XY PSU deactivation unsuccessful.")
else:
print("XY PSU deactivated.")
ui_print("XY PSU deactivated.")
try:
g.Z_DEVICE.disable_all()
except BaseException:
print("Z PSU deactivation unsuccessful.")
ui_print("Z PSU deactivation unsuccessful.")
else:
print("Z PSU deactivated.")
ui_print("Z PSU deactivated.")
def print_status_3():
print("X-Axis:")
ui_print("X-Axis:")
g.X_AXIS.print_status()
print("Y-Axis:")
ui_print("Y-Axis:")
g.Y_AXIS.print_status()
print("Z-Axis:")
ui_print("Z-Axis:")
g.Z_AXIS.print_status()
@@ -282,28 +291,32 @@ 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
print("\nAttempting to safely shut down all devices. Check equipment to confirm.")
try: set_to_zero(g.XY_DEVICE)
ui_print("\nAttempting to safely shut down all devices. Check equipment to confirm.")
try:
set_to_zero(g.XY_DEVICE)
except:
print("XY PSU set to 0 unsuccessful.")
ui_print("XY PSU set to 0 unsuccessful.")
else:
print("XY PSU currents and voltages set to 0.")
try: set_to_zero(g.Z_DEVICE)
ui_print("XY PSU currents and voltages set to 0.")
try:
set_to_zero(g.Z_DEVICE)
except:
print("Z PSU set to 0 unsuccessful.")
ui_print("Z PSU set to 0 unsuccessful.")
else:
print("Z PSU currents and voltages set to 0.")
ui_print("Z PSU currents and voltages set to 0.")
deactivate_all()
try: g.ARDUINO.safe()
try:
g.ARDUINO.safe()
except:
print("Arduino safing unsuccessful.")
ui_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()
# ui_print("Arduino pins set to LOW.") # ToDo: figure out error handling for this
try:
g.ARDUINO.close()
except:
print("Closing Arduino connection failed.")
ui_print("Closing Arduino connection failed.")
else:
print("Serial connection to Arduino closed.")
ui_print("Serial connection to Arduino closed.")
def set_field_simple(vector): # forms magnetic field as specified by vector, w/o cancelling ambient field
@@ -326,22 +339,22 @@ def set_current_vec(vector): # sets needed currents on each axis for given vect
def execute_csv(filepath, printing=0): # runs through csv file containing times and desired field vectors
# csv format: time (s); xField (T); yField (T); zField (T)
# decimal commas
print("Reading File:", filepath)
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
t_zero = time.time()
t_ref = t_zero
i = 0
print("Starting Execution...")
ui_print("Starting Execution...")
while i < len(array):
t = time.time() - t_zero
if t >= array[i, 0]:
field_vec = array[i, 1:4]
print("t = %0.2f s, target field vector = " % (array[i, 0]), field_vec)
ui_print("t = %0.2f s, target field vector = " % (array[i, 0]), field_vec)
set_field(field_vec)
i = i + 1
if t - t_ref >= 1 and printing == 1: # print status every second
print_status_3()
t_ref = t
print("File executed, powering down channels.")
ui_print("File executed, powering down channels.")
power_down_all() # set currents and voltages to 0, set arduino pins to low