¿ªÔÆÌåÓý

Locked Trying to write a script to based on LocoSelectorDropBox.py and BackandForthTimed.py to enable a loco to be selected for a back and forth run rather than hard-coded


 

Hi All

I am completely new to python and python but was intrigued by the sample scripts. I am trying to learn how to integrate the 2 scripts above so that the Loco Selector box is called and that address is used in the back-and-forth script.

My logic is that I need to assign the back and forth script to the click event button in the loco selector box, but, I could do with some pointers of where to start...?

Many Thanks

Andrew


 

This is a modified back and forth script that includes opening a window to select a decoder address.


# Based on script 'Back and Forth with mid stop'.
# Listens to sensors, running a locomotive back and
# forth between three towns, changing its direction when at end sensors.
#
# Author: Howard Watkins, January 2007.
# Part of the JMRI distribution
#
# Modified by Peter Ulvestad, June 2009
# Added Mid Station stop
#
# Modified by Peter Ulvestad, June 2017
# Modified for added sensors
#
# Modified by Peter Ulvestad, Jan 2018
# Updated comments
#
# The next line is maintained by CVS, please don't change it
# $Revision: 1.1 $

import jarray
import jmri
import javax.swing

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

# init() is called exactly once at the beginning to do
# any necessary configuration.
def init(self):
self.status.text = "Initiating"

# set up sensor numbers
# fwdSensor is reached when loco is running forward
self.edSensor = sensors.provideSensor("LS1240")
self.edslowSensor = sensors.provideSensor("LS1158")
self.crSensor = sensors.provideSensor("LS1539")
self.fvfwdSensor = sensors.provideSensor("LS1350")
self.fvrevSensor = sensors.provideSensor("LS1349")

# get loco address.
self.status.text = "Getting throttle"
number = int(self.address.text)
if (number > 127) :
long = True
else :
long = False
self.throttle = self.getThrottle(number, long)
if (self.throttle == None) :
print "Couldn't assign throttle!"
return

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


# set loco to forward
self.status.text = "Setting Loco Forward"
print " "
print "Setting Loco Forward"
self.throttle.setIsForward(True)
self.waitMsec(5000)

# turn on headlight and blow horn
self.throttle.setF0(True)
self.waitMsec(1000)
self.throttle.setF1(True)
self.waitMsec(2000)
self.throttle.setF1(False)

# wait 1 second for layout to catch up, then set speed
self.waitMsec(1000)
self.status.text = "Setting Speed"
self.throttle.setSpeedSetting(0.30)
self.status.text = "Southward to Fire Valley"
print " "
print "Southward to Fire Valley"
self.waitMsec(15000)
self.throttle.setSpeedSetting(0.95)
self.waitMsec(15000)
self.throttle.setSpeedSetting(0.70)

# wait for Fire Valley reverse sensor, slow then stop when Fire Valley forward sensor is triggered.
self.waitSensorActive(self.fvrevSensor)
self.status.text = "Slowing for Fire Valley"
print " "
print "Slowing for Fire Valley"
print " "
self.throttle.setSpeedSetting(0.40)
self.waitSensorActive(self.fvfwdSensor)
self.status.text = "Fire Valley"
print "Fire Valley"
self.throttle.setSpeedSetting(0)
self.waitMsec(500)
self.throttle.setSpeedSetting(-1)
self.waitMsec(1000)

# wait for timed Station stop using random timer then set speed
delay = java.util.Random().nextInt(25000) +5000
print(delay)/1000
print "second Station Stop"
self.waitMsec(delay) # wait for 5 - 30 seconds
self.status.text = "Setting Speed"
self.throttle.setSpeedSetting(0.3)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.4)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.5)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.75)

