forked from zietzm/Helmholtz_Test_Bench
87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
from pyps2000b import PS2000B
|
|
import globals as g
|
|
|
|
|
|
def set_devices(): # creates device objects for all PSUs
|
|
g.xyDevice = PS2000B.PS2000B(g.xyPort)
|
|
g.zDevice = PS2000B.PS2000B(g.zPort)
|
|
g.xAxis = (g.xyDevice, 0, g.relayPins[0], 0) # (device, channel, arduino pin, axis index)
|
|
g.yAxis = (g.xyDevice, 1, g.relayPins[1], 1)
|
|
g.zAxis = (g.zDevice, 0, g.relayPins[2], 2)
|
|
|
|
|
|
def activate_all(): # enables remote control and output on all PSUs and channels
|
|
g.xyDevice.enable_all()
|
|
g.zDevice.enable_all()
|
|
|
|
|
|
def deactivate_all(): # disables remote control and output on all PSUs and channels
|
|
g.xyDevice.disable_all()
|
|
g.zDevice.disable_all()
|
|
|
|
|
|
def safe_arduino(): # sets output pins to low and closes serial connection
|
|
for pin in g.relayPins:
|
|
g.arduino.digitalWrite(pin, "LOW")
|
|
g.arduino.close()
|
|
|
|
|
|
def print_status(axis): # axis as (device, channel), e.g. g.xAxis
|
|
device = axis[0] # PSU
|
|
channel = axis[1] # output channel on the PSU
|
|
print("%s, %0.2f V, %0.2f A"
|
|
% (device.get_device_status_information(channel), device.get_voltage(channel), device.get_current(channel)))
|
|
|
|
|
|
def print_status_3():
|
|
print("X-Axis:")
|
|
print_status(g.xAxis)
|
|
print("Y-Axis:")
|
|
print_status(g.yAxis)
|
|
print("Z-Axis:")
|
|
print_status(g.zAxis)
|
|
|
|
|
|
def set_to_zero(device):
|
|
device.voltage1 = 0
|
|
device.current1 = 0
|
|
device.current2 = 0
|
|
device.current2 = 0
|
|
|
|
|
|
def set_field_simple(vector): # forms magnetic field as specified by vector, w/o cancelling ambient field
|
|
i_vec = vector/g.Coil_const
|
|
set_current_vec(i_vec)
|
|
|
|
|
|
def set_field(vector): # forms magnetic field as specified by vector, corrected for ambient field
|
|
field = vector - g.ambientField
|
|
i_vec = field/g.Coil_const
|
|
set_current_vec(i_vec)
|
|
|
|
|
|
def set_current_vec(i_vec): # sets needed currents on each axis for given vector
|
|
set_axis_current(g.xAxis, i_vec[0])
|
|
set_axis_current(g.yAxis, i_vec[1])
|
|
set_axis_current(g.zAxis, i_vec[2])
|
|
|
|
|
|
def set_axis_current(axis, value): # sets current with correct polarity on one axis
|
|
device = axis[0]
|
|
channel = axis[1]
|
|
ardPin = axis[2]
|
|
axisIndex = axis[3]
|
|
if abs(value) > g.maxAmps[axisIndex]: # prevent excessive currents
|
|
set_to_zero(device) # set currents and voltages to 0
|
|
device.disable_all() # disable outputs on PSU
|
|
safe_arduino() # set arduino pins to low and close serial link
|
|
raise ValueError("Invalid current value. Tried %0.2fA, max. %0.2fA allowed" % (value, g.maxAmps[axisIndex]))
|
|
elif value >= 0: # switch polarity as needed
|
|
g.arduino.digitalWrite(ardPin, "LOW")
|
|
elif value < 0:
|
|
g.arduino.digitalWrite(ardPin, "HIGH")
|
|
else:
|
|
raise Exception("This should be impossible.")
|
|
device.set_current(abs(value), channel)
|
|
device.set_voltage(1.1 * g.maxAmps[axisIndex] * g.resistances[axisIndex]) # set max voltage high enough
|