Python, Linux, and Lionel trains

In Control

Article from Issue 152/2013
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.

Buy this article as PDF

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

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Nerf Target Game

    A cool Nerf gun game for a neighborhood party provides a lesson in Python coding with multiple processors.

  • ReportLab and Panda3D

    A game of bingo illustrates how to use the ReportLab toolkit and Panda3D real-time 3D engine.

  • DIY Scoreboard

    We look at a broadcast video system network that uses Python code to control a video router and check out another program that creates a scoreboard.

  • Serial Communications

    We explore serial communications, from the electrical specs to protocols and libraries, with an example of serial communication with an Arduino.

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

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News