# wait for Edgewood slow sensor in forward direction to trigger, slow, then stop on Edgewood sensor.
self.status.text = "Next Stop - Edgewood"
print " "
print "Next Stop - Edgewood"
self.waitSensorActive(self.edslowSensor)
print " "
self.status.text = "Slowing for Edgewood Station"
print "Slowing for Edgewood Station"
self.throttle.setSpeedSetting(0.6)
self.waitMsec(4000)
self.throttle.setSpeedSetting(0.5)
self.waitMsec(4000)
self.throttle.setSpeedSetting(0.4)
self.waitMsec(4000)
self.throttle.setSpeedSetting(0.3)
self.waitSensorActive(self.edSensor)
self.throttle.setSpeedSetting(0)
self.waitMsec(200)
self.throttle.setSpeedSetting(-1)
self.waitMsec(2000)
# turn off Headlight
self.throttle.setF0(False)
self.throttle.setIsForward(False)

# wait for timed Station stop using random timer then set speed
self.status.text = "Edgewood"
print "Edgewood"
delay = java.util.Random().nextInt(115000) +5000
print(delay)/1000
print "second Station Stop"
self.waitMsec(delay) # wait for 5 - 120 seconds
# turn on Headlight
self.throttle.setF0(True)
self.status.text = "Setting Loco Reverse"
self.throttle.setIsForward(False)
self.waitMsec(1000)
self.throttle.setF0(True)
self.waitMsec(1000)
self.throttle.setF1(True)
self.waitMsec(2000)
self.throttle.setF1(False)
self.waitMsec(1000)
self.status.text = "Setting Speed"
print " "
print "Setting Speed"
self.throttle.setSpeedSetting(0.50)
self.waitMsec(2500)
self.throttle.setSpeedSetting(0.95)
self.waitMsec(10000)
self.throttle.setSpeedSetting(0.75)

# wait for Fire Valley forward sensor to trigger, slow, then stop on Fire Valley reverse sensor.
self.status.text = "Northward to Fire Valley"
print " "
print "Northward to Fire Valley"
self.waitSensorActive(self.fvfwdSensor)
print " "
self.status.text = "Slowing for Fire Valley Station"
print "Slowing for Fire Valley Station"
self.throttle.setSpeedSetting(0.6)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.5)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.4)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.3)
self.waitSensorActive(self.fvrevSensor)
self.status.text = "Fire Valley"
print " "
print "Fire Valley"
self.throttle.setSpeedSetting(0)
self.waitMsec(200)
self.throttle.setSpeedSetting(-1)

# wait for timed Station stop using random timer then set speed
delay = java.util.Random().nextInt(25000) +5000
print(delay)/1000
print "second Station Stop"
self.waitMsec(delay) # wait for 5 - 30 seconds
self.status.text = "Setting Speed"
print " "
print "Setting Speed"
self.throttle.setSpeedSetting(0.3)
self.waitMsec(3000)
self.throttle.setSpeedSetting(0.4)
self.waitMsec(2000)
self.throttle.setSpeedSetting(0.5)
self.waitMsec(2000)
self.throttle.setSpeedSetting(0.75)
self.status.text = "Next Stop - Inonoaklin Creek"
print " "
print "Next Stop - Inonoaklin Creek"

# wait for Inonoaklin Creek to trigger
self.waitSensorActive(self.crSensor)
self.waitMsec(2000)
self.throttle.setSpeedSetting(0)
self.waitMsec(1000)
self.throttle.setSpeedSetting(-1)
self.waitMsec(1000)
self.throttle.setIsForward(True)
self.waitMsec(200)
# turn off Headlight
self.throttle.setF0(False)
self.waitMsec(1000)

# wait for timed Station stop using random timer then set speed
self.status.text = "Inonoaklin Creek"
print " "
print "Inonoaklin Creek"
delay = java.util.Random().nextInt(115000) +5000
print(delay)/1000
print "second Station Stop"
self.waitMsec(delay) # wait for 5 - 120 seconds

# and continue around again
print " "
return 1
# To stop script, either kill the script in using the 'Thread Monitor'
# or terminate JMRI.
# Caution doing either so could leave loco running if not careful

