Arducam_64mp autofocus via libcamera-still

So I received a 64MP camera, installed using the instructions from the website, on a brand new installation.
Unfortunately, it just does not focus at all using libcamera-still.
I can open the Arducam focusing software, and get a sharp image that way, but I want to run this system headless, and with auto-focus, not human-controlled focus.

I have used a slew of commands to no success, and cannot focus using these for example:

libcamera-still --autofocus-on-capture=1 --width 4624 --height 3472 --nopreview --output autofocus-on-capture-1-setwidthHeight.jpg

libcamera-still --lens-position=0 --width 4624 --height 3472 --nopreview --output lens-position-0.jpg

libcamera-still --lens-position=ā€œdefaultā€ --width 4624 --height 3472 --nopreview --output lens-position-default.jpg

How do I get these cameras to auto-focus using libcamera-still?
How can I set the lens position to focus at a given distance?
Is there clear up-to-date documentation for these cameras, in one single place?

Iā€™m having this same issue. Reading recent posts, it looks like edward is still working on this problem. Arducam_64mp autofocus via picamera2 - #43 by Edward
On that same thread, i found this: Arducam_64mp autofocus via picamera2 - #36 by DaveMcD
which is the code that dave used is: libcamera-still -t 0 -n --autofocus-mode manual --lens-position 2 --width 9152 --height 6944 --denoise cdn_off --gain 9 --shutter 10000 --awbgains 2.0,1.8 --signal -o PiCam1_%04d.jpg --framestart 1
It seemed like it was trying to work for me, but it ultimately didnā€™t.
So, I still have the same three questions you do.

@neezem_og
When you say sounds like it was trying to work you mean that you heard the click? My cmd line is in --signal mode which means its waiting for an input before it takes the imaage so you would hear it click but nothing would happen until it gets a SIGUSR1 call.

if you replace that with --keypress (you might want to check the docs on that Iā€™m typing this from memory) then the command should launch and wait for key input (again check docs for which keys it expects)

Otherwise you could remove --signal all together to let it just take the photo. You would probably want to set -t to something other than 0 though. A full res image can take a while to take, compress, and save so Id test it with something like -t 10000 for 10 seconds of open time before it stops and go up or down from there.

Mine is defitively set to manual focus position but was only working in 0.11 of the drivers not 0.12 which is what you get currently following the instructions. I installed via the instructions and then manually downgraded libcam-dev and libcam-apps by downloading the previous releases from the github and installing.

You could also try seeing what happens if you lower the resolution.

A lot of the other commands could be stripped out as they are just my settings for the camera to manually control everything for now.

@dot.dot
Im not in front of my Pi to check right now but the commands looks a little wrong.

yours:
libcamera-still --autofocus-on-capture=1 --width 4624 --height 3472 --nopreview --output autofocus-on-capture-1-setwidthHeight.jpg

I dont think you want those = in either of your commands, just a space and the value.

Ive not tried autofocus on capture myself but did previously use some of the other autofocus commands. It really did need quite good lighting for it to work for me. too dark and the autofocus algorithm couldnt resolve.

you prob need a -t value in there Iā€™d say or the command might close before itā€™s done anything.

You should probably have preview on while youā€™re testing these to see if its trying to work. I guess it does nothing with the on capture focus call but if you change to the others youā€™d see stuff happening perhaps.

You may need to be on a downgraded driver versionā€¦ 0.12 seems to have some potential problems. Iā€™m on 0.11 which I manually installed by downloading libcamera-dev and libcamera-apps from the releases area of the github and then installing.

Iā€™ll try and test autofocus on my setup later today and post what happens.

OK. so Iā€™ve done some experiments and have a list of commands that worked fine for me and some interesting observations. NOTE I have tried this on then 0.11 version of the drivers not newer ones. I may test them later.

Short story is with no preview I can consistently capture a 9152x6944 image with autofocus. Previews with a small preview size at full res dont allocate enough memory and fail but resolution above 4576 but lower than 9152 could work.

If I flushed the buffer by running a no preview and no image taking at all then I could run a full size capture with a small preview. Might not be consistent but it worked for me 3 times in a row,

The picamera2 python libraries are something Iā€™m yet to play with (they talk to libcamera with more controls) but the documentation there mentions being able to allocate and shift buffers as well as previewing then closing it down to take a photo. The libcamera terminal commands are a pretty limited hook into the libcamera apps so once if can get a basic version of them working like my working commands below you might consider looking at using piCamera2

My test commands below with my notesā€¦

