[code language=”Java”]
# This is an example script for a JMRI "Automat" in Python
# It is based on the AutomatonExample.
#
# It runs a locomotive back and forth using time delays.
#
# Times are in milliseconds
#
# Author: Bob Jacobsen, July 2008
# Based on BackAndForth.py
# Author: Howard Watkins, January 2007
# Part of the JMRI distribution
# Modified by: Chris Morgan
# Modified: October 2014
#
# The next line is maintained by CVS, please don’t change it
# $Revision: 17977 $
import jarray
import jmri
class BackAndForthTimed(jmri.jmrit.automat.AbstractAutomaton) :
def init(self):
# init() is called exactly once at the beginning to do
# any necessary configuration.
print "Inside init(self)"
# get loco address. For long address change "False" to "True"
self.throttle = self.getThrottle(101, True) # long address 101
self.throttle.setF1(True) # make sure the sound is on
self.throttle.setF3(True) # turn on short whistle
self.waitMsec(1000) # wait for 1 second
self.throttle.setF3(False) # turn off short whistle
self.waitMsec(5000) # wait for 5 seconds
return
def handle(self):
# handle() is called repeatedly until it returns false.
print "Inside handle(self)"
print "Sound whistle"
self.throttle.setF2(True) # turn on long whistle
self.waitMsec(2000) # wait for 2 seconds
self.throttle.setF2(False) # turn off long whistle
self.waitMsec(1000) # wait for 1 second
self.throttle.setF3(True) # turn on short whistle
self.waitMsec(1000) # wait for 1 second
self.throttle.setF3(False) # turn off short whistle
print "Set Loco Forward"
# set loco to forward
self.throttle.setIsForward(True)
# wait 1 second for engine to be stopped, then set speed
self.waitMsec(1000)
print "Set Speed 0.5"
self.throttle.setSpeedSetting(0.5)
print "Run forward for 15 seconds"
self.throttle.setF7(True)
self.waitMsec(6000)
self.throttle.setF7(False)
# wait for run time in forward direction
self.waitMsec(9000)
# stop the engine
print "Stopping"
self.throttle.setSpeedSetting(0)
# delay for a time (remember loco could still be moving
# due to simulated or actual inertia).
print "Waiting 10 seconds to come to halt"
self.waitMsec(10000)
print "Sound whistle"
self.throttle.setF2(True) # turn on long whistle
self.waitMsec(2000) # wait for 2 seconds
self.throttle.setF2(False) # turn off long whistle
self.waitMsec(1000) # wait for 1 second
self.throttle.setF3(True) # turn on short whistle
self.waitMsec(1000) # wait for 1 second
self.throttle.setF3(False) # turn off short whistle
# set direction to reverse, set speed
print "Set Loco back"
self.throttle.setIsForward(False)
self.waitMsec(1000) # wait 1 second for Xpressnet to catch up
print "Set Speed 0.5"
self.throttle.setSpeedSetting(0.3)
# wait for run time in reverse direction
print "Run back for 25 seconds"
self.throttle.setF7(True)
self.waitMsec(6000)
self.throttle.setF7(False)
self.waitMsec(18000)
print "Stopping"
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 7 seconds to come to a halt"
self.waitMsec(7000)
# 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
BackAndForthTimed().start()
[/code]