Hi there,
I have the pan-tilt hat for the raspberry pi.
I’ve built and installed it and confirmed it works, however it only works from the command line interactively and not from a script.
e.g. if I run:
python3
import pantilthat
pantilthat.pan(60)
The hat pans correctly, however if I have a script, panscript.py:
#!/usr/bin/python3
import pantilthat
pantilthat.pan(60)
that does the same and I run it
python3 panscript.py
the hat does nothing. weirdly the getpan and gettilt still return updated numbers as if the hat had moved.
Any help appreciated!
Well that’s curious. What happens if you put a small delay between import pantilthat
and pantilthat.pan(60)
like:
import time
import pantilthat
time.sleep(0.1)
pantilthat.pan(60)
ok, so some digging later.
If i give it an absolute value like
import pantilthat
pantilthat.pan(90)
it works even in the script, however if i use a variable in my script:
import pantilthat
panv = pantilthat.get_pan()
panv = panv - 10
pantilthat.pan(panv)
it does nothing (even if i add time.sleep(0.1)), yet it works interactively ie if i type
python3
then all the commands manually.
Any suggestions?
If you output your panv
value in your script, what do you see?
Bear in mind get_pan()
doesn’t get the position of the servos, but rather the last pan value sent to the controller so it can be a bit misleading.
I’ve added printing out the panv value:
import pantilthat
panv = pantilthat.get_pan()
panv = panv - 10
print("Panv: “, end=” ")
print(panv)
pantilthat.pan(panv)
and the script outputs a number that corresponds to the panv.
Its odd, but the script seems to be intermittently working. Sometimes if i issue it it now works, but won’t work if i run a second time just after. This is really making me scratch my head.
Given get_pan() returns the last position, i may have to keep a record of the position somewhere else to enable relative movements.