Train routing

If you are using the automatic script generator (you write TDL files) then you can give your trains routes and you can add manual control over these routes.

Routes guide or control the behaviour of various sections of track as the train progresses around a layout. Rather than specify exactly what happens in a hard sequence our routes can define as much or as little behaviour as you wish.

Routes are defined by giving individual track elements instructions for how they should handle a train. For example lets suppose we have a simple loop of track with some passing loops and a siding. We want to tell the train to run around the loop three times and then go into the siding. This could be described by telling the turnout that feeds the siding: "Send the train left (around the loop) three times, and then to the right (into the siding)". This instruction is associated with just that track element, and just for that particular train.

The instructions for a particular track element are in the form of a word in a string. Different words can control different track elements and they can be strung together in the string in any order. Each train can be given a routing string

Each word in the routing string is of the form:

Track name

Colon

Routing instruction list

For example the word: "TO1:L3R1" tells turnout 'TO1' to send the train left three times and then once right. Typically a routing instruction is a single character saying what to do (like 'L' and 'R' in the preceding example) and then a single character defining how many times to do that. As the train travels around the layout the routing instructions are 'used up'. L3R1 becomes L2R1 and then L1R1. The next step takes it to just R1 (the L1 instruction is removed when it is exhausted). After that the R1 is removed, leaving no instructions for TO1 so the whole word is removed.

The count character can in fact be any character with an ASCII code greater than Hex 30 (character '0'). Letter 'A' is Hex 41 would equate to a count of 17. Character '~' is Hex 7E and would equate to a count of 78.

Each type of track element has different routing capabilities, and hence different routing instruction syntax:

Turnouts

Turnout routing instructions are a single route control character and a repeat count character. The route control character can be any of:

L

The train MUST go left here. If the left-hand route is busy then the train will wait for it to be free, as if the turnout was not there.

l (Lower-case 'L')

The train will go left if the route is free. If the left fork is busy and the right fork is free then the train will go right. Useful for indicating preferences in passing loops.

x

If only one route is free, the train will go that way. If both routes are free then the probability of choosing to go right is defined in the TDL file as a parameter for the turnout.

r

The train will go right if that route is free.

R

The train MUST go right.

'x' (or in fact any character other than L,l,r or R) is the default behaviour as if no route were defined.

Networks

Each NetRoute has an identifier which is a string (used together with the network name to identify a particular NetRoute). For routes to function the last character of the route must be unique for routes from a given input to the network. Other characters can have any significance you like.

For example the line:

NetRoute N2 10a from L10 to ...

The network is called "N2", the route within the network is called "10a", the last character of the route name is 'a'. The network input is referred to as "N2L10" (the concatenation of the network name and the track entering the network. This last name is used as the label to control routing and the last letter of the route name to identify particular routes.

Like the other routing words the routing list is a pair of <last letter><count> but instead of defining a sequence of routes to take it defines a priority list where the later in the list the more preferred the route is. For example:

N2L10:a2b3

means for a train arriving on line L10 into network N2 the preferred route is the one whose name ends with 'b', but if that route is currently busy then the train will chose route 'a'. Once the train has takes route 'b' three times OR taken route 'a' twice then the whole word is deleted. If you want the train to take route 'b' twice (and use route 'c' as an alternative) and then route 'a' three times you should use:

N2L10:c2b2 N2L10:a3

Initially the train will prefer route 'b' and once that has been taken twice the word will be deleted so the next word comes into play and the train will then use route 'a'.

The route control characters can take the following values:

any character

The route from the road that the train is approaching that has an identifier ending with this letter is considered. The most preferred route is the last listed in the routing word.

If a route word is present but none of the routes listed are available then the train will wait. If no valid routes are specified (if for example you had a faulty routing word) or deadlock exists then the train will wait forever.

Long track sections

Technically a plain track section cannot take part in routing, but a train can either proceed, pause, or terminate here. Once again each route control character is followed by a count, though a termination instruction having a repeat count is an interesting concept.

A-Z (in fact any character with a code of 65 ('A') or more).

The train will pause here (as if at a station stop) for a period specified by the product of the character code minus 64 and the stoppedFraction variable set using setVar in the TDL file. StoppedFraction is specified in units of 0.1s and defaults to 10 (one second). Thus A means stop for one second and Z for 26 seconds. For longer delays '~' (code 126) will result in a delay of 62 seconds. Even longer delays can be achieved by changing stoppedFraction.

-

The train will proceed as if no route were present.

!

The train will terminate here. This means that the train will be removed from the roster and its calibration data destroyed in preparation for manual actions. A new train will be recognised by the system if the state of the section is set to GetTrain (eg L1.s=GetTrain)

.

The train will terminate here. The train will stop until a new route is assigned and the request variable of the section is set to Starting (eg L1.req=Starting)

Adding routes to your TDL file

To add routes you need to manually insert some TCL script into your TDL file. See here for details on inserting code.

It is probably best to avoid trying to specify everything in one all-encompassing route. Make directing your trains a co-operative venture between TCL script and the route strings. For example if you want a train to select alternate paths through a passing loop it is impossible to make an infinitely long route such as:

TO1:L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1L1R1

So a more flexible strategy might be:

When route[1].wordStarting ("TO1:")=0 DO route[1].append ("TO1:L1R1 ")

This will refresh the route for TO1 every time is is exhausted, as well as inserting it at startup. It means when there is no word in the route for train 1 starting with "TO1:" append a new routing word. Note the space at the end of the string. Without the space all your 'words' will join into a single word and will probably not work as you expect.

You can of course use a manual control panel, or a CTC panel to allow the operator to define when to award routes to trains.

Note that sections reduce their count and possibly remove the route word when the train leaves the section. It is advisable to not change a route while a train is in the section otherwise your new route may get removed before it is actioned.

Last updated 25/08/08

© Howard Amos. 2008