debug and testing with arduino

This commit is contained in:
Martin Zietz
2021-01-27 11:21:03 +01:00
parent 4908338672
commit 239b685fee
7 changed files with 71 additions and 50 deletions
+2
View File
@@ -98,3 +98,5 @@ ENV/
.idea/misc.xml
.idea/Python-PS2000B.iml
.idea/Python-PS2000B.iml
.idea/Python-PS2000B.iml
.idea/misc.xml
+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 (MagnetEnv)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.7 (venv)" 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 (MagnetEnv)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (venv)" project-jdk-type="Python SDK" />
</project>
+1
View File
@@ -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:
+59 -33
View File
@@ -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()
+3 -6
View File
@@ -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
+4 -9
View File
@@ -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