Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel

ShawnHymel/timelapse.py

Last active Sep 14, 2017
Embed
What would you like to do?
Raspberry Pi time lapse and pan camera
#!/usr/bin/env python
import smbus
import calendar
import time
import picamera
import os, errno
wait = 5.0
iso = 100
bus = smbus.SMBus(1)
addr = 0x40
camera = picamera.PiCamera(resolution=(1920, 1080))
photo_dir = '/home/pi/' + str(calendar.timegm(time.gmtime()))
#############################################################
# Functions
#############################################################
def pan(pwm):
bus.write_word_data(addr, 0x06, 0)
bus.write_word_data(addr, 0x08, pwm)
def tilt(pwm):
bus.write_word_data(addr, 0x0a, 0)
bus.write_word_data(addr, 0x0c, pwm)
def deg2pwm(deg):
if deg < 0:
deg = 0
elif deg > 180:
deg = 180
return ((deg * 834) / 180) + 833
#############################################################
# Main
#############################################################
# Create directory for pictures
try:
os.makedirs(photo_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# enable the PC9685 and enable autoincrement
bus.write_byte_data(addr, 0, 0x20)
bus.write_byte_data(addr, 0xfe, 0x1e)
# Set servos to default positions
pan(deg2pwm(0))
tilt(deg2pwm(90))
time.sleep(1)
# Set ISO and let camera automatically set other config
# NOTE: Disabled for all-automatic mode. White balance may change over time.
#print 'Configuring camera'
#camera.iso = iso
#time.sleep(2)
#camera.shutter_speed = camera.exposure_speed
#camera.exposure_mode = 'off'
#g = camera.awb_gains
#camera.awb_mode = 'off'
#camera.awb_gains = g
# Commence timelapse!
print 'Starting timelapse'
for i in range(0, 835):
pan(i + 833)
time.sleep(wait / 2)
filename = photo_dir + '/'
if i < 10:
filename += '00'
elif i < 100:
filename += '0'
filename += str(i) + '.jpg'
camera.capture(filename)
time.sleep(wait / 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment