CS452  RealTime Programming  Spring 2015
Lecture 19  Trains
Public Service Annoucements

Kernel 4 due in class on 18 June.

The exam has three start times.

19.30, August 4

09.30, August 5

19.30, August 5
The end times are 26.5 hours after the start time.
Answers to questions asked from 19.30, 4 August to 22.00,
4 August will be answered on the newsgroup, whether they
arrive by email or on the newsgroup.

You can download data from the terminal, such as the track
graph, by putting a file onto the terminal program's output.

You can upload data to the terminal by sending its input to
a file.
Calibration I
Where is a train?
For your project you choose landmarks

sensors, turnouts, etc.

Remember the importance of fiducial marks: on the track, on
the train.
You then know when the train is at a given landmark, and find a
way  most likely by integrating velocity  to know how far it
is past the landmark at any given time. To do so, you need to know
each train's velocity for a full range of operational parameters.
1. Calibrating Stopping Distance
The simplest objective:
 know where the train stops when you give it a command to stop
 restrict the stop commands to just after the train passes a sensor
 only one train moving
Sequence of events
 Train triggers sensor at $t$
 train at ${S}_{\mathrm{n}}$ + 0 cm
 Application receives report at${t}_{1}=t+{\Delta}_{1}$
 You give command at${t}_{2}=t+{\Delta}_{1}+{\Delta}_{2}$
 Train receives and executes command at${t}_{3}=t+{\Delta}_{1}+{\Delta}_{2}+{\Delta}_{3}$
 Train slows and stops at ${t}_{4}=t+{\Delta}_{1}+{\Delta}_{2}+{\Delta}_{3}+{\Delta}_{4}$
 train at${S}_{n}+y$ cm
 (You measure $y$ with a tape measure.)
Questions you need to answer
 If you do this again, same sensor, same speed, will you get the same
answer?
 If you do this again, different sensor, same speed, will you get the
same answer?
 If you do this again, same sensor, different speed, will you get the
same answer?
 If you do this again, different sensor, different speed, will you get
the same answer?
 Or a different train, or different track condition, or ...
Comments
 The sequence of events above has a whole lot of small delays that get
added together

Each one has a constant part and a random part. Try to use values
that are differences of measurements to eliminate the constant
parts.

Separating a random delay into constant and random parts.

The mean delay is the constant part.

The delay minus the mean delay is the random part.

Adding delays.

The constant parts sum to the constant part of the sum.

How you sum the random parts depend on how you are representing
the randomness.

The most common representation is an interval around the constant
part.

The best case is the constant part minus half the interval.

The worst case is the constant part plus half the interval.
Add together the intervals of the two delays.

Another representation is a probability distribution. Your
long ago probability and statistics course taught you (maybe!)
how to add probability distributions.

Some delays can be eliminated a priori because they are extremely
small compared to other delays. The more you figure this out in
advance the less measurement you have to do.

Knowing where you stop is very important when running the train
on routes that require reversing. Knowing how long it takes the
train to stop is also important.
 Why are reversing routes important?

Clearly, knowing when you stop is equally important.
This is very timeconsuming!
 The simplest way to reduce the number of measurements is to eliminate
factors that are unimportant.
 The only way to know that a factor is always unimportant is to measure.
Developing the ability to estimate quickly, and to find the worst case
quickly is the main way of being smart in tasks like this one.
Now make a table

Sensor 1 
Sensor 2 
... 
Speed 6 



Speed 8 



... 



There are enough measurements in each cell of the table that you
can estimate the random error. (Check with other groups to make
certain that your error is not too big.)
Based on calibrations I have seen in previous terms you will find
substantial variation with speed setting and train, little variation
with sensor.
Group across cells that have the `same' value. Maybe all have the
same value.
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
A good measure of the stopping time is possible only when you have a good
velocity calibration.
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.
 You want to be close to the switch, clear of the switch, and on the
right side of the switch when you stop.
 You want to know when the train has stopped because until then you
cannot give the command to throw the switch.
 You want to know when the switchthrowing is complete because until
then you cannot start the train running in reverse.
To do this successfully you have to be able to give the stop command
anywhere on the track.
Calibrating Velocity
An implicit assumption you make is that the future will closely
resemble the past.

You measure the time interval between two adjacent sensor reports.

Knowing the distance between the sensors you calculate the velocity of
the train
 velocity = distance / time interval
 measured in cm / sec.
Subtraction removes the constant part of delays.
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.

After many measurements you build a table
 Use the table to determine the current velocity
 Use the time since the last sensor report to calculate the distance
beyond the sensor
 distance = velocity * time interval
Using Resources Effectively
The most scarce resources
 Bandwidth to the train controller
 Use of the train itself
The most plentiful resource
Any time you can use a plentiful resource to eliminate use of a
scarce one you have a win. For example
Practical Problems You Have to Solve
 The table is too big.
 You potentially need a ton of measurements
 The values you measure vary randomly.
 You need to average and estimate error.
The values you measure vary systematically
 For example, each time you measure the velocity estimate is slower,
presumably because the train is moving towards needing oiling.
 You need to make fewer measurements or use the measurement you make
more effectively.
How Long does it Take to Stop?
Try the following exercise.
 Choose a sensor.
 Put the train on a course that will cross the sensor.
 Run the train up to a constant speed.
 Give the speed zero command at a location that stops the train with its
contact on the sensor
 Calculate the time between when you gave the command and when the
sensor triggered.
 Look for regularities.
3. Calibrating Acceleration and Deceleration: short distances.
Trains often must travel short distance, starting with the train
stopped, and finishing with it stopped. When doing so the train
spends its whole time either accelerating or decelerating. Your
constant speed calibration is useless because the train doesn't
travel at constant speed. Simmilarly your measured stopping
distances are not useful.
Creating a perfect calibration of the train's position while it
is accelerating is hard. But there is an easy and precise
calibration that covers most of the moves the train makes where
you need a good calibration It's the subject of this section.
Most of the your train project can get away with ignoring
acceleration and decelleration. The one place you can't is when you
are doing a short move, giving a speed command followed by a stop
command before it gets up to speed. How far will the train go? How
long will it be before the train is fully stopped?
Short moves are common when the train is changing direction, which
you need to increase the number of possible paths from one point
to another.
The general idea is to give the train a carefully timed series
of commands knowing how far and for how long the train moves during
the series of commands.
A procedure to calibrate short moves.
Write a small application that performs the following sequence
of actions.
 Place the train on the track in the sort of location where
you expect to make short moves.

Give the train a
speed n
command, where n is big enough to
get the train moving reliably.

Wait
t
seconds.
 Give the train a
speed 0
command.
 Measure how far the train travelled from its initial
location.

You how far the train will travel
for the chosen values of
n
and t
.
Experiment with different values of t
and n
until you have a reasonable set of distances you can travel.
You now know how far the train moves for a given sequence of
commands.

Position the train that distance ahead of a sensor.

Read the time and give a
speed n
command.

After
t
seconds give a speed 0
command.

When the train triggers the sensor read the time again.
The distance between the two readings is the time it takes to make
that short move.
Together with knowing when and where the train will stop if given
the speed 0 command when running at a constant velocity, this
will provide most projects with all the calibration they need.
But you can do better.
Return to: