CS452 - Real-Time Programming - Spring 2017

Lecture 17 - Projects, calibration II.

Public Service Annoucements

  1. Exam schedule is out; 19.30, Friday, 28 July is the date the registrar gave us. Please bring your exam schedule to class on Monday, 19 June, when we will choose the time for our examination.
  2. First Train control milestone, Thursday, 29 June.
  3. PDF documents containing mathematics.
  4. Fiducial marks.

The Train Project

The Ultimate Goal

The goal for your final project is to have several trains driving continuously on the track, doing something interesting. "Interesting" is defined to exclude undesirable things like collisions, derailments and deadlock (no trains moving anywhere).

Two milestones

On your way to this goal you will pass two milestones, for each of which you are expected to provide a demo to the instructor and the TAs. Both milestone descriptions will be edited in the immediate future.

  1. Stop a train driving at constant speed at any requested point on the track. Doing so requires a calibration model that knows the stopping distance of the train, and the velocity it travels. Both should be known for more than one speed. Know where on the tracks a train is whether it is travelling at constant velocity, accelerating or decelerating.
  2. Drive two trains on the track knowing where both trains are at all times. It should be possible to give trains independent destinations at which they can arrive without colliding with each other.
These demos are expected to get you doing the things that have to be done for your project in the right order. For example, there's no point in trying to know the locations of two trains at once if you are unable to know the location of a single train.

Types of Projects

Most projects over the years have fallen into one of two types. But about five years ago a third type emerged, which continues to be rare. I am still waiting for a project of the fourth type.

  1. Ordinary train projects. The trains are trains, or close substitutes like busses or taxis, and they do things like moving freight or passengers from one point to another. One recent project had trains that "broke down" on the tracks and had to be brought into a siding to be repaired.
  2. Games played on a graph. Many games are played on a graph; trains move on a graph. Making a game such as Pac-man, played by trains, is another popular project. If you do such a project be sure to make the game logic very simple, only complex enough that the trains must interact. Complex game logic usually means colliding trains.
  3. Trains that do coordinated stunts. You can devise interesting patterns created by the coordinated motions of trains. Be certain that you discuss such a project with the instructor, because it's easy to make a choice that's impossible, or even worse trivial, to implement.
  4. And, of course, anything a train can do, such as this, is interesting if you can recreate it in the trains lab.


Calibration is hard. The essence of its hardness is the inevitability of uncertainty in


Getting a sensor report indicating a sensor hit occurs at the end of a process requiring two sensor readings. On the first reading the sensor has not yet set; on the second reading the sensor has set. Here is the sequence in detail

  1. Request leaves user code.
  2. Final byte of request accepted by train controller.
  3. Train controller gets sensor values, sensor bit is unset.
  4. Final byte of sensor report arrives at ARM.
  5. Sensor report is time-stamped, t0.
  6. Sensor report arrives at user code.
  7. Second request leaves user code.
  8. Final byte of second request accepted by train controller.
  9. Train controller gets sensor values, sensor bit is set.
  10. Final byte of second sensor report arrives at ARM.
  11. Second sensor report is time-stamped, t1.
  12. Second sensor report arrives at user code.
The sensor was hit between step 3 and step 9. The application gets two times, t0 and t1. What should the the application conclude?

We will work this out in class, based on one thing we can measure fairly precisely and several things we can estimate quite precisely.


Once we know where a train is and how fast it's travelling we can calculate when we want things to happen in the train set, such as a change in a train's speed or the change in direction of a switch. In doing so we need to take into account transaction lag, the time between when user code issues the command and the time when the train or the switch carries out the command. The sequence is more simple than with sensors.

  1. Command leaves user code.
  2. Final byte of command accepted by train controller.
  3. Command received by train or switch.

Other complications

Velocity is controlled by changing the train's speed, BUT, the mapping between speed and velocity is not simple.

Important . Some of these effects matter; some don't. It's part of your task to find out which effects matter and which don't. (If you don't figure out which is which you will spend an unlimited amount of time.)

Furthermore, things can go wrong, such as

Avoiding such failures, or responding sensibly to them, is possible only if you have a `good enough' velocity calibration. (You get a perfect calibration only in the limit t->infinity, and the train you are calibrating falls over dead long before that.)

Such failures like these also pollute your attempt to acquire reliable data for your calibration.

1. Calibrating Stopping Distance

Being able to stop the train at a specific point is a very good warm-up exercise. The easiest case is to stop the train immediately after getting a sensor report. Here's what happens.

  1. The sensor report reaches user code.
  2. The stop command leaves user code.
  3. The final byte of the stop command arrives at the train controller.
  4. The train receives the stop command, and starts stopping.
  5. The train stops.
  6. You measure the distance from the sensor to the fiducial mark you have chosen. The distance you measure is the stopping distance for that sensor and speed.

Questions you need to answer


  1. The sequence of events above has a whole lot of small delays that get added together
  2. Knowing where you stop is very important when running the train on routes that require reversing
  3. Clearly, knowing when you stop is equally important.

This is very time-consuming!

Based on calibrations I have seen in previous terms you will find substantial variation with speed setting and train, little variation with sensor.

Hint. Interacting with other groups is useful to confirm that you are on track. Of course, simply using another group's calibration, with or without saying so, is `academic dishonesty'.

Measuring the time to stop

In addition to the stopping distance you will want to know the time it takes to stop. A simple way to do so is

  1. Start a stopwatch when you give the stop command.
  2. Stop the stopwatch when you see that the train is stopped.

This might not be accurate enough for you. When you have calibrated the velocity and can stop anywhere on the track there's a better way.

  1. Give the stop command so that the train will stop with its pickup on a sensor, recording the time you when you give the command.
  2. When the sensor triggers, check the time.

2. Calibrating Constant Velocity

At this point there are a few places on the track where you can stop with a precision of a train length or better. However, suppose you want to stop not sitting on a switch.

To do this successfully you have to be able to give the stop command anywhere on the track.

Knowing the Current Velocity

An implicit assumption you are making is that the future will closely resemble the past.

  1. You measure the time interval between two adjacent sensor reports.
  2. Knowing the distance between the sensors you calculate the velocity of the train Note that on average the lag mentioned above -- waiting for sensor read, time in train controller, time in your system before time stamp -- is unimportant.
  3. After many measurements you build a table

Using Resources Effectively

The most scarce resources

The two most plentiful resources

Any time you can use a plentiful resource to eliminate use of a scarce one you have a win. Two examples

  1. Squeeze all the functionality you can from every measurement. Every time you pass a sensor you can use the measurement to improve your velocity calibration.
  2. Use your time with the trains efficiently. There's a lot of setup time each time you start using the train. make as many measruements as you can to economize on set-up time effectively. make it possible to change program parameters from the terminal.

Practical Problems You Have to Solve

  1. The table is too big. (Actually both tables!)
  2. The values you measure vary randomly.

The values you measure vary systematically

How Long does it Take to Stop?

Try the following exercise.

  1. Choose a sensor.
  2. Put the train on a course that will cross the sensor.
  3. Run the train up to a constant speed.
  4. Give the speed zero command at a location that stops the train with its contact on the sensor
  5. Calculate the time between when you gave the command and when the sensor triggered.
  6. Look for regularities. For example, the time to stop from a random point on the track should be the same as the time to stop you found when stopping immediately after a sensor report.

Return to: