Thursday, 31 October 2019

One program to bind them all

A couple of years ago I completed a B.Sc. with the Open University. I started this is 1974 so it took me some time, although I took a 33 year break where technology changed a bit! In 1975 they taught me Basic but at the time I was working as a trader in the City so didn't see the use until I started writing software for my own trading rooms. As a result of this, after 20 years as a trader I broke out and started my own software company.  I have now spent the last 34 years making a living writing software or managing software projects.

My final year at the OU comprised a project management course. I used this to develop a three tier cloud based car routing system for US based freight cars. This comprised a back end "Key-Value" database (in the cloud), a dedicated web server and a mobile phone front end where all the output looked like conductor generated switch lists. Subsequent to finishing the course, I disposed of all my US outline HO stuff and went into UK based N gauge. I found that the steam locos in this gauge were extremely fragile and I had to return more than I kept, eventually, disposing of all of that stock as well. Due to restrictions on space, once my 93 year old mother in law took over my hobby room, I moved into my current Marklin set up. I am now on my second layout and have developed a train management program to define random trains within the scope of a set of defined objects.

As I am getting on with my layout almost too quickly - I can spend up to 5 hours, 5 days a week, if I care too, I have decided to go back and hone the software and, in the process, describe it for anyone out there that would be interested. If you are a keen programmer then pin your ears back; if not, then have a go. You may learn something - smile.

Background software

The backbone of an software is the programming language that is used in the development. Although this shouldn't change how the program looks and acts, it has a bearing things like speed of development, complexity and so on. Some languages are highly complex making the likelihood of bugs to increase. I have been committed to developing using Object Oriented (OO) techniques in 29 out of the 43 years that I have been able to program. My development language of choice for OO is Smalltalk - a development environment created at Xerox Parc in the early 1980s. There are a few Smalltalk environments available but for many years I have been committed to that which started out at IBM's VisualAge and is now marketed and developed by Instantiations, in Oregon, USA. In order to develop web based software, I use the Seaside framework that is popular across a wide variety of Smalltalk dialects.

Ever since I had my own model shop and had to develop complex sales and web systems for that, I have used what is known as a "Key-Value" database (KV). This, as the title suggests, stores an object (the value) into a "bucket"under a keyword. It gets more complex than that but not much more. Originally, I developed my own ad-hoc version. For my OU course, I used a 3rd party package called Riak. This has a drawback for my ongoing use in that it requires a Unix box to run. Whilst I could maintain one of these during my OU course, I didn't want to have to keep it going permanently. It wasn't helped by the company that owned Riak shutting down. I returned to my original idea and developed, what I call, my Tiny Key Value DB - TinyKV. This is an extremely compact version of a KV written wholly in Smalltalk and thus easily integrated into my proposed software.

The software with no name

Normally, software packages have names but as this is a personal thing, I haven't got round to giving it one. Let's call it PVDB (Pennstadt-Valdorf Database)? So, what does it do.

Using a web interface, the software generates a string of trains that "could" be run over my current railway. I say "could" because there is no compulsion, unlike my OU software where, if you didn't run a train, all of the freight cars would be in the wrong place when the next one came along! So, it suggests a train. If you like it, you run it, if you don't you skip it. There is an option to record the train but this is so you can run a train and leave it in a station whilst other trains happen. Then, when you bring the train back, you can release it. It is all very simple to operate. Plus, being run through a web interface, I can run it on a tablet by the railway rather than having to lug a laptop around.

To back this up, there is a database that contains information about all the locos and wagons plus a list of possible trains. Using a bunch of random generators (RGs), a possible train is created using a chosen lok and a number of wagons. Through the use of the RGs, the lok, the number of wagons and the chosen wagons all may change from run to run. Plus, as each train may have any of three destinations, there is a lot of variation as the list of trains is run through.

What does PVDB know then?

