From e14dca928f9b774783c9ad5a2e0b4ffe1600fa42 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Fri, 1 Jan 2021 12:54:23 +0100 Subject: [PATCH] Renamed static variables to all caps --- One_Unit_Test.py | 38 +++++++++++++++--------------- cage_func.py | 60 ++++++++++++++++++++++++------------------------ example2.py | 54 +++++++++++++++++++++---------------------- globals.py | 30 ++++++++++++------------ main.py | 18 +++++++-------- 5 files changed, 100 insertions(+), 100 deletions(-) diff --git a/One_Unit_Test.py b/One_Unit_Test.py index a7934a9..fb4e0d5 100644 --- a/One_Unit_Test.py +++ b/One_Unit_Test.py @@ -12,55 +12,55 @@ Test3 = 0 Test4 = 0 # Constants: -g.Coil_const = np.array([38.6, 38.45, 37.9])*1e-9 # Coil constants [x,y,z] in T/A -g.ambientField = np.array([80])*1e-6 # ambient magnetic field in measurement area, to be cancelled out -g.resistances = np.array([3.9, 3.9, 1]) # resistance of [x,y,z] circuits -g.maxWatts = np.array([8, 8, 0]) # max. allowed power for [x,y,z] circuits +g.COIL_CONST = np.array([38.6, 38.45, 37.9]) * 1e-9 # Coil constants [x,y,z] in T/A +g.AMBIENT_FIELD = np.array([80]) * 1e-6 # ambient magnetic field in measurement area, to be cancelled out +g.RESISTANCES = np.array([3.9, 3.9, 1]) # resistance of [x,y,z] circuits +g.MAX_WATTS = np.array([8, 8, 0]) # max. allowed power for [x,y,z] circuits # COM-Ports for power supply units: -xyPort = "COM7" -g.xyDevice = PS2000B.PS2000B(xyPort) +XY_PORT = "COM7" +g.XY_DEVICE = PS2000B.PS2000B(XY_PORT) -g.maxAmps = np.sqrt(g.maxWatts/g.resistances) -print(g.maxAmps) +g.MAX_AMPS = np.sqrt(g.MAX_WATTS / g.RESISTANCES) +print(g.MAX_AMPS) def print_status(): print("Output 1:") - func.print_status(g.xAxis) + func.print_status(g.X_AXIS) print("Output 2:") - func.print_status(g.yAxis) + func.print_status(g.Y_AXIS) -g.xyDevice.enable_all() -func.set_to_zero(g.xyDevice) +g.XY_DEVICE.enable_all() +func.set_to_zero(g.XY_DEVICE) print_status() t.sleep(3) if Test1 == 1: - g.xyDevice.voltage1 = 5 + g.XY_DEVICE.voltage1 = 5 t.sleep(1) print_status() t.sleep(5) - func.set_to_zero(g.xyDevice) + func.set_to_zero(g.XY_DEVICE) if Test2 == 1: - g.xyDevice.current1 = 0.2 + g.XY_DEVICE.current1 = 0.2 t.sleep(1) print_status() t.sleep(5) - func.set_to_zero(g.xyDevice) + func.set_to_zero(g.XY_DEVICE) if Test4 == 1: - func.set_axis_current(g.xyDevice, 0.2) + func.set_axis_current(g.XY_DEVICE, 0.2) t.sleep(1) print_status() t.sleep(10) - func.set_to_zero(g.xyDevice) + func.set_to_zero(g.XY_DEVICE) t.sleep(1) print_status() -g.xyDevice.disable_all() +g.XY_DEVICE.disable_all() print_status() diff --git a/cage_func.py b/cage_func.py index 8d15ae1..3254a0f 100644 --- a/cage_func.py +++ b/cage_func.py @@ -3,36 +3,36 @@ 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) + g.XY_DEVICE = PS2000B.PS2000B(g.XY_PORT) + g.Z_DEVICE = PS2000B.PS2000B(g.Z_PORT) + g.X_AXIS = (g.XY_DEVICE, 0, g.RELAY_PINS[0], 0) # (device, channel, arduino pin, axis index) + g.Y_AXIS = (g.XY_DEVICE, 1, g.RELAY_PINS[1], 1) + g.Z_AXIS = (g.Z_DEVICE, 0, g.RELAY_PINS[2], 2) def activate_all(): # enables remote control and output on all PSUs and channels - g.xyDevice.enable_all() - g.zDevice.enable_all() + g.XY_DEVICE.enable_all() + g.Z_DEVICE.enable_all() def deactivate_all(): # disables remote control and output on all PSUs and channels - g.xyDevice.disable_all() - g.zDevice.disable_all() + g.XY_DEVICE.disable_all() + g.Z_DEVICE.disable_all() def setup_arduino(): - for pin in g.relayPins: - g.arduino.pinMode(pin, "Output") - g.arduino.digitalWrite(pin, "LOW") + for pin in g.RELAY_PINS: + g.ARDUINO.pinMode(pin, "Output") + g.ARDUINO.digitalWrite(pin, "LOW") 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() + for pin in g.RELAY_PINS: + g.ARDUINO.digitalWrite(pin, "LOW") + g.ARDUINO.close() -def print_status(axis): # axis as (device, channel), e.g. g.xAxis +def print_status(axis): # axis control variable, stored in globals.py device = axis[0] # PSU channel = axis[1] # output channel on the PSU print("%s, %0.2f V, %0.2f A" @@ -41,11 +41,11 @@ def print_status(axis): # axis as (device, channel), e.g. g.xAxis def print_status_3(): print("X-Axis:") - print_status(g.xAxis) + print_status(g.X_AXIS) print("Y-Axis:") - print_status(g.yAxis) + print_status(g.Y_AXIS) print("Z-Axis:") - print_status(g.zAxis) + print_status(g.Z_AXIS) def set_to_zero(device): @@ -56,20 +56,20 @@ def set_to_zero(device): def set_field_simple(vector): # forms magnetic field as specified by vector, w/o cancelling ambient field - i_vec = vector/g.Coil_const + 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 + field = vector - g.AMBIENT_FIELD + 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]) + set_axis_current(g.X_AXIS, i_vec[0]) + set_axis_current(g.Y_AXIS, i_vec[1]) + set_axis_current(g.Z_AXIS, i_vec[2]) def set_axis_current(axis, value): # sets current with correct polarity on one axis @@ -77,17 +77,17 @@ def set_axis_current(axis, value): # sets current with correct polarity on one channel = axis[1] ardPin = axis[2] axisIndex = axis[3] - if abs(value) > g.maxAmps[axisIndex]: # prevent excessive currents + if abs(value) > g.MAX_AMPS[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])) + raise ValueError("Invalid current value. Tried %0.2fA, max. %0.2fA allowed" % (value, g.MAX_AMPS[axisIndex])) elif value >= 0: # switch polarity as needed - g.arduino.digitalWrite(ardPin, "LOW") + g.ARDUINO.digitalWrite(ardPin, "LOW") elif value < 0: - g.arduino.digitalWrite(ardPin, "HIGH") + g.ARDUINO.digitalWrite(ardPin, "HIGH") else: raise Exception("This should be impossible.") device.set_current(abs(value), channel) - maxVoltage = min(max(1.1 * g.maxAmps[axisIndex] * g.resistances[axisIndex], 12), g.maxVolts) # limit voltage + maxVoltage = min(max(1.1 * g.MAX_AMPS[axisIndex] * g.RESISTANCES[axisIndex], 12), g.MAX_VOLTS) # limit voltage device.set_voltage(maxVoltage) diff --git a/example2.py b/example2.py index 009b52d..3ba46cf 100644 --- a/example2.py +++ b/example2.py @@ -10,55 +10,55 @@ DEVICE = "COM7" if platform.system() == "Windows" else "/dev/ttyACM0" # connection to the device is automatically opened print("Connecting to device at %s..." % DEVICE) -xyDevice = PS2000B.PS2000B(DEVICE) # create Object of class PS2000B, pass COM-port and channel to functions inside +XY_DEVICE = PS2000B.PS2000B(DEVICE) # create Object of class PS2000B, pass COM-port and channel to functions inside # static device information can be read -print("Connection open: %s" % xyDevice.is_open()) -print("Device: %s" % xyDevice.get_device_information()) +print("Connection open: %s" % XY_DEVICE.is_open()) +print("Device: %s" % XY_DEVICE.get_device_information()) # dynamic device status information can be read -device_status_info1 = xyDevice.get_device_status_information(0) -device_status_info2 = xyDevice.get_device_status_information(1) -print("Device status 1: %s" % xyDevice.get_device_status_information(0)) -print("Device status 2: %s" % xyDevice.get_device_status_information(1)) -print("Current output 1: %0.2f V , %0.2f A" % (xyDevice.voltage1, xyDevice.current1)) -print("Current output 2: %0.2f V , %0.2f A" % (xyDevice.voltage2, xyDevice.current2)) +device_status_info1 = XY_DEVICE.get_device_status_information(0) +device_status_info2 = XY_DEVICE.get_device_status_information(1) +print("Device status 1: %s" % XY_DEVICE.get_device_status_information(0)) +print("Device status 2: %s" % XY_DEVICE.get_device_status_information(1)) +print("Current output 1: %0.2f V , %0.2f A" % (XY_DEVICE.voltage1, XY_DEVICE.current1)) +print("Current output 2: %0.2f V , %0.2f A" % (XY_DEVICE.voltage2, XY_DEVICE.current2)) # device can be controlled if not device_status_info1.remote_control_active: print("...will enable remote control...") - xyDevice.enable_remote_control(0) + XY_DEVICE.enable_remote_control(0) if not device_status_info2.remote_control_active: print("...will enable remote control...") - xyDevice.enable_remote_control(1) + XY_DEVICE.enable_remote_control(1) print("...set voltage 1 to 12V and max current to 1A...") -xyDevice.voltage1 = 12 -xyDevice.current1 = 1 +XY_DEVICE.voltage1 = 12 +XY_DEVICE.current1 = 1 time.sleep(2) print("...now enabling the power output control 1...") -xyDevice.enable_output(0) +XY_DEVICE.enable_output(0) time.sleep(2) print("... set voltage 2 to 5V and max current to 1A...") -xyDevice.voltage2 = 5 -xyDevice.current2 = 1 +XY_DEVICE.voltage2 = 5 +XY_DEVICE.current2 = 1 time.sleep(2) print("...now enabling the power output control 2...") -xyDevice.enable_output(1) +XY_DEVICE.enable_output(1) time.sleep(5) -print("Device status 1: %s" % xyDevice.get_device_status_information(0)) -print("Device status 2: %s" % xyDevice.get_device_status_information(1)) -print("Output 1: %0.2f V , %0.2f A" % (xyDevice.voltage1, xyDevice.current1)) -print("Output 2: %0.2f V , %0.2f A" % (xyDevice.voltage2, xyDevice.current2)) +print("Device status 1: %s" % XY_DEVICE.get_device_status_information(0)) +print("Device status 2: %s" % XY_DEVICE.get_device_status_information(1)) +print("Output 1: %0.2f V , %0.2f A" % (XY_DEVICE.voltage1, XY_DEVICE.current1)) +print("Output 2: %0.2f V , %0.2f A" % (XY_DEVICE.voltage2, XY_DEVICE.current2)) time.sleep(5) -xyDevice.disable_output(0) -xyDevice.disable_output(1) +XY_DEVICE.disable_output(0) +XY_DEVICE.disable_output(1) print("...and disabling remote control again.") -xyDevice.disable_remote_control(0) -xyDevice.disable_remote_control(1) +XY_DEVICE.disable_remote_control(0) +XY_DEVICE.disable_remote_control(1) -print("Device status 1: %s" % xyDevice.get_device_status_information(0)) -print("Device status 2: %s" % xyDevice.get_device_status_information(1)) +print("Device status 1: %s" % XY_DEVICE.get_device_status_information(0)) +print("Device status 2: %s" % XY_DEVICE.get_device_status_information(1)) diff --git a/globals.py b/globals.py index b5e47b3..7181e66 100644 --- a/globals.py +++ b/globals.py @@ -1,22 +1,22 @@ -global Coil_const -global ambientField +global COIL_CONST +global AMBIENT_FIELD -global xyPort -global zPort +global XY_PORT +global Z_PORT -global xyDevice -global zDevice +global XY_DEVICE +global Z_DEVICE -global xAxis -global yAxis -global zAxis +global X_AXIS # object structure: (device, channel, arduino pin, axis index) +global Y_AXIS +global Z_AXIS -global resistances -global maxAmps -global maxWatts -global maxVolts +global RESISTANCES +global MAX_AMPS +global MAX_WATTS +global MAX_VOLTS -global arduino +global ARDUINO -relayPins = [1, 2, 3] # digital pin on the Arduino for switching relay of each axis [x,y,z] +RELAY_PINS = [1, 2, 3] # digital pin on the Arduino for switching relay of each axis [x,y,z] diff --git a/main.py b/main.py index bd92438..9a85037 100644 --- a/main.py +++ b/main.py @@ -9,23 +9,23 @@ from Arduino import Arduino mag_vec1 = np.array([10, 10, 5])*1e-6 # Constants: -g.Coil_const = np.array([38.6, 38.45, 37.9])*1e-9 # Coil constants [x,y,z] in T/A -g.ambientField = np.array([80])*1e-6 # ambient magnetic field in measurement area, to be cancelled out -g.resistances = np.array([3.9, 1, 1]) # resistance of [x,y,z] circuits -g.maxWatts = np.array([8, 0, 0]) # max. allowed power for [x,y,z] circuits -g.maxVolts = 16 # max. allowed voltage, limited to 16V by used diodes! +g.COIL_CONST = np.array([38.6, 38.45, 37.9]) * 1e-9 # Coil constants [x,y,z] in T/A +g.AMBIENT_FIELD = np.array([80]) * 1e-6 # ambient magnetic field in measurement area, to be cancelled out +g.RESISTANCES = np.array([3.9, 1, 1]) # resistance of [x,y,z] circuits +g.MAX_WATTS = np.array([8, 0, 0]) # max. allowed power for [x,y,z] circuits +g.MAX_VOLTS = 16 # max. allowed voltage, limited to 16V by used diodes! # COM-Ports for power supply units: -g.xyPort = "COM1" # placeholders -g.zPort = "COM1" +g.XY_PORT = "COM1" # placeholders +g.Z_PORT = "COM2" # Code starts here------------------------------------------ -g.maxAmps = np.sqrt(g.maxWatts/g.resistances) # calculate maximum currents in each axis +g.MAX_AMPS = np.sqrt(g.MAX_WATTS / g.RESISTANCES) # calculate maximum currents in each axis print("Connecting to PSUs...") func.set_devices() # initiate communication, set handles print("Connecting to Arduino...") -g.arduino = Arduino() # search for connected arduino and set handle +g.ARDUINO = Arduino() # search for connected arduino and set handle print("Arduino found, configuring pins.") func.setup_arduino() print("Activating PSU outputs...")