监视器保持闪烁蟒蛇和超声波传感器

时间:2014-07-11 03:32:26

标签: python sensor monitor

我使用的传感器是HC-SR04。 当它感知超过30厘米的物体时,我用它将显示器转入睡眠模式。 当感觉物体小于或等于30厘米时,它会打开。 当它进入睡眠模式时它工作正常,但是当它打开时就像闪烁一样。请帮帮我~~~ 下面是我的编码

import time
import RPi.GPIO as GPIO
import os
import subprocess
import sys
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) 
# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24

print "Ultrasonic Measurement"
while True:
        # Set pins as output and input
        GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
        GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

        # Set trigger to False (Low)
        GPIO.output(GPIO_TRIGGER, False)

        # Allow module to settle
        time.sleep(2)

        # Send 10us pulse to trigger
        GPIO.output(GPIO_TRIGGER, True)
        time.sleep(0.00001)
        GPIO.output(GPIO_TRIGGER, False)
        start = time.time()
        while GPIO.input(GPIO_ECHO)==0:
          start = time.time()

        while GPIO.input(GPIO_ECHO)==1:
          stop = time.time()

        # Calculate pulse length
        elapsed = stop-start

        # Distance pulse travelled in that time is time
        # multiplied by the speed of sound (cm/s)
        distance = elapsed * 34000

        # That was the distance there and back so halve the value
        distance = distance / 2

        print "Distance : %.1f" % distance
        if(distance > 30):
            time.sleep(3)
            subprocess.call("sudo /opt/vc/bin/tvservice -o",shell=True)


            os.system('clear')
        if(distance <= 30):
                subprocess.call("sudo /opt/vc/bin/tvservice -p",shell=True)


# Reset GPIO settings
GPIO.cleanup()

1 个答案:

答案 0 :(得分:1)

您的测量中存在误差或噪音,导致其波动。这可能部分是由于尝试使用Python GPIO模块测量它,这可能有点慢。您可以尝试两件事:

  1. 向触发点添加一些hysteresis

    if(distance > 40): # switch off if distance is greater than 40
        time.sleep(3)
        subprocess.call("sudo /opt/vc/bin/tvservice -o",shell=True)
    
        os.system('clear')
    if(distance <= 30): # switch on if distance is less than 30
            subprocess.call("sudo /opt/vc/bin/tvservice -p",shell=True)
    
  2. 为您的读数添加一些过滤功能。您可以使用moving average过滤器说

    previous_readings = []
    while 1:
        previous_readings.append(new_reading)
        previous_readings = previous_readings[-10:] # just keep the last 10 readings
        avg_reading = sum(previous_readings) / len(previous_readings)
    
  3. 或者,使用微控制器更准确地测量时间可能有所帮助。

    您可能还应该跟踪屏幕是否已经开启或关闭,并且只根据需要进行切换

    screen_on = False
    ...
    while 1:
    ...
        if (screen_on and distance > 40): # switch off if distance is greater than 40
            time.sleep(3)
            subprocess.call("sudo /opt/vc/bin/tvservice -o",shell=True)
            screen_on = False
    
            os.system('clear')
        if (not screen_on and distance <= 30): # switch on if distance is less than 30
            subprocess.call("sudo /opt/vc/bin/tvservice -p",shell=True)
            screen_on = True