Noise with PanTiltHat

[quote=“stuartiannaylor, post:20, topic:3456”]
the forum has done it again and stripped tabs from the code you will have to indent the code.[/quote]
That’s because you are using the wrong code formatting… have a look at how I edited your post and enclosed the code in triple baccquotes.

Aaah doh I am not used to that method but still seem to have probs but hey no biggie

#!/usr/bin/env python
import time, datetime
import pantilthat

print pantilthat.__file__


clockwise = False
x = 0
pantiltwait = datetime.datetime.now()
panenablewait = datetime.datetime.now()
lastx = 0




#500 - 2400 is max pulsewidth of sg90
pantilthat.servo_pulse_min(1, 575)
pantilthat.servo_pulse_min(2, 575)
pantilthat.servo_pulse_max(1, 2325)
pantilthat.servo_pulse_max(2, 2325)


while True:

    
    if x == 90:
        clockwise = False
    if x == -90:
        clockwise = True

    now = datetime.datetime.now()
    
    if pantiltwait < now:
        if clockwise == False:
            x -= 1
        else:
            x += 1

        pantilthat.servo_enable(1, True)
        pantilthat.servo_enable(2, True)
        time.sleep(.02)
        
        pantilthat.pan(x)
        pantilthat.tilt(x)
        panenablesecs = ((float(abs((x - lastx))) / 60) * 1)
        panenableswait = now + datetime.timedelta(seconds=panenablesecs)
        lastx = x
            
            
        if x == 90 or x == -90:
            pantiltwait = now + datetime.timedelta(seconds=2)
                
            


    if panenablewait < now:
        pantilthat.servo_enable(1, False)
        pantilthat.servo_enable(2, False)

Apart from my dumb ass inability to get the forum formatting right the above is an example of how you can uses timers to disable the servo’s after a period of idle.
The problem still is the i2c delay and still think really its a firmware job but you can get something in code.

Really the api is not set up to be inching a degree at a time and prob needs another command pan(degrees,time)
tilt(degrees,time)

That will complete a movement in a set time.

Again firmware or you will always be fighting against a comms queue.
In the above example I put in a slight delay to enable the servo’s and this will always be needed way things are setup by the look of things.

Things though work fine for me and its fit for purpose but maybe a few things for a to-do list.

It was sort of hard to think of an example but whenever movement stops like at 90 or -90 you just disable the servo’s.
If the pic updated this to enable on a degree change it would be cool but maybe someone could come up with a better example.
Also for the enable I used a sleep command which was just laziness not being bothered to do another datetime check so program execution isn’t stopped elsewhere.

PS the new idle_timeout method has not come through.
You get the script but its missing in the library even on a pip install pantilthat --upgrade

That’s not the correct procedure to roll github-only changes, until a new version of the library is published (to pypi and apt) you need to:

sudo python setup.py install