pupil-labs / pyuvc

python binding to libuvc
Other
119 stars 51 forks source link

Absolute Focus Control holds an older value #71

Open yurikleb opened 4 years ago

yurikleb commented 4 years ago

When updating the "Absolute Focus" Control, the control value is updated in the camera however the dictionary value becomes the old value from the camera.

for example: If the current focus value in the camera is 100, the following command: controls_dict['Absolute Focus'].value = 150 will update the focus in the physical camera to 150 but controls_dict['Absolute Focus'].value will become 100.

Moreover, when the camera is in Autofocus mode, and a re-focus is is triggered — when the camera or the subject moves, or if the camera has a physical AF button or if the controls_dict['Auto Focus'].value = 1 command is triggered the controls_dict['Absolute Focus'] value is not updated.

Also, it seems the only way to get the latest focus value from the camera is to update the controls_dict['Absolute Focus'] by assigning a new value to it, which will update the camera and change controls_dict['Absolute Focus'].value to whatever value the camera had before the change, as described above.

This is especially crucial when switching from autofocus to manual focus, and want to make a smooth transition > to get the manual focus values to begin from the actual current focus value of the camera.

My current (hacky) workaround is a standalone dedicated variable for the "Absolute Focus" value, and using this function whenever I want to get the real focus value from the camera:

def get_current_focus(focus):
    controls_dict['Absolute Focus'].value = focus
    controls_dict['Absolute Focus'].value = controls_dict['Absolute Focus'].value
    focus = controls_dict['Absolute Focus'].value
    return focus

So when adjusting the focus manually I'm doing the following:

def focus_up():
    if controls_dict['Auto Focus'].value == 1:
        controls_dict['Auto Focus'].value = 0
        abs_focus = get_current_focus(abs_focus)

    abs_focus += 1
    controls_dict['Absolute Focus'].value = abs_focus

I'm not sure what would be the best solution for this. Since unlike in all the other controls in which the change takes effect immediately, the focus value update needs to happen only after the physical adjustment of the lens inside the camera is done, which can take from a few milliseconds up to a few seconds, especially when re-adjusting autofocus.

Maybe adding another method which gets the value from the camera is the way to go?