Code for Pan and Tilt camera driving Servos direct from the Raspberry Pi

This is the code I use for my Pan and Tilt camera. The Raspberry Pi is connected directly to the servo's without the need for extra hardware. The servo positive wire is connected to 3.3v, negative to ground, and the servo signal cable connected directly to the GPIO pin.

This might not be the recommended method but I've been using it for two years without issue.

The pan, tilt and photo name are contained in an array, and the sript loops through the array paning and tilting the samera, then names the file. Next the image is given a date and time stamp, and uploaded to an FTP server.


#!/usr/bin/env python
import time
import picamera
import ftplib
import os

from RPIO import PWM

servo = PWM.Servo()

import signal
signal.signal(signal.SIGCHLD, signal.SIG_IGN)

#pan = 23 / 1100
#tilt = 24 / 1000

#################################################################

Pictures = []
Pictures.append("700#1400#hrhg")
Pictures.append("1200#900#Sky")
Pictures.append("1200#1200#CenterCenter")
Pictures.append("1250#1600#FrontDoor")
Pictures.append("1500#1600#hrwbr")

###################################################################

def PDSPanTiltPhoto():
for picturesconcatonated in Pictures:
intPan, intTilt, strPicName = picturesconcatonated.split("#")

print("####################################################Pan-" + str(intPan) + " Tilt-" + str(intTilt) + " Name-" +strPicName)

strTempFilename = "/home/pi/" + strPicName + "Temp.jpg"
strFilename = "/home/pi/" + strPicName + ".jpg"

servo.set_servo(23, int(intTilt))
servo.set_servo(24, int(intPan))

time.sleep( 2 )

servo.stop_servo(23)
servo.stop_servo(24)

print("############################## Taking photo " + str(strTempFilename))
with picamera.PiCamera() as camera:
#camera.resolution = (1024, 768)
camera.resolution = (1920,1080)
#camera.iso=(100)
camera.start_preview()
# Camera warm-up time
time.sleep(2)
camera.led = False
camera.capture(strTempFilename)

######################################################################################################## code here

def ConvertPhoto():
for picturesconcatonated in Pictures:
intPan, intTilt, strPicName = picturesconcatonated.split("#")

strTempFilename = "/home/pi/" + strPicName + "Temp.jpg"
strFilename = "/home/pi/" + strPicName + ".jpg"

commandtocall="convert " + strTempFilename + " -pointsize 30 -fill yellow -draw \"text 0,30 '$(date)'\" " + strFilename
print("############################## convert picture command = #"+str(commandtocall)+"#")
os.system(commandtocall)
print("############################## finished converting photo")

######################################################################################################## code here

def Upload():
print ("Starting FTP")
session = ftplib.FTP('ftp.domain.com','webcamftp@domain.com','password123')

for picturesconcatonated in Pictures:

intPan, intTilt, strPicName = picturesconcatonated.split("#")

strFilename = "/home/pi/" + strPicName + ".jpg"

print ("Uploading " + strFilename)

file = open(strFilename,'rb') # file to send
session.storbinary('STOR ' + strPicName + '.jpg', file) # send the file
file.close()
# close file and FTP

session.quit()
print ("Finished FTP")

########################################################################################################

PDSPanTiltPhoto()
ConvertPhoto()
Upload()

print("############################## resetting")

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>