Example: Model Train Controller
Example: Model Train Controller
Example: Model Train Controller
Purposes of example
Follow a design through several levels of abstraction. Gain experience with UML.
ECC
command
address
header
Requirements
Console can control 8 trains on 1 track. Throttle has at least 63 levels. Inertia control adjusts responsiveness with at least 8 levels. Emergency stop button. Error detection scheme on messages.
Requirements form
name purpose inputs model train controller control speed of <= 8model trains throttle, inertia, emergency stop, train # outputs train control signals functions set engine speed w. inertia; emergency stop performance can update train speed at least 88 times/sec manufacturing cost$88 power wall powered physical console comfortable for 8hands; < 8 size/weight lbs.
DCC documents
Standard S-9.1, DCC Electrical Standard.
Defines how bits are encoded on the rails.
Conceptual specification
Before we create a detailed specification, we will make an initial, simplified specification.
Gives us practice in specification and UML. Good idea in general to identify potential problems before investing too much effort in detail.
Message classes
command
estop
Train:
receive message; interpret message; control the train.
1 receiver 1 detector* 1 1
Detailed specification
We can now fill in the details of the conceptual specification:
more classes; behaviors.
Sketching out the spec first helps us understand the basic relationships in the system.
+ V -
Class descriptions
panel class defines the controls.
new-settings() behavior reads the controls.
Class descriptions
transmitter class has one behavior for each type of message sent. receiver function provides methods to:
detect a new message; determine its type; read its parameters (estop has no parameters).
Formatter class
formatter current-train: integer current-speed[ntrains]: integer current-inertia[ntrains]: unsigned-integer current-estop[ntrains]: boolean send-command() panel-active() : boolean operate()
Controlling throttle/inertia/estop:
read panel, check for changes, perform command.
Panel-active behavior
T panel*:read-train() F T panel*:read-speed() F ... ... current-train = train-knob update-screen changed = true
Controller class
controller current-train: integer current-speed[ntrains]: integer current-direction[ntrains]: boolean current-inertia[ntrains]: unsigned-integer operate() issue-command()
set-speed
receive-command() issue-command()
estop type=000
Summary
Separate specification and programming.
Small mistakes are easier to fix in the spec. Big mistakes in programming cost a lot of time.