It has access to data about each Lok, wagon and train, plus it has an area in the DB to store created trains; i.e. those that are recorded. The original data was created as a series of comma seperated files created on a spreadsheet. These were read in, an object created for each item in the list and then each was saved into the DB.

Lok Data

Each lok has the following stored:
lokNumber - for display purposes
lokName - unique
nationality - to define which wagons or coaches are suitable - currently only German or Swiss are recorded.
usage - defines the type of train that this lok can be used on.
inUse - a flag to show if it is currently saved in a recorded train (and, thus, cannot be used for another train until released)
id - a numeric value that is used to define the image of the lok.
key - the data value that is used to access any item in the bucket. For loks, this is #lokName.

Wagon Data

Wagon data is as follows:
id - a numeric value that is used to define the image of the wagon. This is also used where multiple wagons of the same type are available - e.g. Swiss dumper wagons - there are 6 in the database. Each wagon carries a sticker underneath with its id printed thereon.
usage - defines the type of train that this wagon can be used on.
nationality - to define which wagons or coaches are suitable - currently only German or Swiss are recorded.
usage - defines the type of train that this wagon can be used on.
inUse - a flag to show if it is currently saved in a recorded train (and, thus, cannot be used for another train until released)
key - the data value that is used to access any item in the bucket. For wagons, this is #idString, i.e. a character representation of the id - 1 = '1' and so on.

Train Data

Train data is as follows:
train - unique identifier of train type
category- defines the type of train.
lokType - self explanatory
frequency - out of 1 - 10 - determines how often this train type is used (subject to random selection - see later)
maximum - out of 1 - 10 - determines the maximum number of wagons/coaches in the train
minimum - out of 1 - 10 - determines the minimum number of wagons/coaches in the train

Creating a train

When the program starts, it builds a list of 50 trains. It takes the list of trains and using the frequency of each adds them to the list. Thus, each train type will appear multiple times subject to each trains frequency setting.The process for example, 2 would mean that for every set of trains, this one would appear twice and so on. With the current database, this results in a list of 54 trains with each type  spread randomly across the list.  Trains are processed in order. When the whole list has been used, a new set of trains will be built, with each type occurring in a different place in the list.


There are three destinations available for trains, depending on type, etc. These are Pennstadt, Hennersdorf and Valdorf. Coming out of the storage, trains can leave going left or right. If they go left, they have a choice of circulation and returning (Hennersdorf) or entering the station (Valdorf). If they take the right hand, they can only circulate (Pennstadt). The circulating trains will make a few circuits of the track and then return to the storage. Trains for Valdorf will enter the station. Passenger trains will have their locos released and placed on the outgoing end (or, if a cab coach train) just sit there until time to leave). Goods trains will be split up and processed into the goods shed, coal depot or oil depot as appropriate. Only trains with "local" or "Inter"  in their train type can have Valdorf as a destination.

Displaying a train

Each train will be displayed along with images of the wagons:

Managing Trains

As can be seen from the above screen shot, there are some options along the bottom of the screen:
Reload - create and display the next train without saving this one.
Save Train - This marks the train as persistent and saves it as a "Created Train". This means that the lok and wagons are reserved for this train and cannot be used for other trains. If the system tries to create another train which requires the same lok as this train, that new train will be skipped.
Save to Log - saves the train to the log file for debugging purposes
Cancel Train - discards the train and returns to the main menu.

Managing Created Trains

Once a train has been created; i.e. saved, the lok and wagons cannot be reused until the train is deleted.  Going to the Show Created Trains option on the main menu gives a list of current saved trains. Should one of these have been returned to storage, it can be reset which deletes the train and releases the lok and wagons back into the pool.
There are circumstances when you may wish to release just some of the wagons and lok. If, for instance, there is a local goods in Valdorf station, the lok might return to storage with only some of the wagons, leaving others in the goods yard. Under these circumstances, only the lok and the returned wagons need be released. When the next local goods comes it, it may return with these other wagons, in which case they can now be marked as returned. 
If the situation gets a bit complicated, there is a reset button that will clear everything out.

