0

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, that 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

Pantilt hat https://shop.pimoroni.com/products/pan-tilt-hat

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

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

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 Regard Kirst

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

# Import the PCA9685 module.
import Adafruit_PCA9685
import RPi.GPIO as GPIO
import pantilthat
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```


1 Answers1

0

I’m not sure it is possible to do exactly what you are looking for. Action buttons in motioneye simply run a script, if you want to move the camera more you have to press the button again rather than hold it down. However it is quite simple to get an action button working with the pantilt hat. Firstly, I assume you have motioneye up and running on a raspberry pi running stretch. If so then create a file called ‘left_1’ in the ‘/etc/motioneye’ directory Next edit it and add the following:

#!/bin/bash
/usr/bin/python3 /etc/motioneye/left.py

Now make that file executable: sudo chmod 777 left_1

Now create left.py with the following code:

#!/usr/bin/python

import time
import pantilthat

currentPos = pantilthat.get_servo_one()
newPos = currentPos +20
if newPos >= 80: newPos = 80
pantilthat.servo_one(newPos)
time.sleep(1)

One thing to note here, 80 is the max mine can move left, test yours For right do the same but name the files appropriately, the code for right.py is:

#!/usr/bin/python

import time
import pantilthat

currentPos = pantilthat.get_servo_one()
#print (currentPos)
newPos = currentPos - 20
if newPos <= -80: newPos = -80
pantilthat.servo_one(newPos)
time.sleep (1)

up and down are the same but changing ‘servo_one’ to 'servo_two’

Finally it is useful to have a button to move the camera back to ‘0’. Do to that create a file called ‘preset1_1’ with the following:

#!/bin/bash
/usr/bin/python3 /etc/motioneye/reset.py

and a py file called ‘reset.py’ :

#!/usr/bin/python

import time
import pantilthat

pantilthat.servo_one(0)
pantilthat.servo_two(0)
time.sleep(1)

Good luck!

Tim H
  • 13
  • 3