Skip to content

Instantly share code, notes, and snippets.

@redbo
Last active July 16, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redbo/56d096d9c33f42c081cf6b60232b0068 to your computer and use it in GitHub Desktop.
Save redbo/56d096d9c33f42c081cf6b60232b0068 to your computer and use it in GitHub Desktop.
jogger.py
import collections
import curses, curses.wrapper
import time
import serial
DEVICE = '/dev/ttyUSB0'
def cmd(s, c):
s.write(c.strip() + '\n')
done = False
while True:
resp = s.readline().strip()
if resp == 'ok' or resp.startswith('error:'):
return
class job(object):
def __init__(self, s, f, output):
self.s = s
self.f = f
self.output = output
self.paused = False
self.errored = False
self.completed = False
def filebytes(self):
for line in self.f:
self.output("> " + line.strip())
line = line.strip() + '\n'
unacked.append(len(line))
for c in line:
yield c
while self.paused:
time.sleep(0.5)
def run(self):
outstanding = 0
unacked = collections.deque()
for byte in self.filebytes():
if sum(unacked) < 128:
self.s.write(byte)
outstanding += 1
else:
resp = self.s.readline().strip()
self.output("< " + resp)
if resp == 'ok':
outstanding -= unacked.popleft()
elif resp.startswith('error:'):
self.errored = True
return
self.completed = True
def pause(self):
self.paused = True
def resume(self):
self.paused = False
def main(stdscr):
stdscr.clear()
stdscr.refresh()
j = None
s = serial.Serial(DEVICE, 115200)
s.write("\r\n\r\n")
time.sleep(2)
s.flushInput()
cmd(s, 'G21 G90 G94 G54')
stdscr.addstr(0, 0, " w")
stdscr.addstr(1, 0, " | [")
stdscr.addstr(2, 0, " | |")
stdscr.addstr(3, 0, "a---+---d |")
stdscr.addstr(4, 0, " | |")
stdscr.addstr(5, 0, " | ]")
stdscr.addstr(6, 0, " s")
stdscr.addstr(8, 0, "z = zero work coords q = quit")
stdscr.hline(9, 0, '-', stdscr.getmaxyx()[1])
stdscr.setscrreg(10, 20)
scrreg = stdscr.subwin(10, 0)
scrreg.move(0, 0)
scrreg.refresh()
stdscr.timeout(100)
def output(line):
scrreg.insstr(0, 0, line)
scrreg.insertln()
scrreg.refresh()
k = stdscr.getch()
while (k != ord('q')):
if k == ord('S'):
cmd(s, '$J=G91 F100 G21 Y-1.0')
output("JOG Y -1.0")
elif k == ord('W'):
cmd(s, '$J=G91 F100 G21 Y1.0')
output("JOG Y +1.0")
elif k == ord('D'):
cmd(s, '$J=G91 F100 G21 X1.0')
output("JOG X +1.0")
elif k == ord('A'):
cmd(s, '$J=G91 F100 G21 X-1.0')
output("JOG X -1.0")
elif k == ord('s'):
cmd(s, '$J=G91 F300 G21 Y-10.0')
output("JOG Y -10.0")
elif k == ord('w'):
cmd(s, '$J=G91 F300 G21 Y10.0')
output("JOG Y +10.0")
elif k == ord('d'):
cmd(s, '$J=G91 F300 G21 X10.0')
output("JOG X +10.0")
elif k == ord('a'):
cmd(s, '$J=G91 F300 G21 X-10.0')
output("JOG X -10.0")
elif k == ord('['):
cmd(s, '$J=G91 F300 G21 Z10.0')
output("JOG Z +10.0")
elif k == ord(']'):
cmd(s, '$J=G91 F300 G21 Z-10.0')
output("JOG Z -10.0")
elif k == ord('{'):
cmd(s, '$J=G91 F100 G21 Z1.0')
output("JOG Z +1.0")
elif k == ord('}'):
cmd(s, '$J=G91 F100 G21 Z-1.0')
output("JOG Z -1.0")
elif k == ord('z'):
cmd(s, 'G92 X0 Y0 Z0')
output("Zeroed coords")
elif k == "P":
if j is None:
output("No job to pause.")
else:
j.pause()
output("Pausing job...")
elif k == "R":
if j is None:
cmd(s, 'G92 X0 Y0 Z0')
output("Zeroed coords")
output("Starting job...")
j = job(s, f, output)
threading.Thread(j.run).start()
else:
j.pause()
output("Resuming job...")
elif j and j.errored:
output("JOB ERROR")
j = None
elif j and j.completed:
output("JOB COMPLETED")
j = None
scrreg.refresh()
k = stdscr.getch()
curses.wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment