CS452  RealTime Programming  Spring 2015
Lecture 20  Trains
Public Service Annoucements

Kernel 4 due in class on 18 June.

The exam has three start times.

20.30, August 4

09.30, August 5

20.30, August 5
The end times are 26.5 hours after the start time.
Answers to questions asked from 20.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?
1. Calibrating Stopping Distance
2. Calibrating Constant Velocity
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
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.
Arbitrary Velocity
Suppose you want to drive a train at a velocity that isn't in
your table. Try this:

Find the speeds that provide velocities just above and just
below the speed you wish.

Solve the equation v = (s)v_above + (1s)v_below

Drive at v_above s percent of the time and at v_below (1s)
percent of the time.

If the acceleration profile is the inverse of the deceleration
profile this just works.
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.
4. Calibrating Acceleration and Deceleration: Doing Better
At this point you can do most of the things you will want to do
for your project. But some things can only be done from a standing
stop. It's more elegant to keep the train moving, speeding up
and slowing down as required. To do so it's necessary fully to
calibrate velocity during the act of accelerating and decelerating.
Keeping a train at a predetermined velocity, for example, requires
changing from one speed to another frequently.
To explain velocity changes we must introduce models. On the track
the train has a real location, so mant cm past sensor S. In your
program the train has a position, so many cm past sensor S'. The
model is linked to the real train by the calibration. Neither the
number of cm nor even the sensor is necessarily the same in the
model and in reality because no calibration is perfect. The
performance of a project, such as whether trains collide or not,
depends on the difference between the model and reality. The
remainder of this section is based on minimizing different measures
of discrepancies beteen a model and reality.
Back to real trains. When you give the train a command to change
speed, we know roughly how the velocity changes.
 slowly at first
 increasing
 reaching a maximum, possibly for a nonzero time
 decreasing
 more and more slowly as the new velocity is approached
How should we model the process of speed changes?

One might carefully measure the function that gives velocity
as a function of time or distance after the command is given.
How could one measure the instantaneous velocity?

Video the train driving beside a tape measure, calculating
mm/frame by differencing the positions in successive
frames.

Measure the time it takes for the train to travel at
constant speed from one sensor to another. Then give the
change speed command at varying distances before the
sensor and measure the differences in time to the sensor
trigger. Then construct a linear approximation.

Start with the crudest possible approximation, and improve
it as you require more precision. An example is below.
The simplest possible model is a step change from the initial
velocity to the final velocity. When should the change occur?
 When the velocity change begins? The model gets ahead of the train.
 When the velocity change ends? The model falls behind the train.

Somewhere in between? There is a time when the train gets
ahead of the model for a while, then falls behind exactly the
same amount so that at the end of the speed change the model
exactly matches the train position.
You can improve this by constructing a linear velocity change
model, a bilinear velocity change model, a quadratic velocity
change model, or whatever. Oral comments in class give possibly
helpful, possibly extraneous suggestions.
When you drive a train there are four things you care about, all
of which are functions of time. Particularly you care about the
effects of discontinuities in them.
 Location on the track, specified by x(t), usually anchored at the
previous sensor.
 A discontinuity is teleportation, which requires infinite
velocity.
 Trains do not provide infinite velocity (teleportation)
 Velocity along the track, specified by v(t) = x'(t).
 A discontinuity is an instantaneous change in velocity, which
requires an infinite force.
 Trains do not provide infinite force
 Acceleration, specified as a(t) = v'(t) = x''(t).
 A discontinuity in acceleration, which feels like a jerk. Think of
what it feels like when you are tied to a rope that is being pulled.
First the rope exerts no force on you, then suddenly there is a jerk
and you feel a force. Not nice.
 Do trains provide infinite jerk?
 Jerk, specified as j(t) = a'(t) = v''(t) = x'''(t)
 We can avoid discontinuities in acceleration by keeping jerk
constant.
 Thus, j'(t) = x''''(t) = 0.
Back to real trains. When you drive a train there are four things you care
about, all of which are functions of time. Particularly you care about the
effects of discontinuities in them.
 Location on the track, specified by x(t), usually anchored at the
previous sensor.
 A discontinuity is teleportation, which requires infinite
velocity.
 Velocity along the track, specified by v(t) = x'(t).
 A discontinuity is an instantaneous change in velocity, which
requires an infinte force.
 Acceleration, specified as a(t) = v'(t) = x''(t).
 A discontinuity in acceleration, which feels like a jerk. Think of
what it feels like when you are tied to a rope that is being pulled.
First the rope exerts no force on you, then suddenly there is a jerk
and you feel a force. Not nice.
 Jerk, specified as j(t) = a'(t) = v''(t) = x'''(t)
 We can avoid discontinuities in acceleration by having jerk
constant, possibly with discontinuities.
 Thus, j'(t) = x''''(t) = 0.
Models with infinite acceleration (= infinite force)
One model we discussed in Lecture 19 has a discontinity in velocity.

a(t) = v'(t) says that the acceleration is infinite. (actually
notdefined, but the limit as the change in velocity goes to
zero is infinite.)

F = ma says that the train is subject to an infinite force.
What happens when something is subjected to an infinite force?
Models with infinite jerk
Models with infinite acceleration approximate velocities with
linear segments. We can smooth out the corners using a quadratic
approximation.

Look at the time when the change speed command arrives at the
train.
 Immediately before the command v(t) = v0, a(t) = 0.
 Immediately after the command a(t) = a0, v(t) = a0 t.
 Acceleration is discontinuous => jerk is infinite.

Infinite jerk also occurs in the middle of the velocity change
and at its end.
 Can we tolerate infinite jerk?
Models with finite jerk.
Let's look at this from another direction. We have a collection of constraints.
 x(t0) = x0.
 v(t0) = v0.
 a(t0) = 0.
 v(tf) = vf.
 a(tf) = 0.

Note that x0, the initial location, v0, the initial velocity,
and vf, the final velocity are defined by the boundary
conditions.

tf is a variable for which we solve along with the other
parameters.

A quartic polynomial, Ax^4 + Bx^3 + Cx^2 + Dx + E. We have five
equations. tf is a parameter in terms of which the coefficients
of the quartic polynomial are solvedp>
A quartic function has five parameters. Substituting the constraints
into the function gives five linear equations in five unknowns.
This is the simplest function that has discontinuities in jerk
only where it joins the constant solutions. It is the solution
to x''''(t) = C. Confidence that this is a reasonable solution
is bolstered by noticing that this equation occurs frequently in
biodynamic models.
Return to: