Anyway to disable the autofocus for cameras?

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

thanks for the info..
i'll try to do it more "from scratch" for someone who is not so familiar with linux and commands in general, combining all the previous comments:

  1. install putty if you are in windows and this will let you enter the files inside raspberry pi
  2. at tab session and Host Name field add octopi.local or your raspberry ip (you can find this in your router's page 192.168.1.1..and it will be something like 192.168.1.X), choose SSH, port 22, Open
  3. username is default "pi", and password "raspberry"
  4. you have to go to folder mjpg-streamer, so you enter the command cd mjpg-streamer/
  5. now enter sudo v4l2-ctl --set-ctrl=focus_auto=0 to disable auto focus
  6. open a window with your octoprint camera and fine tune the focus..
    so enter sudo v4l2-ctl --set-ctrl=focus_absolute=70 and change the number until the focus is ok
  7. now, you have your two commands that fine tune your camera. In order to run them every time raspberry open, you have to add them to rc.local file:
    a) enter sudo vi /etc/rc.local, then go to line last line "exit 0"
    b) press shift+O and write your two commands (you can copy and paste with left click)..
    if you mess up something press esc and :q! to quit
    if you are fine press esc and :wq to save
    something like this:
    022

some extra info:
if you set focus_auto to 1 and let the webcam focus an object in the center you can take the "perfect" focus value..and then focus_auto to 0..but which is this value?
you run the command sudo v4l2-ctl -l and you will see:
"focus_absolute (int) : min=0 max=250 step=5 default=8189 value=50"
so your "perfect" value is 50..
you can play with the other listed settings also..(like contrast with sudo v4l2-ctl --set-ctrl=contrast=200)


if you want to check that your changes are permanent, do a restart and see the current values with sudo v4l2-ctl -l

4 Likes

I was struggling with this for a while, thanks for this tip!
Also, a different focus value (50) on my side, but that depends on the distance to the printed object.
Got a crispy image using a Logitech C930 :slight_smile:

Do these settings also work for Logitech C922?

A good hint mentioned above, but not very obvious - there's a control panel for your camera at:

http://(local IP address):8080/

From here, you can play with the focus numbers, settings, etc. to test them before committing them to a script or rc.conf - any changes you make will happen in real time, so you can keep the camera view open in another tab and see it as you play with the numbers.

I know this is old, but for my Logitech C922x this is my rc.local.

Hope this helps anyone.

#!/bin/bash

#turn off autofocus
sudo v4l2-ctl --set-ctrl=focus_automatic_continuous=0
#set manual focus
sudo v4l2-ctl --set-ctrl=focus_absolute=60
#turn off auto exposure
sudo v4l2-ctl --set-ctrl=auto_exposure=1
#manually set the exposure
sudo v4l2-ctl --set-ctrl=exposure_time_absolute=125
#turn off the built-in led
sudo v4l2-ctl --set-ctrl=led1_mode=0

how do I turn off auto exposure?

@PrintedVisionz - to turn of auto exposure, see the post just above yours by Jack_1. He has added comments, so you can see what each command is doing.