forked from HarrisonSteel/ChiBio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
2235 lines (1798 loc) · 94.5 KB
/
app.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
######### Chi.Bio Operating System V1.0 #########
#Import required python packages
import os
import random
import time
import math
from flask import Flask, render_template, jsonify
from threading import Thread, Lock
import threading
import numpy as np
from datetime import datetime, date
import Adafruit_GPIO.I2C as I2C
import Adafruit_BBIO.GPIO as GPIO
import time
import serial
import simplejson
import copy
import csv
import smbus2 as smbus
application = Flask(__name__)
application.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 #Try this https://stackoverflow.com/questions/23112316/using-flask-how-do-i-modify-the-cache-control-header-for-all-output/23115561#23115561
lock=Lock()
#Initialise data structures.
#Sysdata is a structure created for each device and contains the setup / measured data related to that device during an experiment. All of this information is passed into the user interface during an experiment.
sysData = {'M0' : {
'UIDevice' : 'M0',
'present' : 0,
'presentDevices' : { 'M0' : 0,'M1' : 0,'M2' : 0,'M3' : 0,'M4' : 0,'M5' : 0,'M6' : 0,'M7' : 0},
'Version' : {'value' : 'Turbidostat V3.0'},
'DeviceID' : '',
'time' : {'record' : []},
'LEDA' : {'WL' : '395', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LEDB' : {'WL' : '457', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LEDC' : {'WL' : '500', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LEDD' : {'WL' : '523', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LEDE' : {'WL' : '595', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LEDF' : {'WL' : '623', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LEDG' : {'WL' : '6500K', 'default': 0.1, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'LASER650' : {'name' : 'LASER650', 'default': 0.5, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'UV' : {'WL' : 'UV', 'default': 0.5, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0},
'Heat' : {'default': 0.0, 'target' : 0.0, 'max': 1.0, 'min' : 0.0,'ON' : 0,'record' : []},
'Thermostat' : {'default': 37.0, 'target' : 0.0, 'max': 50.0, 'min' : 0.0,'ON' : 0,'record' : [],'cycleTime' : 30.0, 'Integral' : 0.0,'last' : -1},
'Experiment' : {'indicator' : 'USR0', 'startTime' : 'Waiting', 'startTimeRaw' : 0, 'ON' : 0,'cycles' : 0, 'cycleTime' : 60.0,'threadCount' : 0},
'Terminal' : {'text' : ''},
'AS7341' : {
'spectrum' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0, 'NIR' : 0,'DARK' : 0,'ExtGPIO' : 0, 'ExtINT' : 0, 'FLICKER' : 0},
'channels' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0, 'NIR' : 0,'DARK' : 0,'ExtGPIO' : 0, 'ExtINT' : 0, 'FLICKER' : 0},
'current' : {'ADC0': 0,'ADC1': 0,'ADC2': 0,'ADC3': 0,'ADC4': 0,'ADC5' : 0}},
'ThermometerInternal' : {'current' : 0.0,'record' : []},
'ThermometerExternal' : {'current' : 0.0,'record' : []},
'ThermometerIR' : {'current' : 0.0,'record' : []},
'OD' : {'current' : 0.0,'target' : 0.5,'default' : 0.5,'max': 10, 'min' : 0,'record' : [],'targetrecord' : [],'Measuring' : 0, 'ON' : 0,'Integral' : 0.0,'Integral2' : 0.0,'device' : 'LASER650'},
'OD0' : {'target' : 0.0,'raw' : 0.0,'max' : 100000.0,'min': 0.0,'LASERb' : 1.833 ,'LASERa' : 0.226, 'LEDFa' : 0.673, 'LEDAa' : 7.0 },
'Chemostat' : {'ON' : 0, 'p1' : 0.0, 'p2' : 0.1},
'Zigzag': {'ON' : 0, 'Zig' : 0.04,'target' : 0.0,'SwitchPoint' : 0},
'GrowthRate': {'current' : 0.0,'record' : [],'default' : 2.0},
'Volume' : {'target' : 20.0,'max' : 50.0, 'min' : 0.0,'ON' : 0},
'Pump1' : {'target' : 0.0,'default' : 0.0,'max': 1.0, 'min' : -1.0, 'direction' : 1.0, 'ON' : 0,'record' : [], 'thread' : 0},
'Pump2' : {'target' : 0.0,'default' : 0.0,'max': 1.0, 'min' : -1.0, 'direction' : 1.0, 'ON' : 0,'record' : [], 'thread' : 0},
'Pump3' : {'target' : 0.0,'default' : 0.0,'max': 1.0, 'min' : -1.0, 'direction' : 1.0, 'ON' : 0,'record' : [], 'thread' : 0},
'Pump4' : {'target' : 0.0,'default' : 0.0,'max': 1.0, 'min' : -1.0, 'direction' : 1.0, 'ON' : 0,'record' : [], 'thread' : 0},
'Stir' : {'target' : 0.0,'default' : 0.5,'max': 1.0, 'min' : 0.0, 'ON' : 0},
'Light' : {'target' : 0.0,'default' : 0.5,'max': 1.0, 'min' : 0.0, 'ON' : 0, 'Excite' : 'LEDD', 'record' : []},
'Custom' : {'Status' : 0.0,'default' : 0.0,'Program': 'C1', 'ON' : 0,'param1' : 0, 'param2' : 0, 'param3' : 0.0, 'record' : []},
'FP1' : {'ON' : 0 ,'LED' : 0,'BaseBand' : 0, 'Emit11Band' : 0,'Emit2Band' : 0,'Base' : 0, 'Emit11' : 0,'Emit2' : 0,'BaseRecord' : 0, 'Emit1Record' : 0,'Emit2Record' : 0 ,'Gain' : 0},
'FP2' : {'ON' : 0 ,'LED' : 0,'BaseBand' : 0, 'Emit11Band' : 0,'Emit2Band' : 0,'Base' : 0, 'Emit11' : 0,'Emit2' : 0,'BaseRecord' : 0, 'Emit1Record' : 0,'Emit2Record' : 0 ,'Gain' : 0},
'FP3' : {'ON' : 0 ,'LED' : 0,'BaseBand' : 0, 'Emit11Band' : 0,'Emit2Band' : 0,'Base' : 0, 'Emit11' : 0,'Emit2' : 0,'BaseRecord' : 0, 'Emit1Record' : 0,'Emit2Record' : 0 ,'Gain' : 0},
'biofilm' : {'LEDA' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LEDB' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LEDC' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LEDD' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LEDE' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LEDF' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LEDG' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0},
'LASER650' : {'nm410' : 0, 'nm440' : 0, 'nm470' : 0, 'nm510' : 0, 'nm550' : 0, 'nm583' : 0, 'nm620' : 0, 'nm670' : 0,'CLEAR' : 0,'NIR' : 0}}
}}
#SysDevices is unique to each device and is responsible for storing information required for the digital communications, and various automation funtions. These values are stored outside sysData since they are not passable into the HTML interface using the jsonify package.
sysDevices = {'M0' : {
'AS7341' : {'device' : 0},
'ThermometerInternal' : {'device' : 0},
'ThermometerExternal' : {'device' : 0},
'ThermometerIR' : {'device' : 0,'address' :0},
'DAC' : {'device' : 0},
'Pumps' : {'device' : 0,'startup' : 0, 'frequency' : 0},
'PWM' : {'device' : 0,'startup' : 0, 'frequency' : 0},
'Pump1' : {'thread' : 0,'threadCount' : 0, 'active' : 0},
'Pump2' : {'thread' : 0,'threadCount' : 0, 'active' : 0},
'Pump3' : {'thread' : 0,'threadCount' : 0, 'active' : 0},
'Pump4' : {'thread' : 0,'threadCount' : 0, 'active' : 0},
'Experiment' : {'thread' : 0},
'Thermostat' : {'thread' : 0,'threadCount' : 0},
}}
for M in ['M1','M2','M3','M4','M5','M6','M7']:
sysData[M]=copy.deepcopy(sysData['M0'])
sysDevices[M]=copy.deepcopy(sysDevices['M0'])
#sysItems stores information about digital addresses which is used as a reference for all devices.
sysItems = {
'DAC' : {'LEDA' : '00000100','LEDB' : '00000000','LEDC' : '00000110','LEDD' : '00000001','LEDE' : '00000101','LEDF' : '00000011','LEDG' : '00000010','LASER650' : '00000111'},
'Multiplexer' : {'device' : 0 , 'M0' : '00000001','M1' : '00000010','M2' : '00000100','M3' : '00001000','M4' : '00010000','M5' : '00100000','M6' : '01000000','M7' : '10000000'},
'UIDevice' : 'M0',
'Watchdog' : {'pin' : 'P8_11','thread' : 0,'ON' : 1},
'FailCount' : 0,
'All' : {'ONL' : 0xFA, 'ONH' : 0xFB, 'OFFL' : 0xFC, 'OFFH' : 0xFD},
'Stir' : {'ONL' : 0x06, 'ONH' : 0x07, 'OFFL' : 0x08, 'OFFH' : 0x09},
'Heat' : {'ONL' : 0x3E, 'ONH' : 0x3F, 'OFFL' : 0x40, 'OFFH' : 0x41},
'UV' : {'ONL' : 0x42, 'ONH' : 0x43, 'OFFL' : 0x44, 'OFFH' : 0x45},
'LEDA' : {'ONL' : 0x0E, 'ONH' : 0x0F, 'OFFL' : 0x10, 'OFFH' : 0x11},
'LEDB' : {'ONL' : 0x16, 'ONH' : 0x17, 'OFFL' : 0x18, 'OFFH' : 0x19},
'LEDC' : {'ONL' : 0x0A, 'ONH' : 0x0B, 'OFFL' : 0x0C, 'OFFH' : 0x0D},
'LEDD' : {'ONL' : 0x1A, 'ONH' : 0x1B, 'OFFL' : 0x1C, 'OFFH' : 0x1D},
'LEDE' : {'ONL' : 0x22, 'ONH' : 0x23, 'OFFL' : 0x24, 'OFFH' : 0x25},
'LEDF' : {'ONL' : 0x1E, 'ONH' : 0x1F, 'OFFL' : 0x20, 'OFFH' : 0x21},
'LEDG' : {'ONL' : 0x12, 'ONH' : 0x13, 'OFFL' : 0x14, 'OFFH' : 0x15},
'Pump1' : {
'In1' : {'ONL' : 0x06, 'ONH' : 0x07, 'OFFL' : 0x08, 'OFFH' : 0x09},
'In2' : {'ONL' : 0x0A, 'ONH' : 0x0B, 'OFFL' : 0x0C, 'OFFH' : 0x0D},
},
'Pump2' : {
'In1' : {'ONL' : 0x0E, 'ONH' : 0x0F, 'OFFL' : 0x10, 'OFFH' : 0x11},
'In2' : {'ONL' : 0x12, 'ONH' : 0x13, 'OFFL' : 0x14, 'OFFH' : 0x15},
},
'Pump3' : {
'In1' : {'ONL' : 0x16, 'ONH' : 0x17, 'OFFL' : 0x18, 'OFFH' : 0x19},
'In2' : {'ONL' : 0x1A, 'ONH' : 0x1B, 'OFFL' : 0x1C, 'OFFH' : 0x1D},
},
'Pump4' : {
'In1' : {'ONL' : 0x1E, 'ONH' : 0x1F, 'OFFL' : 0x20, 'OFFH' : 0x21},
'In2' : {'ONL' : 0x22, 'ONH' : 0x23, 'OFFL' : 0x24, 'OFFH' : 0x25},
},
'AS7341' : {
'0x00' : {'A' : 'nm470', 'B' : 'U'},
'0x01' : {'A' : 'U', 'B' : 'nm410'},
'0x02' : {'A' : 'U', 'B' : 'U'},
'0x03' : {'A' : 'nm670', 'B' : 'U'},
'0x04' : {'A' : 'U', 'B' : 'nm583'},
'0x05' : {'A' : 'nm510', 'B' : 'nm440'},
'0x06' : {'A' : 'nm550', 'B' : 'U'},
'0x07' : {'A' : 'U', 'B' : 'nm620'},
'0x08' : {'A' : 'CLEAR', 'B' : 'U'},
'0x09' : {'A' : 'nm550', 'B' : 'U'},
'0x0A' : {'A' : 'U', 'B' : 'nm620'},
'0x0B' : {'A' : 'U', 'B' : 'U'},
'0x0C' : {'A' : 'nm440', 'B' : 'U'},
'0x0D' : {'A' : 'U', 'B' : 'nm510'},
'0x0E' : {'A' : 'nm583', 'B' : 'nm670'},
'0x0F' : {'A' : 'nm470', 'B' : 'U'},
'0x10' : {'A' : 'ExtGPIO', 'B' : 'nm410'},
'0x11' : {'A' : 'CLEAR', 'B' : 'ExtINT'},
'0x12' : {'A' : 'DARK', 'B' : 'U'},
'0x13' : {'A' : 'FLICKER', 'B' : 'NIR'},
}
}
# This section of code is responsible for the watchdog circuit. The circuit is implemented in hardware on the control computer, and requires the watchdog pin be toggled low->high each second, otherwise it will power down all connected devices. This section is therefore critical to operation of the device.
def runWatchdog():
#Watchdog timing function which continually runs in a thread.
global sysItems;
if (sysItems['Watchdog']['ON']==1):
#sysItems['Watchdog']['thread']
toggleWatchdog();
time.sleep(0.15)
sysItems['Watchdog']['thread']=Thread(target = runWatchdog, args=())
sysItems['Watchdog']['thread'].setDaemon(True)
sysItems['Watchdog']['thread'].start();
def toggleWatchdog():
#Toggle the watchdog
global sysItems;
GPIO.output(sysItems['Watchdog']['pin'], GPIO.HIGH)
time.sleep(0.05)
GPIO.output(sysItems['Watchdog']['pin'], GPIO.LOW)
GPIO.setup(sysItems['Watchdog']['pin'], GPIO.OUT)
print(str(datetime.now()) + ' Starting watchdog')
sysItems['Watchdog']['thread']=Thread(target = runWatchdog, args=())
sysItems['Watchdog']['thread'].setDaemon(True)
sysItems['Watchdog']['thread'].start();
GPIO.setup('P8_15', GPIO.OUT) #This output connects to the RESET pin on the I2C Multiplexer.
GPIO.output('P8_15', GPIO.HIGH)
GPIO.setup('P8_17', GPIO.OUT) #This output connects to D input of the D-Latch
GPIO.output('P8_17', GPIO.HIGH)
def initialise(M):
#Function that initialises all parameters / clears stored values for a given device.
#If you want to record/add values to sysData, recommend adding an initialisation line in here.
global sysData;
global sysItems;
global sysDevices
for LED in ['LEDA','LEDB','LEDC','LEDD','LEDE','LEDF','LEDG']:
sysData[M][LED]['target']=sysData[M][LED]['default']
sysData[M][LED]['ON']=0
sysData[M]['UV']['target']=sysData[M]['UV']['default']
sysData[M]['UV']['ON']=0
sysData[M]['LASER650']['target']=sysData[M]['LASER650']['default']
sysData[M]['LASER650']['ON']=0
FP='FP1'
sysData[M][FP]['ON']=0
sysData[M][FP]['LED']="LEDB"
sysData[M][FP]['Base']=0
sysData[M][FP]['Emit1']=0
sysData[M][FP]['Emit2']=0
sysData[M][FP]['BaseBand']="CLEAR"
sysData[M][FP]['Emit1Band']="nm510"
sysData[M][FP]['Emit2Band']="nm550"
sysData[M][FP]['Gain']="x10"
sysData[M][FP]['BaseRecord']=[]
sysData[M][FP]['Emit1Record']=[]
sysData[M][FP]['Emit2Record']=[]
FP='FP2'
sysData[M][FP]['ON']=0
sysData[M][FP]['LED']="LEDD"
sysData[M][FP]['Base']=0
sysData[M][FP]['Emit1']=0
sysData[M][FP]['Emit2']=0
sysData[M][FP]['BaseBand']="CLEAR"
sysData[M][FP]['Emit1Band']="nm583"
sysData[M][FP]['Emit2Band']="nm620"
sysData[M][FP]['BaseRecord']=[]
sysData[M][FP]['Emit1Record']=[]
sysData[M][FP]['Emit2Record']=[]
sysData[M][FP]['Gain']="x10"
FP='FP3'
sysData[M][FP]['ON']=0
sysData[M][FP]['LED']="LEDE"
sysData[M][FP]['Base']=0
sysData[M][FP]['Emit1']=0
sysData[M][FP]['Emit2']=0
sysData[M][FP]['BaseBand']="CLEAR"
sysData[M][FP]['Emit1Band']="nm620"
sysData[M][FP]['Emit2Band']="nm670"
sysData[M][FP]['BaseRecord']=[]
sysData[M][FP]['Emit1Record']=[]
sysData[M][FP]['Emit2Record']=[]
sysData[M][FP]['Gain']="x10"
for PUMP in ['Pump1','Pump2','Pump3','Pump4']:
sysData[M][PUMP]['default']=0.0;
sysData[M][PUMP]['target']=sysData[M][PUMP]['default']
sysData[M][PUMP]['ON']=0
sysData[M][PUMP]['direction']=1.0
sysDevices[M][PUMP]['threadCount']=0
sysDevices[M][PUMP]['active']=0
sysData[M]['Heat']['default']=0;
sysData[M]['Heat']['target']=sysData[M]['Heat']['default']
sysData[M]['Heat']['ON']=0
sysData[M]['Thermostat']['default']=37.0;
sysData[M]['Thermostat']['target']=sysData[M]['Thermostat']['default']
sysData[M]['Thermostat']['ON']=0
sysData[M]['Thermostat']['Integral']=0
sysData[M]['Thermostat']['last']=-1
sysData[M]['Stir']['target']=sysData[M]['Stir']['default']
sysData[M]['Stir']['ON']=0
sysData[M]['Light']['target']=sysData[M]['Light']['default']
sysData[M]['Light']['ON']=0
sysData[M]['Light']['Excite']='LEDD'
sysData[M]['Custom']['Status']=sysData[M]['Custom']['default']
sysData[M]['Custom']['ON']=0
sysData[M]['Custom']['Program']='C1'
sysData[M]['Custom']['param1']=0.0
sysData[M]['Custom']['param2']=0.0
sysData[M]['Custom']['param3']=0.0
sysData[M]['OD']['current']=0.0
sysData[M]['OD']['target']=sysData[M]['OD']['default'];
sysData[M]['OD0']['target']=65000.0
sysData[M]['OD0']['raw']=65000.0
sysData[M]['OD']['device']='LASER650'
#sysData[M]['OD']['device']='LEDA'
#if (M=='M0'):
# sysData[M]['OD']['device']='LEDA'
sysData[M]['Volume']['target']=20.0
clearTerminal(M)
addTerminal(M,'System Initialised')
sysData[M]['Experiment']['ON']=0
sysData[M]['Experiment']['cycles']=0
sysData[M]['Experiment']['threadCount']=0
sysData[M]['Experiment']['startTime']=' Waiting '
sysData[M]['Experiment']['startTimeRaw']=0
sysData[M]['OD']['ON']=0
sysData[M]['OD']['Measuring']=0
sysData[M]['OD']['Integral']=0.0
sysData[M]['OD']['Integral2']=0.0
sysData[M]['Zigzag']['ON']=0
sysData[M]['Zigzag']['target']=0.0
sysData[M]['Zigzag']['SwitchPoint']=0
sysData[M]['GrowthRate']['current']=sysData[M]['GrowthRate']['default']
sysDevices[M]['Thermostat']['threadCount']=0
channels=['nm410','nm440','nm470','nm510','nm550','nm583','nm620', 'nm670','CLEAR','NIR','DARK','ExtGPIO', 'ExtINT' , 'FLICKER']
for channel in channels:
sysData[M]['AS7341']['channels'][channel]=0
sysData[M]['AS7341']['spectrum'][channel]=0
DACS=['ADC0', 'ADC1', 'ADC2', 'ADC3', 'ADC4', 'ADC5']
for DAC in DACS:
sysData[M]['AS7341']['current'][DAC]=0
sysData[M]['ThermometerInternal']['current']=0.0
sysData[M]['ThermometerExternal']['current']=0.0
sysData[M]['ThermometerIR']['current']=0.0
sysData[M]['time']['record']=[]
sysData[M]['OD']['record']=[]
sysData[M]['OD']['targetrecord']=[]
sysData[M]['Pump1']['record']=[]
sysData[M]['Pump2']['record']=[]
sysData[M]['Pump3']['record']=[]
sysData[M]['Pump4']['record']=[]
sysData[M]['Heat']['record']=[]
sysData[M]['Light']['record']=[]
sysData[M]['ThermometerInternal']['record']=[]
sysData[M]['ThermometerExternal']['record']=[]
sysData[M]['ThermometerIR']['record']=[]
sysData[M]['Thermostat']['record']=[]
sysData[M]['GrowthRate']['record']=[]
sysDevices[M]['ThermometerInternal']['device']=I2C.get_i2c_device(0x18,2) #Get Thermometer on Bus 2!!!
sysDevices[M]['ThermometerExternal']['device']=I2C.get_i2c_device(0x1b,2) #Get Thermometer on Bus 2!!!
sysDevices[M]['DAC']['device']=I2C.get_i2c_device(0x48,2) #Get DAC on Bus 2!!!
sysDevices[M]['AS7341']['device']=I2C.get_i2c_device(0x39,2) #Get OD Chip on Bus 2!!!!!
sysDevices[M]['Pumps']['device']=I2C.get_i2c_device(0x61,2) #Get OD Chip on Bus 2!!!!!
sysDevices[M]['Pumps']['startup']=0
sysDevices[M]['Pumps']['frequency']=0x1e #200Hz PWM frequency
sysDevices[M]['PWM']['device']=I2C.get_i2c_device(0x60,2) #Get OD Chip on Bus 2!!!!!
sysDevices[M]['PWM']['startup']=0
sysDevices[M]['PWM']['frequency']=0x03# 0x14 = 300hz, 0x03 is 1526 Hz PWM frequency for fan/LEDs, maximum possible. Potentially dial this down if you are getting audible ringing in the device!
#There is a tradeoff between large frequencies which can make capacitors in the 6V power regulation oscillate audibly, and small frequencies which result in the number of LED "ON" cycles varying during measurements.
sysDevices[M]['ThermometerIR']['device']=smbus.SMBus(bus=2) #Set up SMBus thermometer
sysDevices[M]['ThermometerIR']['address']=0x5a
# This section of commented code is used for testing I2C communication integrity.
# sysData[M]['present']=1
# getData=I2CCom(M,'ThermometerInternal',1,16,0x05,0,0)
# i=0
# while (1==1):
# i=i+1
# if (i%1000==1):
# print(str(i))
# sysDevices[M]['ThermometerInternal']['device'].readU8(int(0x05))
# getData=I2CCom(M,which,1,16,0x05,0,0)
scanDevices(M)
if(sysData[M]['present']==1):
turnEverythingOff(M)
print(str(datetime.now()) + " Initialised " + str(M) +', Device ID: ' + sysData[M]['DeviceID'])
def initialiseAll():
# Initialisation function which runs at when software is started for the first time.
sysItems['Multiplexer']['device']=I2C.get_i2c_device(0x74,2)
sysItems['FailCount']=0
time.sleep(2.0) #This wait is to allow the watchdog circuit to boot.
print(str(datetime.now()) + ' Initialising devices')
for M in ['M0','M1','M2','M3','M4','M5','M6','M7']:
initialise(M)
scanDevices("all")
def turnEverythingOff(M):
# Function which turns off all actuation/hardware.
for LED in ['LEDA','LEDB','LEDC','LEDD','LEDE','LEDF','LEDG']:
sysData[M][LED]['ON']=0
sysData[M]['LASER650']['ON']=0
sysData[M]['Pump1']['ON']=0
sysData[M]['Pump2']['ON']=0
sysData[M]['Pump3']['ON']=0
sysData[M]['Pump4']['ON']=0
sysData[M]['Stir']['ON']=0
sysData[M]['Heat']['ON']=0
sysData[M]['UV']['ON']=0
I2CCom(M,'DAC',0,8,int('00000000',2),int('00000000',2),0)#Sets all DAC Channels to zero!!!
setPWM(M,'PWM',sysItems['All'],0,0)
setPWM(M,'Pumps',sysItems['All'],0,0)
SetOutputOn(M,'Stir',0)
SetOutputOn(M,'Thermostat',0)
SetOutputOn(M,'Heat',0)
SetOutputOn(M,'UV',0)
SetOutputOn(M,'Pump1',0)
SetOutputOn(M,'Pump2',0)
SetOutputOn(M,'Pump3',0)
SetOutputOn(M,'Pump4',0)
@application.route('/')
def index():
#Function responsible for sending appropriate device's data to user interface.
global sysData
global sysItems
outputdata=sysData[sysItems['UIDevice']]
for M in ['M0','M1','M2','M3','M4','M5','M6','M7']:
if sysData[M]['present']==1:
outputdata['presentDevices'][M]=1
else:
outputdata['presentDevices'][M]=0
return render_template('index.html',**outputdata)
@application.route('/getSysdata/')
def getSysdata():
#Similar to function above, packages data to be sent to UI.
global sysData
global sysItems
outputdata=sysData[sysItems['UIDevice']]
for M in ['M0','M1','M2','M3','M4','M5','M6','M7']:
if sysData[M]['present']==1:
outputdata['presentDevices'][M]=1
else:
outputdata['presentDevices'][M]=0
return jsonify(outputdata)
@application.route('/changeDevice/<M>',methods=['POST'])
def changeDevice(M):
#Function responsible for changin which device is selected in the UI.
global sysData
global sysItems
M=str(M)
if sysData[M]['present']==1:
for Mb in ['M0','M1','M2','M3','M4','M5','M6','M7']:
sysData[Mb]['UIDevice']=M
sysItems['UIDevice']=M
return ('', 204)
@application.route('/scanDevices/<which>',methods=['POST'])
def scanDevices(which):
#Scans to decide which devices are plugged in/on. Does this by trying to communicate with their internal thermometers (if this communication failes, software assumes device is not present)
global sysData
which=str(which)
if which=="all":
for M in ['M0','M1','M2','M3','M4','M5','M6','M7']:
sysData[M]['present']=1
I2CCom(M,'ThermometerInternal',1,16,0x05,0,0) #We arbitrarily poll the thermometer to see if anything is plugged in!
sysData[M]['DeviceID']=GetID(M)
else:
sysData[which]['present']=1
I2CCom(which,'ThermometerInternal',1,16,0x05,0,0)
sysData[which]['DeviceID']=GetID(which)
return ('', 204)
def GetID(M):
#Gets the CHi.Bio reactor's ID, which is basically just the unique ID of the infrared thermometer.
global sysData
M=str(M)
ID=''
if sysData[M]['present']==1:
pt1=str(I2CCom(M,'ThermometerIR',1,0,0x3C,0,1))
pt2=str(I2CCom(M,'ThermometerIR',1,0,0x3D,0,1))
pt3=str(I2CCom(M,'ThermometerIR',1,0,0x3E,0,1))
pt4=str(I2CCom(M,'ThermometerIR',1,0,0x3F,0,1))
ID = pt1+pt2+pt3+pt4
return ID
def addTerminal(M,strIn):
#Responsible for adding a new line to the terminal in the UI.
global sysData
now=datetime.now()
timeString=now.strftime("%Y-%m-%d %H:%M:%S ")
sysData[M]['Terminal']['text']=timeString + ' - ' + str(strIn) + '</br>' + sysData[M]['Terminal']['text']
@application.route("/ClearTerminal/<M>",methods=['POST'])
def clearTerminal(M):
#Deletes everything from the terminal.
global sysData
M=str(M)
if (M=="0"):
M=sysItems['UIDevice']
sysData[M]['Terminal']['text']=''
addTerminal(M,'Terminal Cleared')
return ('', 204)
@application.route("/SetFPMeasurement/<item>/<Excite>/<Base>/<Emit1>/<Emit2>/<Gain>",methods=['POST'])
def SetFPMeasurement(item,Excite,Base,Emit1,Emit2,Gain):
#Sets up the fluorescent protein measurement in terms of gain, and which LED / measurement bands to use.
FP=str(item)
Excite=str(Excite)
Base=str(Base)
Emit1=str(Emit1)
Emit2=str(Emit2)
Gain=str(Gain)
M=sysItems['UIDevice']
if sysData[M][FP]['ON']==1:
sysData[M][FP]['ON']=0
return ('', 204)
else:
sysData[M][FP]['ON']=1
sysData[M][FP]['LED']=Excite
sysData[M][FP]['BaseBand']=Base
sysData[M][FP]['Emit1Band']=Emit1
sysData[M][FP]['Emit2Band']=Emit2
sysData[M][FP]['Gain']=Gain
return ('', 204)
@application.route("/SetOutputTarget/<item>/<M>/<value>",methods=['POST'])
def SetOutputTarget(M,item, value):
#General function used to set the output level of a particular item, ensuring it is within an acceptable range.
global sysData
item = str(item)
value = float(value)
M=str(M)
if (M=="0"):
M=sysItems['UIDevice']
print(str(datetime.now()) + " Set item: " + str(item) + " to value " + str(value) + " on " + str(M))
if (value<sysData[M][item]['min']):
value=sysData[M][item]['min']
if (value>sysData[M][item]['max']):
value=sysData[M][item]['max']
sysData[M][item]['target']=value
if(sysData[M][item]['ON']==1 and not(item=='OD' or item=='Thermostat')): #Checking to see if our item is already running, in which case
SetOutputOn(M,item,0) #we turn it off and on again to restart at new rate.
SetOutputOn(M,item,1)
return ('', 204)
@application.route("/SetOutputOn/<item>/<force>/<M>",methods=['POST'])
def SetOutputOn(M,item,force):
#General function used to switch an output on or off.
global sysData
item = str(item)
force = int(force)
M=str(M)
if (M=="0"):
M=sysItems['UIDevice']
#The first statements are to force it on or off it the command is called in force mode (force implies it sets it to a given state, regardless of what it is currently in)
if (force==1):
sysData[M][item]['ON']=1
SetOutput(M,item)
return ('', 204)
elif(force==0):
sysData[M][item]['ON']=0;
SetOutput(M,item)
return ('', 204)
#Elsewise this is doing a flip operation (i.e. changes to opposite state to that which it is currently in)
if (sysData[M][item]['ON']==0):
sysData[M][item]['ON']=1
SetOutput(M,item)
return ('', 204)
else:
sysData[M][item]['ON']=0;
SetOutput(M,item)
return ('', 204)
def SetOutput(M,item):
#Here we actually do the digital communications required to set a given output. This function is called by SetOutputOn above as required.
global sysData
global sysItems
global sysDevices
M=str(M)
#We go through each different item and set it going as appropriate.
if(item=='Stir'):
#Stirring is initiated at a high speed for a couple of seconds to prevent the stir motor from stalling (e.g. if it is started at an initial power of 0.3)
if (sysData[M][item]['target']*float(sysData[M][item]['ON'])>0):
setPWM(M,'PWM',sysItems[item],1.0*float(sysData[M][item]['ON']),0) # This line is to just get stirring started briefly.
time.sleep(1.5)
if (sysData[M][item]['target']>0.4 and sysData[M][item]['ON']==1):
setPWM(M,'PWM',sysItems[item],0.5*float(sysData[M][item]['ON']),0) # This line is to just get stirring started briefly.
time.sleep(0.75)
if (sysData[M][item]['target']>0.8 and sysData[M][item]['ON']==1):
setPWM(M,'PWM',sysItems[item],0.7*float(sysData[M][item]['ON']),0) # This line is to just get stirring started briefly.
time.sleep(0.75)
setPWM(M,'PWM',sysItems[item],sysData[M][item]['target']*float(sysData[M][item]['ON']),0)
elif(item=='Heat'):
setPWM(M,'PWM',sysItems[item],sysData[M][item]['target']*float(sysData[M][item]['ON']),0)
elif(item=='UV'):
setPWM(M,'PWM',sysItems[item],sysData[M][item]['target']*float(sysData[M][item]['ON']),0)
elif (item=='Thermostat'):
sysDevices[M][item]['thread']=Thread(target = Thermostat, args=(M,item))
sysDevices[M][item]['thread'].setDaemon(True)
sysDevices[M][item]['thread'].start();
elif (item=='Pump1' or item=='Pump2' or item=='Pump3' or item=='Pump4'):
if (sysData[M][item]['target']==0):
sysData[M][item]['ON']=0
sysDevices[M][item]['thread']=Thread(target = PumpModulation, args=(M,item))
sysDevices[M][item]['thread'].setDaemon(True)
sysDevices[M][item]['thread'].start();
elif (item=='OD'):
SetOutputOn(M,'Pump1',0)
SetOutputOn(M,'Pump2',0) #We turn pumps off when we switch OD state
elif (item=='Zigzag'):
sysData[M]['Zigzag']['target']=5.0
sysData[M]['Zigzag']['SwitchPoint']=sysData[M]['Experiment']['cycles']
elif (item=='LEDA' or item=='LEDB' or item=='LEDC' or item=='LEDD' or item=='LEDE' or item=='LEDF' or item=='LEDG'):
setPWM(M,'PWM',sysItems[item],sysData[M][item]['target']*float(sysData[M][item]['ON']),0)
else: #This is if we are setting the DAC. All should be in range [0,1]
register = int(sysItems['DAC'][item],2)
value=sysData[M][item]['target']*float(sysData[M][item]['ON'])
if (value==0):
value=0
else:
value=(value+0.00)/1.00
sf=0.303 #This factor is scaling down the maximum voltage being fed to the laser, preventing its photodiode current (and hence optical power) being too large.
value=value*sf
binaryValue=bin(int(value*4095.9)) #Bit of a dodgy method for ensuring we get an integer in [0,4095]
toWrite=str(binaryValue[2:].zfill(16))
toWrite1=int(toWrite[0:8],2)
toWrite2=int(toWrite[8:16],2)
I2CCom(M,'DAC',0,8,toWrite1,toWrite2,0)
def PumpModulation(M,item):
#Responsible for turning pumps on/off with an appropriate duty cycle. They are turned on for a fraction of each ~1minute cycle to achieve low pump rates.
global sysData
global sysItems
global sysDevices
sysDevices[M][item]['threadCount']=(sysDevices[M][item]['threadCount']+1)%100 #Index of the particular thread running.
currentThread=sysDevices[M][item]['threadCount']
while (sysDevices[M][item]['active']==1): #Idea is we will wait here if a previous thread on this pump is already running. Potentially all this 'active' business could be removed from this fuction.
time.sleep(0.02)
if (abs(sysData[M][item]['target']*sysData[M][item]['ON'])!=1 and currentThread==sysDevices[M][item]['threadCount']): #In all cases we turn things off to begin
sysDevices[M][item]['active']=1
setPWM(M,'Pumps',sysItems[item]['In1'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In2'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In1'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In2'],0.0*float(sysData[M][item]['ON']),0)
sysDevices[M][item]['active']=0
if (sysData[M][item]['ON']==0):
return
Time1=datetime.now()
cycletime=sysData[M]['Experiment']['cycleTime']*1.05 #We make this marginally longer than the experiment cycle time to avoid too much chaos when you come back around to pumping again.
Ontime=cycletime*abs(sysData[M][item]['target'])
# Decided to remove the below section in order to prevent media buildup in the device if you are pumping in very rapidly. This check means that media is removed, then added. Removing this code means these happen simultaneously.
#if (item=="Pump1" and abs(sysData[M][item]['target'])<0.3): #Ensuring we run Pump1 after Pump2.
# waittime=cycletime*abs(sysData[M]['Pump2']['target']) #We want to wait until the output pump has stopped, otherwise you are very inefficient with your media since it will be pumping out the fresh media fromthe top of the test tube right when it enters.
# time.sleep(waittime+1.0)
if (sysData[M][item]['target']>0 and currentThread==sysDevices[M][item]['threadCount']): #Turning on pumps in forward direction
sysDevices[M][item]['active']=1
setPWM(M,'Pumps',sysItems[item]['In1'],1.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In2'],0.0*float(sysData[M][item]['ON']),0)
sysDevices[M][item]['active']=0
elif (sysData[M][item]['target']<0 and currentThread==sysDevices[M][item]['threadCount']): #Or backward direction.
sysDevices[M][item]['active']=1
setPWM(M,'Pumps',sysItems[item]['In1'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In2'],1.0*float(sysData[M][item]['ON']),0)
sysDevices[M][item]['active']=0
time.sleep(Ontime)
if(abs(sysData[M][item]['target'])!=1 and currentThread==sysDevices[M][item]['threadCount']): #Turning off pumps at appropriate time.
sysDevices[M][item]['active']=1
setPWM(M,'Pumps',sysItems[item]['In1'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In2'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In1'],0.0*float(sysData[M][item]['ON']),0)
setPWM(M,'Pumps',sysItems[item]['In2'],0.0*float(sysData[M][item]['ON']),0)
sysDevices[M][item]['active']=0
Time2=datetime.now()
elapsedTime=Time2-Time1
elapsedTimeSeconds=round(elapsedTime.total_seconds(),2)
Offtime=cycletime-elapsedTimeSeconds
if (Offtime>0.0):
time.sleep(Offtime)
if (sysData[M][item]['ON']==1 and sysDevices[M][item]['threadCount']==currentThread): #If pumps need to keep going, this starts a new pump thread.
sysDevices[M][item]['thread']=Thread(target = PumpModulation, args=(M,item))
sysDevices[M][item]['thread'].setDaemon(True)
sysDevices[M][item]['thread'].start();
def Thermostat(M,item):
#Function that implements thermostat temperature control using MPC algorithm.
global sysData
global sysItems
global sysDevices
ON=sysData[M][item]['ON']
sysDevices[M][item]['threadCount']=(sysDevices[M][item]['threadCount']+1)%100
currentThread=sysDevices[M][item]['threadCount']
if (ON==0):
SetOutputOn(M,'Heat',0)
return
MeasureTemp(M,'IR') #Measures temperature - note that this may be happening DURING stirring.
CurrentTemp=sysData[M]['ThermometerIR']['current']
TargetTemp=sysData[M]['Thermostat']['target']
LastTemp=sysData[M]['Thermostat']['last']
#MPC Controller Component
MediaTemp=sysData[M]['ThermometerExternal']['current']
MPC=0
if (MediaTemp>0.0):
Tdiff=CurrentTemp-MediaTemp
Pumping=sysData[M]['Pump1']['target']*float(sysData[M]['Pump1']['ON'])*float(sysData[M]['OD']['ON'])
Gain=2.5
MPC=Gain*Tdiff*Pumping
#PI Controller Component
e=TargetTemp-CurrentTemp
dt=sysData[M]['Thermostat']['cycleTime']
I=sysData[M]['Thermostat']['Integral']
if (abs(e)<2.0):
I=I+0.0005*dt*e
P=0.25*e
else:
P=0.5*e;
if (abs(TargetTemp-LastTemp)>2.0): #This resets integrator if we make a big jump in set point.
I=0.0
elif(I<0.0):
I=0.0
elif (I>1.0):
I=1.0
sysData[M]['Thermostat']['Integral']=I
U=P+I+MPC
if(U>1.0):
U=1.0
sysData[M]['Heat']['target']=U
sysData[M]['Heat']['ON']=1
elif(U<0):
U=0
sysData[M]['Heat']['target']=U
sysData[M]['Heat']['ON']=0
else:
sysData[M]['Heat']['target']=U
sysData[M]['Heat']['ON']=1
sysData[M]['Thermostat']['last']=sysData[M]['Thermostat']['target']
SetOutput(M,'Heat')
time.sleep(dt)
if (sysData[M][item]['ON']==1 and sysDevices[M][item]['threadCount']==currentThread):
sysDevices[M][item]['thread']=Thread(target = Thermostat, args=(M,item))
sysDevices[M][item]['thread'].setDaemon(True)
sysDevices[M][item]['thread'].start();
else:
sysData[M]['Heat']['ON']=0
sysData[M]['Heat']['target']=0
SetOutput(M,'Heat')
@application.route("/Direction/<item>/<M>",methods=['POST'])
def direction(M,item):
#Flips direction of a pump.
global sysData
M=str(M)
if (M=="0"):
M=sysItems['UIDevice']
sysData[M][item]['target']=-1.0*sysData[M][item]['target']
if (sysData[M]['OD']['ON']==1):
sysData[M][item]['direction']=-1.0*sysData[M][item]['direction']
return ('', 204)
def AS7341Read(M,Gain,ISteps,reset):
#Responsible for reading data from the spectrometer.
global sysItems
global sysData
reset=int(reset)
ISteps=int(ISteps)
if ISteps>255:
ISteps=255 #255 steps is approx 0.71 seconds.
elif (ISteps<0):
ISteps=0
if Gain>10:
Gain=10 #512x
elif (Gain<0):
Gain=0 #0.5x
I2CCom(M,'AS7341',0,8,int(0xA9),int(0x04),0) #This sets us into BANK mode 0, for accesing registers 0x80+. The 4 means we have WTIMEx16
if (reset==1):
I2CCom(M,'AS7341',0,8,int(0x80),int(0x00),0) #Turns power down
time.sleep(0.01)
I2CCom(M,'AS7341',0,8,int(0x80),int(0x01),0) #Turns power on with spectral measurement disabled
else:
I2CCom(M,'AS7341',0,8,int(0x80),int(0x01),0) #Turns power on with spectral measurement disabled
I2CCom(M,'AS7341',0,8,int(0xAF),int(0x10),0) #Tells it we are going to now write SMUX configuration to RAM
#I2CCom(M,'AS7341',0,100,int(0x00),int(0x00),0) #Forces AS7341SMUX to run since length is 100.
AS7341SMUX(M,'AS7341',0,0)
I2CCom(M,'AS7341',0,8,int(0x80),int(0x11),0) #Runs SMUX command (i.e. cofigures SMUX with data from ram)
time.sleep(0.001)
I2CCom(M,'AS7341',0,8,int(0x81),ISteps,0) #Sets number of integration steps of length 2.78ms Max ISteps is 255
I2CCom(M,'AS7341',0,8,int(0x83),0xFF,0) #Sets maxinum wait time of 0.7mS (multiplex by 16 due to WLONG)
I2CCom(M,'AS7341',0,8,int(0xAA),Gain,0) #Sets gain on ADCs. Maximum value of Gain is 10 and can take values from 0 to 10.
#I2CCom(M,'AS7341',0,8,int(0xA9),int(0x14),0) #This sets us into BANK mode 1, for accessing 0x60 to 0x74. The 4 means we have WTIMEx16
#I2CCom(M,'AS7341',0,8,int(0x70),int(0x00),0) #Sets integration mode SPM (normal mode)
#Above is default of 0x70!
I2CCom(M,'AS7341',0,8,int(0x80),int(0x0B),0) #Starts spectral measurement, with WEN (wait between measurements feature) enabled.
time.sleep((ISteps+1)*0.0028 + 0.2) #Wait whilst integration is done and results are processed.
ASTATUS=int(I2CCom(M,'AS7341',1,8,0x94,0x00,0)) #Get measurement status, including saturation details.
C0_L=int(I2CCom(M,'AS7341',1,8,0x95,0x00,0))
C0_H=int(I2CCom(M,'AS7341',1,8,0x96,0x00,0))
C1_L=int(I2CCom(M,'AS7341',1,8,0x97,0x00,0))
C1_H=int(I2CCom(M,'AS7341',1,8,0x98,0x00,0))
C2_L=int(I2CCom(M,'AS7341',1,8,0x99,0x00,0))
C2_H=int(I2CCom(M,'AS7341',1,8,0x9A,0x00,0))
C3_L=int(I2CCom(M,'AS7341',1,8,0x9B,0x00,0))
C3_H=int(I2CCom(M,'AS7341',1,8,0x9C,0x00,0))
C4_L=int(I2CCom(M,'AS7341',1,8,0x9D,0x00,0))
C4_H=int(I2CCom(M,'AS7341',1,8,0x9E,0x00,0))
C5_L=int(I2CCom(M,'AS7341',1,8,0x9F,0x00,0))
C5_H=int(I2CCom(M,'AS7341',1,8,0xA0,0x00,0))
I2CCom(M,'AS7341',0,8,int(0x80),int(0x01),0) #Stops spectral measurement, leaves power on.
#Status2=int(I2CCom(M,'AS7341',1,8,0xA3,0x00,0)) #Reads system status at end of spectral measursement.
#print(str(ASTATUS))
#print(str(Status2))
sysData[M]['AS7341']['current']['ADC0']=int(bin(C0_H)[2:].zfill(8)+bin(C0_L)[2:].zfill(8),2)
sysData[M]['AS7341']['current']['ADC1']=int(bin(C1_H)[2:].zfill(8)+bin(C1_L)[2:].zfill(8),2)
sysData[M]['AS7341']['current']['ADC2']=int(bin(C2_H)[2:].zfill(8)+bin(C2_L)[2:].zfill(8),2)
sysData[M]['AS7341']['current']['ADC3']=int(bin(C3_H)[2:].zfill(8)+bin(C3_L)[2:].zfill(8),2)
sysData[M]['AS7341']['current']['ADC4']=int(bin(C4_H)[2:].zfill(8)+bin(C4_L)[2:].zfill(8),2)
sysData[M]['AS7341']['current']['ADC5']=int(bin(C5_H)[2:].zfill(8)+bin(C5_L)[2:].zfill(8),2)
if (sysData[M]['AS7341']['current']['ADC0']==65535 or sysData[M]['AS7341']['current']['ADC1']==65535 or sysData[M]['AS7341']['current']['ADC2']==65535 or sysData[M]['AS7341']['current']['ADC3']==65535 or sysData[M]['AS7341']['current']['ADC4']==65535 or sysData[M]['AS7341']['current']['ADC5']==65535 ):
print(str(datetime.now()) + ' Spectrometer measurement was saturated on device ' + str(M)) #Not sure if this saturation check above actually works correctly...
return 0
def AS7341SMUX(M,device,data1,data2):
#Sets up the ADC multiplexer on the spectrometer, this is responsible for connecting photodiodes to amplifier/adc circuits within the device.
#The spectrometer has only got 6 ADCs but >6 photodiodes channels, hence you need to select a subset of photodiodes to measure with each shot. The relative gain does change slightly (1-2%) between ADCs.
global sysItems
global sysData
global sysDevices
M=str(M)
Addresses=['0x00','0x01','0x02','0x03','0x04','0x05','0x06','0x07','0x08','0x0A','0x0B','0x0C','0x0D','0x0E','0x0F','0x10','0x11','0x12']
for a in Addresses:
A=sysItems['AS7341'][a]['A']
B=sysItems['AS7341'][a]['B']
if (A!='U'):
As=sysData[M]['AS7341']['channels'][A]
else:
As=0
if (B!='U'):
Bs=sysData[M]['AS7341']['channels'][B]
else:
Bs=0
Ab=str(bin(As))[2:].zfill(4)
Bb=str(bin(Bs))[2:].zfill(4)
C=Ab+Bb
#time.sleep(0.001) #Added this to remove errors where beaglebone crashed!
I2CCom(M,'AS7341',0,8,int(a,16),int(C,2),0) #Tells it we are going to now write SMUX configuration to RAM
#sysDevices[M][device]['device'].write8(int(a,16),int(C,2))
@application.route("/GetSpectrum/<Gain>/<M>",methods=['POST'])
def GetSpectrum(M,Gain):
#Measures entire spectrum, i.e. every different photodiode, which requires 2 measurement shots.
Gain=int(Gain[1:])
global sysData
global sysItems
M=str(M)
if (M=="0"):
M=sysItems['UIDevice']
out=GetLight(M,['nm410','nm440','nm470','nm510','nm550','nm583'],Gain,255)
out2=GetLight(M,['nm620', 'nm670','CLEAR','NIR','DARK'],Gain,255)
sysData[M]['AS7341']['spectrum']['nm410']=out[0]
sysData[M]['AS7341']['spectrum']['nm440']=out[1]
sysData[M]['AS7341']['spectrum']['nm470']=out[2]
sysData[M]['AS7341']['spectrum']['nm510']=out[3]
sysData[M]['AS7341']['spectrum']['nm550']=out[4]
sysData[M]['AS7341']['spectrum']['nm583']=out[5]
sysData[M]['AS7341']['spectrum']['nm620']=out2[0]
sysData[M]['AS7341']['spectrum']['nm670']=out2[1]
sysData[M]['AS7341']['spectrum']['CLEAR']=out2[2]
sysData[M]['AS7341']['spectrum']['NIR']=out2[3]
return ('', 204)
def GetLight(M,wavelengths,Gain,ISteps):
#Runs spectrometer measurement and puts data into appropriate structure.
global sysData
M=str(M)
channels=['nm410','nm440','nm470','nm510','nm550','nm583','nm620', 'nm670','CLEAR','NIR','DARK','ExtGPIO', 'ExtINT' , 'FLICKER']
for channel in channels:
sysData[M]['AS7341']['channels'][channel]=0 #First we set all measurement ADC indexes to zero.
index=1;
for wavelength in wavelengths:
if wavelength != "OFF":