def __init__(self, server): self.tablet = Tablet(server) # Catch SIGINT/SIGTERM for cleanup purposes to stop threads signal(SIGINT, self.cleanup) signal(SIGTERM, self.cleanup) # The Redis channels on which the tablet can receive commands channels = [ 'tablet_control', 'tablet_audio', 'tablet_image', 'tablet_video', 'tablet_web', ] # Create the consumer on those channels self.redis = redis.Redis(host=server, ssl=True, ssl_ca_certs='../cert.pem') self.pubsub = self.redis.pubsub(ignore_subscribe_messages=True) self.pubsub.subscribe(**dict.fromkeys(channels, self.execute)) self.pubsub_thread = self.pubsub.run_in_thread(sleep_time=0.001) # Set the base URI for web pages self.webcontent_uri = 'https://' + server + ':8000/index.html' self.running = True
def main(): instructionStrings = [] line = f.readline() while line: instructionStrings.append(line.rstrip()) line = f.readline() t = Tablet(instructionStrings) t.run() print(t.getFirstRecoveredFrequency())
def __init__(self, server, redis_port, server_port): print 'Redis: {}:{}'.format(server, redis_port) print 'Web: {}:{}'.format(server, server_port) # The HTTP server running on the laptop ('server.py') self.server = server self.server_port = str(server_port) self.tablet = Tablet(self.server, self.server_port) # Catch SIGINT/SIGTERM for cleanup purposes to stop threads signal.signal(signal.SIGINT, self._exit_gracefully) signal.signal(signal.SIGTERM, self._exit_gracefully) # The Redis channels on which the tablet can receive commands channels = [ 'tablet_control', # pages 'tablet_image', 'tablet_video', 'tablet_web', 'tablet_audio', 'tablet_question_yn', 'tablet_question_rate', 'tablet_text', 'tablet_caption', 'tablet_overlay', 'tablet_input_name', 'tablet_input_date', 'tablet_input_numbers', 'tablet_input_text', 'tablet_input_multiple', 'tablet_stream', # control 'audio_language', 'tablet_background', 'tablet_config' ] print channels # Create the consumer on those channels self.redis = redis.Redis(host=server, port=redis_port) self.pubsub = self.redis.pubsub(ignore_subscribe_messages=True) self.pubsub.subscribe(*channels) # Set the base URI for web pages self.webcontent_uri = 'http://' + server + ':' + str( server_port) + '/index.html?' # Initialize variables self.background = '' self.components = [] self.language = 'en-US'
class TabletConsumer(object): """Receives commands from Redis and executes them on the tablet""" def __init__(self, server): self.tablet = Tablet(server) # Catch SIGINT/SIGTERM for cleanup purposes to stop threads signal(SIGINT, self.cleanup) signal(SIGTERM, self.cleanup) # The Redis channels on which the tablet can receive commands channels = [ 'tablet_control', 'tablet_audio', 'tablet_image', 'tablet_video', 'tablet_web', ] # Create the consumer on those channels self.redis = redis.Redis(host=server, ssl=True, ssl_ca_certs='../cert.pem') self.pubsub = self.redis.pubsub(ignore_subscribe_messages=True) self.pubsub.subscribe(**dict.fromkeys(channels, self.execute)) self.pubsub_thread = self.pubsub.run_in_thread(sleep_time=0.001) # Set the base URI for web pages self.webcontent_uri = 'https://' + server + ':8000/index.html' self.running = True def tablet_control(self, command): """Misc commands to control the tablet""" if command == 'hide': self.tablet.hide() elif command == 'show': self.tablet.open_url(self.webcontent_uri) elif command == 'reload': self.tablet.reload() elif command == 'settings': self.tablet.settings() elif command.startswith('volume'): # Convert the percentage to a float between 0 and 1 # The command sent to the channel is e.g. "volume 50" value = float(command.split(' ')[1]) / 100 print 'setting volume to {}'.format(value) try: self.tablet.set_volume(value) except ValueError, exception: print 'error: ', exception.message else:
def test_how_many_times_sent(): instructionStrings = ([ 'snd 1', 'snd 2', 'snd p', 'rcv a', 'rcv b', 'rcv c', 'rcv d', ]) q0 = queue.Queue() q1 = queue.Queue() t0 = Tablet(instructionStrings, 0, q0, q1) t1 = Tablet(instructionStrings, 1, q1, q0) isDeadlock = False while not isDeadlock: processedCount0 = t0.run() processedCount1 = t1.run() if processedCount0 == 0 and processedCount1 == 0: isDeadlock = True assert t1.getTimesSent() == 3
def load_modules(self): self._speech = Speech(self.session) self._motion = Motion(self.session) self._tablet = Tablet(self.session) self._face_detection = FaceDetection(self.session, self) self._wave_detection = WavingDetection(self.session) self._audio_player = AudioPlayer(self.session) self._speech_recognition = SpeechRecognition(self.session) self._system = System(self.session)
def __init__(self, parameters, log, postgres): self.parameters = parameters self.log = log self.postgres = postgres self.sleeping = False self.speaking_semaphore = Semaphore(0) self.looking_semaphore = Semaphore(0) self.listening_semaphore = Semaphore(0) self.listening_looking_semaphore = Semaphore(0) self.current_context = None self.current_keyboard_search = '' self.current_search_found = '' self.try_listen = False self.xplain = Xplain(self.postgres) self.topics = {} self.load_topics() CoronaMonitor(self).clean_floor_occupations() self.tablet = Tablet(self) self.sic = SIC(self, parameters)
class Agent: def __init__(self, parameters, log, postgres): self.parameters = parameters self.log = log self.postgres = postgres self.sleeping = False self.speaking_semaphore = Semaphore(0) self.looking_semaphore = Semaphore(0) self.listening_semaphore = Semaphore(0) self.listening_looking_semaphore = Semaphore(0) self.current_context = None self.current_keyboard_search = '' self.current_search_found = '' self.try_listen = False self.xplain = Xplain(self.postgres) self.topics = {} self.load_topics() CoronaMonitor(self).clean_floor_occupations() self.tablet = Tablet(self) self.sic = SIC(self, parameters) def life_loop(self): self.search_subject() if self.xplain.is_belief( 'has_subject') and not self.xplain.is_belief('user_ready'): self.say_and_wait(belief_type='user_ready', say_text=self.get_sentence( 'corona', 'hi_sanitize'), unexpected_answer_params=[ self.xplain.belief_params('speech_text') ], timeout=20) if not self.xplain.is_belief( 'disclaimer_visible') and self.xplain.is_belief('has_subject'): self.sic.tablet_show( self.tablet.get_body(extras_type='disclaimer')) self.say_and_wait(belief_type='disclaimer_visible', say_text=self.get_sentence( 'general', 'disclaimer_ask'), unexpected_answer_params=[ self.xplain.belief_params('speech_text') ], timeout=self.parameters['timeout_listening']) if self.xplain.is_belief('disclaimer_visible'): if self.xplain.belief_params('disclaimer_visible') == 'no' \ and not self.xplain.is_belief('disclaimer_given'): self.sic.tablet_show(self.tablet.get_body(dialog='')) self.say(self.get_sentence('general', 'disclaimer_content'), extra_text=True) else: CoronaMonitor(self).act() self.xplain.adopt('disclaimer_given', 'cognition') self.help() def search_subject(self): # if it doesnt believe to have a subject, keeps searching for it if not (self.xplain.is_belief('has_subject')): print('\n> searching subject') self.sic.tablet_show(self.tablet.get_body(reset=True)) self.listen_and_look('proactive_subject', self.parameters['timeout_watchlook']) self.xplain.drop('speech_text') self.xplain.drop('input.unknown') def offer_help(self, greetings): self.tablet.extras_type = None if self.xplain.is_belief( 'type_of_help') and not self.xplain.is_belief('helping'): print('\n> offering more help') self.tablet.reset_extras() self.xplain.drop('type_of_help') self.offer_help_flavors( self.get_sentence('general', 'offer_more_help')) if not self.xplain.is_belief('type_of_help'): print('\n> offering help') self.offer_help_flavors( self.get_sentence('general', 'offer_help', greetings)) def offer_help_flavors(self, say_text): self.say_and_wait(belief_type='type_of_help', say_text=say_text, unexpected_answer_params=[ self.xplain.belief_params('speech_text') ], timeout=self.parameters['timeout_listening']) def help(self): if self.xplain.is_belief('type_of_help'): print('\n> trying to help') if self.xplain.belief_params('type_of_help') == 'find employee': FindEmployee(self).act() elif self.xplain.belief_params('type_of_help') == 'entertainment': Entertainment(self).act() # when offer is rejected, agent abandons the subject elif self.xplain.belief_params('type_of_help') == 'nothing': self.say(self.get_sentence('general', 'rejection_taken')) self.give_up() # wait a bit for subject to leave sleep(self.parameters['rejection_tryagain']) # say something and wait for a response; includes fallback; def say_and_wait(self, belief_type, say_text, no_answer_topic='general', no_answer_subtopic='no_answer', no_answer_params=None, unexpected_answer_topic='general', unexpected_answer_subtopic='unexpected_answer', unexpected_answer_params=None, timeout=None): self.try_listen = True # no answer received if self.xplain.is_belief('waiting_answer') and \ not (self.xplain.is_belief(belief_type)) and \ not (self.xplain.is_belief('input.unknown')): # after X attempts, assumes subject is gone attempts = int(self.xplain.belief_params('contact_attempt')[-1]) if attempts < self.parameters['contact_attempts']: self.say( self.get_sentence(no_answer_topic, no_answer_subtopic, no_answer_params)) self.xplain.increment('contact_attempt', str(attempts + 1)) else: self.say(self.get_sentence('general', 'no_answer_limit')) self.try_listen = False self.give_up() # answer is unexpected elif self.xplain.is_belief('input.unknown'): self.xplain.drop('contact_attempt') self.xplain.adopt('contact_attempt', 'action', '1') if self.postgres.check_badwords( self.xplain.belief_params('speech_text')): sentence = self.get_sentence('general', 'badwords') else: sentence = self.get_sentence(unexpected_answer_topic, unexpected_answer_subtopic, unexpected_answer_params) self.say(sentence) self.xplain.drop('input.unknown') self.xplain.drop('speech_text') # say it for the first time elif self.xplain.is_belief('has_subject') and not ( self.xplain.is_belief('waiting_answer')): self.xplain.adopt('contact_attempt', 'action', '1') self.current_context = belief_type if belief_type in self.xplain.get_intents_entities(): self.sic.tablet_show( self.tablet.get_body(buttons=self.xplain. get_intents_entities()[belief_type])) self.say(say_text) if self.try_listen: self.xplain.adopt('waiting_answer', 'action') if self.try_listen: self.listen(belief_type, timeout) def say(self, text, say_animated=True, extra_text=False): self.xplain.adopt('speaking', 'action', text) if not extra_text: self.sic.tablet_show(self.tablet.get_body(dialog=text)) print('Say: ', text) text = "\\rspd=125\\" + text if say_animated: self.sic.say_animated(text) else: self.sic.say(text) self.speaking_semaphore.acquire() self.xplain.drop('speaking') def listen(self, context='', timeout=None): self.xplain.adopt('listening', 'action') self.sic.set_dialogflow_context(context) self.sic.start_listening(0) print('listenting...') if timeout is not None: self.listening_semaphore.acquire(timeout=timeout) else: self.listening_semaphore.acquire() self.sic.stop_listening() print('stop listenting...') self.xplain.drop('listening') # 1 second additional wait to give dialogflow some time to return a result after closing the audio stream. sleep(1) def listen_and_look(self, context='', timeout=None): self.xplain.adopt('listening_looking', 'action') self.sic.set_dialogflow_context(context) self.sic.start_listening(0) self.sic.start_looking(0) print('listenting and looking...') if timeout is not None: self.listening_looking_semaphore.acquire(timeout=timeout) else: self.listening_looking_semaphore.acquire() self.sic.stop_listening() self.sic.stop_looking() print('stoped listenting and looking...') self.xplain.drop('listening_looking') # 1 second additional wait to give dialogflow some time to return a result after closing the audio stream. sleep(1) def look(self, timeout=None): self.xplain.adopt('looking', 'action') self.sic.start_looking() if timeout is not None: self.look_semaphore.acquire(timeout=timeout) else: self.look_semaphore.acquire() self.sic.stop_looking() self.xplain.drop('looking') def has_subject(self): self.xplain.adopt('has_subject', 'cognition') if self.xplain.is_belief('listening_looking'): self.listening_looking_semaphore.release() def load_topics(self): topics = os.listdir('topics') for topic in topics: topic = topic.split('.')[0] self.topics[topic] = {} for topic in self.topics: file = open('topics/{}.txt'.format(topic), 'r') lines = file.readlines() sentence_name = 0 sentence_variation = 1 sentence_text = 2 previous_name = '' for line in lines: line = line.strip().split('|') if line[sentence_name] != previous_name: self.topics[topic][line[sentence_name]] = [] previous_name = line[sentence_name] self.topics[topic][line[sentence_name]].append( line[sentence_text]) file.close() def get_sentence(self, topic, sentence_name, params=None): text = random.choice(self.topics[topic][sentence_name]) if params is not None: text = text.format(*params) return text def clear_answer_beliefs(self): self.xplain.drop('input.unknown') self.xplain.drop('waiting_answer') self.xplain.drop('speech_text') self.xplain.drop('contact_attempt') def drop_perception_of_subject(self): self.xplain.drop('proactive_subject') self.xplain.drop('seen_subject') self.xplain.drop('subject_touched') def load_magic_beliefs(self, magic_beliefs): for belief in magic_beliefs: self.xplain.adopt(belief, magic_beliefs[belief][0], magic_beliefs[belief][1]) # says goodbye, drops any active beliefs, stops SIC, and breaks out the life loop def dropall_and_sleep(self): self.sic.stop_listening() self.sic.say(self.get_sentence('general', 'sleep_order_taken')) self.xplain.adopt('abandon_and_sleep', 'action') self.xplain.dropall() self.sic.stop() # TODO: add cleaning of face encodings here self.postgres.close() self.sleeping = True sys.exit() def try_get_input_again(self, input_belief): self.xplain.drop(input_belief) self.xplain.drop('speech_text') self.xplain.adopt('input.unknown', 'cognition') self.xplain.adopt('waiting_answer', 'action') def give_up(self): self.sic.tablet_show(self.tablet.get_body(reset=True)) self.xplain.dropall()
def main(): """ Simple Pygame to show off tablet pressure sensitivity. """ screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Pressure Test") background = pygame.surface.Surface(screen.get_size()) background.fill(pygame.Color("Black")) background = background.convert() screen.blit(background, (0, 0)) overlay = pygame.surface.Surface(screen.get_size(), flags=pygame.SRCALPHA, depth=32) overlay.convert_alpha() overlay.fill((0, 0, 0, 0)) # Create our tablet object: tablet = Tablet(screen) count = 0 looping = True while looping: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: # allow for exit: looping = False overlay.fill((0, 0, 0, 0)) # Get our tablet data, and draw: tabletData = tablet.getData() # Debug print: # print tabletData button, x, y, pressure = tabletData if button == 1: #ratio = x/y count = count + 1 print count f1 = open(filename, 'a+') p = [] p = str(pressure) x_plot = [] x_plot = str(x) y_plot = [] y_plot = str(y) c = [] c = str(count) # f1.writerow([c, x_plot, y_plot]) ## f1.write(c + ',' + x_plot + ',' + y_plot + ',' + p + ',,,' + '\n') #Turn our pressure into a reasonabliy sized radius value: radius = 5 # If user is pressing the LMB, draw: if button == 1 and pressure <= 256: pygame.draw.circle(background, pygame.Color("green"), (x, y), radius) elif button == 1 and pressure > 256 and pressure <= 512: pygame.draw.circle(background, pygame.Color("blue"), (x, y), radius) elif button == 1 and pressure > 512 and pressure <= 1023: pygame.draw.circle(background, pygame.Color("red"), (x, y), radius) # Draw our mouse pointer representation: pygame.draw.circle(overlay, pygame.Color("orange"), (x, y), 5) screen.blit(background, (0, 0)) screen.blit(overlay, (0, 0)) pygame.display.flip() pygame.quit()
class TabletConsumer(object): """Receives commands from Redis and executes them on the tablet""" def __init__(self, server, redis_port, server_port): print 'Redis: {}:{}'.format(server, redis_port) print 'Web: {}:{}'.format(server, server_port) # The HTTP server running on the laptop ('server.py') self.server = server self.server_port = str(server_port) self.tablet = Tablet(self.server, self.server_port) # Catch SIGINT/SIGTERM for cleanup purposes to stop threads signal.signal(signal.SIGINT, self._exit_gracefully) signal.signal(signal.SIGTERM, self._exit_gracefully) # The Redis channels on which the tablet can receive commands channels = [ 'tablet_control', # pages 'tablet_image', 'tablet_video', 'tablet_web', 'tablet_audio', 'tablet_question_yn', 'tablet_question_rate', 'tablet_text', 'tablet_caption', 'tablet_overlay', 'tablet_input_name', 'tablet_input_date', 'tablet_input_numbers', 'tablet_input_text', 'tablet_input_multiple', 'tablet_stream', # control 'audio_language', 'tablet_background', 'tablet_config' ] print channels # Create the consumer on those channels self.redis = redis.Redis(host=server, port=redis_port) self.pubsub = self.redis.pubsub(ignore_subscribe_messages=True) self.pubsub.subscribe(*channels) # Set the base URI for web pages self.webcontent_uri = 'http://' + server + ':' + str( server_port) + '/index.html?' # Initialize variables self.background = '' self.components = [] self.language = 'en-US' # Handler should technically also have signum and frame as parameters, but we don't use that def _exit_gracefully(self, *_): print 'Exiting gracefully (ignore the runtime error from pubsub)' self.tablet.stop_audio() self.pubsub.close() def _build_url(self, channel, parameters): parameters['bg'] = urlquote(self.background) parameters['components'] = urlquote(dumps(self.components)) parameters['lang'] = urlquote(self.language) param_string = '&'.join( [str(k) + '=' + str(v) for (k, v) in parameters.items()]) query_string = 'view={}&{}'.format(channel, param_string) return self.webcontent_uri + query_string def update(self): """Get a message and execute it""" msg = self.pubsub.get_message() if msg is not None: self.execute(msg['channel'], msg['data']) else: time.sleep(0) def tablet_control(self, command): """Misc commands to control the tablet""" if command == 'hide': self.tablet.hide() elif command == 'show': self.tablet.open_url() elif command == 'reload': self.tablet.reload() elif command == 'settings': self.tablet.settings() elif command.startswith('volume'): # Convert the percentage to a float between 0 and 1 # The command sent to the channel is e.g. "volume 50" value = float(command.split(' ')[1]) / 100 print 'setting volume to {}'.format(value) try: self.tablet.set_volume(value) except ValueError, exception: print 'error: ', exception.message else: