Testing and debug with PSU

This commit is contained in:
Martin Zietz
2021-01-26 17:28:37 +01:00
parent a06357a09f
commit 4908338672
10 changed files with 99 additions and 89 deletions
+3
View File
@@ -95,3 +95,6 @@ ENV/
# VScode
.vscode/
*.pyc
.idea/misc.xml
.idea/Python-PS2000B.iml
.idea/Python-PS2000B.iml
+1 -1
View File
@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.7 (venv)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (MagnetEnv)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
+1 -1
View File
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (venv)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (MagnetEnv)" project-jdk-type="Python SDK" />
</project>
Binary file not shown.
+17 -11
View File
@@ -7,7 +7,7 @@ from pyps2000b import PS2000B
# User Inputs/Configuration----------------------------------
Test1 = 0
Test2 = 0
Test2 = 1
Test3 = 0
Test4 = 0
@@ -22,32 +22,38 @@ XY_PORT = "COM7"
g.XY_DEVICE = PS2000B.PS2000B(XY_PORT)
g.MAX_AMPS = np.sqrt(g.MAX_WATTS / g.RESISTANCES)
print(g.MAX_AMPS)
#print(g.MAX_AMPS)
def print_status():
'''def print_status():
print("Output 1:")
func.print_status(g.X_AXIS)
print("Output 2:")
func.print_status(g.Y_AXIS)
func.print_status(g.Y_AXIS)'''
g.XY_DEVICE.enable_all()
func.set_to_zero(g.XY_DEVICE)
print_status()
#print_status()
t.sleep(3)
if Test1 == 1:
print("setting")
g.XY_DEVICE.voltage1 = 5
g.XY_DEVICE.current1 = 1
g.XY_DEVICE.enable_all()
t.sleep(1)
print_status()
#print_status()
t.sleep(5)
print("setting to zero")
func.set_to_zero(g.XY_DEVICE)
if Test2 == 1:
g.XY_DEVICE.current1 = 0.2
print("setting current")
g.XY_DEVICE.set_current(0.2, 0)
g.XY_DEVICE.set_voltage(5, 0)
g.XY_DEVICE.enable_all()
t.sleep(1)
print_status()
#print_status()
t.sleep(5)
func.set_to_zero(g.XY_DEVICE)
@@ -59,8 +65,8 @@ if Test4 == 1:
func.set_to_zero(g.XY_DEVICE)
t.sleep(1)
print_status()
#print_status()
g.XY_DEVICE.disable_all()
print_status()
#print_status()
+11 -28
View File
@@ -2,7 +2,6 @@ from tkinter import *
from tkinter import ttk
import settings as g
import cage_func as func
import random as rand
import numpy as np
NORM_FONT = ()
@@ -67,7 +66,7 @@ class TestFrame(Frame): # ToDo: remove
one.pack(fill=X)
two = Label(self, text="Two", bg="blue")
two.pack()
button = ttk.Button(self, text="Print stuff", command=lambda: print_stuff("Hello"))
button = ttk.Button(self, text="Print stuff", command=lambda: print("Hello"))
button.pack()
@@ -149,17 +148,17 @@ class ManualMode(Frame):
def execute(self):
function_to_call = self.modes[self.input_mode.get()][0] # get function of appropriate mode
vector = np.array([0, 0, 0])
vector = np.array([0, 0, 0], dtype=float)
i = 0
for var in self.entry_vars:
vector[i] = float(var.get())
i = i + 1
print(vector)
function_to_call(vector) # call function
# ToDo: update status display here
@staticmethod
def execute_field(vector):
print("field executing")
print("field executing", vector)
try:
func.set_field_simple(vector*1e-6) # ToDo: change to set_field
except ValueError as e:
@@ -167,7 +166,7 @@ class ManualMode(Frame):
@staticmethod
def execute_current(vector):
print("current executing")
print("current executing:", vector)
try:
func.set_current_vec(vector)
except ValueError as e:
@@ -220,6 +219,11 @@ class StatusDisplay(Frame):
col = col + 1
# rowCounter = rowCounter + self.rowNo # increase row counter to place future stuff below this
# add button for reinitialization
reinit_button = Button(self, text="Reinitialize", command=func.setup_axes,
pady=5, padx=5, font=BIG_BUTTON_FONT)
reinit_button.grid(row=0, column=self.columnNo+1, rowspan=3)
self.update_labels(controller)
def update_labels(self, controller):
@@ -243,25 +247,4 @@ class StatusDisplay(Frame):
self.label_dict["Target Current:"][i].set("%0.3f A" % axis.target_current)
self.label_dict["Inverted:"][i].set(axis.polarity_switched)
i = i + 1
controller.after(2000, lambda: self.update_labels(controller))
# ToDo: remove
def print_stuff(stuff):
print(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)
controller.after(500, lambda: self.update_labels(controller))
+49 -31
View File
@@ -61,7 +61,7 @@ class Axis:
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:
except (serial.serialutil.SerialException, IndexError):
# print("Connection Error with %s PSU on %s" % (self.name, self.port))
self.connected = "Connection Error"
self.output_active = "Unknown"
@@ -69,16 +69,17 @@ class Axis:
else:
self.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"
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"
@@ -89,28 +90,32 @@ class Axis:
self.target_current = 0
self.target_field = 0
self.target_field_comp = 0
self.device.voltage1 = 0
self.device.current1 = 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")
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")
self.target_current = value
if self.connected == "Connected":
if abs(value) > self.max_amps: # prevent excessive currents
shut_down_all() # safe all devices # ToDo: safe this axis only, not full shutdown
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")
pass # g.ARDUINO.digitalWrite(ardPin, "LOW") ToDo: reactivate and tie to arduino
elif value < 0:
g.ARDUINO.digitalWrite(ardPin, "HIGH")
pass # g.ARDUINO.digitalWrite(ardPin, "HIGH") ToDo: reactivate
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)
maxVoltage = min(max(1.1 * self.max_amps * self.resistance, 8), self.max_volts) # limit voltage
device.set_voltage(maxVoltage)
device.set_voltage(maxVoltage, channel)
device.enable_output(channel)
else:
print(self.name, "not connected, can't set current.")
@@ -152,9 +157,14 @@ class ArduinoCtrl(Arduino):
def setup_axes(): # creates device objects for all PSUs and sets their values
g.AXES = []
print("Connecting to XY Device on %s..." % g.XY_PORT)
try:
if g.XY_DEVICE is not None:
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.")
g.X_AXIS = Axis(0, g.XY_DEVICE, 0, g.ARDUINO.pins[0]) # create axis objects
@@ -178,10 +188,11 @@ def setup_axes(): # creates device objects for all PSUs and sets their values
g.AXES.append(g.Z_AXIS)
i = 0
for axis in g.AXES:
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)
axis.max_volts = g.MAX_VOLTS[i]
axis.coil_constant = g.COIL_CONST[i]
@@ -195,8 +206,19 @@ def activate_all(): # enables remote control and output on all PSUs and channel
def deactivate_all(): # disables remote control and output on all PSUs and channels
g.XY_DEVICE.disable_all()
g.Z_DEVICE.disable_all()
# ToDo: add check if device is connected
try:
g.XY_DEVICE.disable_all()
except BaseException:
print("XY PSU deactivation unsuccessful.")
else:
print("XY PSU deactivated.")
try:
g.Z_DEVICE.disable_all()
except BaseException:
print("Z PSU deactivation unsuccessful.")
else:
print("Z PSU deactivated.")
def print_status_3():
@@ -222,23 +244,19 @@ 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
# 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)
except:
print("PSU XY set to 0 unsuccessful.")
print("XY PSU set to 0 unsuccessful.")
else:
print("PSU XY currents and voltages set to 0.")
print("XY PSU currents and voltages set to 0.")
try: set_to_zero(g.Z_DEVICE)
except:
print("PSU Z set to 0 unsuccessful.")
print("Z PSU set to 0 unsuccessful.")
else:
print("PSU Z currents and voltages set to 0.")
try: deactivate_all()
except:
print("PSU deactivation unsuccessful.")
else:
print("PSUs deactivated.")
print("Z PSU currents and voltages set to 0.")
deactivate_all()
try: g.ARDUINO.safe()
except:
print("Arduino safing unsuccessful.")
+8 -8
View File
@@ -212,16 +212,16 @@ class PS2000B:
def __init__(self, serial_port):
self.__device_status_information1 = None
self.__device_status_information2 = None
self.__serial = serial.Serial(serial_port,
baudrate=Constants.CONNECTION_BAUD_RATE,
timeout=Constants.TIMEOUT_BETWEEN_COMMANDS * 2,
parity=serial.PARITY_ODD,
stopbits=Constants.CONNECTION_STOP_BITS)
self.serial = serial.Serial(serial_port,
baudrate=Constants.CONNECTION_BAUD_RATE,
timeout=Constants.TIMEOUT_BETWEEN_COMMANDS * 2,
parity=serial.PARITY_ODD,
stopbits=Constants.CONNECTION_STOP_BITS)
self.__device_information = self.__read_device_information()
def is_open(self):
return self.__serial.is_open
return self.serial.is_open
def get_device_information(self):
return self.__device_information
@@ -247,8 +247,8 @@ class PS2000B:
return result
def __send_and_receive(self, raw_bytes): # sends request for info to device and reads reply
self.__serial.write(raw_bytes)
result = FromPowerSupply(self.__serial.read(Constants.MAX_LEN_IN_BYTES))
self.serial.write(raw_bytes)
result = FromPowerSupply(self.serial.read(Constants.MAX_LEN_IN_BYTES))
return result
def get_device_status_information(self, channel): # gets dynamic device information (e.g. current, voltage)
Binary file not shown.
+9 -9
View File
@@ -1,23 +1,23 @@
import numpy as np
global XY_DEVICE
global Z_DEVICE
XY_DEVICE = None
Z_DEVICE = None
global X_AXIS # object structure: (device, channel, arduino pin, axis index)
global Y_AXIS
global Z_AXIS
X_AXIS = None # object structure: (device, channel, arduino pin, axis index)
Y_AXIS = None
Z_AXIS = None
AXES = [] # list containing [X_AXIS, Y_AXIS, Z_AXIS]
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([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
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
MAX_VOLTS = [16, 16, 16] # max. allowed voltage, limited to 16V by used diodes!
# COM-Ports for power supply units:
XY_PORT = "COM10" # placeholders
XY_PORT = "COM7" # placeholders
Z_PORT = "COM11"
AXIS_NAMES = ["X-Axis", "Y-Axis", "Z-Axis"]