# Create Pane to enter Locomotive number

# define what button does when clicked and attach that routine to the button
def whenMyButtonClicked(self,event) :
self.start()
# we leave the button off
self.startButton.enabled = False

return

# routine to show the panel, starting the whole process
def setup(self):
# create a frame to hold the button, set up for nice layout
f = jmri.util.JmriJFrame("Lower Level Automation") # argument is the frames title
f.contentPane.setLayout(javax.swing.BoxLayout(f.contentPane, javax.swing.BoxLayout.Y_AXIS))

# create the text field
self.address = javax.swing.JTextField(5) # sized to hold 5 characters, initially empty

# put the text field on a line preceded by a label
temppanel1 = javax.swing.JPanel()
temppanel1.add(javax.swing.JLabel("Address"))
temppanel1.add(self.address)

# create the button
self.startButton = javax.swing.JButton("Start")
self.startButton.actionPerformed = self.whenMyButtonClicked

self.status = javax.swing.JLabel("Enter address & click start")

# Put contents in frame and display
f.contentPane.add(temppanel1)
temppanel2 = javax.swing.JPanel()
temppanel2.add(self.startButton)
f.contentPane.add(temppanel2)
f.contentPane.add(self.status)
f.pack()
f.show()

return 1

# create one of these
a = FireValleyAutoRun()

# set the name, as a example of configuring it
a.setName("FireValleyAutoRun")

# set the time between settings
# a.delay = 3

# and show the initial panel
a.setup()



--
Peter Ulvestad

JMRI Users Group Moderator - ( )
Tam Valley Group Moderator - ( )
Sprog-DCC Group Moderator - ( )
Edmonton Model Railroad Association -


 

Thank you very much, Peter. I will not only use the script but also ?try to learn from it. I really appreciate it. Andrew


 

Hi, so I got around to trying this having first tried to work out what it was doing. The piece that I could not understand was how the throttle selection entered in the box gets passed to the throttle. I commented out sensors to start with as I haven't set them up yet and just wanted to understand what is happening. My IDE only deals with Python 3.6 so I updated the print statements to functions.

I then added additional self.status.text display lines and print comments to help me debug. My code is below. However, I cannot get past the selection box. I enter the number and press start and nothing else happens - I get my print messages in the script output box (code highlighted in BOLD but nothing else happens). Any help appreciated!