How does it all work in reality

It actually works very well. It is uncomplicated, fast and easy to use. It is almost bug free (just one that I know about). What it does is make the running of trains subject to a proper random process. Without it, I find that I run the trains that are on the layout and never swap them out. Too lazy, I guess. Using the program forces me to run different trains and to stick to the three destination protocol. There are two current issues that make it not quite great. My local passenger train lok is going back to Marklin to be fixed and I, currently, don't have any lok to run the local goods train service. My budget has run out but it will sort itself out in December when I have some software consultancy money coming from the USA. Let's hope that the pound tanks by then (that's the currency trader in me speaking!).

Monday, 28 October 2019

Planning the track layout

The idea of the new layout was to provide for a double track main line with one station and access to storage. This involved negotiating a further 1 square meter from my wife. If fact, she was quite amenable to the whole thing and gave up a part of her dressing table top to make the plan work. It also involved moving her bedside cabinet under the railway so overall she was very kind.

The extra real-estate was required because the outside track had to be R2 sized and this would result in a lot less room if I was to go the route using only the 2 square metres that I had previously. As it happened, the top of the dressing table (or at least the small amount that I had garnered) was great for supporting the new extension. I will show how it all looks later in the blog.

I now had to come up with a plan to use the new space. There were a few druthers (as our friends in the USA call them); i.e. wants rather than constraints. My three main wants were

  1. To have the double track main line
  2. To have access in both directions to the storage tracks
  3. To rationalise the station to suit a small terminus with a few local trains
The double track would also allow for main line trains to traverse and leave without any problems.

The track plan was worked out using Anyrail 6. Not everything connected perfectly but the minor issues shown up by the software were easily sorted with a bit of wiggle in the real world.

My first plan, and one that I actually laid, was as follows:

This provided for a two track passenger station, a goods arrival track and two local goods sidings.

There were two things wrong with this. Firstly, at A, there was no way to get any wagons into here unless the delivery loco pushed them there. The local shunter would always be on the wrong end. Secondly, the third uncoupler at the front of the second main track of the station would never be used, so it was moved to the third incoming line. I did think about putting a release crossover in but the available track left would be very short, plus I didn't have a budget for two more digital points!

My next attempt looked like this.

It now came to light, after running a few trains, that there was no way for a main line train with right hand running; i.e on the inside loop, to get back out to the wye to use the storage. This required a rethink. Fortunately, my friend Adrian - from whom I buy a lot of my Marklin stuff, came up with a  couple of points, motors and decoders so I could put a second crossover into the main line.

The above still shows the uncoupler in the wrong place but I sorted that as well. Now we have a layout where, using right hand running, there can be two trains on the main line and a shunter shunting in the yard. Admittedly, trains running on the inner track have to reverse run from the wye to the crossover at the rear. To avoid crashes, the crossover is protected by a colour light signal - the signal controls a dead section just in front of it so, when red, the track is dead. Additionally, any train that wants to leave the station must reverse run until they get back to that crossover.

It isn't perfect but it will let me run three types of train - main line leaving the wye to run clockwise; main line leaving the wye to go anti-clockwise and local trains leaving the wye to go to the station. I have had to create an extra name for the train routing. Pennstadt is clockwise, Valdorf is the station and, the new one, Hennersdorf is anti-clockwise. Pennstadt gets its name from our surname - Pennington; Valdorf from my wife's name - Valerie; Hennersdorf after my wife's favourite golden retriever, Henry (always known as Henners).

That's about all for now. Just note that the one square metre board is totally removable. Also, the main board can be rotated across the desk with the "dangling" end over the edge of the desk supported on two temporary legs. Thus, I can get to all parts of the layout whilst in my office chair - crucial given the state of my arthritis!

Finally, as promised, here are a couple of shots of the new extension. As you can see, it rests on the dressing table top. The basic support for the railway is an "electric" desk which supports the main board. This desk is 1600 x 800 in size and has an electric mechanism for raising and lowering the desk height. This was supplied to my by the Open University as part of their disability support when I was completing my B.Sc. This means that I can get the height of the desk to be exactly the same as the dressing table top. It makes everything nice and easy and very firm.

The separation line is along the demarcation between the brown and the white. The two holes in the top of the brown side are for power to the two fairground rides as the main open part of this board will be the fairground.

Tuesday, 22 October 2019

Building the basic layout

First steps, as always, are to build a decent baseboard and lay the track accurately. Following that, the electrics need to be put in place. I will deal with the steps in that order.

Making the Baseboard

I am relegated to one end of our bedroom. My wife doesn't mind this (or at least, so she says) as I have given up my hobby room so that her 93 year old mother can come and live with us. My wife's two passions are murder mystery novels and completing 1,000 piece jigsaws. She has her comfy armchair and a nice jigsaw workstation (with storage for her stash of 30 jigsaws!). Apart from a B.Sc. the only other thing that I got from the Open University was a large (1600x800mm) electric desk. By electric, I mean that the desk work surface can be raised and lowered as needed. My basic board is 2000 x 1000mm so there is about 200mm overhang at each end and 200mm at the back which is very workable.

I negotiated another 1 square metre when my wife offered that I could cover some of her dressing table. This really is a cooperation project as she likes to see the railway, when it has lighted houses and a fun fair, when she lays in bed! This gave me a nice L-shaped layout that would allow a double track continuous circuit, a small terminus with a goods yard and a wye going off to my storage board. This is a major leap forward from my existing - first attempt - layout.

Hobbycraft have a super offer - four sheets of 5mm foam core A1 sized for £10.00. Three packs will be enough so £30 is not a bad cost for the baseboard of a railway of this size. You may wonder at 5mm foam core but remember two things. One, I have very bad arthritis so cannot climb underneath and with a foam core build I can lift it up and work on the underneath with me sitting or standing up. Secondly, the firm base of the desk means that, so long as I do build a proper support structure underneath then everything will be OK. In fact, so long as I get the outer rim strong, the rest is really there to make sure that the flat boards don't warp. Anyway, that's how it goes.

The outer frame is made of  three ply 60mm strips. I make these as tongue and groove so the middle one is extended out one end, leaving a groove in the other. This makes butt joints very strong. With the last layout, I use three ply all over the underneath but the cross braces didn't add anything to the strength and just made the construction time longer whilst I made the strips. This time, I used single ply strips but, instead of at 12" spacing, I brought it down to 8".

This is the basic 2 x 1m board finished and in place.

The 1 x 1m extension was a bit more complicated as it needed to be cut back on the curves to lessen the bulk in the room. Hence, I needed to lay out the loops on a plain sheet to work out where to cut everything.

Above, you can see me carrying out this operation and laying the track.

Once the track was down, I wanted to create some power busses under the board so that I didn't end up with a rats nest again. First off, I purchased some clever wire connectors.

As you can see  there are four connectors in this example. Place a wire in each hole and drop the lever gives very firm connections for almost any size of wire. This example is for the 12V bus. There is also a 5V and a DCC bus. These connectors come in a pack from Amazon (where else?). The pack contains connectors with 2, 3, 4 and 5 levers so they are very flexible.

I also kitted out the 1 m2 board with the same busses. There will be a wander lead to connect both boards together electrically.

As you can see in the above image, there are two 1cm dowels fixed within the edge of the board. These fit into two holes in  the main board. As the removable board sits on the dressing table, there is no need for anything more to keep them aligned. Remember that there is Märklin C track crossing the gap. C track is very robust so the physical rail connection is secure. Also, as the desk height can be altered, using the electric mechanism, the main desk is made to be at exactly the same height as the dressing table top. All I need is one removable leg to support the overhang and everything is tidy.

The next step was to install the powered uncoupler ramps. These need a DCC accessory manager. I chose the ESU Switch Pilot as my controller is an ESU product and I like what they do. Mind you, I couldn't work out how to change the accessory numbering from 1 - 4 and the manual printing size was for  someone with the eyes of a 10 year old. A trip to Coastal DCC, where I bought the item, had Kevin reprogramming it for me to address 21 - 24, which keeps these away from my point assignments. It was me being thick as it was the same process as with the Cobalt motors that I used with my other layouts. The final wiring looked like this (the Switch Pilot is the black box along the bottom):

At this point, the DCC bus is connected to the track in three locations as well as the main connection from the ECOS.  The last thing to do was to replace the curtains so that my wife wasn't offended by all my junk underneath the railway - grin. Mind you, the curtains don't quite reach so I need to sew some more - sometime!

Note: the curtains actually went up earlier so this picture is a bit on the premature side!

Wednesday, 9 October 2019

A new start

So, there was I, just idly chatting to my wife saying how much I like seeing trains pass each other and how this wasn't possible on my existing layout. "Well, take it down and start again" she said. This made some sense because of the way that the railway began.

I bought a Marklin train set and made some simple adjustments to the layout to fit onto my 2 x 1 metre space. I didn't give it much thought but now, looking at it, I can see the flaws. There were twin tracks where I could park one train whilst the other ran. I also put what has turned out to be a little used goods yard. What I realised that I wanted was a twin track run with a decent station for passenger trains and a small goods yard with a couple of factories rather than a three lane shunting yard with no need for that amount of wagons.

Out came Anyrail and I came up with the following.

Valerie agreed to a small, 1m square addition to the layout, which would cover some of her dressing table - remember that as mother-in-law lives with us, I have lost my railway room and now reside down the end of our, admittedly large, bedroom. The red line denotes the edge of my board and the wye feeds out to my detachable shadow station.  We now have a double track, access to a main station from the inner loops. The main station (Pennstadt) has two nice long reception tracks. There is also a couple of goods sidings and space for my V60 to hide away. There is also a head shunt to so that operations in the station won't interfere with the outer tracks. Hence, in theory, I could have three trains in operation at the same time! One of my Marklin friends suggested that I add another crossover to let the outer line access the station but this isn't how I want it to work. I am trying to use German right hand running so the outer track is for anti-clockwise and the inner for clockwise operations. This is fine except for a little bit of reverse running for the trains accessing the inner track from the wye around the bottom part of the outer loop.

I now realise that this running only works if the inner run enters the station where, coming out, it can process to the outer loop via the crossover at the top. If a train just traverses the loop and wants to come back out, there is no way without reversing. As an example, my crocodile with some SBB hoppers would have no reason to enter the station so, if travelling on the inner loop, would have an issue. I am now putting a crossover in to the left of the wye using left hand points so any train can pass the station entrance and still get out to the wye.

On the first layout, I took my wife seriously - "DON'T use a computer to run the trains!" was her comment. This meant that I only powered the points at the back of the layout where they were hard to reach. On discussion, she agreed that she didn't mean that quite so I have now got an agreement to power all of the points. Little does she know that the layout on the JMRI screen will just become one of the ECOS!

The layout was torn down - foam core is very light and easy to break up. We live in an apartment block and have a large bin room in the basement so the remains were quickly disposed of.  Our local Hobbycraft sells top quality A1 foam core at £10 for 4 sheets so that was a cheap way to get the new boards built. I had a small annual pension pay out in September and mother-in-law gave me a small bonus so I had all the funds that I needed. I put the plan together and ran up a track parts list. Working out what I didn't have was easy - I am a bit of a spreadsheet buff - and off went the requirements to my friend Adrian, who always has lots to sell at good prices! He was able to fill my needs so I was able to start. Being retired, I can dedicate lots of whole days to my railway so it will all go together very quickly.

Next, I will cover the construction of the boards and the wiring.