def setup_dispenser(self): addr = int(self.config['Dispenser'].get('Address', '0x00'), 16) single = int(self.config['Hardware'].get('SwitchSingle')) double = int(self.config['Hardware'].get('SwitchDouble')) self.dispenser = Dispenser(addr, spin=single, dpin=double)
def on_enter_init(self): conveyor.stop() #TODO : better way to handle when hardware is not there or working if self.isDispenseEnabled: self.dispenserUSB0 = Dispenser( connect_to_serial_port('/dev/ttyUSB0')) if self.isEjectEnabled: # create an instance of the ejector class try: self.ejector1 = Ejector("Bin-1", 0x40, 1) except IOError: print "no Ejector found"
def dispense(slot_id=0, amount=0): """Dispense a certain amount of a certain spice. Args: slot_id (str): The ID of the slot to dispense from. amount (str): The amount of the spice to dispense in 1/100 grams. Must be able to cast to an int. """ dispenser = Dispenser() print("dispencer") print(amount) amount_in_grams = int(amount) * 100 slot_id = int(slot_id) dispenser.dispense(slot_id, amount_in_grams) #return json({'data': {'slot_id': slot_id,'amount': amount_in_grams}}) print("in dispense loop") return ''
def getDispenser(**kwargs): config = getConfig() addr = int(config['Dispenser'].get('Address', '0x00'), 16) mspoz = int(config['Dispenser'].get('MsPerOz', '2000')) single = int(config['Hardware'].get('SwitchSingle')) double = int(config['Hardware'].get('SwitchDouble')) prime = kwargs.get('prime', config['Prime']) return Dispenser(addr, mspoz=mspoz, prime=prime, spin=single, dpin=double)
def Gpio_Intent(status,room): GPIO.setwarnings(False) if 1 : dispenser = Dispenser() else: dispenser = TestDispenser() if status in STATUSON: return statement('turning {} Pestle Home'.format(status)) elif status in STATUSOFF: return statement('turning {} Pestle Home'.format(status)) elif status in STATUSPROTIEN: dispenser.dispense(0, 3) dispenser.clean_and_exit() return statement('dispensing {} from Pestle Home'.format(status)) elif status in STATUSCHIA: dispenser.move(1) dispenser.dispense(1, 3) dispenser.calibrate_track() dispenser.clean_and_exit() return statement('dispensing {} from Pestle Home'.format(status)) elif status in STATUSLEMONADE: dispenser.dispense(0, 3) dispenser.clean_and_exit() return statement('dispensing {} from Pestle Home'.format(status)) elif status in STATUSSHAKE: dispenser.dispense(0, 3) dispenser.move(1) dispenser.dispense(1, 3) dispenser.calibrate_track() dispenser.clean_and_exit() return statement('dispensing {} from Pestle Home'.format(status)) else: return statement('Sorry {} is not possible Right now'.format(status))
class MainApp(App): config = AppConfig() dispenser = Dispenser(0x04) #drinks = [{'img': './images/shark_cat.jpg', 'name': 'Shark Cat'}, {'img': './images/Batman.jpg', 'name': 'Batman!'}, {'img': './images/squirtle.jpg', 'name': 'Squirtle'}] def __init__(self): self.setup_config() self.setup_repository() self.setup_dispenser() self.setup_encoder() self.drinks = self.repository.getAvailableDrinks() super().__init__() def setup_config(self): self.config.read(curpath + '/config.ini') def setup_repository(self): db_path = self.config['Database'].get('Path', 'data.db') if not os.path.isabs(db_path): db_path = os.path.join(curpath, db_path) self.repository = DrinkRepository(db_path) def setup_dispenser(self): addr = int(self.config['Dispenser'].get('Address', '0x00'), 16) single = int(self.config['Hardware'].get('SwitchSingle')) double = int(self.config['Hardware'].get('SwitchDouble')) self.dispenser = Dispenser(addr, spin=single, dpin=double) def setup_encoder(self): clk = int(self.config['Hardware'].get('RotaryClk')) dt = int(self.config['Hardware'].get('RotaryDt')) btn = int(self.config['Hardware'].get('SelectBtn')) r = int(self.config['Hardware'].get('rotaryRLED')) g = int(self.config['Hardware'].get('rotaryGLED')) b = int(self.config['Hardware'].get('rotaryBLED')) self.encoder = EncoderInput(clk, dt, btn, r, g, b) def build(self): self.screen = MainScreen(self.drinks) Window.bind(on_keyboard=self.check_hotkey) self.encoder.setupEncoderEvents(lambda dir: self.select_next(dir), self.dispense_current) return self.screen def check_hotkey(self, window, key, scancode, codepoint, modifier): # yay thanks https://stackoverflow.com/a/47922465/2993366 if modifier == ['shift'] and codepoint.lower() == 'q': self.stop() elif modifier == [] and codepoint.lower() == 'enter': self.dispense_current() def allow_selection(self): self.screen.dismiss_dispensing_modal() self.encoder.enableInput() # remove dispensing popup def prevent_selection(self): self.screen.open_dispensing_modal() self.encoder.disableInput() # show dispensing popup def select_next(self, dir): self.screen.next_widget(dir, callback=lambda: self.highlight_current()) def highlight_current(self): drink_id = self.screen.get_current_drink().drink_id recipe = self.repository.getDrinkRecipe(drink_id) # self.dispenser.highlightDrink(recipe) def dispense_current(self): drink_id = self.screen.get_current_drink().drink_id if (drink_id < 0): self.screen.set_random() drink_id = self.screen.get_current_drink().drink_id self.dispense_current() else: self.dispense_drink(drink_id) def dispense_drink(self, drink_id): print(drink_id) recipe = self.repository.getDrinkRecipe(drink_id) self.prevent_selection() self.dispenser.dispenseDrink(recipe, self.allow_selection)
amount (str): The amount of the spice to dispense in 1/100 grams. Must be able to cast to an int. """ amount_in_grams = int(amount) / 100 slot_id = int(slot_id) dispenser.dispense(slot_id, amount_in_grams) return json({'data': {'slot_id': slot_id, 'amount': amount_in_grams} }) @app.route("/") async def test(request): # This route could be useful for debugging by displaying all information as a web page. return json({"hello": "world"}) if __name__ == "__main__": if os.uname()[1] == 'pi': dispenser = Dispenser() else: # This is a test running on a desktop dispenser = TestDispenser() myGui = Gui if myGui.ready_to_dispense: dispenser.dispense(myGui.get_slot_id, myGui.get_amount_in_grams) myGui.dispensing_flag = False atexit.register(lambda: dispenser.clean_and_exit()) app.run(host="0.0.0.0", port=8000)
from dispenser import TestDispenser, Dispenser import os if __name__ == "__main__": if 1: dispenser = Dispenser() else: dispenser = TestDispenser() dispenser.dispense(1, 100) dispenser.clean_and_exit()
def command_callback(command): if command.command == 'connect' and not dispenser.running: print 'Connecting Beverage ' + str(beverage) + '...' dispenser.connect_device() elif command.command == 'disconnect' and not dispenser.disconnect: print 'Disconnecting Beverage ' + str(beverage) + '...' dispenser.disconnect_device() try: options = ibmiotf.device.ParseConfigFile('config/bluemix/bev' + str(beverage) + '.cfg') client = ibmiotf.device.Client(options) client.connect() client.commandCallback = command_callback dispenser = Dispenser(client, beverage - 1, BEV_PIN[beverage - 1]) while True: if dispenser.running: dispenser.loop() except ibmiotf.ConnectionException as e: print e except KeyboardInterrupt: dispenser.cleanup() if client: client.disconnect() finally: GPIO.cleanup()
@app.route('/api/dispense/<slot_id>/<amount>') async def dispense(slot_id = 0, amount = 0): """Dispense a certain amount of a certain spice. Args: slot_id (str): The ID of the slot to dispense from. amount (str): The amount of the spice to dispense in 1/100 grams. Must be able to cast to an int. """ amount_in_grams = int(amount) / 100 slot_id = int(slot_id) dispenser.dispense(slot_id, amount_in_grams) #return json({'data': {'slot_id': slot_id,'amount': amount_in_grams}}) return '' @app.route('/api/<amount>') async def test(amount = 0): print("testing") # This route could be useful for debugging by displaying all information as a web page. #return html(html_data) return '' if __name__ == "__main__": if os.uname()[1] == 'pestle-spice-dispenser': dispenser = Dispenser() else: # This is a test running on a desktop dispenser = TestDispenser() atexit.register(lambda: dispenser.clean_and_exit()) app.run(host="0.0.0.0", port=8000)
line = line.split(separator) return_dict[line[0]] = line[1], line[2], line[3] return return_dict def userInput(diction, index): index = str(index) x = int(diction[index][0]) y = int(diction[index][1]) amount = int(diction[index][2]) return x, y, amount if __name__ == "__main__": dispenser = Dispenser() #dispenser = TestDispenser() dispenser.calibrate_track() dispenser.calibrate_scale(100) command = text_parser("input.txt") x, y, amount = userInput(command, 1) while ((x != 3) & (y != 3)): for i in range(1, len(command)): dispenser.move(x, y) dispenser.dispense(amount) dispenser.calibrate_track() dispenser.calibrate_scale(100) x, y, amount = userInput(command, i + 1)
class Conveyor_FSM(Machine): # states and transitions def __init__(self): self.running = True self.isEnabled = False self.isEjectEnabled = False self.isDispenseEnabled = False self.isOK = False # break beam sensor status self.break_beam_sensor = False # conveyor counter reset self.idxcount = 0 #setup break beam sensor GPIO.setmode(GPIO.BCM) # GPIO 24 set up as input, pulled up to avoid false detection. GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) states = ['init', 'ready', 'start', 'stop', 'dispense', 'eject'] Machine.__init__(self, states=states, initial='init') self.add_transition('trigger', 'init', 'ready', conditions='is_enabled') self.add_transition('trigger', 'ready', 'start', conditions='is_hw_ok') self.add_transition('trigger', 'start', 'stop', conditions='is_beam_broken') self.add_transition('trigger', 'stop', 'dispense', conditions='is_dispense_ready') self.add_transition('trigger', 'dispense', 'eject') self.add_transition('trigger', 'eject', 'ready') # external transition control from GUI def shutdown(self): self.running = False def enable(self): self.isEnabled = True def disable(self): self.isEnabled = False def enable_eject(self): self.isEjectEnabled = True def disable_eject(self): self.isEjectEnabled = False def enable_dispense(self): self.isDispenseEnabled = True def disable_dispense(self): self.isDispenseEnabled = False def get_dispenser_status(self): return self.dispenserUSB0.request_status( connect_to_serial_port(list_of_serial_ports())) def get_break_beam_sensor_status(self): return self.break_beam_sensor, self.idxcount #what to do when you enter a state # define hardware objects at start def on_enter_init(self): conveyor.stop() #TODO : better way to handle when hardware is not there or working if self.isDispenseEnabled: self.dispenserUSB0 = Dispenser( connect_to_serial_port('/dev/ttyUSB0')) if self.isEjectEnabled: # create an instance of the ejector class try: self.ejector1 = Ejector("Bin-1", 0x40, 1) except IOError: print "no Ejector found" def on_enter_ready(self): self.isOK = True def on_enter_start(self): conveyor.fwd() #sleep(.2) def on_enter_stop(self): conveyor.stop() def on_enter_dispense(self): # conveyor tracking- indexing with a dummy card if self.isDispenseEnabled: self.dispenserUSB0.dispense( connect_to_serial_port(list_of_serial_ports())) q.index(Mcard('Dummy')) sleep(1) #min time else: # no card q.index(None) sleep( 1 ) #slow down to not index too many times, wont work without it def on_enter_eject(self): if self.isEjectEnabled: #first bin!!! if q.get_name(13) is None: pass else: self.ejector1.eject(150, 400) q.update(13, None) else: sleep(1) #slow down for testing #transition conditions def is_enabled(self): return self.isEnabled def is_beam_broken(self): self.break_beam_sensor = False #print "Waiting for falling edge on port 24" GPIO.wait_for_edge(24, GPIO.FALLING) self.idxcount += 1 conveyor.stop() #TODO maybe faster when stpped right here ? self.break_beam_sensor = True return True #return not wiringpi.digitalRead(24) #212break beam sensor def is_hw_ok(self): #TODO use for something, check HW status ? return self.isOK def is_dispense_ready(self): if self.isDispenseEnabled: try: status = self.dispenserUSB0.is_ready( connect_to_serial_port(list_of_serial_ports())) except AttributeError: status = False # remains in STOP state until dispenser disabled else: return status else: return True # bypass