The Trains Course

CS 452, informally known as the trains course, is notorious amongst CS students from the University of Waterloo. It is often regarded as one of the hardest undergrad electives offered by the department, however taking the course is also considered to be an invaluable experience. Students enrolled in CS 452 spend the term writing code that controls a system of trains and track switches. Needless to the say, the system needs to plan the train routes and switching patterns so that no trains collide.

Designing an efficient train scheduler is challenging on its own, however the course project also requires that students write the underlying operating system for the train control code to run on. The OS is deployed to a board with nothing but hardware and a simple boot loader — everything else has to be written from scratch in a combination of C and assembly.

I decided to take the trains course this term since it’s my last semester before graduation. I’ve been interested in CS 452 since I first heard about it, but it’s never seemed to fit with my busy engineering schedule. Additionally, its usually best to take the course with a friend since the project component is done in teams of two. One of my close friends was also interested in taking the course this term, so things worked out nicely.

Hardware Setup

The trains run on one of two pre-configured track layouts. Each layout has a number of sensors embedded in the track pieces that can be used to detect passing trains. Forks in the track, referred to as turnouts in railway-speak, are controlled with solenoid switches.

Train Tracks

the track layouts, a digital controller and some of the engines

The first layer of computing hardware is a digital control unit provided by the train manufacturer. The unit can be used to query the current sensor state, set train speed/direction and set switch positions. The digital controller has an analog interface, e.g. buttons and dials, but it can also be operated programmatically by sending a stream of bytes through a serial port. The train hardware itself is quite old — the documentation references the Apple II computer and presents examples written in MS BASIC.

A TS-7200 system with an ARM processor is used to run the operating system code and communicate with the train hardware. The board is also connected to a Linux desktop system so that it can print debug information and respond to commands. All of the code for the project runs on the processor attached to the board.

Assignment 0

In almost all of the courses I’ve taken at UW, real “work” doesn’t start until the second week of school. There are no tutorial periods scheduled during the first week, and most assignments aren’t due until the end of the second week at the earliest. This was absolutely not the case for the trains course — our first assignment, A0, took several days of work and was due one week after the first class.

Assignment 0 was independent from the main course project, and served as a general introduction to the hardware, the trains and the protocol used to control the various moving parts. The A0 code needed to display information about the track/switches, and allow users to enter speeds for trains on the track. I spent some time working with ANSI escape sequences and ASCII art to make my terminal display more interesting:

Console Output

console output from my implementation

The first assignment ended up taking around 35 hours, including the documentation portion submitted alongside the code.

First Impressions

CS 452 definitely caters to students with a specific set of interests. The class size right now is only 13 people, and the majority of the marks for the course are allocated to the project. The project is very rewarding for those interested in low level systems development, but I imagine it could be stressful and unpleasant for a programmer without that interest.

One of the criteria I use to evaluate courses is the likelihood that I would do course projects even if I didn’t receive academic credit for them. Out of all the courses I’ve taken to complete my degree, only two have received a score of “extremely likely”. The first was an introductory compilers course I took during my second year and the other was an operating system course from my third year. Both courses are mandatory parts of Waterloo’s software engineering program.

Even though classes have only been running for a week, I’m already certain that the trains course is going to fall in the same category. The best thing about CS 452 is there are very few restrictions on the project, as long as it performs the required operations and supports the features listed in the project description. There’s nothing stopping teams from adding extra features, experimenting with different algorithms or researching OS kernel design on their own.

The course also has much less guidance; lectures discuss high level concepts relating to the project, but most of the tricky problems need to be solved by student teams on their own. Personally, I find this makes the project even more exciting. All of the necessary tools and resources are provided, but it’s up to the students to figure out how to best use the tools. We’re given all of the documentation we could possibly need (6,000 pages of it!) for the train system and computing hardware. Additionally, the train lab is always accessible — its not uncommon to see people working and testing their code at all hours of the day.

Going Forward

I probably won’t write another post about the trains course for a while. The next major milestone is to have a completed operating system done by the end of February, so I may write another post then. At some point I would like to discuss the engineering challenges of the course, but it seems more appropriate to do that after this school term is over.