-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaml_relay.py
More file actions
executable file
·75 lines (60 loc) · 2.04 KB
/
aml_relay.py
File metadata and controls
executable file
·75 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import time, serial
"""
This is the Relay object that controls communication between
the computer and the Arduino controlling the relays in the AML.
"""
class Relay(object):
def __init__(self, port='/dev/tty.usbmodem14241'):
self.port = port
self.start_serial(self.port)
# Power on the relays by default
self.connectMotors()
#Open a port for serial communication.
def start_serial(self, port):
# Configure the serial connections for Arduino
try:
self.ser = serial.Serial(
port=port,
baudrate=9600,
parity=serial.PARITY_NONE,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
timeout=None # blocking
)
time.sleep(2) # Wait for Arudino to restart
except:
print("Couldn't connect to the AML relay serial...")
def write(self, command):
self.ser.write(command)
time.sleep(.01)
def close_serial(self):
self.ser.close()
# Not used
def read(self):
out = None
# Flush buffer of old data
while self.ser.inWaiting() > 0:
self.ser.read()
while True:
data = str(self.ser.read())
# Wait for next new measurement
if data == '\n' and out == None:
out = []
elif data != '\n' and out != None:
out.append(data)
elif data == '\n' and out != None:
out.append(data)
break
return int(''.join(out)[:-2])
# Not used
def write_and_read(self, command):
self.write(command)
return self.read()
# Send commands to Arduino
def connectMotors(self):
self.write('t')
time.sleep(1)
def disconnectMotors(self):
self.write('f')
time.sleep(1)