Sensor shuttle

[code language=”Java”]
# This is an example script for a JMRI "Automat" in Python
# It is based on the AutomatonExample.
#
# It listens to two sensors, running a locomotive back and
# forth between them by changing its direction when a sensor
# detects the engine.
#
# Author: Howard Watkins, January 2007.
# Part of the JMRI distribution
#
# Modified: October 2014
# Modified by: Chris Morgan
#
# The next line is maintained by CVS, please don’t change it
# $Revision: 17977 $

import jarray
import jmri

class Test14(jmri.jmrit.automat.AbstractAutomaton) :

def init(self):
# init() is called exactly once at the beginning to do
# any necessary configuration.
print "Inside init(self)"

# set up sensor numbers
# fwdSensor is reached when loco is running forward
self.fwdSensor = sensors.provideSensor("quayside")
self.revSensor = sensors.provideSensor("mainline")

# get loco address. For long address change "False" to "True"
self.throttle = self.getThrottle(101, True) # long address 101

return

def handle(self):
# handle() is called repeatedly until it returns false.
print "Inside handle(self)"

# set approach points
self.waitMsec(1000) # time is in milliseconds
# just for the hell of it toggle the approach points
turnouts.provideTurnout("approachpointsop").setState(CLOSED)
self.waitMsec(3000)
turnouts.provideTurnout("approachpointsop").setState(THROWN)
self.waitMsec(2000)

# set loco to forward
print "Set Loco Forward"
self.throttle.setIsForward(True)

# wait 1 second for layout to catch up, then set speed
self.waitMsec(1000)
print "Set Speed"
self.throttle.setSpeedSetting(0.7)

# wait for sensor in forward direction to trigger, then stop
print "Wait for Forward Sensor"
self.waitSensorActive(self.fwdSensor)
print "Set Speed Stop"
self.waitMsec(1500)
self.throttle.setSpeedSetting(0)

# delay for a time (remember loco could still be moving
# due to simulated or actual inertia). Time is in milliseconds
print "wait 20 seconds"
self.waitMsec(20000) # wait for 20 seconds

# turn on whistle, set direction to reverse, set speed
self.throttle.setF3(True) # turn on whistle
self.waitMsec(1000) # wait for 1 seconds
self.throttle.setF3(False) # turn off whistle
self.waitMsec(1000) # wait for 1 second

print "Set Loco Reverse"
self.throttle.setIsForward(False)
self.waitMsec(1000) # wait 1 second for Xpressnet to catch up
print "Set Speed"
self.throttle.setSpeedSetting(0.4)

# wait for sensor in reverse direction to trigger
print "Wait for Reverse Sensor"
self.waitSensorActive(self.revSensor)
print "Set Speed Stop"
self.waitMsec(1500)
self.throttle.setSpeedSetting(0)

# delay for a time (remember loco could still be moving
# due to simulated or actual inertia). Time is in milliseconds
print "wait 20 seconds"
self.waitMsec(20000) # wait for 20 seconds

# turn on whistle, set direction to forward, set speed
self.throttle.setF3(True) # turn on whistle
self.waitMsec(1000) # wait for 1 seconds
self.throttle.setF3(False) # turn off whistle
self.waitMsec(1000) # wait for 1 second

# and continue around again
print "End of Loop"
return 1
# (requires JMRI to be terminated to stop – caution
# doing so could leave loco running if not careful)

# end of class definition

# start one of these up
Test14().start()
[/code]