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(5)

def goToBin(bin):
    adjustedBin = math.floor(bin / 2)
    distance = adjustedBin * 18
    delay = 0.5 + 0.93 * adjustedBin
    command = 'G0 X'
    command += distance
    ser.write(command)
    time.sleep(delay)
    if bin % 2 == 0: # tilt to right
        pwm.ChangeDutyCycle(0)
        time.sleep(1)
        pwm.ChangeDutyCycle(20)
    else:
        pwm.ChangeDutyCycle(40)
        time.sleep(1)
        pwm.ChangeDutyCycle(20)
    ser.write(b'G0 X0')
    time.sleep(delay)