CS452 - Real-Time Programming - Winter 2017

Lecture 20 - Short moves; Anthropomorphic Programming.

Public Service Annoucements

  1. On Friday the 17th we decided that the final exam will start at 12.30 on Thursday 6 April, 12.30 and end on Friday 7 April at 15.00.
  2. First Train Control Milestone: Tuesday 7 March.

Calibration I

1. Calibrating Stopping Distance

2. Calibrating Constant Velocity

Moving the train at a Pre-chosen Velocity

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.

3. Short Moves.

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.

  1. Place the train on the track in the sort of location where you expect to make short moves.
  2. Give the train a speed n command, where n is big enough to get the train moving reliably.
  3. Wait t seconds.
  4. Give the train a speed 0 command.
  5. Measure how far the train travelled from its initial location.
  6. 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.

  1. Position the train that distance ahead of a sensor.
  2. Read the time and give a speed n command.
  3. After t seconds give a speed 0 command.
  4. 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.

Anthropomorphic Programming

We all, even most programmers (!), have effective intuitions about human relations

Tasks are independent entities

Servers and Attendant Tasks

Why do servers need attendant tasks?

1. Proprietor with a Notifier

Proprietor `owns' a service, which usually means a resource.


  1. Notifier is usually of higher priority than server
  2. The server buffers both clients and data from the notifier. In this implementation client and data buffering are duals of each other. Our server code should

2. Using a Courier

We can handle two interrupts coming very close together using a courier. Simplest is best, so we shouldn't go beyond a courier unless we expect more than two.

Transmit Notifier Code

Transmit Courier Code

Transmit Proprietor Code


This gets you through a bottleneck where at most than two events come too fast.

Remember that all the calls provide error returns. You can/should use them for error recovery

Another possible arrangement for task creation

Another possible arrangement for initialization

Distributed gating

I am showing you collections of tasks implemented together because sets of related tasks is a level of organization above the individual task.

E.g., the decision to add a courier requires revision of code within the group, but not outside it.

3. The Warehouse

Add a warehouse between the courier and the notifier.


The initialization given for the courier, above, generalizes to include the warehouse, essentially without change.


The notifier is now talking directly to a server and has the shape given above for the proprietor. The warehouse cannot talk directly to the proprietor because both are servers.


This structure clears up most problems when a burst of requests to the server would leave the notifier waiting in a long sendQ.


  1. Handles bottlenecks of all sizes. Give a precise and quantitative definition of `bottleneck'.

What this amounts to is that a server should be lean and hungry

Return to: