RPI3 Camery + Pimioroni (Adafruit) Pan/tilt + RPi-Cam-Web-Interfac

It isn’t too hard to use the Pimoroni Pan/Tilt Python library with the RPi-Cam.

Install the Pimoroni Pan/Tilt library per the instructions here (and test basic operation using one of the examples): https://github.com/pimoroni/pantilt-hat

Follow the RPi-Cam instructions for the mindsensors setup per the link below with the following changes:
http://elinux.org/RPi-Cam-Web-Interface#Pan-Tilt_or_Pi-Light

  1. Change the contents of the pipan_pipe.py to the code at the bottom of the post which will act as a shim and allow the Pan/Tilt library to act like the mindsensors interface with the same pipe (this isn’t the best solution but at least it won’t be overwritten when the RPi-Cam software is updated). Please note - I am not a Python programmer, feel free so suggest improvements.

  2. Save the pipan_pipe.py somewhere sensible (e.g. /home/pi/Pimoroni) and run it to make sure it has no errors (python pipan_pipe.py, ^c to stop)

  3. When editing /etc/rc.local use the path above (e.g. python /home/pi/Pimoroni/pipan_pipe.py &)

If all other instructions are followed it should just work… I don’t have a light on mine so haven’t tried to get that working, if I get one I will update the post

#!/usr/bin/env python
 
import time
import os, sys
import pantilthat
 
MIN_PIPAN_PAN = 50
MAX_PIPAN_PAN = 250
MIN_PIPAN_TILT = 80
MAX_PIPAN_TILT = 220

MIN_PANTILT_PAN = -90
MAX_PANTILT_PAN = 90
MIN_PANTILT_TILT = -86
MAX_PANTILT_TILT = 90

# Number to add to the given pan or tilt input
pan_corr  = ((MAX_PANTILT_PAN  + MIN_PANTILT_PAN)  - (MAX_PIPAN_PAN  + MIN_PIPAN_PAN))/2.0
tilt_corr = ((MAX_PANTILT_TILT + MIN_PANTILT_TILT) - (MAX_PIPAN_TILT + MIN_PIPAN_TILT))/2.0

# Scaling factor to apply after correction (force floating point division)
pan_scale  = 1.0 * (MAX_PANTILT_PAN  - MIN_PANTILT_PAN)  / (MAX_PIPAN_PAN  - MIN_PIPAN_PAN)
tilt_scale = 1.0 * (MAX_PANTILT_TILT - MIN_PANTILT_TILT) / (MAX_PIPAN_TILT - MIN_PIPAN_TILT)
   
while True:
  pipein = open("/var/www/html/FIFO_pipan", 'r')
  line = pipein.readline()
  line_array = line.split(' ')
  if line_array[0] == "servo":
    # Pantilt expects an int, int is also used for string to int conversions of the parameters
    pantilthat.pan( int((int(line_array[1]) + pan_corr)  * pan_scale))
    pantilthat.tilt(int((int(line_array[2]) + tilt_corr) * tilt_scale))
  #elif line_array[0] == "led":
    # p_led.createPiLight(int(line_array[1]),int(line_array[2]),int(line_array[3]))
  pipein.close()