乗車日記

自転車ときのこ

動体検出

今度はじっとしていると部屋の照明が消えて困るという苦情が出ました。そうなんです、焦電センサーは熱源が動いていないと反応してくれないのです。センサーの光路を定期的に遮るチョッパーをつけて見たのですが、回転数が速すぎるのか効果なし。仕方がないのでカメラをつけて動きを検出すると照明をオンにするという仕様にしてみました。
しかし、照明が消えると、カメラ画像がガラリと変わるので動きとして検出されてしまうのです。そうすると、照明がオン。しばらくすると消えるのですが、またすぐにオン。永久運動です。
色々やって、結局照明がオフになったあと15秒ぐらいは動体検出を無視するというプログラムを組んで回避しました。
それからもう一点大きな改善、2個目につけた焦電センサーがどうも誤動作しがちだったのですが、これはラズベリーパイ の内部プルアップのノイズらしいということがわかりました。外側でプルアップ抵抗をつけたら誤動作は無くなりました。

#!usr/bin/python
# -*- coding: utf-8 -*-
import pigpio
from time import sleep
import subprocess
import time
import os

pi=pigpio.pi()
pi.set_mode(4,pigpio.INPUT)
pi.set_pull_up_down(4,pigpio.PUD_UP)
pi.set_glitch_filter(4,300000)
pi.set_mode(23,pigpio.INPUT)
origin=time.time()
start_time=time.time()
off_time=time.time()
detect_time_=time.time()

def cb_interrupt(gpio,level,tick):
  global start_time
  start_time=time.time()
  subprocess.Popen(['irsend','SEND_ONCE','light','on'])

def cb_interrupt_(gpio,level,tick):
  global start_time
  sleep(0.25)
  if pi.read(4)==0:
    start_time=time.time()
    subprocess.Popen(['irsend','SEND_ONCE','light','on'])

cp=pi.callback(23,pigpio.RISING_EDGE, cb_interrupt)
cp=pi.callback(4,pigpio.FALLING_EDGE, cb_interrupt_)

if os.path.exists('/home/pi/tmp/motion_detected')==False:
  subprocess.Popen(['touch','/home/pi/tmp/motion_detected'])
try:
  while True:
  sleep(5)
  detect_time=os.stat('/home/pi/tmp/motion_detected').st_mtime
  if detect_time>detect_time_ and detect_time-off_time >15:
    subprocess.Popen(['irsend','SEND_ONCE','light','on'])
    start_time=detect_time
    detect_time_=detect_time
  if (time.time()-start_time)>120.0:
    subprocess.Popen(['irsend','SEND_ONCE','light','off'])
    start_time=time.time()
    sleep(1)
    detect_time=os.stat('/home/pi/tmp/motion_detected').st_mtime
    if detect_time >detect_time_ and detect_time >start_time:
      off_time=time.time()
      detect_time_=detect_time
except KeyboardInterrupt:
 pass
pi.stop()
<||