-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcode.py
More file actions
119 lines (97 loc) · 3.9 KB
/
gcode.py
File metadata and controls
119 lines (97 loc) · 3.9 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
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
import serial
import time
import glob, os
import curses
testing = False
# Open grbl serial port ==> CHANGE THIS BELOW TO MATCH YOUR USB LOCATION
try:
s = serial.Serial('/dev/ttyUSB0',115200) # cu.wchusbserial1450 GRBL operates at 115200 baud. Leave that part alone.
except Exception as e:
testing = True
print "Not connected"
# import gcode files
shapes = []
numclearing = len(glob.glob("Clearing Paths/*.gcode"))
numshapes = len(glob.glob("Shape Paths/*.gcode"))
for f in glob.glob("Clearing Paths/*.gcode"):
shapes.append(f[15:-6])
for f in glob.glob("Shape Paths/*.gcode"):
shapes.append(f[12:-6])
while True:
# print menu
print "0. Manual"
print "== Clearing Paths =="
for i in range(1,numclearing+1):
print str(i)+".",shapes[i-1]
print "== Shape Paths =="
for i in range(numclearing+1,numclearing+numshapes+1):
print str(i)+".",shapes[i-1]
print ""
choice = raw_input("Your choice: ")
try:
if "," not in choice: # single path
choices = [int(choice) - 1]
else: # multiple paths
choicestemp = choice.split(",")
choices = [int(c.strip()) for c in choicestemp]
direction = ''
if -1 in choices: #ignore everything else
while direction != "q":
direction = raw_input("Direction:")
if not testing:
# Wake up grbl
s.write("\r\n\r\n")
time.sleep(2) # Wait for grbl to initialize
s.flushInput() # Flush startup text in serial input
s.write("G21 (mm mode)\n")
s.write("G91 (relative mode)\n")
s.write("F2000\n")
if direction == "a":
output = "G1 X-10"
elif direction == "d":
output = "G1 X10"
elif direction == "w":
output = "G1 Y10"
elif direction == "s":
output = "G1 Y-5\10"
elif direction == "q":
break
print output
if not testing:
s.write(output + '\n') # Send g-code block to grbl
grbl_out = s.readline() # Wait for grbl response with carriage return
print ' : ' + grbl_out.strip()
else: # not manual mode
for choice in choices: # loop through selected patterns
filename = shapes[choice-1]
if choice < numclearing:
filename = "Clearing Paths/" + filename
else:
filename = "Shape Paths/" + filename
# Open g-code file
f = open(filename+'.gcode','r');
# Wake up grbl
s.write("\r\n\r\n")
time.sleep(2) # Wait for grbl to initialize
s.flushInput() # Flush startup text in serial input
# Stream g-code to grbl
for line in f:
l = line.strip() # Strip all EOL characters for consistency
print 'Sending: ' + l,
s.write(l + '\n') # Send g-code block to grbl
grbl_out = s.readline() # Wait for grbl response with carriage return
print ': ' + grbl_out.strip()
# Wait here until grbl is finished to close serial port and file.
while 1<2:
s.write("?")
grbl_out = s.readline()
if "Run" in grbl_out:
time.sleep(1)
elif "Idle" in grbl_out:
break
# Close file
f.close()
except Exception as e:
print "Error:", e
# Close serial port
s.close()