Weird actions from ServoHat

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:

  • index.php

    pipan.php

    pipan.js


  • 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()