forked from zietzm/Helmholtz_Test_Bench
41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
from src.arduino import Arduino
|
|
from src.utility import ui_print
|
|
import src.config_handling as config_handling
|
|
import src.globals as g
|
|
|
|
|
|
class ArduinoDevice(Arduino):
|
|
""" Main class to control the electronics box (which means commanding the arduino inside).
|
|
Inherits from the Arduino library. All axis indices are go from 0-2 for x,y and z respectively"""
|
|
|
|
def __init__(self, com_port):
|
|
self.pins = [0, 0, 0] # initialize list with pins to switch relay of each axis
|
|
for i in range(3): # get correct pins from the config
|
|
self.pins[i] = int(config_handling.read_from_config(g.AXIS_NAMES[i], "relay_pin", config_handling.CONFIG_OBJECT))
|
|
|
|
# try to set up the arduino. Exceptions are handled by caller
|
|
# search for connected arduino and connect by initializing arduino library class
|
|
Arduino.__init__(self, port=com_port, timeout=2)
|
|
for pin in self.pins:
|
|
self.pinMode(pin, "Output")
|
|
self.digitalWrite(pin, "LOW")
|
|
|
|
def set_axis_polarity(self, axis_index, reverse):
|
|
"""Sets the polarity of the axis (indexed with 0-2). True is reverse polarity"""
|
|
if reverse:
|
|
self.digitalWrite(self.pins[axis_index], "HIGH")
|
|
else:
|
|
self.digitalWrite(self.pins[axis_index], "LOW")
|
|
|
|
def get_axis_polarity(self, idx):
|
|
"""Returns a bool indicating whether the axis polarity is reversed (True)."""
|
|
return self.digitalRead(self.pins[idx]) # pin is HIGH --> relay is switched
|
|
|
|
def idle(self):
|
|
"""Sets relay switching pins to low to de-power most of the electronics box"""
|
|
for pin in self.pins:
|
|
self.digitalWrite(pin, "LOW")
|
|
|
|
def shutdown(self):
|
|
self.idle()
|