声源定位专项

之前发布了一款声源定位机器人,但是其中还包括图像处理和超声波传感器,以及单片机开发的内容,对于专门想要研究声源定位的朋友可能过于复杂。

这次把声源定位的程序摘取出来,并且保证可以独立运行(只需要放到镜像里项目文件夹下),给想研究声源定位的朋友参考。

#!/usr/bin/env python
import socket
import sys
import threading
import random
import os
import time
import struct

import Image, StringIO
import numpy as np


from voice_engine.source import Source
from voice_engine.channel_picker import ChannelPicker
from voice_engine.doa_respeaker_4mic_array import DOA
from pixels import pixels

import datetime


exit_now = 0

last_movement_timestamp = datetime.datetime.now()


def exit_signal_handle(sig,stack_frame):
    global exit_now
    print "EXIT sig"
    exit_now = 1


def main():
    global last_movement_timestamp
    src = Source(rate=16000, channels=4, frames_size=320)
    doa = DOA(rate=16000)
    src.link(doa)
    src.recursive_start()


    while exit_now == 0:
        try:
            time.sleep(1)
            current_timestamp = datetime.datetime.now()
            if ((current_timestamp - last_movement_timestamp).seconds > 2):
                position, amplitute = doa.get_direction()
                if amplitute > 2000:
                    pixels.wakeup(position)
                    print "doa",amplitute,position
                    if position > 0  and position < 180:
                        print "pivot right"
                        time.sleep(position/200)
                    elif position >= 180 and position < 360:
                        print "pivot left"
                        position = 360 - position
                        time.sleep(position/200)
                    time.sleep(2)
                else:
                    pixels.speak()
            else:
                pixels.think()
        except:
            print "doa", sys.exc_info()
    src.recursive_stop()

if __name__ == "__main__":
    main()

 

你可能感兴趣的:(自动跟随机器人)