101 lines
3.7 KiB
Python
101 lines
3.7 KiB
Python
import Beertap
|
|
import RPi.GPIO as GPIO
|
|
import time, pigpio
|
|
from configparser import ConfigParser
|
|
|
|
config = ConfigParser(allow_no_value=True)
|
|
config.read('configs/config.beer')
|
|
|
|
def WriteCount(count):
|
|
with open('track/BeerCount.txt','w',encoding = 'utf-8') as f:
|
|
f.write(str(count))
|
|
|
|
def ReadCount():
|
|
with open('track/BeerCount.txt') as f:
|
|
count = f.read()
|
|
if count == None or count == "":
|
|
count = 1
|
|
return int(count)
|
|
|
|
SERVO_GPIO = 18
|
|
GPIO.setwarnings(False)
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
pwm = pigpio.pi()
|
|
pwm.set_mode(SERVO_GPIO, pigpio.OUTPUT)
|
|
pwm.set_PWM_frequency(SERVO_GPIO, 50)
|
|
|
|
def AngleToPulseWidth (angle) :
|
|
if angle > 180 or angle < 0 :
|
|
return False
|
|
start = 566
|
|
end = 2475
|
|
ratio = (end - start)/180
|
|
angle_as_percent = angle * ratio
|
|
return round(start + angle_as_percent)
|
|
|
|
def MoveServo(start,end,duration): #move from start to end, using delta number of seconds
|
|
incMove=(end-start)/100.0
|
|
incTime=duration/100.0
|
|
for x in range(100):
|
|
pwm.set_servo_pulsewidth(SERVO_GPIO, int(start+x*incMove)) ;
|
|
time.sleep(incTime)
|
|
|
|
def CloseTap(angle_closed, angle_opened, duration):
|
|
start = AngleToPulseWidth(angle_opened)
|
|
end = AngleToPulseWidth(angle_closed)
|
|
MoveServo(start,end,duration)
|
|
|
|
def OpenTap(angle_closed, angle_opened, duration):
|
|
start = AngleToPulseWidth(angle_closed)
|
|
end = AngleToPulseWidth(angle_opened)
|
|
MoveServo(start,end,duration)
|
|
|
|
def CloseTapManually():
|
|
config.read('configs/config.beer')
|
|
pwm.set_servo_pulsewidth(SERVO_GPIO, AngleToPulseWidth(config.getint('CALIBRATION', 'SERVO_ANGLE_CLOSED'))) ;
|
|
|
|
def OpenTapManually():
|
|
config.read('configs/config.beer')
|
|
pwm.set_servo_pulsewidth(SERVO_GPIO, AngleToPulseWidth(config.getint('CALIBRATION', 'SERVO_ANGLE_OPEN'))) ;
|
|
|
|
#gets called from Beertap.py - when finished its fires "on_tapping_completed" event in beertap
|
|
def StartTapping():
|
|
config.read('configs/config.beer')
|
|
SERVO_ANGLE_OPEN = config.getint('CALIBRATION', 'SERVO_ANGLE_OPEN')
|
|
SERVO_ANGLE_CLOSED = config.getint('CALIBRATION', 'SERVO_ANGLE_CLOSED')
|
|
|
|
INCREASE_TAP_DURATION_OVER_KEG_LEVEL = TAP_DURATION=config.getboolean('CALIBRATION', 'increase_tapduration_over_keg_level')
|
|
if INCREASE_TAP_DURATION_OVER_KEG_LEVEL:
|
|
FULLKEG_DURATION = config.getfloat('CALIBRATION', 'fullkeg_duration')
|
|
EMPTYKEG_DURATION = config.getfloat('CALIBRATION', 'emptykeg_duration')
|
|
LITER_PER_BEER = config.getfloat('CALIBRATION', 'LITER_PER_BEER')
|
|
BEERS_PER_KEG = round(6 * (1/LITER_PER_BEER))
|
|
count = ReadCount()
|
|
print(str(count-1) + " beer been tapped already")
|
|
if count > BEERS_PER_KEG:
|
|
count = BEERS_PER_KEG
|
|
if count == 0:
|
|
count = 1
|
|
TAP_DURATION = FULLKEG_DURATION + ((count-1) * (EMPTYKEG_DURATION-FULLKEG_DURATION)/(BEERS_PER_KEG-1))
|
|
TAP_DURATION = round(TAP_DURATION, 3)
|
|
print(str(count) + " beer is now being tapped with a duration of: "+ str(TAP_DURATION))
|
|
WriteCount(count+1)
|
|
else:
|
|
TAP_DURATION = config.getfloat('CALIBRATION', 'TAP_DURATION')
|
|
|
|
SMOOTH = config.getboolean('CALIBRATION', 'SMOOTH')
|
|
if SMOOTH:
|
|
TAP_DURATION_OPEN=TAP_DURATION/40
|
|
TAP_DURATION_CLOSE=TAP_DURATION/40
|
|
TAP_DURATION_BETWEEN=TAP_DURATION-TAP_DURATION_OPEN-TAP_DURATION_CLOSE
|
|
|
|
OpenTap(SERVO_ANGLE_CLOSED, SERVO_ANGLE_OPEN, TAP_DURATION_OPEN)
|
|
time.sleep(TAP_DURATION_BETWEEN)
|
|
CloseTap(SERVO_ANGLE_CLOSED, SERVO_ANGLE_OPEN, TAP_DURATION_CLOSE)
|
|
else:
|
|
OpenTapManually()
|
|
time.sleep(TAP_DURATION)
|
|
CloseTapManually()
|
|
|
|
Beertap.events.on_tapping_completed() |