Anyway to disable the autofocus for cameras?

Focus%20C925%20Logitech

My Logitech c925 is a bit out of focus but its not so bad. I just use it to monitor my print.
I noticed if your webcam is too close its worst.
90% of the time i use it in my 3D printer and the other 10% i use it as a normal webcam on my PC.

So that why i dont investigate a lot to manualy set a constant focus value.

PS : Dont forget to admire this beautiful benchy on my prusa MK3 :heart_eyes:

I can't help you on disabling autofocus on a C920 (I own one, but have not used it with OctoPrint. I just ended up using a C270 with OctoPrint. It's fixed focus, and fairly inexpensive: The cheapest I've found on Amazon is $20 with free shipping. The focus was set it bit to far for print monitoring in my set up, so I opened it up and tweaked the focus ring a bit, and it's been fine ever since.

Yeah i also think that the C270 is also the better choice for octoprint.

  • It's cheap but with a good quality (higher than the raspberry pi Cam)
  • The focus can be adjust manualy by removing the front cover.

Even though I ended up using a C270, I could swear I read somewhere that someone was able to send a command to disable autofocus on some of these cameras. I just can't find it now.

Until you find something, you might try moving the camera further away from the print. The focus gets a little less sensitive when you are not right in close.

I've got a C910 so don't know if this would work for you, but I create a script called "fixed-focus" on my system that I call from /etc/rc.local (which makes the linux sysadmin in me cry a little) that contains:

#!/bin/bash

uvcdynctrl -s "Focus, Auto" 0
uvcdynctrl -s 'Focus (absolute)' 122

You need to install uvcdynctrl (that's got to be the worst program name I've run across) before this will work:

sudo apt install uvcdynctrl

I've also discovered that with certain versions of mjpg_streamer, when you connect on port 8080 you'll have a "control" option which gives you full control.

I've detailed this on my camera mount for my old printer.

6 Likes

I re-tried the programm uvcdynctrl and used it succefully. To keep my settings when i restart i used this two line on the /etc/rc.local it work great !

Thanks !

3 Likes

Thanks. Confirm. It works fine for my setup.
I changed the value 122 to 20.

uvcdynctrl -s 'Focus (absolute)' 30
1 Like

Just want to leave this here for anyone who stumbles across it:
To get the current focus distance to use in your script, put an object on the build plate, make sure the camera is focused and then run the following to get the current value:

uvcdynctrl -g 'Focus (absolute)'
2 Likes

You can ssh into Octoprint and paste these two lines into the terminal window to manually focus a Logitech C920. Keep pasting and hitting enter while watching the webcam stream but change the last value in the second line until it's focused where you want...

sudo v4l2-ctl --set-ctrl=focus_auto=0
sudo v4l2-ctl --set-ctrl=focus_absolute=40

(The default values are auto=1 and absolute=8189)

I'm using an OctoPi so it doesn't have the Raspian desktop or all the usual places you'd stick a startup script so I finally figured out you can add those two lines to the top of mjpg-streamer/start.sh if you want your values to stick on reboot...

cd\
nano mjpg-streamer/start.sh

2018-09-28_013944

7 Likes

Thanks for this thread gang - you helped me resolve a thing that has been making me crazy for a while in a easy, quick and concise fashion!

This works - but please - do not ever put code above the hashbang-line :slight_smile:

Unless very sure of what you are doing - add the new code at the end of the script.

3 Likes

@replayreb Thanks for sharing the command strings!

Worked great for my Logitech C910 camera without having to add any additional programs to the Pi!

I was about ready to throw this thing in the trash, sure glad I didn't. It may be a bit aged by today's standards but the picture quality is beautiful at 1920x1080 @ 15FPS, and now that I have the autofocus under control I don't have to bother with any new mounts.

:+1:

Thanks for the help this worked perfect on my C920.

I was wondering if anyone can tell me with why I can't change directories. I have been using the commands
sudo v4l2-ctl --set-ctrl=focus_auto=0
sudo v4l2-ctl --set-ctrl=focus_absolute=25
to stop the auto focus on my c920. that works great. Until I restart.
I wanted to edit the mjpg-streamer/start.sh
I am using this command
cd
nano mjpg-streamer/start.sh
and I get this.
-bash: cdnano: command not found

using this command
cd
mjpg-streamer/start.sh
gets me this.
mjpg-streamer/start.sh: 30: mjpg-streamer/start.sh: ./mjpg_streamer: not found

any help with this would be greatly appreciated. As one can see I am not a Linux expert.
OctoPrint version : 1.3.10
OctoPi version : 0.16.0

you must tell cd (change directory) which directory you wish to change to:

cd mjpg-streamer
nano start.sh

is what you're looking for.

1 Like

Thanks BerndJM. that worked perfect! many thanks.

Thanks for posting this. I used the v4l2 commands and they work when manually running them from command line. I then tried to include them into the start.h file, and they are being ignored. I have tried them at the bottom of the file and at the top with no luck. Anyone else having this same issue? Should I instead create a batch job or a cron job and fire this off separately?

Thanks for any help

Nordicblue- I thought I was in the clear, but after a reboot. I noticed that my Logitech 920 is back to the auto focus dance. Sigh. I am in the same boat as you. I wish someone might have a fix/answer. -All the best.

Had the same problem with my Logitech C922.

From this post Anyway to disable the autofocus for cameras? I added the two lines to /etc/rc.local instead. Works great after restart.

## Custom hack for webcam focus
sudo v4l2-ctl --set-ctrl=focus_auto=0
sudo v4l2-ctl --set-ctrl=focus_absolute=15
1 Like

@tropic, thanks for the assistance! I will try it today