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.

Read full article as PDF:

Price $2.95

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.

  • Stackless Python

    The Stackless extension brings lightweight processes to Python, opening a new style of programming with dynamic heap access.

  • PHP for Sysadmins

    Most admins tend to use the shell, Perl,or Python if they need a system administration script. But there is no need for web programmers to learn another language just to script a routine task. PHP gives admins the power to program command-line tools and even complete web interfaces.

  • 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.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News