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

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.