Pan Tilt Hat Error

I’ve had my Pan/Tilt hat working fine for a while now. This morning it refuses to work. When I turn the Pi on, the camera moves down and left. If I run a script to move the hat by 5 degrees I get:

Traceback (most recent call last):
  File "./down_1", line 4, in <module>
    zpos = pantilthat.get_tilt()
  File "/usr/lib/python2.7/dist-packages/pantilthat/pantilt.py", line 449, in get_servo_two
    return self._servo_us_to_degrees(us, us_min, us_max)
  File "/usr/lib/python2.7/dist-packages/pantilthat/pantilt.py", line 148, in _servo_us_to_degrees
    self._check_range(us, us_min, us_max)
  File "/usr/lib/python2.7/dist-packages/pantilthat/pantilt.py", line 137, in _check_range
    max=value_max))
ValueError: Value 51200 should be between 575 and 2325

This is the script I’m using:

#!/usr/bin/env python
import pantilthat
import time
zpos = pantilthat.get_tilt()
if zpos < 90:
	pantilthat.tilt(zpos+5)
else:
	zpos = zpos
print "Here: ",zpos
time.sleep(1)

I’ve recently been messing with some other Python and installed some other libraries - mainly to do with BLE detection. Is it possible I’ve messed something up? I’ve also added Apache support for Python.

If push comes to shove, I’ll flash the card and start again but I’d like to get to the bottom of this if possible.

Cheers
Andy

It’s a bug in pantilthat.get_tilt(), it’s actually requesting the last set tilt value from the servo driver, which powers on to an out of range value. It doesn’t give you the servos starting position at all, and thus is meaningless to run before you’ve set the servo position manually.

It’s possible during some earlier tinkering that you set an in-range servo position, and that’s just been remembered by the driver when your new code ran. But now you’ve power cycled your Pi, it’s initialised back to the invalid value.

I need to figure out a bugfix for this- I’d thought I had already fixed it.

In your case you could probably:

zpos = 0
try:
    zpos = pantilthat.get_tilt()
except ValueError:
    pass

Which will make zpos default to 0 instead of throwing the ValueError.

Thanks for that. That’s now working again in as much as I can move the camera.

This is VERY new to me so forgive me if this is a daft question.

I want to move the camera by a certain amount, not to a certain position. How can I do that if I don’t know the current position?

Once you’ve moved it, the get_tilt() and get_pan() methods should return a sensible figure that will tell you approximately where the camera is positioned, and you can offset it from there.

Alternatively you could use variables in your code to store the pan/tilt position and update that before writing it to the servos. This would produce basically the same results, except if your program exits and re-starts it would have to query the PanTiltHAT again for what it thinks the servo positions are or risk jolting the servos abruptly to a new default position.

Ie, if your program starts with:

deg_pan = 0
deg_tilt = 0
pantilthat.pan(deg_pan)
pantilthat.tilt(deg_tilt)

Then every time it launches the servos will reset back to that position. Now you have a known good starting position you can just adjust your variables:

deg_pan += 2 # Add 2 degrees to pan
deg_tilt -= 2 # Subtract 2 degrees from tilt

And update the servos again.

Thank you for that! Much appreciated. Bad day yesterday and I was starting to feel despondent about everything to do with technology. This gets me back on track.

When I first set things up, I changed the server script to set the servos back to 0. I changed that at some point but I’d forgotten. That’s probably what caused this.

All I have to do now is get my head round Flask and Python (properly) and my plans for global domination will be a step closer.

1 Like