# Based on script 'Back and Forth with mid stop'.
# Listens to sensors, running a locomotive back and
# forth between three towns, changing its direction when at end sensors.
#
# Author: ?Howard Watkins, January 2007.
# Part of the JMRI distribution
#
# Modified by Peter Ulvestad, June 2009
# Added Mid Station stop
#
# Modified by Peter Ulvestad, June 2017
# Modified for added sensors
#
# Modified by Peter Ulvestad, Jan 2018
# Updated comments
#
# The next line is maintained by CVS, please don't change it
# $Revision: 1.1 $
?
import jarray
import jmri
import javax.swing
?
class FireValleyAutoRun(jmri.jmrit.automat.AbstractAutomaton) :
?
# init() is called exactly once at the beginning to do
# any necessary configuration.
? ? def init(self):
? ? ? ? self.status.text = "Initiating"
?
? ? ? ? # set up sensor numbers
? ? ? ? # fwdSensor is reached when loco is running forward
?## ? ? ? self.edSensor = sensors.provideSensor("LS1240")
?## ? ? ? self.edslowSensor = sensors.provideSensor("LS1158")
?## ? ? ? self.crSensor = sensors.provideSensor("LS1539")
?## ? ? ? self.fvfwdSensor = sensors.provideSensor("LS1350")
?## ? ? ? self.fvrevSensor = sensors.provideSensor("LS1349")
?
? ? ? ? # get loco address.
? ? ? ? print ("getting throttle")
? ? ? ? self.status.text = "Getting throttle"
? ? ? ? number = int(self.address.text)
? ? ? ? print (number)
? ? ? ? self.status.text = number
? ? ? ? if (number > 127) :
? ? ? ? ? ? long = True
? ? ? ? else :
? ? ? ? ? ? long = False
? ? ? ? ? ? self.throttle = self.getThrottle(number, long)
? ? ? ? if (self.throttle == None) :
? ? ? ? ? ? print ("Couldn't assign throttle!")
? ? ? ? ? ? self.status.text = "no throttle"
? ? ? ? return
?
? ? def handle(self):
? ? ? ? # handle() is called repeatedly until it returns false.
? ? ? ? print ("Inside handle(self)")
?
?
? ? ? ? # set loco to forward
? ? ? ? self.status.text = "Setting Loco Forward"
? ? ? ? print (" ")
? ? ? ? print ("Setting Loco Forward")
? ? ? ? self.throttle.setIsForward(True)
? ? ? ? self.waitMsec(5000)
?
? ? ? ? # turn on headlight and blow horn
? ? ? ? self.throttle.setF0(True)
? ? ? ? self.waitMsec(1000)
? ? ? ? self.throttle.setF1(True)
? ? ? ? self.waitMsec(2000)
? ? ? ? self.throttle.setF1(False)
?
? ? ? ? # wait 1 second for layout to catch up, then set speed
? ? ? ? self.waitMsec(1000)
? ? ? ? self.status.text = "Setting Speed"
? ? ? ? self.throttle.setSpeedSetting(0.30)
? ? ? ? self.status.text = "Southward to Fire Valley"
? ? ? ? print (" ")
? ? ? ? print ("Southward to Fire Valley")
? ? ? ? self.waitMsec(15000)
? ? ? ? self.throttle.setSpeedSetting(0.95)
? ? ? ? self.waitMsec(15000)
? ? ? ? self.throttle.setSpeedSetting(0.70)
?
? ? ? ? # wait for Fire Valley reverse sensor, slow then stop when Fire Valley forward sensor is triggered.
? ## ? ? ?self.waitSensorActive(self.fvrevSensor)
? ? ? ? self.status.text = "Slowing for Fire Valley"
? ? ? ? print (" ")
? ? ? ? print ("Slowing for Fire Valley")
? ? ? ? print (" ")
? ? ? ? self.throttle.setSpeedSetting(0.40)
? ## ? ? ?self.waitSensorActive(self.fvfwdSensor)
? ? ? ? self.status.text = "Fire Valley"
? ? ? ? print ( "Fire Valley")
? ? ? ? self.throttle.setSpeedSetting(0)
? ? ? ? self.waitMsec(500)
? ? ? ? self.throttle.setSpeedSetting(-1)
? ? ? ? self.waitMsec(1000)
?
? ? ? ? # wait for timed Station stop using random timer then set speed
? ? ? ? delay = java.util.Random().nextInt(25000) +5000
? ? ? ? print(delay)/1000
? ? ? ? print ("second Station Stop")
? ? ? ? self.waitMsec(delay) # wait for 5 - 30 seconds
? ? ? ? self.status.text = "Setting Speed"
? ? ? ? self.throttle.setSpeedSetting(0.3)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.4)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.5)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.75)
?
? ? ? ? # wait for Edgewood slow sensor in forward direction to trigger, slow, then stop on Edgewood sensor.
? ? ? ? self.status.text = "Next Stop - Edgewood"
? ? ? ? print (" ")
? ? ? ? print ("Next Stop - Edgewood")
?## ? ? ? self.waitSensorActive(self.edslowSensor)
? ? ? ? print (" ")
? ? ? ? self.status.text = "Slowing for Edgewood Station"
? ? ? ? print ("Slowing for Edgewood Station")
? ? ? ? self.throttle.setSpeedSetting(0.6)
? ? ? ? self.waitMsec(4000)
? ? ? ? self.throttle.setSpeedSetting(0.5)
? ? ? ? self.waitMsec(4000)
? ? ? ? self.throttle.setSpeedSetting(0.4)
? ? ? ? self.waitMsec(4000)
? ? ? ? self.throttle.setSpeedSetting(0.3)
? ? ? ? self.waitSensorActive(self.edSensor)
? ? ? ? self.throttle.setSpeedSetting(0)
? ? ? ? self.waitMsec(200)
? ? ? ? self.throttle.setSpeedSetting(-1)
? ? ? ? self.waitMsec(2000)
? ? ? ? # turn off Headlight
? ? ? ? self.throttle.setF0(False)
? ? ? ? self.throttle.setIsForward(False)
?
? ? ? ? # wait for timed Station stop using random timer then set speed
? ? ? ? self.status.text = "Edgewood"
? ? ? ? print ("Edgewood")
? ? ? ? delay = java.util.Random().nextInt(115000) +5000
? ? ? ? print(delay)/1000
? ? ? ? print ("second Station Stop")
? ? ? ? self.waitMsec(delay) # wait for 5 - 120 seconds
? ? ? ? # turn on Headlight
? ? ? ? self.throttle.setF0(True)
? ? ? ? self.status.text = "Setting Loco Reverse"
? ? ? ? self.throttle.setIsForward(False)
? ? ? ? self.waitMsec(1000)
? ? ? ? self.throttle.setF0(True)
? ? ? ? self.waitMsec(1000)
? ? ? ? self.throttle.setF1(True)
? ? ? ? self.waitMsec(2000)
? ? ? ? self.throttle.setF1(False)
? ? ? ? self.waitMsec(1000)
? ? ? ? self.status.text = "Setting Speed"
? ? ? ? print (" ")
? ? ? ? print ( "Setting Speed")
? ? ? ? self.throttle.setSpeedSetting(0.50)
? ? ? ? self.waitMsec(2500)
? ? ? ? self.throttle.setSpeedSetting(0.95)
? ? ? ? self.waitMsec(10000)
? ? ? ? self.throttle.setSpeedSetting(0.75)
?
? ? ? ? # wait for Fire Valley forward sensor to trigger, slow, then stop on Fire Valley reverse sensor.
? ? ? ? self.status.text = "Northward to Fire Valley"
? ? ? ? print (" ")
? ? ? ? print ( "Northward to Fire Valley")
? ## ? ? ?self.waitSensorActive(self.fvfwdSensor)
? ? ? ? print (" ")
? ? ? ? self.status.text = "Slowing for Fire Valley Station"
? ? ? ? print ("Slowing for Fire Valley Station")
? ? ? ? self.throttle.setSpeedSetting(0.6)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.5)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.4)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.3)
? ## ? ? ?self.waitSensorActive(self.fvrevSensor)
? ? ? ? self.status.text = "Fire Valley"
? ? ? ? print (" ")
? ? ? ? print ( "Fire Valley")
? ? ? ? self.throttle.setSpeedSetting(0)
? ? ? ? self.waitMsec(200)
? ? ? ? self.throttle.setSpeedSetting(-1)
?
? ? ? ? # wait for timed Station stop using random timer then set speed
? ? ? ? delay = java.util.Random().nextInt(25000) +5000
? ? ? ? print(delay)/1000
? ? ? ? print ("second Station Stop")
? ? ? ? self.waitMsec(delay) # wait for 5 - 30 seconds
? ? ? ? self.status.text = "Setting Speed"
? ? ? ? print (" ")
? ? ? ? print ( "Setting Speed")
? ? ? ? self.throttle.setSpeedSetting(0.3)
? ? ? ? self.waitMsec(3000)
? ? ? ? self.throttle.setSpeedSetting(0.4)
? ? ? ? self.waitMsec(2000)
? ? ? ? self.throttle.setSpeedSetting(0.5)
? ? ? ? self.waitMsec(2000)
? ? ? ? self.throttle.setSpeedSetting(0.75)
? ? ? ? self.status.text = "Next Stop - Inonoaklin Creek"
? ? ? ? print (" ")
? ? ? ? print ("Next Stop - Inonoaklin Creek")
?
? ? ? ? # wait for Inonoaklin Creek to trigger
? ## ? ? ?self.waitSensorActive(self.crSensor)
? ? ? ? self.waitMsec(2000)
? ? ? ? self.throttle.setSpeedSetting(0)
? ? ? ? self.waitMsec(1000)
? ? ? ? self.throttle.setSpeedSetting(-1)
? ? ? ? self.waitMsec(1000)
? ? ? ? self.throttle.setIsForward(True)
? ? ? ? self.waitMsec(200)
? ? ? ? # turn off Headlight
? ? ? ? self.throttle.setF0(False)
? ? ? ? self.waitMsec(1000)
?
? ? ? ? # wait for timed Station stop using random timer then set speed
? ? ? ? self.status.text = "Inonoaklin Creek"
? ? ? ? print (" ")
? ? ? ? print ("Inonoaklin Creek")
? ? ? ? delay = java.util.Random().nextInt(115000) +5000
? ? ? ? print(delay)/1000
? ? ? ? print ("second Station Stop")
? ? ? ? self.waitMsec(delay) ? ? ? ?# wait for 5 - 120 seconds
?
? ? ? ? # and continue around again
? ? ? ? print (" ")
? ? ? ? return 1
? ? ? ? # To stop script, either kill the script in using the 'Thread Monitor'
? ? ? ? # or terminate JMRI.
? ? ? ? # Caution doing either so could leave loco running if not careful
?
? ? ? ? # Create Pane to enter Locomotive number
?
? ? ? ? # define what button does when clicked and attach that routine to the button
? ? def whenMyButtonClicked(self,event) :
? ? ? ? self.start()
? ? ? ? # we leave the button off
? ? ? ? self.startButton.enabled = False
?
? ? ? ? return
?
? ? ? ? # routine to show the panel, starting the whole process ? ??
? ? def setup(self):
? ? ? ? # create a frame to hold the button, set up for nice layout
? ? ? ? f = jmri.util.JmriJFrame("Lower Level Automation") ? ? ? # argument is the frames title
? ? ? ? f.contentPane.setLayout(javax.swing.BoxLayout(f.contentPane, javax.swing.BoxLayout.Y_AXIS))
?
? ? ? ? # create the text field
? ? ? ? self.address = javax.swing.JTextField(5) ? ?# sized to hold 5 characters, initially empty
?
? ? ? ? # put the text field on a line preceded by a label
? ? ? ? temppanel1 = javax.swing.JPanel()
? ? ? ? temppanel1.add(javax.swing.JLabel("Address"))
? ? ? ? temppanel1.add(self.address)
?
? ? ? ? # create the button
? ? ? ? self.startButton = javax.swing.JButton("Start")
? ? ? ? self.startButton.actionPerformed = self.whenMyButtonClicked
?
? ? ? ? self.status = javax.swing.JLabel("Enter address & click start")
?
? ? ? ? # Put contents in frame and display
? ? ? ? f.contentPane.add(temppanel1)
? ? ? ? temppanel2 = javax.swing.JPanel()
? ? ? ? temppanel2.add(self.startButton)
? ? ? ? f.contentPane.add(temppanel2)
? ? ? ? f.contentPane.add(self.status)
? ? ? ? f.pack()
? ? ? ? f.show()
?
? ? ? ? return 1
?
# create one of these
a = FireValleyAutoRun()
?
# set the name, as a example of configuring it
a.setName("FireValleyAutoRun")
?
# set the time between settings
# a.delay = 3
?
# and show the initial panel
a.setup()


 

A little update. I have now got this working (to a degree!). I rebooted and seems to have cleared some things. It seemed it also didn't like the delay functions. Replacing those with a straight number, and changing the print (delay)/1000 to print (delay/1000) has got ?it working, so I am learning!