Servo pHAT: Initial position wrong

After being initialized, the pHAT’s Python driver does not return correct position of a servo until that servo is moved.

Here’s a minimal working example showing the anomaly:

import pi_servo_hat

drv = pi_servo_hat.PiServoHat()
drv.restart()

print(drv.get_servo_position(0, 180))  # prints: 3420.0 (Incorrect!)
drv.move_servo_position(0, 90, 180)
print(drv.get_servo_position(0, 180))  # prints: 89.82421875000004 (Correct)

Why is this an issue? Getting proper servo position is important in many if not all applications. Without the ability to establish the initial position, the only workaround is to move the servo blindly. Not knowing from where the motion originates precludes easing (i.e., interpolating motion to be smoother) from being implemented. That in turn results in unstable behavior of the physical assembly which can contain multiple servos and can often be delicate.

You have to set the position first, you can’t “read” the position out of the servo.

Thanks for your comment. That made me think that not resetting the PCA 9685 should solve this, and it does.

SparkFun: Perhaps you could update the library to return a value (e.g., None or -1) to indicate the chip has been reset and no position has been set yet. Or is 3420.0 that value and I’ve missed it in the docs?

The issue is in your code, not the library, you are attempting to read a variable that hasn’t been set yet so it has an undetermined value.

Set it first, then try to read it back and you’re all good.

I disagree. The library should ensure a value corresponding to “unset” should be returned.