# Weird actions from ServoHat

**URL:** <https://community.sparkfun.com/t/weird-actions-from-servohat/39695>\
**Category:** Kits\
**Created:** [May 25, 2020, 12:48pm UTC](https://community.sparkfun.com/t/weird-actions-from-servohat/39695 "2020-05-25T12:48:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![impartit](https://avatars.discourse-cdn.com/v4/letter/i/5e9695/32.png) [@impartit](https://community.sparkfun.com/u/impartit)\
**Post date:** [May 25, 2020, 12:48pm UTC](https://community.sparkfun.com/t/weird-actions-from-servohat/39695/1 "2020-05-25T12:48:40Z")

</div>

I have successfully added a new set of controls to my ‘RPi\_Cam\_Web\_Interface’ in addition to the embedded ‘pan’ and ‘tilt’ buttons for the Pi camera.

These work just fine using just ‘Ch 14’ and ‘Ch15’ as defined in ‘pantilt\_50Hz\_tuned.py’ below.

However in adding four new arrow buttons in the “viewport” of index.php and in reference in pantilt\_50Hz\_tuned.py on ‘Ch 13’ and ‘Ch 12’ for the “Rudder Port and Starboard” and the “Engine Ahead and Astern” (both controls for my model cabin cruiser) they are not working as they should. In addition to the modified ‘pantilt\_50Hz\_tuned.py’ in ‘/etc/rc.local’ the other files in which additions and changes were made are:

- 

  
the file

piBoat.js was also created from pipan.js.

I trust these attached files will help your diagnosis and if you can correct this you are welcome to include it in your tutorials as I thinks it is of value to user of Sylvan’s code working in conjunction with a remote model (boat, car or drone). If the files were NOT attached correctly please respond with an acceptable way of uploading them.

As they stand the outer arrows for the Boat ALSO activate the inner adjacent Camera servos.

below is the modified pantilt\_50Hz\_tuned.py file. I had difficulty in uploading this as a txt file. Getting tired I expect having spent 48 hours or more troubleshooting this. Sylvan’s “RPi\_Cam\_Web\_Interface” is OF COURSE IMPECCABLE!!!

## Modified by: Wes @ SparkFun

## Written by: Mike Hord @ SparkFun

## Modified example code for 50Hz and calibrated with servo using an oscilloscope.

#!/usr/bin/env python

import smbus

import time

bus = smbus.SMBus(1)

addr = 0x40

def scale(x, in\_min, in\_max, out\_min, out\_max):

return (x - in\_min)\*(out\_max - out\_min)/(in\_max - in\_min) + out\_min

## enable the PC9685 and enable autoincrement

bus.write\_byte\_data(addr, 0, 0x20) # enable the chip

time.sleep(.1)

bus.write\_byte\_data(addr, 0, 0x10) # enable Prescale change as noted in the datasheet

time.sleep(.1) # delay for reset

bus.write\_byte\_data(addr, 0xfe, 0x88) #changes the Prescale register value for 50 Hz, using the equation in the datasheet (I later adjusted the value to fine tune the signal with an oscilloscope. The original value was 0x79.)

time.sleep(.1)

bus.write\_byte\_data(addr, 0, 0x20) # enables the chip

time.sleep(.1)

bus.write\_word\_data(addr, 0x3e, 0) # chl 14 start time = 0us PAN CAMERA

bus.write\_word\_data(addr, 0x40, 350) # chl 14 stop time = 1.5ms

bus.write\_word\_data(addr, 0x42, 0) # chl 15 start time = 0us TILT CAMERA

bus.write\_word\_data(addr, 0x44, 350) # chl 14 stop time = 1.5ms

bus.write\_word\_data(addr, 0x3A, 0) # chl 13 start time = 0us RUDDER

bus.write\_word\_data(addr, 0x3C, 350) # chl 13 stoptime = 1.5ms

bus.write\_word\_data(addr, 0x36, 0) # chl 12 start time = 0us ENGINE

bus.write\_word\_data(addr, 0x38, 350) # chl 12 stop time = 1.5ms

while True:

pipein = open(“/var/www/html/FIFO\_pipan”, ‘r’)

line = pipein.readline()

line\_array = line.split(’ ')

if line\_array[0] == “servo”:

##pan\_setting = scale(int(line\_array[1]), 80, 220, 250, 440)

##tilt\_setting = scale(int(line\_array[2]), 50, 250, 250, 440)

rudder\_setting = scale(int(line\_array[1]), 50, 250, 250, 440)

##engine\_setting = scale(int(line\_array[2]), 50, 250, 250, 440)

##bus.write\_word\_data(addr, 0x40, pan\_setting)

##bus.write\_word\_data(addr, 0x44, tilt\_setting)

bus.write\_word\_data(addr, 0x3C, rudder\_setting)

##bus.write\_word\_data(addr, 0x38, engine\_setting)

pipein.close()
