Raspberry Pi 3B +通过GPIO事件在3.5mm插孔上获得静态噪声

时间:2019-05-03 18:08:20

标签: python raspberry-pi pygame

我对Raspberry Pi和Python的经验很少,但是我设法集成了一个解决方案,该系统可以在有人在场时做出反应(使用PIR传感器),并根据人数来命令改变蠕动速度由Arduino控制的泵(使用HC-06通过蓝牙进行通信)。

  

RPi所做的工作是通过PIR传感器获取事件,使用摄像机计数人数,发送BT命令以及播放或更改音轨。

     

在播放音频时,我刚刚遇到的问题是3.5毫米插孔中的静态声音。

我已经检查过的东西: +扬声器和连接与其他设备配合正常 + GPIO连接良好且隔离 +程序外的音频播放效果很好 +在config.txt中添加了“ pwm_mode = 2”和“ disable_audio_dither = 1”

Raspberry Pi 3B +在virtualenv上运行NOOBS,OpenCV和Python 3 代码:

from picamera import PiCamera
from time import sleep
from threading import Timer
from random import randint
import numpy as np
import imutils
import RPi.GPIO as GPIO
import serial
import cv2
import pygame


pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=4096)

pir = 4
crowd = 0
last = 0
lv = 1
waiting = True
state = False
active = False

GPIO.setmode(GPIO.BCM)
GPIO.setup(pir, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

camera = PiCamera()
camera.resolution = (640, 480)

intro = pygame.mixer.Sound('./audio/0.wav')
first = pygame.mixer.Sound('./audio/1.wav')
second = pygame.mixer.Sound('./audio/2.wav')
third = pygame.mixer.Sound('./audio/3.wav')
outro = pygame.mixer.Sound('./audio/m1.wav')

btSerial = serial.Serial( "/dev/rfcomm0", baudrate=9600 )
sleep(2)

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

def shot(channel):
    global waiting
    print(channel)
    waiting = False
    t.cancel()
    k.cancel()
    camera.start_preview()
    sleep(2)
    camera.capture('/home/pi/Desktop/crowd.bmp')
    camera.stop_preview()
    print ('Photo captured')
    people_counter()
    if not active:
            start()
    else:
        update()

def deactiv():
    global active
    global last
    active = False
    intro.fadeout(8200)
    first.fadeout(8200)
    second.fadeout(8200)
    third.fadeout(8200)
    outro.play(0,10000,6500)
    last = 0
    send_bt('OFF')

def keep_on():
    send_bt('READY')

def people_counter():
    global crowd
    image = cv2.imread('crowd.bmp')
    image = imutils.resize(image, width=min(400, image.shape[1]))
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
        padding=(16, 16), scale=1.003)
    crowd = len(rects)
    print(crowd)

def start():
    global active
    active = True
    intro.play(0,-1, 6500)
    send_bt('TRIG')

def update():
    global last
    global lv
    if 0 <= crowd <= 2 :
        lv = 1
    elif 2 < crowd <= 5 :
        lv = 2
    elif crowd >= 5 :
        lv = 3
    if lv == last and last != 3:
        pass
    else:
        if lv == 1:
            intro.fadeout(8200)
            first.fadeout(8200)
            second.fadeout(8200)
            third.fadeout(8200)
            first.play(-1,-1,6500)
            send_bt('SET:50')
        elif lv == 2:
            intro.fadeout(8200)
            first.fadeout(8200)
            second.fadeout(8200)
            third.fadeout(8200)
            second.play(-1,-1,6500)
            send_bt('SET:160')
        elif lv == 3:
            if last != 3:
                intro.fadeout(8200)
                first.fadeout(8200)
                second.fadeout(8200)
                third.fadeout(8200)
                third.play(-1,-1,6500)
            n_sp = randint(200, 350)
            send_bt('SET:'+str(n_sp))
        last = lv

def send_bt(pump_com):
    print('sending'+pump_com)
    btSerial.write(pump_com.encode())
    #Wait "Done!"

send_bt('READY')
t = Timer(180, deactiv)
k = Timer(30, keep_on)
GPIO.add_event_detect(pir, GPIO.RISING, shot)
while True:
        state = GPIO.input(pir)
        if active and not state:
                if not waiting:
                        t = Timer(180, deactiv)
                        t.start()
                        waiting = True
                elif flag:
                        k = Timer(30, keep_on)
                        k.start()
                        flag = False

0 个答案:

没有答案