def run(self, steps_taken, angle=None): direction = self.get_stabilized_bearing() update_direction = angle if angle is not None else direction if direction == -1: if (time.time() - self.start) > 5: self.start = time.time() print("[LOCALIZATION] Not receiving compass readings") tts("ERROR, Check connection with Arduino... Reset when ready") if direction >= 0: # Update stabilized bearing if time.time() - self.start > 5: self.start = time.time() self.stabilized_bearing = direction if self.new_data: # Update incoming data self.process_new_data() if steps_taken > 0 and direction > 0: print("[LOCALIZATION] %d steps taken in %.2f" % (steps_taken, update_direction)) # Update x, y self.calculate_new_position(steps_taken, direction=update_direction) if self.is_plot: # Update plot if (time.time() - self.start) > 1/self.FPS: start = time.time() self.plot(self.linex, self.rotate_x) self.plot(self.liney, self.rotate_y) self.plot(self.linez, self.rotate_z) self.plot(self.lineh[0], self.heading) self.fig.canvas.draw()
def timeout_handler(): global app try: os.kill(int(app.serial_pid), signal.SIGUSR1) print("[MAIN] Killed serial process %s" % app.serial_pid) tts("Error...timeout on serial data. Check Arduino connection and reset when ready") except Exception as e: pass # Process may have terminated already connect_picomms(platform=app.platform_)
def main(): MATRIX = { 18: { 4: False, 17: False, 22: False }, 23: { 4: False, 17: False, 22: False }, 24: { 4: False, 17: False, 22: False }, 25: { 4: False, 17: False, 22: False } } setup() tts("Software ready! Please enter building number") try: while (True): for j in COL: GPIO.output(j, 0) for i in ROW: if (GPIO.input(i) == 0): if not MATRIX[i][j]: MATRIX[i][j] = True time.sleep(0.1) key = KEY((i, j)) handler(key) while (GPIO.input(i) == 0): pass #else: # Key is already pressed # pass else: MATRIX[i][j] = False pass GPIO.output(j, 1) except KeyboardInterrupt as e: GPIO.cleanup() except Exception as e: print e
def passing_var(audio_input, passing): if audio_input in keyword_list and passing == 0: programs.tts(response) if arg == '-t': command(input("> ").lower()) else: reg_input = audio.record(command_seconds, 1) if reg_input is not None and reg_input != False: passing_var(reg_input, 1) elif reg_input == "invalid input": audio.tts(reg_input) elif audio_input in running_list and passing == 0: command(audio_input) elif audio_input not in keyword_list and passing == 0: listen() elif passing == 1: command(audio_input)
def say_(self, cmd): """ Say command """ result = eval(cmd, globals(), self.slots) MSG = "{}: {}".format(self.name, result) print(MSG) if self.client: data = { 'msg': result, 'spk': self.name, 'webclient_sid': self.webclient_sid } self.client.emit('say', data, namespace="/cv") if self.tts: stop_listening() tts(result) start_listening() else: pass
def reply_msg_by_content_type(self, msg): # print msg user_id = msg['user']['id'] content_type = msg['content']['type'] msg_type_id = msg['msg_type_id'] msg_id = msg['msg_id'] data = '' scr_name = '' if msg_type_id == 0 and content_type == 11: return try: scr_name = msg['content']['user']['name'] except Exception: try: scr_name = msg['user']['name'] except Exception: pass if content_type == 0 and scr_name == 'self': return has_scr_name = False if scr_name != 'unknown' and scr_name != '': scr_name = '@' + scr_name + "\n" has_scr_name = True is_at_me = True if msg_type_id == 3: is_at_me = False if 'detail' in msg['content']: my_names = self.get_group_member_name(msg['user']['id'], self.my_account['UserName']) if my_names is None: my_names = {} if 'NickName' in self.my_account and self.my_account['NickName']: my_names['nickname2'] = self.my_account['NickName'] if 'RemarkName' in self.my_account and self.my_account['RemarkName']: my_names['remark_name2'] = self.my_account['RemarkName'] is_at_me = False for detail in msg['content']['detail']: if detail['type'] == 'at': for k in my_names: # print detail['value'] if my_names[k] and my_names[k] == detail['value']: is_at_me = True break try: data = msg['content']['desc'] except Exception: data = msg['content']['data'] if content_type == 0: self.auto_switch(msg) if self.robot_switch != True: return videos_key = [u'视频', u'mp4'] for key in videos_key: if key in data: self.send_file(user_id) return girls_key = [u'美女', u'girl', u'来张', u'来个'] for key in girls_key: if key in data: self.send_girl(user_id) return songs_key = [u'音乐', u'mp3', u'歌曲'] for key in songs_key: if key in data: data = data.replace(key, '') self.send_song(data, user_id) return apks_key = [u'app', u'apk', u'下载'] for key in apks_key: if key in data: data = data.replace(key, '') self.send_apk(data, user_id) return films_key = [u'电影', u'种子', u'迅雷', u'磁力'] for key in films_key: if key in data: data = data.replace(key, '') self.send_film(data, user_id) return if is_at_me: # print data reply = self.tuling_auto_reply(user_id, data) if has_scr_name: reply = scr_name + reply self.send_msg_by_uid(reply.encode('utf-8'), user_id) elif content_type == 4: return pcm = mp3topcm(os.path.join(self.temp_pwd, self.get_voice(msg_id))) tts_data = tts('1d2678900f734aa0a23734ace8aec5b1', pcm) try: msg['content']['data'] = tts_data except Exception: tts_data = u"无法识别" # print u'[语音识别:]'+ tts_data self.auto_switch(msg) if self.robot_switch != True: return if scr_name == 'unknown' or scr_name != '': scr_name = '' reply = u'语音识别:' + tts_data + '\n\n' + scr_name reply += self.tuling_auto_reply(user_id, tts_data) # print reply self.send_msg_by_uid(reply, user_id); elif content_type == 3: self.auto_switch(msg) if self.robot_switch != True: return self.send_img(user_id) elif content_type == 11 or content_type == 6: self.auto_switch(msg) if self.robot_switch != True: return self.send_img(user_id) elif content_type == 7: self.auto_switch(msg) if self.robot_switch != True: return self.send_msg_by_uid(self.tuling_auto_reply(user_id, u'已经转帐给你'), user_id); elif content_type == 13: self.auto_switch(msg) if self.robot_switch != True: return self.send_file(user_id) else: self.auto_switch(msg) if self.robot_switch != True: return
def action_on_transit(val, action): """ Do something upon transition to next state ONCE""" global send # print("Action : "), # print(action) if action is Action.APPEND: tts(AFFIRMS[action], (val, )) send += str(val) print(send) elif action is Action.CLEAR: tts(AFFIRMS[action]) print(send) clear_send() elif action is Action.DOWNLOAD_MAP: tts(AFFIRMS[action]) print(send) os.write(pipe_out, "*" + "\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.CONFIRM_START_BUILDING: tts(AFFIRMS[action], (send, )) print(send) os.write(pipe_out, send + "\n") os.kill(int(pid), signal.SIGUSR2) clear_send() elif action is Action.CONFIRM_START_LEVEL: tts(AFFIRMS[action], (send, )) print(send) os.write(pipe_out, send + "\n") os.kill(int(pid), signal.SIGUSR2) clear_send() elif action is Action.CONFIRM_START: print(send) tts(AFFIRMS[action], (send, )) os.write(pipe_out, send + "\n") os.kill(int(pid), signal.SIGUSR2) clear_send() elif action is Action.CONFIRM_END_BUILDING: tts(AFFIRMS[action], (send, )) print(send) os.write(pipe_out, send + "\n") os.kill(int(pid), signal.SIGUSR2) clear_send() elif action is Action.CONFIRM_END_LEVEL: tts(AFFIRMS[action], (send, )) print(send) os.write(pipe_out, send + "\n") os.kill(int(pid), signal.SIGUSR2) clear_send() elif action is Action.CONFIRM_END: tts(AFFIRMS[action], (send, )) print(send) os.write(pipe_out, send + "\n") os.kill(int(pid), signal.SIGUSR2) clear_send() elif action is Action.INCR: print(send) os.write(pipe_out, "++\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.DECR: print(send) os.write(pipe_out, "--\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.PLAY_MUSIC: print(send) args = shlex.split("omxplayer --vol -2000 good_life_remix.mp3") process = subprocess.Popen(args) elif action is Action.QUIT: print(send) os.write(pipe_out, "q\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.NULL: pass elif action is Action.NAV: os.write(pipe_out, "NAVIGATE\n") os.kill(int(pid), signal.SIGUSR2) pass elif action is Action.START: tts(AFFIRMS[action]) pass elif action is Action.GET_INSTR: os.write(pipe_out, "GET_INSTR\n") os.kill(int(pid), signal.SIGUSR2) pass elif action is Action.GET_PREV: os.write(pipe_out, "GET_PREV\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.STEP_ON: tts(AFFIRMS[action]) os.write(pipe_out, "STEP_ON\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.STEP_OFF: tts(AFFIRMS[action]) os.write(pipe_out, "STEP_OFF\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.REACHED: tts(AFFIRMS[action]) os.write(pipe_out, "USER_CHECKPOINT_REACHED\n") os.kill(int(pid), signal.SIGUSR2) elif action is Action.CONFIRM_RESET: tts(AFFIRMS[action]) pass elif action is Action.CANCEL_RESET: tts(AFFIRMS[action]) pass else: raise Exception("Unrecognized action!")
def issue_instruction(self): """ Only issue instruction after interval""" tts(self.instruction) self.instruction = ""
def run_once_on_transition(self, userinput): """ Run once upon transition to new state""" print("[MAIN] TRANSITION : " + str(self.state)) if self.state is State.END: tts("Shutting down.") pass elif self.state is State.ACCEPT_START_BUILDING: # tts("Please enter building") if self.transition is Transitions.KEY_NODE: try: self.start_building = int(userinput) except Exception as e: print e pass elif self.state is State.ACCEPT_START_LEVEL: # tts("Please enter level") if self.transition is Transitions.KEY_NODE: try: self.start_level = int(userinput) self.PathFinder = PathFinder() except ValueError as e: print("[MAIN] Error! Wrong building and level entered") tts("Please enter a valid building and level") except Exception as e: print(e) pass elif self.state is State.ACCEPT_START: # tts("Please enter start node") if self.transition is Transitions.KEY_NODE: try: self.curr_start_node = int(userinput) except Exception as e: print '[MAIN] AEND exception' print e pass elif self.state is State.ACCEPT_END_BUILDING: # tts("Please enter building") if self.transition is Transitions.KEY_NODE: try: self.end_building = int(userinput) except Exception as e: print e pass elif self.state is State.ACCEPT_END_LEVEL: # tts("Please enter level") if self.transition is Transitions.KEY_NODE: try: self.end_level = int(userinput) self.PathFinder = PathFinder() except ValueError as e: print("[MAIN] Error! Wrong building and level entered") tts("Please enter a valid building and level") except Exception as e: print(e) pass elif self.state is State.ACCEPT_END: # tts("Please enter end destination") if self.transition is Transitions.KEY_NODE: try: self.curr_end_node = int(userinput) (self.combined_start_node, self.combined_end_node) = self.combine_node_from_building_and_level() (x, y) = self.PathFinder.get_coordinates_from_node(self.combined_start_node) if x is None and y is None: print("[MAIN] Error! Invalid start node given, please try again") tts("Error, invalid start, please enter again") else: bearing = self.Localization.stabilized_bearing self.PathFinder.update_coordinate(x, y, bearing) self.Localization.update_coordinates(x, y) self.update_steps() self.PathFinder.update_source_and_target(self.combined_start_node, self.combined_end_node) print("[MAIN] Source : %d, Dest: %d" % (self.combined_start_node, self.combined_end_node)) print("[MAIN] Current location: %.2f, %.2f, %.2f" % (self.PathFinder.get_x_coordinate(), self.PathFinder.get_y_coordinate(), self.Localization.stabilized_bearing)) except Exception as e: print e pass pass elif self.state is State.NAVIGATING: print("[MAIN] Transition for State.NAVIGATING") bearing = self.Localization.stabilized_bearing (x, y) = self.PathFinder.get_coordinates_from_node(self.curr_start_node) if self.transition is Transitions.KEY_GET_PREV: tts("Previous " + str(self.PathFinder.get_prev_visited_node())) elif self.transition is Transitions.SW_REACHED_NODE: print("[MAIN] %s triggered " % (str(self.transition))) elif self.transition is Transitions.KEY_REACHED_NODE: # When user press 6 new_coord = self.PathFinder.get_next_coordinates() if new_coord[0] is not None and new_coord[1] is not None: self.Localization.update_coordinates(new_coord[0], new_coord[1]) self.PathFinder.update_coordinate(new_coord[0], new_coord[1], self.Localization.stabilized_bearing) else: print("[MAIN] Error! Invalid new coordinates for reached node") elif self.transition is Transitions.KEY_GET_INSTR: self.get_instruction() elif self.transition is Transitions.KEY_DECR or self.transition is Transitions.KEY_INCR: angle = self.PathFinder.get_angle_to_next_node() self.update_steps(angle) elif self.transition is Transitions.KEY_RESTART: print("[MAIN] Restarting. Press start building and level") pass elif self.transition is Transitions.KEY_STEP_ON: self.is_stepcounter_on = True elif self.transition is Transitions.KEY_STEP_OFF: self.is_stepcounter_on = False else: print("[MAIN] Error unrecognized transition: %s" % str(self.transition)) pass elif self.state is State.REACHED: print("[MAIN] Transition for State.REACHED") # self.curr_reached_node_instr = self.PathFinder.get_audio_reached(self.curr_end_node) if self.transition is Transitions.KEY_GET_PREV: tts("Previous " + str(self.PathFinder.get_prev_visited_node())) elif self.transition is Transitions.KEY_GET_INSTR: self.get_instruction() elif self.transition is Transitions.KEY_DECR or self.transition is Transitions.KEY_INCR: angle = self.PathFinder.get_angle_to_next_node() self.update_steps(angle) elif self.transition is Transitions.KEY_NAV: print("[MAIN] %s triggered " % (str(self.transition))) elif self.transition is Transitions.KEY_RESTART: print("[MAIN] unhandled Transition : %s" % str(self.transition)) pass elif self.transition is Transitions.KEY_SHUTDOWN: print("[MAIN] %s triggered " % str(self.transition)) pass elif self.transition is Transitions.KEY_RESTART: tts("[MAIN] Restarting. Press building and level") elif self.transition is Transitions.KEY_STEP_ON: self.is_stepcounter_on = True elif self.transition is Transitions.KEY_STEP_OFF: self.is_stepcounter_on = False else: print("[MAIN] Error unrecognized transition: %s" % str(self.transition)) pass elif self.state is State.RESET: tts("Resetting. ") self.StepDetector.reset_step() self.Localization.reset() pass else: pass try: self.state = State.transitions[self.state][self.transition] except KeyError as e: print("[MAIN] Ignoring state %s -> transition %s " % (str(self.state), str(self.transition))) print e print("[MAIN] " + str(self.state)) self.transit = False