Untitled

 avatar
unknown
plain_text
3 years ago
2.5 kB
6
Indexable
from picamera import PiCamera
#from datetime import datetime
import datetime
import time
from time import sleep
import RPi.GPIO as GPIO
from gpiozero import LED, Button

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(13, GPIO.OUT) # Indicate that camera is shooting
GPIO.setup(19, GPIO.OUT) # Indicate that program is running
button1 = Button(17) # Pin for use as Trigg signal when Camera should take picture
button2 = Button(27) # Pin for use as Trigg signal when Motor should feed one frame
running = True
GPIO.output(19, GPIO.HIGH) # Turn on LED to indicate that program is running

#From motor control
def motor():
    DIR = 20    # Direction GPIO pin
    STEP = 21   # Step GPIO pin
    CW = 0      # Clockwise Rotation
    CCW = 1     # Counterclockwise Rotation
    SPR = 600   # Steps per Revolution

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(DIR, GPIO.OUT)
    GPIO.setup(STEP, GPIO.OUT)
    GPIO.output(DIR, CW)

    MODE = (14, 15, 18)  # Microstep Resoultion GPIO Pins
    GPIO.setup(MODE, GPIO.OUT)
    RESOLUTION = {'Full': (0, 0, 0),
                  'Half': (1, 0, 0),
                  '1/4':  (0, 1, 0),
                  '1/8':  (1, 1, 0),
                  '1/16': (0, 0, 1),
                  '1/32': (1, 0, 1)}
    GPIO.output(MODE, RESOLUTION['1/32'])

    step_count = SPR * 32
    delay = .005 / 32  #delay = .005 / 32

    for x in range(step_count):
        GPIO.output(STEP, GPIO.HIGH)
        sleep(delay)
        GPIO.output(STEP, GPIO.LOW)
        sleep(delay)

#From motor control
def on_closing():  # Ny kod för att frigöra GPIO-pins
    GPIO.cleanup() # ej testat
    
def picture():
    GPIO.output(13, GPIO.HIGH)
    camera = PiCamera()
    camera.rotation = 180
    camera.exposure_mode = 'sports'
    camera.awb_mode = 'sunlight'
    camera.iso = 500
    timestamp=datetime.datetime.now().strftime("%Y-%m-%d %H:%M.%S") # Timestamp with format Year-Month-Date Hour:Minute.Second
    camera.capture('/home/pi/Desktop/pictures/'+str(timestamp)+'.png') # Name picture with timestamp
    camera.close()
    GPIO.output(13, GPIO.LOW)


try:
    while running:
        button2.when_pressed=motor
        print('Active') # Display 'active' to the shell
        button1.when_pressed=picture
        time.sleep(1)

    
        
except KeyboardInterrupt:
        running = False
        GPIO.output(19, GPIO.LOW) # Turn off "System running"-LED
        GPIO.output(13, GPIO.LOW) # Turn off "Camera shooting"-LED
        GPIO.cleanup()

Editor is loading...