Python, Linux, and Lionel trains

In Control

Article from Issue 152/2013

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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Universal Plug and Play

    Universal Plug and Play provides an easy framework for seamless integration of network devices. Learn how to build your own UPnP solution using the open source BRisa framework.

  • Command Line: Data Flow

    Working in the shell has many benefits. Pipelines, redirectors, and chains of commands give users almost infinite options.

  • Barcode

    Machine-readable codes aren’t just for big companies anymore. With the right programs and some low-cost hardware, you can create and read the most important codes.

  • Workspace: EasyGUI

    If you need to add graphical elements to Python script, EasyGUI can help beginners and experts alike.

  • eZ Components

    eZ Components is a free enterprise-level PHP library that uses state-of-the-art language features. We show you the library’s potential on the web and at the command line.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95


njobs Europe
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia