forked from iandobbie/StatusLED
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusRunner.py
More file actions
377 lines (306 loc) · 12.4 KB
/
StatusRunner.py
File metadata and controls
377 lines (306 loc) · 12.4 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
In this module we create a state machine to follow the state of the executor and launch other tasks form the
Raspberry. The current setup wil manage the status lights.
"""
from multiprocessing import Process, Queue
from transitions.extensions import HierarchicalMachine as Machine
from time import sleep
import json
import socket
import logging
from LEDs import StatusLED
## TODO: get status led and UDP config from file
UDP_IP_ADDRESS = "localhost"
UDP_PORT_NO = 6666
FPGA_UPDATE_RATE = .1 # At which rate is the FPGA sending update status signals
RING_START = (512 - 64)
RING_LEDS = (1, 6, 16, 24)
TOTAL_LEDS = sum(RING_LEDS)
CABINET_START = 0
CABINET_LEDS = 30
OPC_HOST = '127.0.0.1'
OPC_PORT = '7890'
STATES = ['default',
'start',
'configure',
'idle',
'error',
{'name': 'action', 'children': ['default',
'prepare',
'snap',
'experiment',
'mosaic',
]},
'shutdown',
]
TRANSITIONS = [
['on_default', '*', 'default'],
['on_start', 'shutdown', 'start'],
['on_configure', 'start', 'configure'],
['on_idle', '*', 'idle'],
['on_error', '*', 'error'],
['on_action_prepare', 'idle', 'action_prepare'],
['on_action_snap', 'idle', 'action_snap'],
['on_action_experiment', 'idle', 'action_experiment'],
['on_action_mosaic', 'idle', 'action_mosaic'],
['on_shutdown', '*', 'shutdown'],
]
MainFPGA_to_FSMachine_state = {
'0': 'default', # Default
'1': 'start', # Start
'2': 'configure', # Configuring
'3': 'idle', # Idle
'4': 'error', # Aborted
'5': 'action', # Running Action
'6': 'shutdown', # Shutdown
}
ActionFPGA_to_FSMachine_state = {
'0': 'default', # Default
'1': 'experiment', # Executing Experiment
'2': 'prepare', # Transferring Digitals
'3': 'prepare', # Transferring Analogues
'4': 'prepare', # Writing Indexes
'5': 'prepare', # Writing Digitals
'6': 'prepare', # Writing Analogue
'7': 'snap', # Taking Snap
'8': 'prepare', # Flushing FIFOs
'9': 'prepare', # Updating Repetitions
'10': 'mosaic', # Running Slow Mosaic
'11': 'mosaic', # Running Fast Mosaic
}
class FSMachine:
"""This is a class to hold a Finite State Machine.
It is intended to handle the different states and control a response according to this state.
The actions are running as separate processes and
there is a main loop getting status from the executor through a UDP socket and triggering the transitions"""
# Define a State Machine
def __init__(self, states, transitions, timerQueue, effectQueue, initialState='start'):
self.machine = Machine(model=self,
states=states,
transitions=transitions,
initial=initialState,
auto_transitions=False)
# Create queues to pass state and time point
self.timerQueue = timerQueue
self.effectQueue = effectQueue
## Create separate processes to run stuff and start them
# A status LED processor
self.statusLEDs = StatusLEDProcessor(effectQueue=self.effectQueue,
timerQueue=self.timerQueue,
totalLEDs=TOTAL_LEDS,
ringStart=RING_START,
ringLEDs=RING_LEDS,
cabinetStart=CABINET_START,
cabinetLEDs=CABINET_LEDS,
host=OPC_HOST,
port=OPC_PORT,
)
self.statusLEDs.start()
# Configure the callbacks of the machine
def on_enter_start(self):
self.effectQueue.put(['on_enter_start', ()])
def on_enter_configure(self):
self.effectQueue.put(['on_enter_configure', ()])
def on_enter_idle(self):
self.effectQueue.put(['on_enter_idle', ()])
def on_enter_error(self):
self.effectQueue.put(['on_enter_error', ()])
def on_enter_action_experiment(self):
self.effectQueue.put(['on_enter_action_experiment', ()])
def on_enter_action_prepare(self):
self.effectQueue.put(['on_enter_action_prepare', ()])
def on_enter_action_snap(self):
self.effectQueue.put(['on_enter_action_snap', ()])
def on_enter_action_mosaic(self):
self.effectQueue.put(['on_enter_action_mosaic', ()])
def on_enter_shutdown(self):
self.effectQueue.put(['on_enter_shutdown', ()])
def on_kill(self):
self.effectQueue.put(['kill', ()])
while not self.timerQueue.empty(): # Clean the timer queue
self.timerQueue.get(block=False)
class FPGAStatus:
def __init__(self, host, port):
## Create a dictionary to store the full FPGA state
self.currentFPGAStatus = {}
## Create the queues to communicate with the FSM
self.timerQueue = Queue()
self.effectQueue = Queue()
## Create the FSM
self.machine = FSMachine(states=STATES,
transitions=TRANSITIONS,
timerQueue=self.timerQueue,
effectQueue=self.effectQueue,
)
## create a socket to listen
self.socket = self.createReceiveSocket(host, port)
## Create a handle to stop the thread
self.shouldRun = True
def createReceiveSocket(self, host, port):
"""
Creates a UDP socket meant to receive status information
form the RT-host
returns the bound socket
"""
try:
# Create an AF_INET, Datagram socket (UDP)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error as e:
print(f'Failed to create socket. Error message: {e}')
return
try:
# Bind Socket to local host and port
s.bind((host, port))
except socket.error as e:
print(f'Failed to bind address. Error message: {e}')
return
return s
def get_status(self, key=None):
"""
Method to call from outside to get the status
"""
if key and self.currentFPGAStatus is not None:
try:
return self.currentFPGAStatus[key]
except:
print('Key does not exist')
else:
return self.currentFPGAStatus
def poll_fpga_status(self):
"""
This method polls to the UDP socket and gets the status information
of the RT-host and FPGA.
Returns a json object that we can use to update the status dictionary
"""
try:
# Receive Datagram
datagramLength = int(self.socket.recvfrom(4)[0])
datagram = self.socket.recvfrom(datagramLength)[0]
except socket.error as e:
print(f'Failed to get Datagram. Error message: {e}')
return None
return json.loads(datagram.decode())
def trigger_event(self, newStatus):
"""
FInd 'interesting' status or state changes in the FPGA and trigger events or
the corresponding machine transitions.
return the newStatus but with the status reset so not to queue multiple times
"""
# Get a state change
if self.currentFPGAStatus['FPGA Main State'] != newStatus['FPGA Main State']:
new_state = MainFPGA_to_FSMachine_state[newStatus['FPGA Main State']]
# TODO: We have to generalize this into the Hierarchical SM. I do not know how to do this best
if new_state == 'action':
new_state = new_state + '_' + ActionFPGA_to_FSMachine_state[newStatus['Action State']]
print(new_state)
try:
getattr(self.machine, 'on_' + new_state)()
except:
print('Could not get that new state')
# get a timer update and post it into the timer_queue
if self.currentFPGAStatus['Timer'] != newStatus['Timer']:
self.timerQueue.put(newStatus['Timer'])
print(newStatus)
return newStatus
def run(self):
self.currentFPGAStatus = self.poll_fpga_status()
while self.shouldRun:
newFPGAStatus = self.poll_fpga_status()
if newFPGAStatus is not None and newFPGAStatus != self.currentFPGAStatus:
# Trigger a transition and update current state
self.currentFPGAStatus = self.trigger_event(newStatus=newFPGAStatus)
## wait for a period of half the broadcasting rate of the FPGA
sleep(FPGA_UPDATE_RATE / 2)
class StatusLEDProcessor(Process):
"""This class runs the status LEDs. It provides
the link between the state and a particular effect
with its parameters so we can sync it with the state machine"""
def __init__(self,
effectQueue,
timerQueue,
totalLEDs,
ringStart,
ringLEDs,
cabinetStart,
cabinetLEDs,
host,
port):
Process.__init__(self)
self.effectQueue = effectQueue
self.timerQueue = timerQueue
self.LEDs = StatusLED(effectQueue=self.effectQueue,
timerQueue=self.timerQueue,
totalLEDs=totalLEDs,
ringStart=ringStart,
ringsLEDs=ringLEDs,
cabinetStart=cabinetStart,
cabinetLEDs=cabinetLEDs,
host=host,
port=port,)
def run(self):
self.initializeLEDs()
while True:
f, args = self.effectQueue.get()
if f != 'kill':
getattr(self, f)(*args)
else:
return
def initializeLEDs(self):
self.LEDs.setLEDs(intensity=None)
def on_enter_start(self):
pass
def on_enter_configure(self):
pass
def on_enter_idle(self):
"""
Function to call on idle
:return: None
"""
self.LEDs.sineBeat(color=[50, 50, 50],
glow=[20, 20, 20],
frequency=1.0)
def on_enter_error(self):
"""
Method to call on error. It blinks on the defined color, duty and frequency
:return: None
"""
self.LEDs.squareBeat(color=[150, 0, 0],
glow=[50, 0, 0],
frequency=2,
duty=.3)
def on_enter_action_experiment(self):
"""
Function to call on experiment start
:return: None
"""
self.LEDs.chaseLEDsTimer(chaseColor=[128, 0, 0],
timerColor=[0, 128, 0],
decay=8.0,
chaseRing=-1,
timerRing=-2,
speed=2,
frequency=1
)
while not self.timerQueue.empty(): # Clean the timer queue in case things go to quick or we abort
self.timerQueue.get(block=False)
def on_enter_action_prepare(self):
pass
def on_enter_action_snap(self):
"""
Function to call on an image snap
:return: None
"""
self.LEDs.multiplePulse(pattern=[[[0, 0, 255], 0.005]])
def on_enter_action_mosaic(self):
pass
def on_enter_shutdown(self):
pass
def on_reset(self):
pass
def on_terminate(self):
self.terminate()
if __name__ == '__main__':
Status_controller = FPGAStatus(host=UDP_IP_ADDRESS, port=UDP_PORT_NO)
print('Status Controller created')
print('Status Controller running')
Status_controller.run()