-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplottercontroller.py
More file actions
68 lines (59 loc) · 2.3 KB
/
Copy pathplottercontroller.py
File metadata and controls
68 lines (59 loc) · 2.3 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
from serial.tools import list_ports
import serial
import time
class PlotterController:
"""
By default:
* Stepper motor 1 is named S, and is on the LEFT
* Stepper motor 2 is named T, and is on the RIGHT
* Both steppers wind out clockwise, and wind in counter-clockwise
* Decreasing the s or t values will decrease the length of the string, winding the module upwards
"""
def __init__(self, port_regex=r'(/dev/cu.usbmodem)'):
available_ports = list(list_ports.grep(port_regex))
while len(available_ports) == 0:
input("No ports detected that match r'(/dev/cu.usbmodem)'. Check the connection, then press Enter")
available_ports = list(list_ports.grep(port_regex))
if len(available_ports) == 1:
self.ser = serial.Serial(available_ports[0].device, 9600)
else:
print(available_ports)
port = input("Which port?: ")
self.ser = serial.Serial(port, 9600)
# Wait a bit for the Arduino to get a solid serial connection
time.sleep(2)
def close(self):
"""
Close the serial connection, freeing up resources
"""
self.ser.close()
def pen_up(self):
"""
Move the pen up
"""
self.ser.write('pu\n'.encode())
self.ser.read(1) # block until command complete byte is received
def pen_down(self):
"""
Move the pen down
"""
self.ser.write('pd\n'.encode())
self.ser.read(1) # block until command complete byte is received
def move(self, s, t):
"""
:param s: amount to change the length of the left string.
:param t: amount to change the length of the right string.
:return:
"""
self.ser.write('m {} {}\n'.format(s, t).encode())
self.ser.read(1) # block until command complete byte is received
def turn_off(self):
"""
Cut power to all the coils in both steppers
The steppers will NOT keep their position in this state
No special command is required to wake them up, just send through a move command
The servo is not affected in any way
:return:
"""
self.ser.write('o\n'.encode())
self.ser.read(1) # block until command complete byte is received