-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirmware.py
More file actions
90 lines (75 loc) · 2.24 KB
/
firmware.py
File metadata and controls
90 lines (75 loc) · 2.24 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import serial
import time
from tqdm import tqdm
def InitializeSerial():
'''
Initialize serial communication with the Arduino.
Returns: Arduino serial object
'''
#arduino = serial.Serial("/dev/ttyACM0",115200, timeout=.1)
arduino = serial.Serial("COM8",115200, timeout=.1)
for i in tqdm(range(100), ascii= "---------#", desc = 'Connecting to Arduino'):
time.sleep(.025)
return arduino
def ORbytes(abytes, bbytes):
'''
Bitwise OR for two bytearrays.
Args:
abytes: a bytearray
bbytes: another bytearray
Returns:
a byte array with the result
'''
return bytes([int(a) | int(b) for a, b in zip(abytes[::-1], bbytes[::-1])][::-1])
# # this has to be manually synced with the firmware if the opcodes are changed
# opcodes = {
# "ZERO": b'\x00\x00',
# "T+X": b'\x00\x10',
# "T+Y": b'\x00\x20',
# "T-X": b'\x00\x30',
# "T-Y": b'\x00\x40',
# "X": b'\x00\x50',
# "Y": b'\x00\x60',
# "READY": b'\x00\x70',
# }
def ReadArduino(arduino):
data = arduino.readline()
return data
def MoveMotors(arduino, location: tuple):
'''
Send a command to the arduino to move the motors to a location
Args:
Tuple (X,Y) for where to move gantry
Returns:
NONE
'''
# this has to be manually synced with the firmware if the opcodes are changed
opcodes = {
"ZERO": b'\x00\x00',
"T+X": b'\x00\x10',
"T+Y": b'\x00\x20',
"T-X": b'\x00\x30',
"T-Y": b'\x00\x40',
"X": b'\x00\x50',
"Y": b'\x00\x60',
"READY": b'\x00\x70',
}
# the arduino is little endian which makes byte stuff slightly unintuitive
# send the x command
x_data = location[0].to_bytes(2, 'little');
x_packet = ORbytes(x_data, opcodes["X"])
arduino.write(x_packet);
# then the y command
y_data = location[1].to_bytes(2, 'little');
y_packet = ORbytes(y_data, opcodes["Y"])
arduino.write(y_packet);
def ZeroGantry(arduino):
# the zero command is just two bytes of 0s
arduino.write(bytearray(b'\x00\x00'))
time.sleep(8)
print('Axis zeroed')
def CenterGantry(arduino):
# moves it to the center
MoveMotors(arduino,(185,205))
time.sleep(5)
print('Gantry centered')