Laser pattern rangefinding using a diffraction grating

Hi all! I’m experimenting with a RPI5-1gb and green laser diffraction grating (nearly coaxial with Pi camera module 3). The goal is to reference the projected pattern, and calculate range and angles based upon the scale and angles of the pattern shown. As you can see I’ve had some success but so far its not been robust and not consistent over a dynamic moving non-flat surface. I’m coding in Python but thinking that somebody(s) much smarter than me has probably refined a version of this approach long before now and I’d like to reference or learn from that approach before going down this same road and duplicating all that effort. I’m actually pretty new to all of this and of course have been vastly helped along with AI tools. Anyway, thanks for any suggestions!

how do you imagine you’d calculate the distance?

That part should be relatively easy since the scale grows at a known rate with distance.

and how is the camera going to see this?

that sounds to me as if the laser and the camera are physically linked.

are they, or are they not?

what you have described so far does not constitute a clear and unmistakable “picture” of the situation.

perhaps show the situation instead of describing it.

Ok. Here it is. Yes, the laser and camera are mounted to the same platform which is itself mounted to a servo actuator for pitch control.

given that situation, your camera will see the pattern be the same apparent scale (size in the image) no matter the distance.

did you expect that?

I didn’t expect that and I’m not sure I’m in agreement (yet), but its an interesting point to consider thanks for bringing it up- my original presumption was wrong. In my mind the ranging was defined by the difference between the divergence angle of the laser and the FOV angle of the camera, but it seems like that ends up cancelling out within the present equation(which I think is your point). That said, as I was continuing my discussion with (Claude) AI, the point being made is that the separation between points is what’s being used to triangulate and derive ranging and so it’s (allegedly) still valid. I’m interested if you agree with this reasoning/quote:

Why your system actually works:

The camera lens nodal point and the laser emission point are NOT at exactly the same physical location — they’re close together on the same surface but separated by some small distance, maybe 2-5cm. That separation is a real triangulation baseline. It’s small, which is why our ranging has needed a correction factor, but it’s real and it’s what’s actually enabling the distance measurement.

if you are planning to make something like a Kinect, where the optical axes of the projector and the camera are parallel, but have a specific lateral distance, “baseline”, one could say an “inter-pupillary distance”, then that makes depth perception possible.

this only works because the optical axes do not coincide. the more distance, the more accurate this becomes.

This relies on parallax, not scale. The scale will be the same, hence meaningless. the geometry of that is similar triangles, or close enough to it (if IPD/baseline goes to zero or distance goes to infinity).

if you took the AI’s “separated by some small distance” to mean you should get those arbitrarily close, that is not so. aim for a large baseline, as large as feasible, not a small one. there is a practical limit to the distance, where the cones of the one eye and the other eye/projector don’t overlap at a near enough distance.

You should get your hands on a decent book. Multiple-View Geometry is one of the old tomes (mid 2000s). every library probably has one. if you google for it, you’ll probably find its full text on the internet. there are other books on computer vision with less of a focus and depth on mult-view geometry. as long as the book covers multi-view geometry at all, it should help you.

you should also look for a structured class or course on computer vision. this is complex stuff. you should not wade into this on your own.

Yes- I understand the principal more clearly now and this iteration was designed with a different agenda in mind with regards to placement. I could maybe double the present distance given my revised understanding but maybe it means that I just don’t do the laser ranging that I’d hoped(I do understand the separation distance value). The laser is still useful for determining angle geometry but the whole laser notion was meant primarily as a learning experiment and to that end it served its purpose. I’m not looking to commercialize anything here or build a career out of this so I’m content with the experience as is. I already know an easy solution is to use an ultrasonic sensor or similar off the shelf assembly here, but I wanted to learn something new(particularly I wanted to play with opencv and maybe I lost sight of the goal!). I’m not dismissing your suggestion for the course or book- maybe if I find myself with more time in the future. Thanks for your help!