Pan tilt hat with motioneye

I posted this question in stackoverflow but this might be a better forum.

Being new to code and python, I have trouble integrating buttons to control Pimoroni pan tilt hat in the Motioneye surveillance software. I found code for adafruit PCA9685 servo controller. The code will call a bash script when a button is pressed. The bash script will then call a python script that will pan or tilt the servo continously as long as the buttons is pressed which is something I would like to do. Unfortunately I don´t know what to change to make it work with the Pimoroni servo.

Motioneye https://github.com/ccrisan/motioneye/wiki

Script for controlling pan tilt in motioneye with the adafruit PCA9685 servo controller https://github.com/luetzel/motion_eye_servo_action

I have tried to change GPIO pins in the code, and imported pantilthat into the code. However, that doesnt seem to work. That will get me

Remote I/O error

If anyone could help me get this working with pimoroni the pan tilt hat or point me in the right direction, I would be very grateful!

Kind regards Kirst

Below is an example code for tilt down. Again this is for the Adafruit PCA9685 driver.

/usr/bin/env python
from __future__ import division
import time

# Import the PCA9685 module.
import Adafruit_PCA9685
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# power-up servo controller
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, 0)
time.sleep(.5)

# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()

# Configure min and max servo pulse lengths (450 total, 25 corresponds to 10 degrees)
servo_min = 225  # Min pulse length out of 4096
servo_max = 725  # Max pulse length out of 4096

with open('/etc/motioneye/vert_pos', 'r') as f:
vert_pos = int(f.readline())
print vert_pos
 f.close()

# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)

 if ( vert_pos != servo_max ):
           vert_pos += 25
           pwm.set_pwm(0, 0, vert_pos)
           with open('/etc/motioneye/vert_pos', 'w') as f:
                 f.write(str(vert_pos))
                 f.close()

# power-up servo controller
time.sleep(.5)
 GPIO.output(18, 1)

raise SystemExit

I have a pan tilt on my wish list and I am currently running motion eye on a few raspberry Pi’s. I can see myself trying to do what you want to do at some point so I’ll tag along in this thread to see what you come up with.
The function reference for the pan tilt is here, I’d start there. Do up some python code that works, makes the pan tilt move, and go from there.
http://docs.pimoroni.com/pantilthat/

If you have already run the installer for the pan tilt there should be some example code in a subfolder under a pimoroni folder. These ones.

That should get you started.

The pinout is here


Looks like its all done via i2c.

The pantilt hat has a micro controller on it to do the servo driving. If you want to incorporate it into motioneye the easiest way is to cross reference the library and replace servo commands with ones that match the library. Little bit tricky.

Thanks for all the suggestions and replies, does any one know what command will pan or tilt the head continously as long as the script is run?

For example

import pantilthat
pantilthat.pan(-90)

Will make the head pan to a fixed angle, I guess I need to know what command will make it run coninously as long as the script is run? Does any one know the code/command for that?

/Kirst

There isn’t a sweep/continuous function built into the library.

It’s achievable with the pan commands, a gentle wait, and some looping logic though.

Unfortunately I don’t see such a command listed in the function reference.
One way to do it is to save its position, get pan and get tilt and same them as X and Y.
Then do an X = X + 1 or X = X - 1 depending on which way you want to move it. Save it and pan or tilt to the new value for X or Y. Put it in a loop where it just repeatedly adds or subtracts for X or Y until you tell it to stop. A bit of a kludge to be honest.
@gadgetoid might know of a better way. My python skills are about fair to average or so.

hmm, well thats beats me guys. Thank you for your replies and suggestions. Unfortunately that sends me to the shop for an arduino board… I am no where near writing python for something like that. If you come up with something else for for us that would like to use the pan and tilt hat for motioneye, please don´t hesitate to write here! otherwise I wish you guys a happy easter!

/Kirst

Unfortunately it might be a while until I buy my pan tilt. =( I have a limited budget for stuff like this and I have some other things I need more at the moment.
I’m just getting into Arduino. Its not all that much easier to be honest. Instead of python your coding in C. If you use the Arduino IDE anyway. You can use Circuit Python on some Arduino boards though. Some of the ones Adafruit does in house etc.
Another option is to figure out what chip Pimoroni uses on the pan tilt hat, then Google search for a different python library for it. Somebody else may have done one with the features your looking for?
You could also post on the Motion eye forum to see if anybody else tried what your trying.

I have a simple set of Python scripts for this that work. They do not do smooth pan/tilt but work in sensible and easily altered increments. Given most Pi cameras have a field of view of about 70deg this works just fine for me.

Happy to share.