-works, autofocuses and saves to home directory. No preview
libcamera-still -t 10000 -n --autofocus-mode auto --width 9152 --height 6944 -o PiCam1.jpg

-Same as above but letting there be a preview of default size. Unable to request 1 buffers: Cannot allocate memory
libcamera-still -t 10000 --autofocus-mode auto --width 9152 --height 6944 -o PiCam1.jpg

-tried lowering previewsize still failed.Unable to request 1 buffers: Cannot allocate memory
libcamera-still -t 10000 -p 100,100,400,300 --autofocus-mode auto --width 9152 --height 6944 -o PiCam1.jpg

-works fine, half res with small previewā€¦ pretty sure default preview works here too.
libcamera-still -t 10000 -p 100,100,400,300 --autofocus-mode auto --width 4576 --height 3472 -o PiCam1.jpg

-works - tried upping the res a little
libcamera-still -t 10000 -p 100,100,400,300 --autofocus-mode auto --width 6864 --height 5208 -o PiCam1.jpg

-fails to allocate buffers again
libcamera-still -t 10000 -p 100,100,400,300 --autofocus-mode auto --width 9000 --height 6828 -o PiCam1.jpg

-back to no preview but trying autofocus on capture. seemed to work but the focus might be off a little
libcamera-still -t 10000 -n --autofocus-on-capture --width 9152 --height 6944 -o PiCam1.jpg

Now an intersting thing came up.
If I ran just

libcamera-still -t 10000 -n

to clear the buffer then it would work the next time I ran a full size capture with a small preview window such as

libcamera-still -t 10000 -p 100,100,400,300 --autofocus-mode auto --width 9152 --height 6944 -o PiCam1.jpg

@DaveMcD thanks for the very complete answers.
The equal sign is just implicit notation by default, so you can use them or not.

I removed libcamera-dev + libcamera-apps v0.0.12 and reverted back to v0.0.11 from here, but even using your commands does not work for me. the images are completely out of focus, even when used somewhere with loads of contrast and light.
Even using lens-position with a variety of values yields no change in focus point at all.

My working theory is that somewhow libcamera-still does not trigger the focusing routine of the camera. I can only use my system in headless mode via SSH.

My methodology:

set the focus to 423, itā€™s sharp enough for a test,and close the FocuserExample:

python3 ./Arducam-Pivariety-V4L2-Driver/focus/FocuserExample.py -d /dev/v4l-subdev1 --focus-step 100

Take a photo, without invoking any focusing commands, this photo is sharp as we have manually focused it beforehand.

libcamera-still -n -o manual_focus_via_FocuserExample_set_to_423.jpg

Take a photo invoking the lens-position flag, to move the focus to 0 (infinity), i expect the focus point to move, but it does not, checking with the FocuserExample, it is still set at 423:

libcamera-still -n --lens-position 0 -o manual_focus_via_LensPosition_to_0.jpg

Running the same command that worked for you, does not move the focus point at all, i removed the resolution, iā€™m using a Pi3B, and just using the default all along:

libcamera-still -t 10000 -n --autofocus-mode auto -o PiCam1.jpg

Now perhaps my focus point was good enough for the focusing algorithm to not need to change it, so I am going to unfocus the camera manually, set it to 823 using FocuserExample, close the FocuserExample, then try again your command, then check using the FocuserExample.py, and itā€™s still at 823, which means no autofocus took place.

libcamera-still -t 10000 -n --autofocus-mode auto -o PiCam1.jpg

In my mind the driver is bugged. it is the most obvious place, as the failure mode is exactly the same between v0.0.11 and v0.0.12.

@dot.dot
Sorry this isnā€™t working for you. Iā€™m on pi4B so not sure if itā€™s difference between the 4 and 3 model.

My wife has a 3 that I might be able to hijack to see if it works.

Iā€™ve also not played with the focuser example app but I really should.

Have you tried downgrading even to 0.10 at all? someone else mentioned 0.11 not working for them either.

Ive also not tried headless through SSH. I usually vnc to mine or am connected with a desktop.

@DaveMcD
v0.0.10 seems to be changing the values when using the --autofocus flag.
Itā€™s too dark to test the AF right now, but Iā€™ll run a few tests tomorrow.

Hopefully success awaits, after 3 days of tinkering.

@dot.dot

This is the best focusing way to save the image without displaying the image

At present, I still recommend that you use the latest version 0.0.12, which is consistent with the parameters of the official Libcamera-Apps. If there is a problem in this version, please ask at any time, we will find a way to solve it.

image

