Last active
December 26, 2015 09:39
-
-
Save paulkaplan/7130582 to your computer and use it in GitHub Desktop.
A brief example of how to connect to a GRBL shield via serial port and send basic machine movements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import atexit | |
def initController(): | |
port = serial.Serial( | |
port='/dev/tty.usbmodem1411', | |
baudrate=9600 | |
) | |
closePort(port) | |
try: | |
port.open() | |
except Exception, e: | |
print "error open serial port: " + str(e) | |
exit() | |
def preciseMove(x=None, y=None, z=None, feed=50): | |
port.write( preciseMoveGcode(x,y,z,feed)+"\n" ) | |
def fastMove(x=None, y=None, z=None): | |
port.write( fastMoveGcode(x,y,z)+"\n" ) | |
def sendLine(line): | |
port.write(line+"\n") | |
def keyboardMode(): | |
while(1): | |
key = getch() | |
if key == 'a': | |
print 'nudge -X' | |
sendLine("G91 G0 X-1") | |
elif key == 'd': | |
print 'nudge +X' | |
sendLine("G91 G0 X1" ) | |
elif key == 's': | |
print 'nudge -Y' | |
sendLine("G91 G0 Y-1" ) | |
elif key =='w': | |
print 'nudge +Y' | |
sendLine("G91 G0 Y1" ) | |
else : | |
return | |
getch = _Getch() | |
import code | |
code.InteractiveConsole(locals=locals()).interact() | |
def preciseMoveGcode(x,y,z,feed): | |
gcode = "G1" | |
gcode += xyzToString(x,y,z) | |
gcode += " F"+str(feed) | |
print gcode | |
return gcode | |
def fastMoveGcode(x,y,z): | |
gcode = "G0" | |
gcode += xyzToString(x,y,z) | |
print gcode | |
return gcode | |
def xyzToString(x,y,z): | |
gcode =" X"+str(x) if x else "" | |
gcode+=" Y"+str(y) if y else "" | |
gcode+=" Z"+str(z) if z else "" | |
return gcode | |
def closePort(port): | |
if(port.isOpen()): | |
port.close() | |
class _Getch: | |
"""Gets a single character from standard input. Does not echo to the | |
screen.""" | |
def __init__(self): | |
try: | |
self.impl = _GetchWindows() | |
except ImportError: | |
self.impl = _GetchUnix() | |
def __call__(self): return self.impl() | |
class _GetchUnix: | |
def __init__(self): | |
import tty, sys | |
def __call__(self): | |
import sys, tty, termios | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
class _GetchWindows: | |
def __init__(self): | |
import msvcrt | |
def __call__(self): | |
import msvcrt | |
return msvcrt.getch() | |
if __name__ == "__main__": | |
initController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment