jim-easterbrook / python-gphoto2

Python interface to libgphoto2
GNU Lesser General Public License v3.0
376 stars 59 forks source link

Changing focus on Canon R - is this the best way to do it? #135

Closed pootle closed 2 years ago

pootle commented 2 years ago

I'm writing a program to adjust focus for focus stacking, with my camera the valid values of 'manualfocusdrive' are ['Near 1', 'Near 2', 'Near 3', 'None', 'Far 1', 'Far 2', 'Far 3']

But if I change setting (say) 'Far 2' several times, then only the first takes effect

It seems you have to alternate 'Far 2' with 'None' for subsequent settings to take effect. Is this the proper way to rack the focus by multiple steps?

Watching the lens' focus while running this, the focus shifts only once in the first set of tests, but every change shifts focus on the second set of tests. I'm running the standard distro version (Fedora 34) 2.0.0, and I get the same result running on a Raspberry Pi which is more up to date:

python -m gphoto2
python-gphoto2 version: 2.3.1
libgphoto2 version: ['2.5.28.1', 'standard camlibs (SKIPPING docupen lumix)', 'gcc', 'no ltdl', 'no EXIF']
libgphoto2_port version: ['0.12.0', 'iolibs: disk ptpip serial usb1 usbdiskdirect usbscsi', 'gcc', 'ltdl', 'EXIF', 'USB', 'serial without locking']
python-gphoto2 examples: /usr/local/lib/python3.9/dist-packages/gphoto2/examples

Here is the tests program

#!/usr/bin/env python

import gphoto2 as gp, time

wtest = 'manualfocusdrive'

def main():
    camera=gp.Camera()
    try:
        camera.init()
    except gp.GPhoto2Error as gpe:
        if gpe.code == -105:
            print('ERROR: Unable to contact camera - is it connected? has it timed out?', file=sys.stderr)
            sys.exit(1)
        else:
            raise
    cam_cfg=camera.get_config()
    fchangewidg = cam_cfg.get_child_by_name(wtest)
    for x in range(6):         # this cycle only changes focus once
        fchangewidg.set_value('Far 2')
        camera.set_config(cam_cfg)
        time.sleep(.5)
    for x in range(6):        # this cycle only changes focus once
        fchangewidg.set_value('Near 2')
        camera.set_config(cam_cfg)
        time.sleep(.5)
    time.sleep(1)
    for x in range(6):        # this cycle changes focus every time
        fchangewidg.set_value('None')
        camera.set_config(cam_cfg) 
        fchangewidg.set_value('Far 2')
        camera.set_config(cam_cfg)
        time.sleep(.5)
    for x in range(6):        # this cycle changes focus every time
        fchangewidg.set_value('None')
        camera.set_config(cam_cfg) 
        fchangewidg.set_value('Near 2')
        camera.set_config(cam_cfg)
        time.sleep(.5)
if __name__ == '__main__':
    main()
jim-easterbrook commented 2 years ago

I have no idea how to set the focus on your camera. If your experiments have found a way that works then you already know more than I do. This page is for reporting bugs in the python interface. Questions about how to use libgphoto2 should be sent to one of the gphoto2 mailing lists.

pootle commented 2 years ago

Ah! righto thanks

jim-easterbrook commented 2 years ago

One possibility: try calling fchangewidg.set_changed instead of changing the value back to None. I guess this tells libgphoto2 that the widget value has changed (even though it hasn't) so the value gets sent to the camera again when you call set_config.