-
Notifications
You must be signed in to change notification settings - Fork 0
/
JMRI-back-and-forth.py
executable file
·78 lines (61 loc) · 2.26 KB
/
JMRI-back-and-forth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# This is an example script for a JMRI "Automat" in Python
# It is based on the AutomatonExample.
#
# It runs a loco back and forth on a timer forever
#
# Author: Adam Knowles 2023 based on Howard Watkins, January 2007.
import jarray # for java array # type: ignore
import jmri # for sensors, turnouts, etc # type: ignore
# Configure train
GREEN_LOCO_ID = 9744
LONG_ADDRESS = True
RUN_FOR = 5 * 1000 # seconds
WAIT_FOR = 1 * 1000 # seconds
SPEED_SETTING = 0.7
class Test(jmri.jmrit.automat.AbstractAutomaton):
def __init__(self, locoAddress):
print("init(self)")
# super().__init__()
self.locoAddress = locoAddress
# get loco address. For long address change "False" to "True"
self.throttle = self.getThrottle(self.locoAddress, LONG_ADDRESS)
return
def handle(self):
# handle() is called repeatedly until it returns false.
print("handle(self)")
# set loco to forward
print("Set Loco Forward")
self.throttle.setIsForward(True)
# wait for layout to catch up
print("Wait for {} seconds".format(WAIT_FOR / 1000))
self.waitMsec(WAIT_FOR)
# set speed
print("Set speed to {}".format(SPEED_SETTING))
self.throttle.setSpeedSetting(SPEED_SETTING)
# Run for a moment, then stop
print("Run for {} seconds".format(RUN_FOR / 1000))
self.waitMsec(RUN_FOR)
print("Set speed: Stop")
self.throttle.setSpeedSetting(0)
# set direction to reverse, set speed
print("Set Loco Reverse")
self.throttle.setIsForward(False)
# wait for layout to catch up
print("Wait for {} seconds".format(WAIT_FOR / 1000))
self.waitMsec(WAIT_FOR)
# set speed
print("Set speed to {}".format(SPEED_SETTING))
self.throttle.setSpeedSetting(SPEED_SETTING)
# wait, stop, wait
print("Run for {} seconds".format(RUN_FOR / 1000))
self.waitMsec(RUN_FOR)
print("Set speed: Stop")
self.throttle.setSpeedSetting(0)
print("Wait for {} seconds".format(WAIT_FOR / 1000))
self.waitMsec(WAIT_FOR)
# Loop
print("End of Loop")
return 1
# end of class definition
# start one of these up
Test(GREEN_LOCO_ID).start()