Advertisement
Guest User

Untitled

a guest
Dec 27th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import serial
  2. import time
  3.  
  4. def sendCommands(cmds, delay=0):
  5.    for cmd in cmds.strip().splitlines():
  6.        cmd = cmd.strip()
  7.        print('Sending: ' + cmd)
  8.        cmd = cmd + '\n'
  9.  
  10.        s.write(cmd.encode()) # Send g-code block to grbl
  11.  
  12.        if (delay != 0):
  13.            time.sleep(delay)
  14.  
  15.        while True:  
  16.            grbl_out_raw = s.readline()
  17.            grbl_out = grbl_out_raw.decode('utf-8').strip()
  18.            print(grbl_out)
  19.            if grbl_out == 'ok':
  20.                break
  21.  
  22. def homeDevice():
  23.    cmd = """
  24.           $$
  25.           $H
  26.           G10 P0 L20 X0 Y0 Z0
  27.           """
  28.    sendCommands(cmd)
  29.  
  30.  
  31. def pyramidPattern():
  32.    cmd=''
  33.    # Count up to 600
  34.    for i in range(100,700,100):
  35.        cmd += """
  36.               G0 X{0}
  37.               ?
  38.               """.format(i)
  39.  
  40.    # Count down to 100
  41.    for i in range(500,0,-100):
  42.        cmd += """
  43.               G0 X{0}
  44.               ?
  45.               """.format(i)
  46.    sendCommands(cmd, delay=0)
  47.  
  48.  
  49. # Open grbl serial port
  50. s = serial.Serial('/dev/cu.usbmodem146401',115200)
  51.  
  52. # Wake up grbl
  53. serialCmd = "\r\n\r\n"
  54. s.write(serialCmd.encode())
  55. time.sleep(2)   # Wait for grbl to initialize
  56. s.flushInput()  # Flush startup text in serial input
  57.  
  58. homeDevice()
  59. pyramidPattern()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement