]> wolfpit.net Git - hardware/fuck_tomatoes/.git/blob - Printer.py
bd311c254c60f94c96eecaf724580aff93116094
[hardware/fuck_tomatoes/.git] / Printer.py
1 import serial, time
2 from settings import frameSettings
3 from utils import MatrixConversion, Geometry, Gcode, PrinterUtils
4
5 class Printer():
6 def __init__(self, PrinterCOM, bedSize):
7 self.COM = PrinterCOM
8 self.max_X, self.max_Y = bedSize
9 self.position = (0,0)
10 self.sendSerial = False
11 self.settings = frameSettings.frameSettings()
12 self.coeffs = MatrixConversion.find_coeffs(self.settings.image_frame.corners, self.settings.laser_frame.corners)
13 try: self.printerSerial = serial.Serial(PrinterCOM, 115200, timeout = 25)
14 except: print("Connection Failure")
15
16
17 def writePoint(self, xy):
18 x,y = xy
19 x = self.max_X - x
20 self.position = xy
21 command = 'G0 F5000 X%d Y%d\n'%(x, y) + 'G4 P1000\n' + 'G0 F5000 X0 Y0\n'
22
23 if self.printerSerial is not None and Geometry.pointWithinBounds(xy, (self.max_X, self.max_Y)):
24 self.printerSerial.write(command.encode())
25 return 1
26 else:
27 return 0
28
29 def writePackage(self, package):
30 if self.printerSerial is not None:
31 self.printerSerial.write(package.encode())
32
33 def adjustXY(self, xy):
34 printer_points = [(self.max_X,self.max_Y), (0,self.max_Y), (0,0), (self.max_X,0)]
35 self.coeffs = MatrixConversion.find_coeffs(self.settings.laser_frame.corners, printer_points)
36 return MatrixConversion.warped_xy((xy), self.coeffs)
37
38 def write(self, xy):
39 self.writePoint(self.adjustXY(xy))
40
41 def sendPackage(self, points):
42 package = Gcode.buildGcodePackage(list(map(self.adjustXY, points)))
43 self.writePackage(package)
44
45 def packageIsExecuting(self):
46 #Query the printer for position
47 self.printerSerial.reset_input_buffer()
48 self.printerSerial.write('M114'.encode())
49 printerData = ''
50 while self.printerSerial.in_waiting > 0:
51 printerData += str(self.printerSerial.readline())
52
53 if 'Count' in printerData:
54 x, y = PrinterUtils.parsePrinterXY(printerData)
55 if x == 0 and y == 0: return False
56 return True
57
58 def raiseZ(self):
59 self.printerSerial.write('G0 F5000 Z50\n'.encode())
60
61 def callibrate(self):
62 self.printerSerial.write('G28\n'.encode())
63
64 def home(self):
65 self.printerSerial.write('G0 F5000 X0 Y0\n'.encode())
66
67 def read(self):
68 print(self.printerSerial.readline())