/* I re-posted this with links instead of videos because the embedded videos weren't working */
Hi All,
It's been awhile since I posted something fun. So a few months ago we had a long talk about an RF block occupancy sensor in the old post (Old Post) that tries to minimize interference with track wiring and layout. Interestingly, gunrunnerjohn , stan2004 and others challenged me to think if there is any way we could do fool-proof block occupancy without touching the layout at all. I always love an engineering challenge.... so I've spent some time thinking about this and came up with an interesting and simple answer..... computer vision!
My test track at home is only 10 ft long so I can't do much of a test, but I was able to work something out using a raspberry pi 3, with the new camera (camera) module that runs about $20.
Basically you write a super simple code that captures frames from the camera and detects objects (IE Trains) then look for changes in a defined polygon drawn around each block of the layout. If there is a gradient \/.F = (F/dx+F/dy+F/dz) change (IE new edges appear) then a train is there, otherwise the block is empty. At first I thought about doing a differential image (reference image with no trains vs live image), but when the background light changes it gets screwed up. Then I though about a running average but if the train is stopped, that eventually disappears. So the best way I've found so far is to do simple edge finding and compare to a reference frame of edges. This way it isn't affected by light conditions. The only trick is that your block polygon needs to be bigger than 1 car so there is always an edge to find (the gap between the two cars). There's a free open source library for this stuff that's really easy to use in python/c called (open CV) that you can download for free that has all these functions inside.
Once the train lights up the detection block or "train detection box" .... you can just wire one of the raspberry pi's output wires to the LEDs in your signal towers (or incandescent bulbs with a relay if you're old school). In my example below I just have 1 block polygon (since I only have 10 ft of test track) but you can have as many as you want, and you don't have to use rectangles so you can make the blocks arbitrary shaped polygons.
So at first I thought about doing this looking from the side but the field of view is lousy and there's no depth perception so it doesn't work too well. Also with a club layout or something on a table people will be walking by and that will screw it up if the camera can't see the layout (obviously).
Side View Example (this is a link because the embedded videos weren't working)
Then after thinking about it for like 1 minute, I realized that maybe mounting the camera over the layout looking straight down is probably a better option. That way as long as people aren't reaching into the layout (which shouldn't happen in normal operation) it should be pretty robust imaging. At $30 a raspberry pi you could easily have 3-4 units cover the entire layout. Other stuff like estimating speed, or detecting when two trains are about to collide and cutting the power is also pretty easy to do with that openCV library since it has functions that can take directional derivatives (velocities).
Top View Example (this is a link because the embedded videos weren't working)
There's a few things that still need work. The train can glitch a bit so you need like a 2-3 second timeout on the block to make sure the signal lights don't flicker and occasionally if you have parallel tracks sometimes the train casts a shadow and the system gets confused about which track the train is on (this can be solved with adding some illumination and some image thresholding), but it is an interesting direction. To control the signal lights or whatever in your layout there is a really elegant open source library for GPIO on the raspberry pi available here (wiringpi).
I'm sure many people will think this is way too complicated, but honestly.... if you think about it.... you can run a raspberry pi headless (no kb/mouse/monitor) and it has a wifi connection, so all you need is a 5V power supply over your layout. You don't need the kb/mouse/monitor for maintenance or adjustments becasue you can just VNC into it and it's like your plugged in. Then all you need are a few wires somewhere to drive the signals that you would have to do anyways if you were doing insulated rails, hall sensors or anything else. This does save you a lot of work on mounting sensors, or gaping the track with a dremel tool..... and if you change the layout all you need to do is adjust some X,Y coordinates that define the blocks.
Anyways... hope this sparks an interesting discussion!
~Adrian