-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.py
124 lines (99 loc) · 3.17 KB
/
printer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from serial import Serial
import XInput
import time
feedrates = [200, 500, 1000, 3000, 6000, 12000]
modes = {1 : "Relative", 2:"Absolute"}
MIN_PRINT_TEMP = 170
PREHEAT_HOTEND_TEMP = 200
PREHEAT_BED_TEMP = 75
class Printer:
def __init__(self, port:str, baudrate:int, x:int = 230, y:int = 230, test:bool=False):
## Connection
self.test = test
if not self.test:
self.port = port
self.baudrate = baudrate
self.ser = Serial(port, baudrate)
time.sleep(5) # Wait for printer to reset
self.output_line("M117 Hello World!") # dDisplay to LCD
self.preheat()
## Printer specs
self.x = x
self.y = y
self.hotend_temp = 0
self.bed_temp = 0
## Printer variables
self.set_relative()
self.fr_index = 4
self.increase_feedrate()
print(feedrates[self.fr_index])
def increase_feedrate(self):
self.fr_index = min(len(feedrates) - 1, self.fr_index + 1)
self.output_line("G0 F" + str(feedrates[self.fr_index]))
def decrease_feedrate(self):
self.fr_index = max(len(feedrates), 0)
self.output_line("G0 F" + str(feedrates[self.fr_index]))
def home(self, x:bool = True, y:bool = True, z:bool = True):
line = "G28"
if not (x and y and z):
if x:
line += " X"
if y:
line += " Y"
if z:
line += " Z"
self.output_line(line)
def home_x(self):
self.home(y=False, z=False)
def home_y(self):
self.home(x=False, z=False)
def home_xy(self):
self.home(z = False)
def home_z(self):
self.home(x=False, y=False)
def set_relative(self):
self.output_line("G91")
self.relative = True
def set_absolute(self):
self.output_line("G90")
self.relative = False
def move(self, e:int=0, x:int = 0, y:int = 0, z:int = 0):
if e == x == y == z == 0:
return
line = "G0"
if e != 0 and self.hotend_temp > MIN_PRINT_TEMP:
line += " E" + str(e)
if x != 0:
line += " X" + str(x)
if y != 0:
line += " Y" + str(y)
if z!= 0:
line += " Z" + str(z)
self.output_line(line)
def preheat(self, hotend=True, bed=True):
if hotend:
self.set_hotend_temp(PREHEAT_HOTEND_TEMP)
if bed:
self.set_bed_temp(PREHEAT_BED_TEMP)
def set_hotend_temp(self, temp, wait:bool = False):
if wait:
cmd = "M109 S"
else:
cmd = "M104 S"
self.output_line(cmd + str(temp))
self.hotend_temp = temp
def set_bed_temp(self, temp:int, wait:bool = False):
if wait:
cmd = "M190 S"
else:
cmd = "M140 S"
self.output_line(cmd + str(temp))
self.bed_temp = temp
def cooldown(self):
self.set_hotend_temp(0)
self.set_bed_temp(0)
def output_line(self, line:str):
print(line)
line += "\r\n"
if not self.test:
self.ser.write(line.encode())