def chat(): # play_sound(file_path='sayHi.wav') while True: try: record_sound() text = sound2text() print('我 : %s' % text) # monitor(text) reply = tuling(text) print('机器人: %s' % reply) text2sound(reply) play_sound() except: pass
def main(): current_state = 0 previous_state = 0 msg = '机器人上线了....' # 红外感应打开 # on_infrared() # 聊天模式打开 chat(msg) while True: try: # 超声波感应距离 dist = checkdist() print('Distance: %.3f m' % dist) current_state = 1 if dist < 0.5 else 0 if current_state == 1 and previous_state == 0: print('-----------有人----------') door_voice_path = [ './voice/door.wav', './voice/door_sayhi.wav', './voice/door_praise.wav' ] play_sound(file_path=random.choice(door_voice_path)) time.sleep(random.randint(1, 4)) play_sound(file_path='./voice/door_chat.wav') if dist < 0.3: play_sound(file_path='./voice/door_nearest.wav') previous_state = 1 elif current_state == 0 and previous_state == 1: print('------------没人----------') if 3 > dist > 0.: play_sound(file_path='./voice/door_away.wav') previous_state = 0 time.sleep(0.2) except: pass
def ultrasonic_on(self): while True: dist = self.checkdist() print('------------- Distance: %.3f m --------------' % (dist)) if dist < 0.4: self.rgb(red=True) for i in range(0, 3): self.beep(0.25) text2sound('亲,我们靠的太近了,离我有%.3f 公分' % (dist * 100), file_path='temp.wav') play_sound(file_path='temp.wav') time.sleep(1) else: self.rgb(blue=True) time.sleep(0.1)
def on_ultrasonic(): setup() while True: dist = ultrasonic.distance() print('------------- Distance: %.3f m --------------' % (dist)) if dist < 0.4: rgb.setColor(color['Red']) for i in range(0, 3): beep_active.beep(0.25) text2sound('亲,我们靠的太近了,离我有%.3f 公分' % (dist * 100), file_path='temp.wav') play_sound(file_path='temp.wav') time.sleep(1) else: rgb.setColor(color['Blue']) time.sleep(0.1)
def chat(msg): global tulingReply print(msg) while True: try: tulingReply = True onLed = True print('---------Record Begin-------------') record_sound() text = sound2text() print('我 : %s' % text) monitor(text) if onLed == True: rgb2.rgb(green=True) if tulingReply == True: reply = tuling(text) print('机器人: %s' % reply) text2sound(reply) play_sound() except: pass
def sensor(self): current_state = 0 previous_state = 0 while True: try: # 超声波感应距离 dist = self.checkdist() print('Distance: %.3f m' % dist) current_state = 1 if dist < 0.5 else 0 if current_state == 1 and previous_state == 0: print('-----------有人----------') door_voice_path = [ './voice/door.wav', './voice/door_sayhi.wav', './voice/door_praise.wav' ] play_sound(file_path=random.choice(door_voice_path)) time.sleep(random.randint(1, 4)) play_sound(file_path='./voice/door_chat.wav') if dist < 0.3: play_sound(file_path='./voice/door_nearest.wav') previous_state = 1 elif current_state == 0 and previous_state == 1: print('------------没人----------') # if 3 >dist > 0.: # play_sound(file_path='./voice/door_away.wav') previous_state = 0 time.sleep(0.2) except: pass
def chat(self): now = '你好主人,我是一个可爱的机器人,现在是北京时间: ' + time.strftime( '%Y年 %m月 %d日 %H时 %M分 %S秒', time.localtime()) + ",你有什么问题可以问我" text2sound(now) play_sound() while True: try: self.tulingReply = True self.onLed = True print('-Record Begin-') record_sound() text = sound2text() print('我 : %s' % text) self.monitor(text) if self.onLed == True: self.rgb(green=True) if self.tulingReply == True: reply = tuling(text) print('机器:%s' % reply) text2sound(reply) play_sound() except: pass
def sensor(): current_state = 0 previous_state = 0 chat() while True: try: dist = checkdist() print(dist) current_state = 1 if dist < 0.45 else 0 if current_state == 1 and previous_state == 0: print('-----------有人----------') door_voice_path = [ './voice/door.wav', './voice/door_sayhi.wav', './voice/door_praise.wav' ] play_sound(file_path=random.choice(door_voice_path)) previous_state = 1 elif current_state == 0 and previous_state == 1: print('------------没人----------') play_sound(file_path='./voice/door_away.wav') previous_state = 0 time.sleep(0.2) except: pass
def monitor(self, status): if status is not None: if ('跑马灯' in status and 'LED' in status) or '开灯' in status: play_sound(file_path='./voice/open_led.wav') self.rgb() self.bright_on() print('LED灯已经打开') self.tulingReply = False self.onLed = False elif u'关灯' in status or '关闭LED灯' in status: play_sound(file_path='./voice/shut_led.wav') self.destroy() print('LED灯已经关闭') self.tulingReply = False elif '红' in status: self.rgb(red=True) self.tulingReply = False self.onLed = False elif '绿' in status: self.rgb(green=True) self.tulingReply = False self.onLed = False elif '蓝' in status: self.rgb(blue=True) self.tulingReply = False self.onLed = False elif u'打开超声波' in status: play_sound(file_path='./voice/open_ultrasonic.wav') self.ultrasonic_on() self.tulingReply = False elif u'关闭超声波' in status: play_sound(file_path='./voice/shut_ultrasonic.wav') self.buzzer_off() print('超声波已经关闭') self.tulingReply = False elif '打开' in status and '红外线' in status: play_sound(file_path='./voice/open_infrared.wav') self.infrared_detect() self.tulingReply = False elif u'音乐' in status or u'歌曲' in status or u'再来一首' in status or u'music' in status: file_path = '/home/pi/Music/' if os.path.isdir(file_path): music_path = [ os.path.join(file_path, i) for i in os.listdir(file_path) ] music_name = [ os.path.split(i)[-1].split('.')[0] for i in music_path ] play_sound(file_path='./voice/playmusic.wav') song = random.choice(music_name) text2sound('一首' + song + '送个你') play_sound() for music in music_path: if song in music: self.playMusic(music) self.tulingReply = False elif u'拍张照片' in status or u'再来一张' in status: play_sound(file_path='./voice/camera.wav') self.take_picture() play_sound(file_path='./voice/send_success.wav') self.tulingReply = False elif '关机' in status: play_sound(file_path='./voice/shutdown.wav') self.tulingReply = False os.system('sudo poweroff') elif '重启' in status: play_sound(file_path='./voice/reboot.wav') self.tulingReply = False os.system('sudo reboot')
def monitor(status): if status is not None: global tulingReply if ('打开' in status and 'LED' in status) or '开灯' in status: play_sound(file_path='./voice/open_led.wav') on_bright() print('LED灯已经打开') tulingReply = False onLed = False elif u'关灯' in status or '关闭LED灯' in status: play_sound(file_path='./voice/shut_led.wav') rgb2.destroy() print('LED灯已经关闭') tulingReply = False elif u'打开超声波' in status: play_sound(file_path='./voice/open_ultrasonic.wav') on_ultrasonic2() tulingReply = False elif u'关闭超声波' in status: play_sound(file_path='./voice/shut_ultrasonic.wav') destroy() print('超声波已经关闭') tulingReply = False elif '打开' in status and '红外线' in status: play_sound(file_path='./voice/open_infrared.wav') tulingReply = False elif u'音乐' in status or u'歌曲' in status or u'再来一首' in status or u'music' in status: file_path = '/home/pi/Music/' if os.path.isdir(file_path): music_path = [ os.path.join(file_path, i) for i in os.listdir(file_path) ] music_name = [ os.path.split(i)[-1].split('.')[0] for i in music_path ] play_sound(file_path='./voice/playmusic.wav') song = random.choice(music_name) text2sound('一首' + song + '送个你') play_sound() for music in music_path: if song in music: on_music(music) self.tulingReply = False elif u'拍张照片' in status or u'再来一张' in status: play_sound(file_path='./voice/camera.wav') take_picture() play_sound(file_path='./voice/send_success.wav') tulingReply = False elif '关机' in status: play_sound(file_path='./voice/shutdown.wav') tulingReply = False os.system('sudo poweroff') elif '重启' in status: play_sound(file_path='./voice/reboot.wav') tulingReply = False os.system('sudo reboot')