/
traffic.py
345 lines (293 loc) · 10.5 KB
/
traffic.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
import argparse
import random
from chan import Chan, chanselect, Timeout, ChanClosed
from nanomsg import Socket, BUS
from SimpleXMLRPCServer import SimpleXMLRPCServer
import time
import threading
import lights
BRAIN_PORT = 55755
SERVER_PORT = 8085
DEFAULT_PERIOD = 12
DEFAULT_YELLOW = 2.5
CMD_PING = 'PING'
CMD_LIGHTS = 'LIGHTS'
class DiscoTimer(object):
def __init__(self, base=1000, jitter=500):
self.trip = False
self._stop = threading.Event()
self.chan = Chan()
self._thread = threading.Thread(name='LightTimer', target=self.run)
self._thread.daemon = True
self._thread.start()
self.base = base
self.jitter = jitter
random.seed()
def run(self):
while not self._stop.is_set():
random_lights = []
for l in lights.ALL:
maybe = random.randint(0, 1)
if maybe == 1:
random_lights.append(l)
self.chan.put(random_lights)
d = random.randint(0, 1)
ti = self.base
ji = random.randint(0, self.jitter)
if d == 0:
ti = ti + ji
else:
ti = ti - ji
time.sleep(ti / 1000)
def stop(self):
self._stop.set()
class SolidTimer(object):
def __init__(self, color, blink=False, period=0.5):
self.blink = blink
self.period = period
self.color = color.split('-')[0]
self.chan = Chan()
self._stop = threading.Event()
self._thread = threading.Thread(name='LightTimer', target=self.run)
self._thread.daemon = True
self._thread.start()
def run(self):
STATE = []
if self.color == 'red':
STATE = [lights.RED1, lights.RED2]
elif self.color == 'yellow':
STATE = [lights.YLW1, lights.YLW2]
elif self.color == 'green':
STATE = [lights.GRN1, lights.GRN2]
on = True
while not self._stop.is_set():
if on:
if self.blink:
self.chan.put([STATE[0]])
on = False
time.sleep(self.period)
else:
self.chan.put(STATE)
time.sleep(5)
else:
self.chan.put([STATE[1]])
on = True
time.sleep(self.period)
self.chan.close()
def stop(self):
self._stop.set()
class LightTimer(object):
def __init__(self, period=DEFAULT_PERIOD, yellow=DEFAULT_YELLOW):
self.period = period
self.yellow = yellow
self.trip = False
self._stop = threading.Event()
self.chan = Chan()
self._thread = threading.Thread(name='LightTimer', target=self.run)
self._thread.daemon = True
self._thread.start()
def run(self):
STATES = [
[lights.RED1, lights.GRN2],
[lights.RED1, lights.YLW2],
[lights.GRN1, lights.RED2],
[lights.YLW1, lights.RED2],
]
state_i = len(STATES) - 1
t_last = 0
while not self._stop.is_set():
t_yellow = min(0.25 * self.period, DEFAULT_YELLOW)
t_green = self.period - t_yellow
dur = t_green if state_i % 2 == 0 else t_yellow
now = time.time()
if now > t_last + dur or self.trip:
t_last = now
state_i = (state_i + 1) % len(STATES)
self.chan.put(STATES[state_i])
self.trip = False
time.sleep(0.1)
self.chan.close()
def stop(self):
self._stop.set()
class Server(object):
def __init__(self, args):
self.chan = Chan()
self._server = SimpleXMLRPCServer(
('0.0.0.0', SERVER_PORT),
logRequests=False,
allow_none=True)
self.mode = args.mode
self._server.register_introspection_functions()
self._server.register_instance(self)
self._stop = threading.Event()
self.changed = 0
if args.controls:
self._thread = threading.Thread(name='Server', target=self.run)
else:
self._thread = threading.Thread(name='Server', target=self._server.serve_forever)
self._thread.daemon = True
self._thread.start()
def run(self):
while not self._stop.is_set():
mode = read_mode(self.mode)
now = int(round(time.time() * 1000))
if mode != self.mode and (now - self.changed > 250):
self.mode = mode
self.chan.put(('mode', self.mode), timeout=2.0)
self.changed = now
time.sleep(0.1)
def stop(self):
self._stop.set()
def status(self):
st = {'server': 'ok'}
try:
resp_chan = Chan(1)
self.chan.put(('status', resp_chan), timeout=0.5)
resp = resp_chan.get(timeout=0.5)
st.update(resp)
except Timeout:
st['loop'] = 'Error: Did not hear from main thread in time'
return st
def set_period(self, period):
self.chan.put(('set_period', period), timeout=2.0)
return True
def trip(self):
self.chan.put(('trip',), timeout=2.0)
return True
def set_mode(mode, existing=None):
if existing:
existing.stop()
if mode == 'cycle':
return LightTimer()
elif mode == 'red' or mode == 'green' or mode == 'yellow':
return SolidTimer(mode)
elif mode == 'red-blink' or mode == 'green-blink' or mode == 'yellow-blink':
return SolidTimer(mode, True)
elif mode == 'disco':
return DiscoTimer()
def solid_color(mode, button):
if mode == 'disco' or mode == 'cycle' or mode == 'red-blink' or mode == 'yellow-blink' or mode == 'green-blink':
return 'red'
elif mode == 'red' and button:
return 'green'
elif mode == 'green' and button:
return 'yellow'
elif mode == 'yellow' and button:
return 'red'
else:
return mode
def blink_color(mode, button):
if mode == 'disco' or mode == 'cycle' or mode == 'red' or mode == 'yellow' or mode == 'green':
return 'red-blink'
elif mode == 'red-blink' and button:
return 'green-blink'
elif mode == 'green-blink' and button:
return 'yellow-blink'
elif mode == 'yellow-blink' and button:
return 'red-blink'
else:
return mode
def read_mode(mode):
disco_switch = lights.input(lights.DISCO_SWT)
solid_switch = lights.input(lights.SOLID_SWT)
cycle_switch = lights.input(lights.CYCLE_SWT)
color_button = lights.input(lights.COLOR_BTN)
# print("%d%d%d%d" % (disco_switch, solid_switch, cycle_switch, color_button))
if disco_switch and mode != 'disco':
return "disco"
elif solid_switch and not disco_switch:
return solid_color(mode, color_button)
elif (not solid_switch and not disco_switch and not cycle_switch):
return blink_color(mode, color_button)
elif cycle_switch and not disco_switch and mode != 'cycle':
return "cycle"
return mode
def brain_loop(args):
sock_node = Socket(BUS)
sock_node.bind('tcp://*:%s' % BRAIN_PORT)
sock_node.recv_timeout = 250
#sock_node.send_buffer_size = 1000
sock_node.send_timeout = 200
seq = 0
timer = set_mode(args.mode)
server = Server(args)
while True:
ch, value = chanselect([timer.chan, server.chan], [])
#print 'AFTER chanselect', ch is timer.chan, time.time()
if ch is timer.chan:
lights.only(*value)
#print "LIGHTS", value
try:
seq += 1
sock_node.send('%i %s %s' % (
seq,
CMD_LIGHTS,
' '.join(lights.rev_lookup[led_pin] for led_pin in value)))
except Exception as ex:
#print ex
pass
elif ch is server.chan:
if value[0] == 'status':
node = None
try:
seq += 1
sock_node.send('%i PING' % seq)
while True:
node_msg = sock_node.recv().split()
if int(node_msg[0]) == seq:
node = 'ok'
break
elif int(node_msg[0]) > seq:
raise Exception('Skipped ping message')
except Exception as ex:
node = repr(ex)
value[1].put({'loop': 'ok', 'node': node})
elif value[0] == 'set_period':
timer.period = value[1]
elif value[0] == 'trip':
timer.trip = True
elif value[0] == 'mode':
mode = value[1]
print("new mode tho %s" % mode)
timer = set_mode(mode)
else:
print "UNKNOWN COMMAND:", value
time.sleep(2)
def node_loop(brain):
sock_brain = Socket(BUS)
sock_brain.connect('tcp://%s:%s' % (brain, BRAIN_PORT))
while True:
msg = sock_brain.recv()
#print 'HEARD', msg
bits = msg.split()
msgid = bits[0]
cmd = bits[1]
if cmd == CMD_PING:
sock_brain.send('%s PONG' % msgid)
elif cmd == CMD_LIGHTS:
which_pins = [lights.lookup[lgt] for lgt in bits[2:]]
lights.only(*which_pins)
else:
print 'Unhandleable message: %r' % msg
def main():
parser = argparse.ArgumentParser(description='Traffic light control system')
parser.add_argument('--brain', '-b', help='Address of brain')
parser.add_argument('--fake', '-f', help='Fake Mode', default=False, action='store_true')
parser.add_argument('--mode', '-m', help='Force a different mode', default='cycle')
parser.add_argument('--controls', '-c', help='Use hardware controls', default=False, action='store_true')
args = parser.parse_args()
if args.mode != 'cycle' and args.mode != 'red' and args.mode != 'yellow' and args.mode != 'green' and args.mode != 'disco' and args.mode != 'red-blink' and args.mode != 'yellow-blink' and args.mode != 'green-blink':
raise Exception('Bad mode')
with lights.setup_manager(args):
if args.brain is None:
msg = 'I am the brain'
if args.controls:
msg = "%s with hw controls" % msg
print(msg)
brain_loop(args)
else:
print('Listening to:', args.brain)
node_loop(args.brain)
if __name__ == '__main__':
main()