|
|
|
import math
|
|
|
|
import serial
|
|
|
|
import RPi.GPIO as gpio
|
|
|
|
import time
|
|
|
|
|
|
|
|
ser = serial.Serial('/dev/ttyUSB0', 115200)
|
|
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
gpio.setup(13, gpio.OUT)
|
|
|
|
pwm = gpio.PWM(13, 100)
|
|
|
|
pwm.start(13)
|
|
|
|
verbose = True
|
|
|
|
|
|
|
|
print("Initializing Grbl...")
|
|
|
|
ser.write(b'\r\n\r\n')
|
|
|
|
time.sleep(2)
|
|
|
|
ser.write(b'$RST=#\n')
|
|
|
|
time.sleep(1)
|
|
|
|
ser.flushInput()
|
|
|
|
|
|
|
|
def goToBin(bin):
|
|
|
|
adjustedBin = math.floor(bin / 2)
|
|
|
|
distance = adjustedBin * 18
|
|
|
|
delay = 0.5 + 0.93 * adjustedBin
|
|
|
|
command = 'G0 X-'
|
|
|
|
command += str(distance)
|
|
|
|
command += '\n'
|
|
|
|
ser.write(b'$X\n')
|
|
|
|
time.sleep(1)
|
|
|
|
print(command)
|
|
|
|
ser.write(command.encode('utf-8'))
|
|
|
|
# s.write("$C\n")
|
|
|
|
while True:
|
|
|
|
grbl_out = str(ser.readline().strip()) # Wait for grbl response with carriage return
|
|
|
|
print(grbl_out.find('error'))
|
|
|
|
if int(grbl_out.find('error')) >= 0 :
|
|
|
|
print("REC:",grbl_out)
|
|
|
|
print(" Grbl reported error!")
|
|
|
|
quit()
|
|
|
|
elif int(grbl_out.find('ok')) >= 0 :
|
|
|
|
if verbose: print('REC:',grbl_out)
|
|
|
|
break
|
|
|
|
time.sleep(delay)
|
|
|
|
if bin % 2 == 0: # tilt to left
|
|
|
|
pwm.ChangeDutyCycle(5)
|
|
|
|
time.sleep(1)
|
|
|
|
pwm.ChangeDutyCycle(14)
|
|
|
|
else:
|
|
|
|
pwm.ChangeDutyCycle(25)
|
|
|
|
time.sleep(1)
|
|
|
|
pwm.ChangeDutyCycle(13)
|
|
|
|
time.sleep(1)
|
|
|
|
ser.write(b'G0 X0\n')
|
|
|
|
while True:
|
|
|
|
grbl_out = str(ser.readline().strip()) # Wait for grbl response with carriage return
|
|
|
|
if int(grbl_out.find('error')) >= 0 :
|
|
|
|
print("REC:",grbl_out)
|
|
|
|
print(" Grbl reported error!")
|
|
|
|
quit()
|
|
|
|
elif int(grbl_out.find('ok')) >= 0 :
|
|
|
|
if verbose: print('REC:',grbl_out)
|
|
|
|
break
|
|
|
|
time.sleep(delay)
|