-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_controller.py
105 lines (80 loc) · 2.95 KB
/
arduino_controller.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import serial
import serial.tools.list_ports
import time
class Arduino( object ):
def __init__(self, com=None, baud=9600):
self.baud = baud
self._connected = False
if com == None:
self.auto_connect()
else:
self.port = com
self.connect()
self.belts = [Belts( self, 'm', 'n', 'o' ),
Belts( self, 'r', 's', 't' ),
Belts( self, 'u', 'v', 'w' )]
def all_forward(self):
for belt in self.belts:
belt.forward()
def all_stop(self):
for belt in self.belts:
belt.stop()
def all_backwards(self):
for belt in self.belts:
belt.backwards()
def connect(self):
# Declare the port and baudrate for the Arduino
self.connection = serial.Serial( self.port, self.baud, timeout=1 )
time.sleep( 2 )
self._connected = True
return self.connected
def auto_connect(self):
port_list = self.list_ports()
self.port = port_list[0].device
self.connect()
@staticmethod
def list_ports():
ports = list( serial.tools.list_ports.comports() )
return ports
@property
def connected(self):
return self._connected
@property
def gate_state(self):
self.connection.write(b'l')
self.connection.flush()
arduinoData = int(self.connection.readline().decode('ascii'))
if arduinoData == 1: # broken
return True
elif arduinoData == 2: # unbroken
return False
def convert_to_bytes(self, character):
return bytes( character, 'ascii' ) if type( character ) == str else bytes( character )
def write_and_get_data(self, character):
self.connection.write( self.convert_to_bytes(character) )
self.connection.flush() # flush the writes from the buffer to the serial port
return self.connection.readline().decode('ascii')
def blocker_open(self):
self.write_and_get_data('e')
def blocker_close(self):
self.write_and_get_data('f')
def bulk_feeder_start(self):
self.write_and_get_data('x')
def bulk_feeder_stop(self):
self.write_and_get_data('y')
def bin_open(self):
self.write_and_get_data('b')
def bin_closed(self):
self.write_and_get_data('c')
class Belts( object ):
def __init__(self, connection: Arduino, forward_letter : str, stop_letter: str, reverse_letter: str ):
self.f = bytes(forward_letter, 'ascii') # forward
self.s = bytes(stop_letter, 'ascii') # stop
self.r = bytes(reverse_letter, 'ascii') # reverse
self.arduino: Arduino = connection
def forward(self):
arduinoData = self.arduino.write_and_get_data( self.f )
def stop(self):
arduinoData = self.arduino.write_and_get_data( self.s )
def backwards(self):
arduinoData = self.arduino.write_and_get_data( self.r )