CS452 - Real-Time Programming - Winter 2016

Lecture 25 - Multiple Trains

Public Service Annoucements

  1. Train Control I demo on Friday, 11 March.
  2. The exam will start at 12.30, April 19, 2016 and finish at 15.00, 20 April 2016.

Multi-Train Control

By the next milestone you will be able to control two trains at the same time.

Sensor Attribution

Route Finding and Following

Collision Avoidance

Route Finding and Following

Collision Avoidance

Treating the track as a shared resource

There is a good analogy with the X server providing parts of the display to different processes. The division into parts is dynamic, with the process that owns a part of the screen unaware that it, for example, loses parts of its window when other windows cover parts of it.

A track server gives out and takes back pieces of track. Pieces can


Collision avoidance is the goal. We want a policy that controls how the track server gives out track, and how the train uses the track that it gets. The policy should have two properties.

  1. Its should be easy to prove to yourself that the policy prevents collisions.
  2. The policy should be easy to implement, taking into account the real properties of the trains and the track.

Here is an example of a typical policy.

  1. The server ensures that zero or one train owns each section of track.
  2. A train may only occupy track that it owns.
  3. A train can only operate on track it owns. In practice, "operate on" means "switching turn-outs".
  4. Track should be returned to the track server as soon as a train leaves it.
  5. To avoid leapfrog deadlocks, all the track owned by a train must be contiguous.
Items 1 and 2 ensure that no collisions occur. Item 3 ensures that the state of track owned by a train is never changed by another train. Item 4 allows more than one train to drive on each piece of track so that the demo continues. Item 5 saves us from the leapfrog bug, which surprisingly common.

There are many successfully policies. But make sure that you understand the properties of the policy you use.


Somebody has been doing something right for over a century. The answer is reservations.

Two Level Train Control

The two levels are completely independent of one another. The upper level determines which track is given to trains; the lower level is a set of rules that a train driver must obey when driving.

Upper Level

  1. Train asks dispatcher for a route.
  2. Dispatcher provides a route that he/she expects to be conflict free.
  3. Train follows the route, reporting back to the dispatcher as landmarks (sensors) are passed.

Lower Level

The lower level is also communicated by the coloured lights. In cases of conflict between the upper and lower levels, the lower level overrides the upper level.

Something Essential that You Must Do

Design your reservation system before coding it.

Before coding your reservation system work it out on paper and make sure that it works for all the generic cases you can think of

  1. One train following another
  2. Two trains on a collision course
  3. Short routes with much switching
  4. Single point failures.

There are one or more switches in the path

Implementing the policies.

No over-driving

Here is the sequence of events that occurs when a train stops.

  1. The train has enough track to continue driving.
  2. The train decides that it needs more track.
  3. The train requests track, and is turned down.
  4. The train gives a "speed zero" command.
  5. The train slows, stopping one stopping distance from where it gave the sp 0 command.
If the train is to avoid overdriving its track when does step 2 occur?

Operating reserved track

How much must be controlled to ensure that this constraint is respected? Try listing all the things that might go wrong? Are willing to trust the train driver?

Single owner

Something atomic, presumably a server, has to control who has what track.

No leapfrog deadlock

The server can control this. Or the server can be less smart and assume that input from the train is reliable. Then the train driver must be programmed to asked for pieces of track in the right order.

Returning reservations

Based on past history train drivers very commonly make errors when giving back reserved track. But once they have the track it's hard to get it back from them.

In the past students have experimented with timed reservations, where the reservation is reused, like it or not, when its time has expired. Results have not been good. How can a server be sure that a train driver can exit a reservation before a pre-specified time? How can a train driver figure out that it won't leave in time, and if so what can it do?

Return to: