More on the MSTS interface

Just briefly – my theory on how the controls work is correct. Certainly diesel and electric are controlled from different memory locations – but it’s a bit more complex than that.

For example, you can see my code below driving the default MSTS Tokyo-Hakone route with the standard electric set (which has an EP brake as well just like the BSet).

What does appear to be the case is that it is done  by control type not locomotive type. i.e. my code will control the Series 2000 default electric loco, but not the Series 7000 (as the series 7000 has a combined power handle/brake setup). So what we need to do is write the interface to handle all the different types.

Driving the default electric set.

More on the MSTS Interface

The weather here has been rather awful the past few weeks, with temperatures exceeding 35C. This coupled with a number of comments I have received have encouraged me to concentrate on the interfacing part of the simulator – i.e. the code that allows external control of MSTS.

My intention is to create a generic API for MSTS that programmers can use to interface any number of devices into MSTS.

To this end I have considerably tidied up the VB code and modified it to make it easy to add new functionality – primarily by ensuring all the memory addresses are maintained in global variables, rather than just ad-hoc as it was before 🙂

Below are a couple of screenshots of me driving MSTS entirely “keyboard free” using the VB “MSTS_CONTROL” app. At this stage I only have throttle, brake, “zero brake pipe”, amp gauge and brake pipe gauge controls, but I’ll add the remaining ones in shortly. I will then expose all the functions (as a dll) which will complete version 1 of the msts_control library.

MSTS being controlled "keyboard free"

MSTS being controlled "Keyboard Free"

Amp gauge PWM working

Driving the amp gauge from the DSE kit was easier than I anticipated – the DSE kit does not use a number of pins on the 16F877A – simply configuring one of these for PWM was all that was needed. I removed the ULN2003’s from the circuit and are just driving the output directly from the microcontroller via the pullup resistor. I’ve added an extra control command which simply varies the duty cycle and I can make the gauge read from 0 to 300A with all points in between.

Picture below – this really needs a movie which I’ll upload later.

Simulator Construction - Amp gauge control working

Todo List

Still to finish:

– Finish installing remaining cab controls (Bell, Fuse Tester, Trip Switch, Horn, Wiper)

– Finish PWM amp gauge control

– Build air gauges / stepper motors

– Optics – this will be a 40″ monitor mounted in the “cab” window

– Finish MSTS interface

A fair bit to do…

MSTS and EP Braking

What what I can determine – MSTS believes a EP brake is what we Aussies would call a “straight air” brake – with an emergency function. This is irritating as the Red Rattlers had a dual EP/Westinghouse system in which there were electric application and holding valves – but a full Westinghouse system was also available if the EP brake failed or by selection on the brake stand.

This is proving hard to simulate – I can almost get there by using EP mode and directly manipulating the brake pipe pressure externally – however MSTS won’t activate the brakes on the cars unless the BP pressure drops to near 0 – i.e. it won’t allow gradual Westinghouse and EP – however there are some more avenues of poking around inside the MSTS memory space to see what we can find…

I’ll keep working on it – any ideas email me!

Getting data in and out of MSTS

One of the key challenges to building a simulator such as this one is to utilise good software.

The Simpit builders have it lucky – as they have such great sims as Flight Simulator X and Falcon 4. Both of these include support for external interfacing.

The train simulator community isn’t so lucky – MSTS is still an excellent program and it is the one that I have chosen to power my simulator, however lacks any form of API – something that is a huge drawback. There have been an number of attempts to over come this – the Raildriver product uses a “screen scrape” of the data – a novel idea but something I would like to avoid.

Doug Johnson, with his “My Ultimate Toy Train” project, provided some answers – if you read his excellent project report (site link on the Blogroll) he did a lot of the ground work in getting data out of MSTS using the excellent “Cheat Engine” tool.

However, I have taken his ideas one step further. MSTS allows for data to both be extracted and input back into the simulator – avoiding any need to develop a keyboard emulator or the like.

Utilising the same concepts as Doug, I developed a series of patches for MSTS that create a code cave in which a sequence of pointers is stored to key values within MSTS. Then it is simply a matter of writing and reading to these addresses and you can control MSTS externally!

What is also interesting is that as well as providing input for the brake and master controller inputs, MSTS will also allow you to change other operational parameters and it will behave accordingly.

For example, it is easy to simulate a breakaway by reducing the value of the brakepipe to 0, MSTS then automatically applies the brakes (as is prototypical of the Westinghouse system). Also prototypically, it does attempt to keep recharging the brakes – until of course you lap the brake valve.

It’s a bit hard to see in the screenshot below – however this is Chris Jennings’ B-Set sitting at Central platform – on the right is a quick piece of VB code that is reading (and writing) data into the memory space of MSTS. The number at the top reads 95.6666 which is the brake pipe pressure – the number on the left is 0 which is the current amps being pulled. (The throttle is closed here). The words are “CAFEBABE” which is a test hex string for debugging and the buttons activate the brakes externally to MSTS.

Simulator Software - MSTS showing connection to a VB6 app

This next screenshot shows us accelerating at the end of the platform – the amp gauge now reads 379.5977 and the brake pipe 110. Note – this is configured as “straight westinghouse” in MSTS. The Red Rattlers used an EP brake – but I’ll get to that in the next post.

Simulator Software - Accelerating Screenshot

Interfacing

One of the key challenges in this project is to interface all the various cab components to MSTS. I will deal with the hardware aspects first – and move onto the software in a post a bit later on.

The master controller and the brake controller are reasonably straight forward – they both have electrical contacts in all the relevant positions. My intention for these is to utilise a commercial USB 32-port input/output unit I purchased from eBay, which has not arrived yet.

As for the gauges – I unfortunately don’t have a full set of air gauges as these were missing from C7487 and I don’t wish to destroy my Elcar ones. However – a simple solution will be to make my own utilising stepper motors – more on this one to follow.

The amp gauge, line lights etc were much easier – I was able to get a hold of some K-3090 Dick Smith Electronics Serial I/O Kits – going cheap from a large kit clearance through Allbids. These are in fact the Silicon Chip project from November 2005 – they provide 10 inputs, 10 outputs some analog inputs and a couple of relays. Most interestingly – they are driven by a PIC16F877A – quite a powerful processor for such a simple kit. The analog inputs aren’t much use however, the digital outputs are driven from the microcontroller through a ULN2003 in an open collector setup. Installing a pot and adjusting accordingly leads to being able to drive the gauge from 0 to full based on the digital output… not so useful really .. however… we should now be able to drive the amp gauge by utilising the hardware PWM of the 16F877A to vary the average voltage seen by the meter.. (remember there is a variable resistance as well)

Simulator Construction - Amp gauge being driven by the DSE kit on right

I have not yet started on rewriting the PIC code to achieve this – however it shouldn’t take me all that long to get the amp gauge up and running properly. I will post a photo when I do.

The relay outputs will come in very handy for switching on and off the line lights and brake stand control lights.

Fast forward to 2010…and the build starts

After three house moves and six years, I finally bit the bullet and decided to start building the simulator proper.

With the wife’s encouragement (she was sick of hearing about how I was going to get to it one day) – I started the build.

Some compromises had to be made – the simulator slightly narrower and is about half the length of the original cab – in other words, it is the portion of the cab facing forward from just behind the driver’s seat.

As I was expecting my first attempt to fail – I neglected to take any pictures during initial construction – however the simulator is basically a wooden frame on which I have nailed up coreflute as a lining material. Coreflute is a great material – it is light, easy to paint (if you use the correct plastic primer) and I got most of it for free.

After I had completed the frame, lined it and installed the basic cab equipment – I started to believe that my first attempt would result in a working sim!

Simulator Construction - Brake stand and master controller

Note the brake stand above is mounted on a wooden frame – the original frame is the one part we couldn’t get out of the cab of C7487 as it was piped below the carriage and we didn’t have an oxy to cut the bolt that wouldn’t budge. However I have the original cover and it is indistinguishable when the cover is on – it also serves as a useful place to house the electronics.

Simulator Contruction - Master Controller

This is actually the Master Controller I got from Elcar in 1992 – C7487’s original controller was in very bad condition.

Simulator Construction - breaker panel

Simulator Construction - Amp Gauge

Simulator Construction - Gauge Panel

Some history of C7487

A bit of history of C7487 – courtesy of John Horne, Michael Kerry and Paul Rogers.

C3487 – available for service – 19.12.1952.

C3487 – to Tulloch Ltd (for four-motor conversion) – 14.12.1969.

C7487 (ex C3487) ex Tulloch Ltd – 13.10.1970.

C7487 – withdrawn – 3.12.1992.  (interestingly this makes it one of the last “manual door”cars to be withdrawn – it must have featured on the M-Sets that survived post Jan-1992)

C7487 – condemned – 22.12.1992.

C7487 – sold – 27.4.1993.

Colour Schemes

C3487 – 12.1952 (as built) – indian red with buff lining.

C7487 – 10.1970 (as remotored) – venetian red with State Coat-of-Arms on white oval background.

C7487 – 5.1975 – blue & white with “Sydney-NSW-Transport” logo.

C7487 – 7.1980 – indian red with white “L7” logo.

C7487 – 9.1990 – indian red with blue/yellow “L7” logo.

When remotored in 1970, the leading passenger vestibule of C7487 was enlargened by moving the transverse bulkhead separating that vestibule from the guard’s area slightly forward. All cars in the 7480-7510 range (except for 7500, the first four-motor conversion) had this modification.