-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMRBusThrottle.py
140 lines (116 loc) · 5.02 KB
/
MRBusThrottle.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# *************************************************************************
# Title: Client driver for MRBus Throttle (mainly the ProtoThrottle)
# Authors: Michael D. Petersen <[email protected]>
# Nathan D. Holmes <[email protected]>
# File: MRBusThrottle.py
# License: GNU General Public License v3
#
# LICENSE:
# Copyright (C) 2018 Michael Petersen & Nathan Holmes
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# DESCRIPTION:
# This class provides a way parse incoming MRBus throttle packets (primarily
# from the ProtoThrottle ( http://www.protothrottle.com/ ) and send them
# on to a variety of command stations as a form of protocol translator.
#
# *************************************************************************
import mrbus
import sys
import time
class MRBusThrottle:
def __init__(self, addr):
self.locAddr = 0
self.locAddrLong = True
self.locSpeed = 0
self.locDirection = 0
self.locObjID = 0
self.locEStop = 0
self.locFunctions = None
self.throttleAddr = addr
self.lastUpdate = 0
return
def getLastUpdateTime(self):
return self.lastUpdate
def disconnect(self, cmdStn):
cmdStn.locomotiveSpeedSet(self.locObjID, 0, 0)
cmdStn.locomotiveDisconnect(self.locObjID)
def update(self, cmdStn, pkt):
if pkt.cmd != 0x53 or len(pkt.data) != 9: # Not a status update, bump out
return
# print "MRBusThrottle (0x%02X): UPDATE loco %d" % (self.throttleAddr, self.locAddr)
addr = pkt.data[0] * 256 + pkt.data[1]
if 0 != (addr & 0x8000):
self.locAddrLong = False
addr = addr & 0x007F
else:
self.locAddrLong = True
speed = pkt.data[2] & 0x7F
if 1 == speed:
speed = 0
estop = 1
elif speed > 1:
estop = 0
speed = speed - 1
elif speed == 0:
estop = 0
if pkt.data[2] & 0x80:
direction = 0
else:
direction = 1
if (addr != self.locAddr):
self.locAddr = addr
self.locObjID = cmdStn.locomotiveObjectGet(self.locAddr, self.throttleAddr, self.locAddrLong)
print("MRBusThrottle (0x%02X): Acquiring new locomotive %d - objID = %s" % (self.throttleAddr, self.locAddr, self.locObjID))
# Only send ESTOP if we just moved into that state
if estop != self.locEStop and estop == 1:
print("MRBusThrottle (0x%02X): Set ESTOP loco %d" % (self.throttleAddr, self.locAddr))
cmdStn.locomotiveEmergencyStop(self.locObjID)
self.locEStop = estop
if self.locEStop != 1 and (speed != self.locSpeed or direction != self.locDirection):
print("MRBusThrottle (0x%02X): Set loco [%d] speed %d %s" % (self.throttleAddr, self.locAddr, speed, ["FWD","REV"][direction]))
cmdStn.locomotiveSpeedSet(self.locObjID, speed, direction)
self.locSpeed = speed
self.locDirection = direction
# On the first pass, get the function statuses from the command station
# The LNWI / WiThrottle support this, others may in the future
if self.locFunctions is None:
try:
self.locFunctions = cmdStn.locomotiveFunctionsGet(self.locObjID)
print("MRBusThrottle (0x%02X): Got loco [%d] functions from cmd station" % (self.throttleAddr, self.locAddr))
print(self.locFunctions)
except Exception as e:
print("MRBusThrottle (0x%02X): Exception in locomotiveFunctionsGet() for loco [%d]" % (self.throttleAddr, self.locAddr))
self.locFunctions = [ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ]
functions = [ 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0 ]
for i in range(29):
if i >= 0 and i < 8:
if pkt.data[6] & (1<<i):
functions[i] = 1
elif i >= 8 and i < 16:
if pkt.data[5] & (1<<(i-8)):
functions[i] = 1
elif i >= 16 and i < 24:
if pkt.data[4] & (1<<(i-16)):
functions[i] = 1
elif i >= 24 and i < 29:
if pkt.data[3] & (1<<(i-24)):
functions[i] = 1
for i in range(29):
if functions[i] != self.locFunctions[i]:
print("MRBusThrottle (0x%02X): Set loco [%d] function [%d] to [%d]" % (self.throttleAddr, self.locAddr, i, functions[i]))
cmdStn.locomotiveFunctionSet(self.locObjID, i, functions[i])
self.locFunctions = functions
self.lastUpdate = time.time()
return