Arducam 64MP autofocus: cannot get/set LensPosition with libcamera C++ on Pi 4

Hi, I’m stuck trying to get AutoFocus working the way I need on an Arducam 64MP camera and I can’t find a way forward.

My setup: Raspberry Pi 4, headless, accessed over SSH. libcamera v0.0.0+3896-6019b245, and I’m coding in C++ against libcamera directly (not via the Python bindings). My project is about 90% of the libcamera-demo_cpp example, just with OpenCV stripped out.

What I’m trying to do:

  • Grab 3 frames as fast as possible, so I have to use RAW buffers
  • (grabbing a full 64MP frame takes ~400ms on a PI4, so time matters)
  • Run AutoFocus once, read the resulting “lens setting”, and then always reuse that focus distance
  • Be able to GET libcamera::controls::LensPosition after an AF completes
  • And be able to SET libcamera::controls::LensPosition again whenever I want

The AF itself works now — earlier the problem was the processing time autofocus needs to do its job, and once I dealt with that it started focusing fine. But I cannot find any way to actually read the lens position after AF, or to set it back afterwards. Is libcamera::controls::LensPosition the right way to do the GET/SET? Because I’m not getting anything usable out of it. Any pointers would really help, thanks.

Hi, thanks for the detailed writeup. LensPosition is indeed the control meant for exactly that (read the focus distance after AF, then restore it later), so you’re looking at the right API.

Before we dig into LensPosition, let’s make sure the sensor is being brought up cleanly, because a few things in startup can silently break control handling. Could you paste:

  1. The full libcamera log from your app start (the INFO/WARN/ERROR lines)
  2. The media device layout — which nodes the sensor, Unicam and ISP land on
  3. How you’re triggering AF and how many frames you run before you try to read the lens position

That’ll tell us whether the issue is your code or the build.

Sure, here’s the log and the layout.

Sensor: arducam_64mp, registered at /base/soc/i2c0mux/i2c@1/arducam_64mp@1a. Unicam is /dev/media1, ISP is /dev/media4. Doing all this over SSH, prompt is admin@raspberrypi:~/ramdisk/build $.

[3:48:12.533275815] [1551]  INFO Camera camera_manager.cpp:293 libcamera v0.0.0+3896-6019b245
[3:48:12.563548235] [1552]  WARN CameraSensorProperties camera_sensor_properties.cpp:174 No static properties available for 'arducam_64mp'
[3:48:12.563630326] [1552]  WARN CameraSensorProperties camera_sensor_properties.cpp:176 Please consider updating the camera sensor properties database
Open i2c bus error: No such file or directory
[3:48:12.574850517] [1552]  WARN RPI raspberrypi.cpp:1280 Mismatch between Unicam and CamHelper for embedded data usage!
[3:48:12.575708110] [1552] ERROR DelayedControls delayed_controls.cpp:87 Delay request for control id 0x009a090a but control is not exposed by device /dev/v4l-subdev0
[3:48:12.575905106] [1552]  INFO RPI raspberrypi.cpp:1404 Registered camera /base/soc/i2c0mux/i2c@1/arducam_64mp@1a to Unicam device /dev/media1 and ISP device /dev/media4
Camera configuration adjusted
[3:48:12.576463890] [1551]  INFO Camera camera.cpp:1035 configuring streams: (0) 9152x6944-SBGGR10_CSI2P
[3:48:12.576801346] [1552]  INFO RPI raspberrypi.cpp:765 Sensor: /base/soc/i2c0mux/i2c@1/arducam_64mp@1a - Selected sensor format: 9152x6944-SBGGR10_1X10 - Selected unicam format: 9152x6944-pBAA
AE
AE
AE
AE
AE
AE
END
admin@raspberrypi:~/ramdisk/build $

Let me know what you make of it.

Thanks, that’s useful. A few things stand out:

  • No static properties available for 'arducam_64mp' + the sensor properties DB warning: harmless for now, it just means the sensor isn’t in the properties database, so some tuning defaults are missing.
  • Open i2c bus error: No such file or directory: check that line — it usually means the wrong i2c bus or an index mismatch when the sensor driver probes.
  • Mismatch between Unicam and CamHelper for embedded data usage!: this affects how embedded metadata is read, which can matter for controls.
  • ERROR DelayedControls ... Delay request for control id 0x009a090a but control is not exposed by device /dev/v4l-subdev0: that’s the one I’d look at closely, it means a control the pipeline expects isn’t being exposed.

The registration itself looks fine (9152x6944-SBGGR10 on Unicam, pBAA out), and you’re clearly getting frames since the AE lines run.

Two questions:

  1. When exactly do you try to read LensPosition — during the AF, or after?
  2. How are you triggering AF in your app? The reference libcamera-demo_cpp sets up the display and triggers AF once at 10 frames; triggering once at 3 frames tends to be too early and can give you a bogus reading. Worth matching that pattern first.

OK so I took your suggestion and added the display, and set it to trigger once at 10 frames. But 3 frames triggered once still feels way too fast to me, and honestly I’m not sure the trigger is landing at the right time.

AF is confirmed working now — the earlier problem was just the processing time autofocus needs, and after sorting that it focuses correctly. But even with the 10-frame trigger I still can’t see any lens position value coming back. Trying to read LensPosition gives me nothing usable, and writing to it doesn’t seem to change anything either.

So the AF works, but the GET/SET of the lens position is where I’m totally stuck. Am I even looking in the right place with LensPosition, or is there some other control I should be using?

You’re looking in the right place — libcamera::controls::LensPosition is the intended API for getting the AF lens position after autofocus completes and for setting it back. So the concept is correct.

Can you describe exactly what happens when you try to use it? Specifically:

  • When you read it after AF, do you get an error, a default value (like 0), or does it just never appear in the request metadata?
  • When you set it, does the AF immediately re-run / override it, or does nothing happen at all?

That distinction tells us whether the control is wired up but being overwritten, or whether it isn’t available in this build at all.

That’s the thing — it doesn’t really behave like it exists. When I try to set it, nothing happens, no error, and no visible effect on focus. When I try to read it after an AF, I don’t get a meaningful position out of it at all — it’s like the value just isn’t there in the metadata. No error from libcamera, just silence.

So my code thinks it’s requesting LensPosition, but nothing comes back and nothing changes. Does that mean the control isn’t actually implemented in this version?

Yeah — that behaviour points to exactly that. The LensPosition control isn’t exposed/implemented in the libcamera build you’re running (v0.0.0+3896-6019b245). That’s why your request compiles and runs fine but you get no value back and setting it does nothing: the control simply isn’t there to handle yet, so it isn’t the code on your side that’s wrong.

It’s the correct control to use for your use case (once AF has finished, read the position, then restore it), it just hasn’t been added to the release you have. We’ll be adding the LensPosition control in a later libcamera release.

For now the practical thing is to hang tight on this specific feature — I’ll let you know as soon as the updated release with LensPosition support is available so you can retest the GET/SET. Everything else (the RAW capture path and the AF itself) is working, so you’re only blocked on the lens-position control.

Understood, that explains why nothing I tried made a difference. Just to confirm my plan once it lands: I run AF once, read LensPosition to get the resulting focus distance, and then set LensPosition back to that value whenever I need the same focus — that’s the flow?

Is there any estimate at all on when the release with it will be out, or should I just keep an eye out?

Yes, that’s exactly the intended flow once it’s available: trigger AF, wait for it to complete, read libcamera::controls::LensPosition back to capture the focus distance, then set that same value whenever you want to lock to it.

No firm date I can give you right now — the control will be added in a later libcamera release, and I’ll ping you as soon as it’s there so you can pull it and retest. Quick recap of where we landed for anyone following:

  • LensPosition is the right API for getting the AF lens position after AF and setting it back.
  • In libcamera v0.0.0+3896-6019b245 the control isn’t exposed/implemented yet, so reads return nothing meaningful and sets have no effect — this isn’t a code problem on your side.
  • AF working is confirmed; the earlier AF issue was just the processing time it needed, and that’s resolved.
  • Match the reference libcamera-demo_cpp behaviour of triggering once at ~10 frames rather than 3, which is too fast.

So the interim advice is to wait for the LensPosition control to be added, and retest your GET/SET flow with that build. I’ll let you know the moment it’s available.