Hi @Edward thanks for jumping in.

I am running a Pi3B.

I went ahead and removed the libcamera-apps and libcamera-dev i had on my system:

sudo apt remove libcamera-dev
sudo apt remove libcamera-apps

Then:

wget https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/libcamera-v0.0.5/libcamera-dev-0.0.12-bullseye-armhf.deb
wget https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/libcamera-apps-v0.0.5/libcamera-apps-0.0.12-bullseye-armhf.deb

installed them:

sudo apt install ./libcamera-dev-0.0.12-bullseye-armhf.deb
sudo apt install ./libcamera-apps-0.0.12-bullseye-armhf.deb

I went in and manually set the focusing using the FocuserExample.py to 0 and closed the FocuserExample.py by hitting q to be able to compare values using a second method.
Then I went to use the camera:

libcamera-still --autofocus-on-capture=1 --width 4624 --height 3472 --nopreview --output autofocus-on-capture-1-setwidthHeight.jpg

And itā€™s still not focusing:

Checked the focus value on FocuserExample.py and it is still set to 0.
Donā€™t know what to do at this point.

Edit:
For completeness this is my ssh cli:

user@raspberrypi:~ $ libcamera-still --autofocus-on-capture=1 --width 4624 --height 3472 --nopreview --output autofocus-on-capture-1-setwidthHeight_resetFocusTo0afterReboot2.jpg
[4:18:43.464627023] [1538] INFO Camera camera_manager.cpp:299 libcamera v0.0.0+4186-d1dc0373
[4:18:43.475171851] [1540] WARN CameraSensorProperties camera_sensor_properties.cpp:205 No static properties available for ā€˜arducam_64mpā€™
[4:18:43.475260705] [1540] WARN CameraSensorProperties camera_sensor_properties.cpp:207 Please consider updating the camera sensor properties database
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
Ioctl read i2c error:: Remote I/O error
[4:18:43.521024074] [1540] WARN RPI raspberrypi.cpp:1308 Mismatch between Unicam and CamHelper for embedded data usage!
[4:18:43.522503655] [1540] INFO RPI raspberrypi.cpp:1425 Registered camera /base/soc/i2c0mux/i2c@1/arducam_64mp@1a to Unicam device /dev/media3 and ISP device /dev/media0
[4:18:43.523628914] [1538] INFO Camera camera.cpp:1028 configuring streams: (0) 4624x3472-YUV420
[4:18:43.524230580] [1540] INFO RPI raspberrypi.cpp:805 Sensor: /base/soc/i2c0mux/i2c@1/arducam_64mp@1a - Selected sensor format: 4624x3472-SBGGR10_1X10 - Selected unicam format: 4624x3472-pBAA
[4:19:02.431118314] [1538] INFO Camera camera.cpp:1028 configuring streams: (0) 4624x3472-YUV420 (1) 4624x3472-SBGGR10_CSI2P
[4:19:02.532334257] [1540] INFO RPI raspberrypi.cpp:805 Sensor: /base/soc/i2c0mux/i2c@1/arducam_64mp@1a - Selected sensor format: 4624x3472-SBGGR10_1X10 - Selected unicam format: 4624x3472-pBAA
Still capture image received

And I hear 2 clicks when I run the command, straight away. one right after the other, like a second a way, clack clack

Thanks for your response. No, i heard no click. I used to hear a click before, but when I updated to 0.0.12, I donā€™t hear it anymore. But, i think that i was thinking more in terms of blurriness. Itā€™s like the code needed something to be changed to get the focus to work.
I replaced the --signal command with --keypress and it took the picture, but there was no focus adjustment. I removed --signal and --keypress, and changed the -t to 10000, like you recommended, and still blurry. At the same time, Iā€™m working with 0.12 still as edward recommends, so maybe thatā€™s why your commands arenā€™t working with mine. I did try downgrading though, but iā€™m not sure if I used your commands. Iā€™ll try downgrading to 0.11 and work with your commands.
I also did try lowering the resolution, but that didnā€™t change the blurriness of the photo.

I tried this on pi4B using 0.12 drivers with no success, as well.

@dot.dot

This normal phenomenon means that the motor is working.

Can you look at your kernel version?

uname -a

I want to ask if all the scenes are like this?

What happens if you shoot a textured object in a bright environment.

@neezem_og

Hearing the clicking sound is not a criterion for judging whether the motor is working. Judging whether the focus function is normal or not depends on whether the focus has changed.

The commands of version 0.12 are different from those of version 0.11, and they are definitely not applicable.

Can you also give your information in detail? What command to use, what is the effect of the image, dmesg information. Like @dot.dot .

Hi, here is the output from uname -a

The same happens regardless of what I try to shoot, light, dim, textured, close, far.
I can also not use lens-position at all.

@dot.dot

Itā€™s kind of weird, it seems like the motor doesnā€™t really work.

I can take a look remotely.
You can email me [email protected]

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux

Sorry for the late reply, my dmesg log is too long, i will try to get it in tomorrow, but here is my output, still with blurry photos

pi@raspberrypi:~ $ libcamera-still -t 0 --autofocus-mode auto
Made X/EGL preview window
[0:02:16.266837289] [1206] INFO Camera camera_manager.cpp:299 libcamera v0.0.0+4186-d1dc0373
[0:02:16.273505544] [1208] WARN CameraSensorProperties camera_sensor_properties.cpp:205 No static properties available for ā€˜arducam_64mpā€™
[0:02:16.273641413] [1208] WARN CameraSensorProperties camera_sensor_properties.cpp:207 Please consider updating the camera sensor properties database
[0:02:16.320172889] [1208] WARN RPI raspberrypi.cpp:1308 Mismatch between Unicam and CamHelper for embedded data usage!
[0:02:16.321685283] [1208] INFO RPI raspberrypi.cpp:1425 Registered camera /base/soc/i2c0mux/i2c@1/arducam_64mp@1a to Unicam device /dev/media2 and ISP device /dev/media0
[0:02:16.322797977] [1206] INFO Camera camera.cpp:1028 configuring streams: (0) 4096x3074-YUV420
[0:02:16.323389139] [1208] INFO RPI raspberrypi.cpp:805 Sensor: /base/soc/i2c0mux/i2c@1/arducam_64mp@1a - Selected sensor format: 4624x3472-SBGGR10_1X10 - Selected unicam format: 4624x3472-pBAA
pi@raspberrypi:~ $ libcamera-still -t 0 --autofocus-mode continuous
Made X/EGL preview window
[0:02:47.268224995] [1219] INFO Camera camera_manager.cpp:299 libcamera v0.0.0+4186-d1dc0373
[0:02:47.272583216] [1221] WARN CameraSensorProperties camera_sensor_properties.cpp:205 No static properties available for ā€˜arducam_64mpā€™
[0:02:47.272642085] [1221] WARN CameraSensorProperties camera_sensor_properties.cpp:207 Please consider updating the camera sensor properties database
[0:02:47.301509922] [1221] WARN RPI raspberrypi.cpp:1308 Mismatch between Unicam and CamHelper for embedded data usage!
[0:02:47.302400803] [1221] INFO RPI raspberrypi.cpp:1425 Registered camera /base/soc/i2c0mux/i2c@1/arducam_64mp@1a to Unicam device /dev/media2 and ISP device /dev/media0
[0:02:47.303302239] [1219] INFO Camera camera.cpp:1028 configuring streams: (0) 4096x3074-YUV420
[0:02:47.303923623] [1221] INFO RPI raspberrypi.cpp:805 Sensor: /base/soc/i2c0mux/i2c@1/arducam_64mp@1a - Selected sensor format: 4624x3472-SBGGR10_1X10 - Selected unicam format: 4624x3472-pBAA

@neezem_og

Use the following command to see if the focal length value changes before and after using the libcamera command.

v4l2-ctl -l -d /dev/v4l-subdev1

To make sure weā€™re in a consistent state, use libcamera 0.0.12 with the following commands.

libcamera-still --autofocus-on-capture=1 --width 4624 --height 3472 --nopreview --output autofocus-on-capture-1-setwidthHeight.jpg

If you can, please record all test information and send it to me.

@dot.dot

I checked and found that the latest libcamera needs to be used with the latest kernel.

We will update our installation script later, the specific reasons I list below.

The new version of libcamera obtains the motor driver node through the following code.

Since there is no MEDIA_LNK_FL_ANCILLARY_LINK media link on version 5.10.92, the motor driver node cannot be obtained. So autofocus cannot be used.

MEDIA_LNK_FL_ANCILLARY_LINK Media Link Submission Record.

v4l2 driver creates link submission record

You can see that the commit is in version 5.15.30 of the Raspberry Pi kernel.
2bbdf031-d50f-4553-95a4-008f268babac

So the current libcamera v0.0.12 version only supports Raspberry Pi kernel 5.15.30 and later.

Way to update:

sudo apt-get update
sudo apt-get upgrade

Then reboot