Computer Controlled Train Set

 
Submitted by Jane L, Northampton.  
 

    I had two objectives in starting this mini project. One was to help me get a better understanding of using a PC for control and automation. The other was to make my younger brotherís train set a little less boring. I never could understand the pleasure in watching a train just going round and round in a circle.
    The train set was a modest "Hornby Rural Rambler" which came complete with some track, a power supply  a train and some carriages. The idea was to be able to vary the speed and direction of the train under computer control while monitoring itís position around the track with some strategically placed sensors. While I considered starting from scratch and trying to hook up some inputs and outputs to the PC printer port I found that using the Motor-Bee from PC User was a better option for getting me into the fun part a bit earlier. This offered control of up to 4 motors and, crucially for me, also had some digital inputs to hook up to my sensors. On closer inspection the 4 motor capability was for uni-directional motors. Since I wanted a reversible train I needed to use it in itís bi-directional mode which meant using two of the motor outputs. This was fine for me and left me wondering how I could use the remaining two. The Motor-Bee also had 4 digital outputs and an output for a servo which I had no immediate need for.
    The first stage was to get the train working with the Motor-Bee without any sensors. To do this I used the supplied ďMotor-WayĒ software and connected it to the train set as shown on the left, and also shown below with the USB cable from the computer connected. The connection was very simple. I cut the twin core cable that normally connects the power supply to the track (out of sight of my brother) and connected the  +12v and GND from the supply to Vm+ and GND on the Motor-Bee respectively . It was important to get the supply connections to the Motor-Bee in the correct polarity so I had to be careful which way the direction selector switch was set on the train power supply.  I then connected OP1 and OP2 on the Motor-Bee to the track connector end of the cut cable It didn't matter which way round this was connected since the drive was reversible.
    I was pleasantly surprised how effective this simple, 5 minute, connection was. I initially just used Motor-Way in manual mode moving the sliders on the screen to run the train forwards and backwards at different speeds. I then progressed to using the automatic mode which allowed a number of ďstepsĒ to be specified. Each step had a duration and a speed and direction setting for the train. I soon had the train speeding up, slowing down and stopping at the stations. Whilst a lot of fun and definitely much better than just going round in a circle I aimed for still more authenticity. i.e. setting fixed durations and speed you can estimate where the train is going to stop (i.e. at the station) but it eventually ends up stopping at other locations since there is no actual feedback to say it is at the station. This is where sensors make their contribution.
   Initially I thought about using simple micro-switches to sense the position of the train but eventually decided on a non-contact optical method. I used a combination of an infra-red photo-emitter and a matching photo-transistor. These were available from an online component supplier and were relatively cheap (about 30p each). The circuit I used is shown below.

    I used 4 of these altogether, positioned at key locations around the track; at the station entrance and exit, half way round and at the spur (more on that later)..
   This was as far as I could go with the software supplied with the Motor-Bee. It was now time to write my own. I normally write using Visual Basic wherever possible and did so here. The DLL (Dynamic Link Library) supplied with the Motor-Bee cut through all of of the USB comms problems and provided me with three basic functions. InitMotoBee() to initialise the environment, SetMotors() to set speeds and directions etc.., and Digital_IO() to read the digital inputs and set the digital outputs.
    My first stab at the task was to copy the example Visual Basic program supplied on the Motor-Bee installation disk and start there. I use Microsoft Visual Studio for my VB and this example code came in a Visual Studio compatible workspace form, so it dropped straight in and compiled. It is a very basic program but at least it highlights how to do the basics (forgive the pun). It allows you to type in the numerical values for speed and then a button to send that configuration to the Motor-Bee. It also reads the digital inputs and puts them on the screen as tick boxes. I used this to test my optical input sensors.
    Without going into the fine details of my VB program, the essence of it is to repeatedly scan the digital inputs and, depending on when individual sensors are activated, switch to new control sequences to implement a series of speed and direction changes. This leads to slow down at station, pause at station, speed up exiting etcÖ Not only does this allow a variety of motions around the track, but by keeping count of the activations of the sensor inputs, I changed the control sequences each time, giving the impression of a very complex train behaviour (schedule). Very pleasing to the eye.
    There is also a spur track with a track switch (points), which I decided to try and use in some way. Although itís a mechanical movement to switch the track I decided to try and automate it. My initial thoughts were to use a solenoid actuator controlled by one of the digital outputs via some relay driving IC . After consideration, and in the interests of furthering my knowledge, I decided to use a servo. I have never used a servo before so had to do a bit of reading up. Although it is a bit of an overkill to use a servo for this simple task , it does the business with some style. The servo I chose to use is a Hitec model HS310. This uses 6v power and the signal direct from the Motor-Bee. Connection details below.

   The servo was purchased for 5 GBP from the (aptly named) Servo Shop website. It came with a small bag of various attachments for the moving part allowing me to improvise a push rod type linkage to the track switch.
   Control of the servo was fairly simple once I realised that there was no off condition. i.e. sending the number 128 to the MotorBee DLL function for the servo position actually sets it to itís mid way (or neutral ) position. A number between 128 and 0 makes it go anti-clockwise by that amount (0 being the extreme anti-clockwise position) and a number between 128 and 255 goes clockwise. The total range of movement of the servo was 180 degrees. I found that the extreme ends of the range (i.e. above 220 and less than 25) had no effect on the servo since it was already at itís limit. In fact setting it to these values caused it to buzz slightly. On further reading I found out that, due to the range of servo types, you canít assume that 255 is maximum position. It varies with each servo design and the MotorBee control allows for worst case in its range. When you set a servo beyond its range it doesnít explode (thankfully) it just gets a little warm and buzzes !  On the left you can see how the servo was arranged beside the track switch before it was bolted to the support platform.
    With the track switch now under my program control I then wrote some program to create a sequence that stopped the train just past the spur, switch the points and then reverse the train into the branch line. Very, very pleasing to the eye. I considered the possibilities of having more than one train on the track and using the spur to select the active one. This would involve shutting off the supply to sections of track under program control. This would be possible using some of the digital outputs and perhaps a relay or two but I decided it was a bridge too far at this point.
    With a little more work on the program and itís sequences I managed to put together some interesting and varied combinations of train movements. The net effect of running this program was quite stunning to watch. It transformed a simple train set into something that held your attention. It certainly stunned and re-enthused my younger brother.

   
                   
   

View our full range of USB automation boards

   
                   
       

         
 

.      .

 
 

© Copyright pc-control.co.uk 2008