Skip to content

Instantly share code, notes, and snippets.

@hdo
Created August 27, 2017 07:48
Show Gist options
  • Save hdo/06fa618b2abafe1a52342e8d26535bd8 to your computer and use it in GitHub Desktop.
Save hdo/06fa618b2abafe1a52342e8d26535bd8 to your computer and use it in GitHub Desktop.
Simple GRBL Jog test script
import pygame
import os
import serial
import time
def create_screen():
if os.name == "nt":
return pygame.display.set_mode((320, 200))
else:
drivers = ['fbcon', 'directfb', 'svgalib']
found = False
for driver in drivers:
# Make sure that SDL_VIDEODRIVER is set
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print 'Driver: {0} failed.'.format(driver)
continue
found = True
break
#return pygame.display.set_mode((320, 200), pygame.FULLSCREEN)
return pygame.display.set_mode((320, 200))
pygame.init()
#pygame.mouse.set_visible(0)
clock = pygame.time.Clock()
screen = create_screen()
gameExit = False
print "start"
jog_active = False
trigger_up = False
trigger_down = False
trigger_left = False
trigger_right = False
counter = 0
speed = 400
cmd_base = "$J=G91 G21 X%d Y%d F%d\n"
cmd = "$J=G91 G21 X-1 F1500\n"
ser = serial.Serial('/dev/ttyACM0', baudrate=115200)
print ser.portstr
up_last_ms_down = 0
up_last_state = 0 # not pressed
down_last_ms_down = 0
down_last_state = 0
while not gameExit:
last_ms = time.time()
x_value = 0
y_value = 0
mods = pygame.key.get_mods();
if mods & pygame.KMOD_CTRL:
speed = 800
if mods & pygame.KMOD_ALT:
speed = 1500
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
gameExit = True
if event.key == pygame.K_UP:
trigger_up = True
up_last_ms_down = last_ms
if event.key == pygame.K_DOWN:
trigger_down = True
down_last_ms_down = last_ms
if event.type == pygame.KEYUP:
counter = 0
speed = 400
if event.key == pygame.K_UP:
trigger_up = False
up_last_state = 0
if event.key == pygame.K_DOWN:
trigger_down = False
down_last_state = 0
clock.tick(30)
send = False
if trigger_up and not trigger_down:
counter = counter + 1
#print last_ms
if up_last_state == 0:
up_last_state = 1
print "UP once"
cmd = "$J=G91 G21 Y1 F500\n"
send = True
if last_ms - up_last_ms_down > 0.5:
print "UP repeat"
if not jog_active:
jog_active = True
print "send LONG UP command"
cmd = "$J=G91 G21 Y1000 F3000\n"
send = True
if trigger_down and not trigger_up:
counter = counter + 1
#print last_ms
if down_last_state == 0:
down_last_state = 1
print "DOWN once"
cmd = "$J=G91 G21 Y-1 F500\n"
send = True
if last_ms - down_last_ms_down > 0.5:
print "DOWN repeat"
if not jog_active:
jog_active = True
print "send LONG DOWN command"
cmd = "$J=G91 G21 Y-1000 F3000\n"
send = True
if send:
print cmd
ser.write(cmd)
if not trigger_up and not trigger_down:
if jog_active:
jog_active = False
print "cancel jog"
ser.write([0x85])
print "done"
pygame.quit()
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment