Python, Linux, and Lionel trains

In Control

Author(s):

Controlling a miniature train empire with concurrent Linux processes.

Two hobbies are prevalent in my house: Lionel trains and Linux computers. The train layout isn’t a permanent fixture; it gets set up a couple of times a year – usually around Christmas – but it’s been known to appear during the summer as well. The last time it appeared, I added a Linux computer to control the setup. Although I still run the trains with traditional throttles, I let the computer play with the rest. 

Rather than write one massive control program, I decided to split the design into single-purpose modules that run independently (Figure 1). Each module performs a single task in either the input, output, or control categories. Any given input or output module corresponds directly to a specific piece of hardware. Control modules don’t have hardware equivalents but instead provide the logic that links the inputs and outputs. Any number of modules can be run simultaneously.

The modules are written in Python and use mmap to communicate among themselves. The files inputs.txt, which represents the current state of the railroad’s sensors, and outputs.txt, which has the requested state for the relays or LEDs that activate working accessories, are memory-mapped files. They are both initialized to a single line of 100 zeros.