Adjust webcam focus based on printhead location

I've been trying to work out if there's a way to get the realtime location of the printhead during a print. I'd like to use that streaming location data to focus my webcam, using uvcdynctrl -s 'Focus (absolute)' 122 or v4l2-ctl --set-ctrl=focus_absolute=32. So far it seems like the only way to get the print head location is by writing a plugin that observes the gcode and determines the printhead location from there.

I was wondering if anyone else had any other ideas? Likely this will turn into a plugin of some kind.

Thanks!

I can't say how your camera would react to anything (Not my thing) but if we just talk about how to get where the print head/Axis is, I think you are on track. That said, there really is no existing way to track actual position. You could go the hardware path and add hardware encoders and managing that at the IO level. But that seems like a lot of work for a camera focus. Also not really implementable by many users.(may not matter)

Under normal gcode processing , marlin does not send any real time feedback related to "this is the current position". The stream is to the printer. That is the wrong direction for your requirement. Meaning the streamed commands from Octoprint to the printer are "go here commands". This info is where is it going next not where is it now. To get the printer to tell you where it is, you could force/place a special @ command and a M114 in the gcode being run. From that you could trigger your Plugin to see that @ command and look for a response from the M114.

Honestly this sounds a little hacky but also seems like it would work.

2 Likes

Appreciate you steering me clear from banging my head regarding Marlin realtime information. I was also considering a janky setup where the x axis was tied to some kind of encoder that was fed to the gpio pin, and I would attach a process to that. That also feels kinda jank too. I'm going to work on how to elegantly figure this out. Thanks for the feedback!

To get the printer to tell you where it is, you could force/place a special @ command and a M114 in the gcode being run. From that you could trigger your Plugin to see that @ command and look for a response from the M114.

This is pretty much what octolapse does right? It inserts gcode to get the printhead in the defined location for a snapshot. Perhaps I can take some pointers from that code.

Marlin does have a realtime position reporting option in newer versions, but you would still have to process that information with a plugin somehow to run the commands you want. There is a PositionUpdate event that you could utilize with the event manager plugin, but that would be responding to M114 commands as mentioned earlier as I'm not sure it takes into account auto position reporting yet or not since I haven't tested that Marlin feature.

1 Like

I'll look into this. This is definitely becoming a plugin. I'm kinda surprised no one else has cared about this. :laughing:

Not all cameras are software focus controllable is maybe why.

1 Like