Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import pigpio
from time import sleep
import signal
import sys
import subprocess #コマンドプロントのコマンドを使うのに必要
#GPIOのピンを指定
GPIO.setmode(GPIO.BCM)
sclk = 11
miso = 9
mosi = 10
ce0 = 8
GPIO.setup(sclk, GPIO.OUT)
GPIO.setup(miso, GPIO.IN)
GPIO.setup(mosi, GPIO.OUT)
GPIO.setup(ce0, GPIO.OUT)
GPIO.setup(14, GPIO.OUT)
#pigpioの初期設定
pi = pigpio.pi()
pin_az = 18
pin_el = 13
#camera config
camera = "sudo fswebcam -F 10 --no-timestamp --no-banner --rotate 90 /home/pi/blog/test1.jpg"
def read(adcnum, sclk, mosi, miso, ce0): #cdsによって光を検出する関数を定義
if adcnum > 7 or adcnum < 0:
return -1
GPIO.output(ce0, GPIO.HIGH)
GPIO.output(sclk, GPIO.LOW)
GPIO.output(ce0, GPIO.LOW)
commandout = adcnum
commandout |= 0x18
commandout <<= 3
for i in range(5):
if commandout & 0x80:
GPIO.output(mosi, GPIO.HIGH)
else:
GPIO.output(mosi, GPIO.LOW)
commandout <<= 1
GPIO.output(sclk, GPIO.HIGH)
GPIO.output(sclk, GPIO.LOW)
adcout = 0
for i in range(13):
GPIO.output(sclk, GPIO.HIGH)
GPIO.output(sclk, GPIO.LOW)
adcout <<= 1
if i>0 and GPIO.input(miso) == GPIO.HIGH:
adcout |= 0x1
GPIO.output(ce0, GPIO.HIGH)
return adcout
try:
while True:
data = read(0, sclk, mosi, miso, ce0) #ドアが開いたことを確認する光データを取得
print('data = %s'% data)
#camera config
camera = "sudo fswebcam -F 10 --no-timestamp --no-banner --rotate 90 /home/pi/blog/test1.jpg"
count =1
print('count =%s' % count)
if data < 1500:
for pwm_width in range(1350,1700,150):
print(pwm_width)
sleep(0.5)
pi.set_servo_pulsewidth(pin_az, pwm_width)
subprocess.call(camera,shell = True)
#保存する写真する名前の数字を変更
sleep(0.5) #このsleepがないとサーボがしっかり動かない
count = count +1
print('count =%s' % count)
camera = camera.replace('test'+str(count-1),'test'+str(count))
sleep(0.2)
except KeyboardInterrupt:
pass
pi.set_mode(pin_az ,pigpio.INPUT)
pi.stop()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment