Skip to main content

/* 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

Last edited by Adrian!
Original Post

Replies sorted oldest to newest

If a train is stopped on the desired section when you first turn the system "on" how does it know to report the section is occupied?

The camera-based lane occupancy sensors for traffic signal control at intersections work at night though I don't know if they use the reference based image method.  Since they must deal with snow on the road I'm thinking not.  It too has the advantage of not having to dig up the road to install the sensor loop.  I figure they must use a similar method of polygons to define traffic lanes and additional tricks such as counting the number of vehicles queued. 

Hi, The pi (raspian) has a program you may be interested in called motion.

Type <apt-get install motion> to install it. The author has a Web site found in the documentation. You can use the pictures the program takes to overlay a masked picture. When the program triggers a hit (motion) it can call a script file or executable program (and more). You do this all in the configuration files. You can have up to 4 camera's that I know of, each camera would have it's own config file. So your masked part of the picture you overlay the real-time picture with to detect motion could be quite small (or big).

<man motion> will give the quick documentation or  <motion  --help>

There is another very powerful program called <pigpio>  (search the web).  With that you can control the gpio's either a scrip file, c code, c++ or python.

WiringPi is a great tool! pigpio is the best I've found so far. If you Run pigpiod (which is a daemon running in the background) you can access the pi through an open socket(network) just as if typing at the keyboard or running a program on the pi. So you compile pigpio on you 64 bit machine and run the program on your 64bit machine talking to the pi on it's open socket to access the pigpio daemon.

I've been wanting to experiment with your idea(s) and haven't had the time. Great work so far, very interesting.

Last edited by pops3301
stan2004 posted:

If a train is stopped on the desired section when you first turn the system "on" how does it know to report the section is occupied?

The camera-based lane occupancy sensors for traffic signal control at intersections work at night though I don't know if they use the reference based image method.  Since they must deal with snow on the road I'm thinking not.  It too has the advantage of not having to dig up the road to install the sensor loop.  I figure they must use a similar method of polygons to define traffic lanes and additional tricks such as counting the number of vehicles queued. 

If you stick with edge based, as long as the train is not in exactly the same place, it will see it. I guess this could be an issue with freight yards, but you can just take the reference frame 1 time and save it to a hard drive. Provided you don't need to move the camera it should be stable.

Another one I was thinking was maybe using edge finding to look for the 3 track rails to pass through the block completely. That may solve this. I'm not sure how the cameras do this at night either, maybe they use IR cameras?

pops3301 posted:

Hi, The pi (raspian) has a program you may be interested in called motion.

Type <apt-get install motion> to install it. The author has a Web site found in the documentation. You can use the pictures the program takes to overlay a masked picture. When the program triggers a hit (motion) it can call a script file or executable program (and more). You do this all in the configuration files. You can have up to 4 camera's that I know of, each camera would have it's own config file. So your masked part of the picture you overlay the real-time picture with to detect motion could be quite small (or big).

<man motion> will give the quick documentation or  <motion  --help>

There is another very powerful program called <pigpio>  (search the web).  With that you can control the gpio's either a scrip file, c code, c++ or python.

WiringPi is a great tool! pigpio is the best I've found so far. If you Run pigpiod (which is a daemon running in the background) you can access the pi through an open socket(network) just as if typing at the keyboard or running a program on the pi. So you compile pigpio on you 64 bit machine and run the program on your 64bit machine talking to the pi on it's open socket to access the pigpio daemon.

I've been wanting to experiment with your idea(s) and haven't had the time. Great work so far, very interesting.

That's also a pretty good way to do the interface also, by calling a script instead of having a single thread running all the time. Thanks for the suggestion, I ll check out these packages.

Adrian,

This is really fascinating.  I am hoping that you are continuing this work.  I may be missing something here, but for block detection, why not go ultrasonic sensors with the Pi?  I have used similar products with a STM micros and have had great success for detection and rate of change calculations.  You could even do a train based detection system.  This would require complex filtering like Kalman filters for self awareness(adjusting the output using velocity calculations and tailoring it to corrective position display), but you theoretically could communicate with each locomotive and display which block it is on.  You could use MATLAB to model the layout geometry and such, or nestle in checkpoints that blend in with scenery to make the corrections easier.  Or even a Passive RFID system (This seems like it would be really troubling, though.  Between common mode noise and EMC compliance it sounds like it would be a nightmare.)  I am wondering if "not touching the layout at all" literally meant nothing, but just a camera.  Either way, I am loving this idea and I am excited to see what else comes of it.  I may have to try it myself.

 

James

Datdupa46_Novotronics posted:

Adrian,

This is really fascinating.  I am hoping that you are continuing this work.  I may be missing something here, but for block detection, why not go ultrasonic sensors with the Pi?  I have used similar products with a STM micros and have had great success for detection and rate of change calculations.  You could even do a train based detection system.  This would require complex filtering like Kalman filters for self awareness(adjusting the output using velocity calculations and tailoring it to corrective position display), but you theoretically could communicate with each locomotive and display which block it is on.  You could use MATLAB to model the layout geometry and such, or nestle in checkpoints that blend in with scenery to make the corrections easier.  Or even a Passive RFID system (This seems like it would be really troubling, though.  Between common mode noise and EMC compliance it sounds like it would be a nightmare.)  I am wondering if "not touching the layout at all" literally meant nothing, but just a camera.  Either way, I am loving this idea and I am excited to see what else comes of it.  I may have to try it myself.

 

James

Hi James,

 

The challenge was to do the most non-invasive and minimal hardware block detector possible. The camera is essentially an entirely software solution, and its a good one for layouts with modular sections that change often, because you can just load new coordinate geometry for the block definitions. I'm going to try this on a full scale layout when I get a moment. In this post, I was just getting a feel for how well it works before I go after a full scale one.

Cheers!

When I was finishing my basement at night I was up on a ladder holding something heavy with my left hand, drill in my right hand and screws hanging from my mouth. Wife comes in and says "Honey I'm going out for about an hour", see you later. When she left she accidentally shut the lights out. You know the rest.

Think about it............how to plan for that?

This is a huge project, good luck. When I get time I'll will try to capture pictures and calculate the delay. I believe the pi's camera might be a problem with pictures. Streaming video would be faster, but to process this information takes up a lot of machine cycles, not to mention the speed of the locos. 

Adrian, I've been using the new 8 megapixel camera instead of the 5 megapixel cam. For your info the new 8 meg camera has much better lower light sensitivity. But I'm finding that taking pictures still takes valuable time.

I've been thinking more along the lines of how facial recognition works. From big faces to small faces the mac computer always (usually) identifies the person (after you name them). Instead of a person, how about an object. There has to be software out there that already does this. Just throwing up ideas.

Again, what happens when the lights go out.  Haven't gotten past that one yet.

Last edited by pops3301
Adrian! posted:
Datdupa46_Novotronics posted:

Adrian,

This is really fascinating.  I am hoping that you are continuing this work.  I may be missing something here, but for block detection, why not go ultrasonic sensors with the Pi?  I have used similar products with a STM micros and have had great success for detection and rate of change calculations.  You could even do a train based detection system.  This would require complex filtering like Kalman filters for self awareness(adjusting the output using velocity calculations and tailoring it to corrective position display), but you theoretically could communicate with each locomotive and display which block it is on.  You could use MATLAB to model the layout geometry and such, or nestle in checkpoints that blend in with scenery to make the corrections easier.  Or even a Passive RFID system (This seems like it would be really troubling, though.  Between common mode noise and EMC compliance it sounds like it would be a nightmare.)  I am wondering if "not touching the layout at all" literally meant nothing, but just a camera.  Either way, I am loving this idea and I am excited to see what else comes of it.  I may have to try it myself.

 

James

Hi James,

 

The challenge was to do the most non-invasive and minimal hardware block detector possible. The camera is essentially an entirely software solution, and its a good one for layouts with modular sections that change often, because you can just load new coordinate geometry for the block definitions. I'm going to try this on a full scale layout when I get a moment. In this post, I was just getting a feel for how well it works before I go after a full scale one.

Cheers!

Adrian,

I kind of figured that was the case.  Just some thoughts.  I was looking over some of your research and PhD. topic yesterday... Very fascinating.  It really seems like you are a true expert and a gem in your field.  IC design is an art, but all the applications you're using it for is a level above that, quite amazing.  I'm more on the embedded side and am looking at PhD. programs myself right now.  I will have to reach out to you sometime. I'm using a SoC FPGA from Intel for a DNA sequencing project to finish my undergrad right now.  I probably will stay with genomics on SoC moving forward in academia.  it would be good to get some insight from someone who designs them. 

 

James

Add Reply

Post

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
Link copied to your clipboard.
×
×