Plugin to control arducam motorized focus?

I am using an arudcam raspi motorized focus camera. I would really like to set the focus from the octoprint UI. Is there a way to do this with a plugin?

The focus is a trivial I2C interface where you send a value from 0 to 4095 in the first two data bytes to the camera at I2C address 0x0C. Right now I am setting it from a bash script that just runs ...

i2cset -y 0 0x0c 0x01 0x00  # set focus to 100

Is there a plugin that can run this?

This could do it.

You would use that to define your bash script commands as gcode and then this one to add buttons to the UI if you don't want to use the terminal tab to send the command.

1 Like

Cool. I'll try it right away.

The g-code works great. But I couldn't get the custom control editor to work. Is there another option for a menu or button to send a g-code?

You could technically add it without the plugin, just requires some editing in the config.yaml.

https://docs.octoprint.org/en/master/configuration/config_yaml.html#controls

https://docs.octoprint.org/en/master/features/custom_controls.html#sec-features-custom-controls

That is awesome. I wish I had know about custom controls before. I guess I should ratfm (read all the freaking manuals). I was happy to see I could even easily add a slider control. This would be great for focusing.

But, to add a slider I need a way to send a param with my system (bash) command. I don't see how I can do this with the GCODE Systems Commands plugin. Is there a way I can send a system command without that plugin? Or should I just ratfm. :slight_smile:

If I'm not mistaken I think custom controls can be system commands, but would have to look at the docs again. BTW, the custom control plugin is just a front end to set those in config.yaml.

I take that back, those are system commands that can run external scripts but not custom controls.

@kantlivelong is an active developer so he might know a trick for passing parameters to his gcode system commands plugin.

It looks like there's an active pull request to his plugin too that enables that feature.

1 Like

Passing arguments was intentionally left out for security reasons. That PR does seem to implement the support for arguments but it's not something I want to merge in its current state. I was actually looking at it a little today to brainstorm a way to sanitize arguments. Have some ideas but nothing concrete yet.

1 Like

I don't understand the security risk. No one can get into my plugin setup so how could damage be done?

There are configurations out there that are multi-user where not everyone is an admin. Blindly passing arguments allows someone to easily execute whatever shellcode they want. Could it just be a tick box on the settings? Sure, but that's not really how I like to implement things.

Just made me think of a fairly simple solution though. I could pass the line as an environment variable which would then put the role of checks on the user. The downside is that users that would possibly need to make use of simple built in executable commands would need a wrapper. Not too bad though really.

1 Like

I don't understand that but it's no problem for me since I'm already calling a bash script with my system command.

P.S. If I get my custom focus control working is there any way to make it available as a plugin without all the python stuff?

Not quite sure what you mean. Plugins are mostly python, or at least require some python to at least install load the web/javascrpt stuff within the OctoPrint framework. Doing a quick search there appears to be some pretty easy interfacing for I2C interfaces from python using smbus module. If that truly is the case then adding buttons on the web interface to run commands on the python back-end is fairly trivial.

https://learn.sparkfun.com/tutorials/python-programming-tutorial-getting-started-with-the-raspberry-pi/experiment-4-i2c-temperature-sensor

The other option from python side would be running the i2cset command directly using subprocess module or sarge.

1 Like

Looks like their own python examples utilize the system command method here.

You probably know by now that there is a plug in for the motorized focus camera..
I tried to use it and all I get is an error - Unable to use SMBUS/i2c
I have a raspberry pi4 with Raspian buster firmware
connected to my WIFI and desktop computer to Octoprint... (desktop enviorment is not needed )
I also installed the Python Dependency Libraries..
Support at Arducam thinks that Octoprint might be using i2c for something else...

Anyway, can you tell me how to use your script..
i2cset -y 0 0x0c 0x01 0x00 # set focus to 100

I tried to enter it directly as is into my pi terminal - NG- bad command
I tried to enter it with a bash, sudo, ssh, in front... bad command
All I need is to adjust the focus for my Timelapse / Octolapse works fine but out of focus
Any help would sure go a long way.... Thank you......... LouRBK from CT.

You probably want to get help from @moof

From the installation instructions and for your reference:

Please add these lines to /boot/config.txt

#ArduCamFocus
dtparam=i2c_vc=on
dtparam=i2c_arm=on

If you don't know how to do that, then ssh to your pi and copy and paste these commands, all at once, on the pi:

if ! grep -Fxq "^#ArduCamFocus$" /boot/config.txt; then
sudo cat << end_of_file >> /boot/config.txt
#ArduCamFocus
dtparam=i2c_vc=on
dtparam=i2c_arm=on
end_of_file
fi

now, reboot your pi

Hey moof !

adding the lines on the bottom of config.txt worked !!
the error "unable to use i2c " disappeared and the arrow keys increment the setting...

the only other mystery to me is I tried to paste the other commands
into pi and got -bash /boot/config.txt permission denied... don't know why...
Thanks.... LouRBK from CT.

Hi Moof, thanks for this plugin, I want to give you some feedback about it. So first when I try to edit the boot.txt file using your script I get a permission denied so I had to su as root. Then after I enabled i2c with raspi-config, I rebooted but the arrows to control the focus are still greyed out. My camera is a 8MP IMX21. I tried to test the focus using its python examples from here and everything works fine so at least I know that the focus actually works. BTW, with a i2cdetect command my camera is active on bus 0. I'm running Octoprin 1.4.2. Any advice? thanks again!