Main Line Program Code




This page contains the program code portion of the Basic Stamp 2 program that controls the main line automated functions. The complete code and documentation can be downloaded using this link:  ReverseLoop.zip

' ======================================================================
' ReverseLoop.bs2                                             2-05-2004
'
' Automatic Reverse Loop Control
'
' This program controls the track turnouts in three areas of the D&B
' model railroad; reverse loop and hold-over tracks, midway siding
' tracks, and he yard approach wye. For tracks that involve a reverse
' loop, control includes automatic track power polarity matching with
' the mainline track.
'
' General Description:
'
' This program code utilizes a poling loop to test inputs and control
' outputs. The code has been designed to minimize the effects to the
' programs response time for multiple active inputs or other special
' conditions.
'
' Sensor, button, and turnout position input to the program is always
' "live" and reflects current operational conditions. Program controls
' are used to act on or ignore active input. Outputs are direct connected
' to the devices they control. The level (high or low) of the connection
' is used to activate the device curcuit.
'
' The MainLoop code cycles and tests the various inputs. It branches
' to specific program code via subroutine calls for the processing of
' active input conditions and the activation of associated outputs
' as required. The instruction execution speed must be fast enough so
' that the continual branching in and out of the MainLoop code does not
' effect its ability to control necessary functions in a timely manner.
' Where possible, subroutines make use of common code to minimize the
' overall program size.
'
' The "Turnout" routine most effects the overall response time due to
' the time period needed to switch a turnout. A balance has been found
' between the turnout coil voltage, its active duty cycle, and the
' program response time. Turnout coil voltage considerations include:
' switching noise, coil burnout risk, and reliable operation of all
' turnouts based upon the use of a single set of working parameters.
'
' Reverse Loop and Hold-over Tracks:
'
'                             B1/P1
'      /-------------------------------------------------\
'     /   /-------------------------------------------\   \
'     \  |                    B2/P2                    |  /
'      \ | S2                                          | / S3
'       \|                                             |/
'   TP2  \ T2                                      T3  /  TP3
'         \-------------------\   /-------------------/
'                              | /
'                          TP1 |/ T1
'                              |
'                              |  S1
'                              ~
'
' Most of the trackage involved with this section is hidden and used for
' train trip hold-over and return. Two sidings are available each with a
' train present block detector (Bx), track power polarity reverse relay
' (Px), and optical sensors (Sx) to detect train movement. Turnout sensors
' (TPx) provide the position of the turnout (Tx) points.
'
' Reverse loop operation requires that for an inbound or outbound oper-
' ation, with respect to a siding, the rail polarity must match mainline
' rail polarity. This rail polarity match is required only while power
' drawing portions of a train are in transit across the siding rail gaps.
'
' In operation, a train on the mainline approaches the reverse loop. It
' is detected by sensor S1. If block detector B1 is inactive, T1, T2,
' and P1 are set to direct the train to siding B1. If block detector B1
' is active, T1, T3, and P2 are set to direct the train to siding B2. If
' B2 is also active, the train wreck warning is sounded. Turnouts are used
' this way to take advantage of the 'straight' portion of hidden turnouts
' T2 and T3 to help minimize derailments. Trains always move clockwise
' through siding B1 and counter-clockwise through siding B2.
'
' A train leaving B1 or B2 will be detected by S3 or S2 respectively.
' T1/T3/P1 or T1/T2/P2 are set to direct the train back onto the mainline.
' These conditions are maintained until the outbound train is no longer
' detected by S1. After a short additional delay, the siding turnouts
' and power polarity relays are reset to default inbound conditions.
'
' For an inbound or outbound operation, it is necessary to disable acting
' on S1 active indications following the initial one. For the inbound
' direction, this prevents turnouts from changing as the block detector
' begins reporting the presence of a train. In the outbound direction,
' it prevents assumption of an inbound train and T1 operation.
'
' The TPx sensors are used to determine the current position of a turn-
' out. Thus, a turnout command is sent only if the respective turnout
' is in the wrong position for a sensor detected operation. The TPx
' sensors also serve to ensure a turnout has moved to the commanded
' position. If a turnout fails to position, a turnout position warning
' is sounded.
'
' Stopping or backing a inbound or outbound train will have no effect
' on the above operations unless the outbound sensor S2 or S3 has been
' reached. If so, the turnouts and block power polarity will be set for
' an outbound condition and incorrectly set for a backup operation. A
' train should not be backed up once it is more than half way into a
' siding.
'
' In a power up condition where S1 is active, an inbound operation will
' be assumed and turnouts T1, T2, and T3 will be set appropriately. If
' S2 or S3 is active at power up, an outbound operation will be assumed.
' Sensor S1 will be disabled and turnouts T1, T2, and T3 will be set
' appropriately.
'
' Midway Sidings:
'
'                             B4               S5   T5
'      /------------------------------------------------ B3 -- ~
'     /   /---------------------------------------/
'     \  |                    B5                    TP5
'      \ | S6
'       \|
'   TP6  | T6
'        |
'       B6
'        |
'        ~
'
' The track involved with this section provides a place for mainline
' trains to pass each other. The associated turnouts simulate proto
' typical automatic turnouts that are "spring loaded" to a specific
' position. When entering, the train is always directed to a specific
' track. When exiting, the turnout is moved to to permit train passage
' by the wheels of the train. Once the last car passes through the
' turnout, springs reposition it to the "normal" position.
'
' In operation, a train approching T5 is routed to siding B5. A train
' approaching T6 is routed to siding B4. A train leaving B4 or B5 will
' be detected by sensors S5 or S6 respectively. T5 or T6 will be set to
' direct the train back onto the mainline. A two second retriggerable
' timeout is used to debounce the sensor inputs to prevent a position
' change during train transit of a turnout. Once no additional activity
' is reported by sensor S5 or S6, T5 or T6 is set to its default position
' after a short delay for the last car to clear the turnout.
'
' In a power up condition where sensor S5 or S6 is active, respective
' turnouts T5 or T6 will be set appropriately. Otherwise, T5 or T6 will
' be set to its default position.
'
' Buttons are provided for manually toggling the position of T5 or T6.
' This functionality is used for special train operations involving this
' section of track. Button input is ignored if the respective timeout
' counter is non-zero indicating an inprogress operation.
'
' After manually toggling T5 or T6 to the "non-normal" position, these
' turnouts will automatically reset to their normal position once a
' train transits the turnout. Turnouts T5 or T6 can be "locked" into the
' non-normal position by pressing the appropriate turnout toggle button
' a second time within half a second of the first button depression. The
' turnout will remain in the non-normal position until manually set to
' the normal position to using the respective toggle button. Both T5 and
' T6 cannot be locked at the same time; a derailment would occur. Locking
' either T5 or T6 permits a train to be stopped on one of the sidings for
' an extended period of time and not interfere with mainline traffic
' movements.
'
' Yard Approach Wye:
'
'         ~             ~
'      B7  \    P3     /  B8
'            \       /
'          S8  \   /  S9
'               |/
'           TP7 |  T7
'               |
'               |  S7
'               ~
'
' The track involved with this section provides a "wye" turnout; the legs
' of which are approach tracks leading to opposite ends of the yard tracks.
' This forms a reverse loop that includes all of the yard tracks. Blocks
' B7 and B8 are wired in the same polarity block as the yard tracks. They
' are individual only for the purposes of signaling. All track leading to
' and including the yard tracks upgrade from T7 are on the same polarity
' control relay P3.
'
' Turnout T7 is only partially controlled. The engineer must select the
' desired track for trains approaching from the S7 direction. The T7
' turnout will be set appropriately by this code for trains approaching
' from the S8 or S9 direction.
'
' In operation, a train approaching T7 is detected by sensor S7. The
' current position of T7 is determined by reading the state of TP7. If
' the track corresponding to block B7 will be used, power polarity relay
' P3 will be reset. Otherwise, power polarity relay P3 will be set. This
' will ensure that yard track power polarity matches main line track
' power.
'
' Trains approaching from B7 or B8 will be likewise detected by S8 or
' S9. Turnout T7 will be set appropriately for train passage. Power
' control relay P3 will then be set based upon the current state of TP7.
'
' In all cases, it is not necessary to "ignore" sensor inputs in either
' direction of travel. Detections by S8 or S9 following S7 will not
' change T7 or P3 from their current states. The same is true for S7
' detections following S8 or S9.
'
' Buttons are provided for manually toggling the position of turnout T7.
' This functionality is used for selecting the desired approach track
' to the yard. Button input is ignored if the Wye retriggerable timeout
' counter is non-zero indicating an inprogress operation. Turnout T7
' manual change will be inhibited until one second after the last active
' detection by S7, S8, or S9.
'
' Hardware Description:
'
' Refer to schematics for additional information. This program utilizes
' the Basic Stamp II microcontroller which has 16 I/O bit positions.
' External addressable input multiplexors (74HC151) and an output gate
' array (74154) are used for interfacing the turnouts and sensor/button
' inputs. Two output bit positions are assigned to control each turnout.
' A single bit controls each of the track power polarity relays via a
' 4 bit latch (74LS75 and ULN2803). Each block detector, optical sensor,
' turnout sensor, and control button is assigned an input bit position.
' See constant definitions below.
'
' Turnout operational power requires that a driver curcuit be used
' for each turnout coil. Each turnout control bit is connected to the
' base circuit of a TIP127 5 amp darlington transistors. When a turnout
' is to be positioned, this program pulses the appropriate output bit
' for a nominal 200 ms. This causes the driver circuit to energize the
' turnout coil.
'
' The power polarity relays are driven using a ULN2803 driver. This IC
' contains 8 independent NPN open collector driver circuits capable of
' 500 mA each. These circuits include back EMF protection diodes. Five
' volt relays are used to control the reverse loop track power polarity.
' When energized, each relay draws approximately 200 mA of current.
'
' Each train detection sensor consists of an infared emitter/detector
' pair positioned on each side of the track. A train passing between
' them breaks the infared light beam. The detector side is connected
' to a multiplexor input pin. Under normal conditions, the addressed
' input bit will be low. The input bit will be high whenever the
' infared light beam is blocked.
'
' The block detector circuit measures the power consumption of the
' section of track to which it is connected. In a DCC train control
' system, voltage is always present on the track. Engine movement or
' resistor wheel equipt cars will cause an input bit high state from
' the block detector circuit. When no train is present, the input
' state is low. This type of block detector is used to ensure an
' accurate train presence indication following a power interruption.
'
' The turnout position sensor is a modular infared emitter/detector
' positioned such that an open turnout breaks the light beam. The
' turnout throw bar is used for this. Alternately, a turnout that
' incorporates a position switch can also be used. In the normal or
' 'straight' position, the input state is low. In the 'open' position,
' the input state is high.
'
' An output pin of the Basic Stamp is connected to a speaker and used
' to sound warning tones. Five high tones are used to indicate a train
' wreck warning. Low tones are used to indicate a turnout position
' warning. The number of tones indicates the turnout number.
'
' An output pin of the Basic Stamp is connected to a Led and used to
' indicate operation (heartbeat). Flashing indicates that the code
' is cycling in the main program loop.
'
' An input pin of the Basic Stamp is connected to an on/off switch.
' When on (0), automatic reverse loop control as described herein is
' enabled. When off (1), manual mode is enabled and all automatic
' operations are inhibited. A test/exercise routine is run in manual
' mode involving the cycling of the turnouts and power control relays.
' Changing the switch from manual to automatic setting causes a
' software reset to be performed. All internal working variables are
' set to default conditions.
' ======================================================================
      

Color Bar

Navigation:   Main line   D&B Home   Buczynski.com Index

Copyright © 2006 Don Buczynski
San Diego, California