-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1141 lines (1102 loc) · 51.7 KB
/
main.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
#!/usr/bin/python
from __future__ import division, print_function
import ms5837 # From https://github.com/bluerobotics/ms5837-python
import rsc # From https://github.com/tin-/ascp
import hsc
import numpy as np
import math
import sys
import os
import time
import threading
import signal
from timeit import default_timer as timer
from common import *
#sudo apt-get install python-smbus python-spidev python-pyqtgraph
# or
#sudo pip install smbus
#sudo pip install spidev
#sudo pip install pyqtgraph
# if it did not work and/or try also with python3 and pip3.
# Raspberry Pi 4 configuration (GPIO that will be used : 0 for PWM expiration
# valve, 1, 2, 3, 14, 15 for SPI3 Honeywell RSC for flow (expiration), 4, 5
# for I2C3 Honeywell HSC (expiration)[, Bar02 (room), 6 for digital
# output inspiration valve], 7, 8, 9, 10, 11 for SPI0 Honeywell RSC for flow
# (air), 12, 13 for PWM O2 and air valves, 16 for
# digital input UP button, 17 for digital input DOWN button, 18, 19, 20, 21, 27
# for SPI6 Honeywell RSC for flow (O2), 22, 23 for I2C6 Honeywell HSC
# (inspiration)[, Bar02 (inspiration), RTC clock], 24 for digital input
# SELECT button, 25 for POWER button, 26 for PWM buzzer)
#sudo nano /boot/config.txt
# Add/modify in /boot/config.txt (SPI6 might appear as 4, check /dev...)
#enable_uart=0
#dtparam=i2c_arm=on
#dtoverlay=i2c1,pins_44_45
#dtoverlay=i2c-gpio,bus=3,i2c_gpio_delay_us=1,i2c_gpio_sda=4,i2c_gpio_scl=5
#dtoverlay=i2c-gpio,bus=6,i2c_gpio_delay_us=1,i2c_gpio_sda=22,i2c_gpio_scl=23
#dtparam=spi=on
#dtoverlay=spi3-2cs,cs0_pin=14,cs1_pin=15
#dtoverlay=spi6-2cs
#dtoverlay=gpio-shutdown,gpio_pin=25,active_low=1,gpio_pull=up
# Disable touch screen sleep (see https://www.raspberrypi.org/forums/viewtopic.php?t=255163)
#sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
#@xset s off
#@xset -dpms
# Then reboot and
#sudo -E python main.py & python gui.py
# Parameters
###############################################################################
# User parameters
mode = 0 # 0 : ventilator, 1 : ventilator in assistance mode, 2 : only O2:Air mix
flow_control = 40 # In L/min, >= flow_control_max means no limit...
O2_percent = 50 # In %, 0 means no pure O2 will be added to air, 100 means no air, ignored if flow_control >= flow_control_max...
ramp = 10 # In % of the inspiration time, to increase progressively flow and avoid pressure peak just due to strong and fast flow...
Ppeak = 30 # In mbar (= approx. cmH2O)
PEEP = 5 # In mbar (= approx. cmH2O)
respi_rate = 20 # In breaths/min
inspi_percent = 33 # Inspiration time in % of cycle time
# User advanced parameters
PEEP_dec_rate = 100 # In %, to limit the maximum expiration flow before reaching the PEEP
PEEP_tuning = 100 # In %, to tune the expiration flow when maintaining the PEEP
Fl_PEEP = 100 # In % of flow_control, to help detecting inspiration after reaching the PEEP
O2_PEEP = 100 # In % of O2_percent, to help detecting inspiration after reaching the PEEP
PEEP_inspi_detection_delta = 2.5 # In mbar (= approx. cmH2O)
vol_inspi_detection_delta = 15 # In ml
inspi_detection_delta_duration = 250 # In ms
flow_thresh = 4 # To avoid noise in volume computation, in L/min
# Advanced parameters
mode_step = 1
mode_min = 0
mode_max = 2
flow_control_step = 2
flow_control_min = 0
flow_control_max = 300
O2_percent_step = 1
O2_percent_min = 0
O2_percent_max = 100
ramp_step = 1
ramp_min = 0
ramp_max = 100
Ppeak_step = 1
Ppeak_min = 0
Ppeak_max = 150
PEEP_step = 1
PEEP_min = 0
PEEP_max = 150
respi_rate_step = 1
respi_rate_min = 1
respi_rate_max = 100
inspi_percent_step = 1
inspi_percent_min = 0
inspi_percent_max = 100
PEEP_dec_rate_step = 2
PEEP_dec_rate_min = 1
PEEP_dec_rate_max = 100
PEEP_tuning_step = 2
PEEP_tuning_min = 1
PEEP_tuning_max = 100
Fl_PEEP_step = 5
Fl_PEEP_min = 0
Fl_PEEP_max = 1000
O2_PEEP_step = 5
O2_PEEP_min = 0
O2_PEEP_max = 1000
PEEP_inspi_detection_delta_step = 0.1
PEEP_inspi_detection_delta_min = 0
PEEP_inspi_detection_delta_max = 50
vol_inspi_detection_delta_step = 1
vol_inspi_detection_delta_min = 0
vol_inspi_detection_delta_max = 100
inspi_detection_delta_duration_step = 10
inspi_detection_delta_duration_min = 0
inspi_detection_delta_duration_max = 1000
flow_thresh_step = 0.25
flow_thresh_min = 0
flow_thresh_max = 50
enable_alarms = True
enable_buzzer = True
enable_p_ms5837 = False
enable_p0_ms5837 = False
enable_p_inspi_hsc = True
enable_p_expi_hsc = True
enable_air_rsc = True
enable_expi_rsc = True
enable_O2_rsc = True
rho0_air = 1.292 # In kg/m3 at 0 C
rho0_expi = 1.292 # In kg/m3 at 0 C
#rho0_O2 = 1.428 # In kg/m3 at 0 C
rho0_O2 = 1.292 # In kg/m3 at 0 C
#R1_air = 0.019100/2.0 # In m
R1_air = 0.009000/2.0 # In m
#R2_air = 0.011651/2.0 # In m
R2_air = 0.006500/2.0 # In m
R1_expi = 0.019100/2.0 # In m
#R1_expi = 0.009000/2.0 # In m
#R2_expi = 0.011651/2.0 # In m
R2_expi = 0.010100/2.0 # In m
#R2_expi = 0.006500/2.0 # In m
#R2_expi = 0.008000/2.0 # In m
#R1_O2 = 0.019100/2.0 # In m
R1_O2 = 0.009000/2.0 # In m
#R2_O2 = 0.011651/2.0 # In m
R2_O2 = 0.006500/2.0 # In m
rho_air = rho0_air
rho_expi = rho0_expi
rho_O2 = rho0_O2
A1_air = math.pi*R1_air**2 # In m2
A2_air = math.pi*R2_air**2 # In m2
A1_expi = math.pi*R1_expi**2 # In m2
A2_expi = math.pi*R2_expi**2 # In m2
A1_O2 = math.pi*R1_O2**2 # In m2
A2_O2 = math.pi*R2_O2**2 # In m2
speed_rsc = 175 # In SPS
delay_rsc = 0.010 # In s
coef_filter_rsc = 0.95
nb_count_auto_zero_filter_rsc = 100 # 100
nb_count_offset_filter_rsc = 100 # 100
valves_pwm_freq = 1200 # In Hz
valves_init = 50
valves_closed = 10 # Should be <= 99
valves_delay = 0.020 # In s
P_err = 50 # In mbar (= approx. cmH2O)
P_absolute_min = -10 # In mbar (= approx. cmH2O)
P_absolute_max = 159 # In mbar (= approx. cmH2O)
coef_offset_filter_flow = 0.99
coef_filter_flow = 0.0
positive_filter_flow = True
flow_control_PEEP = flow_control*Fl_PEEP*0.01
flow_control_pressure_excess = flow_control
flow_control_PEEP_pressure_excess = flow_control_PEEP
err_pressure_PEEP_thresh = 0.5 # In mbar (= approx. cmH2O)
coef_PEEP_pressure_control_valve_expi = 2.0
coef_PEEP_flow_control_valve_expi = 0.0
coef_PEEP_pressure_control_valve_air = 0.0
coef_PEEP_pressure_control_valve_O2 = 0.0
coef_PEEP_flow_control_valve_air = 2.0
coef_PEEP_flow_control_valve_O2 = 2.0
coef_pressure_excess = 8.0
coef_flow_control_valve_air = 2.0
coef_flow_control_valve_O2 = 2.0
debug = False
###############################################################################
alarms = 0
os.system('pigpiod -x 0x0FFFFFFF') # To be able to use GPIO 0 and 1 also...
time.sleep(0.2)
import pigpio
pi = pigpio.pi()
if not pi.connected:
print('Unable to connect to pigpio')
exit(1)
print('pigpio connected')
# Software PWM init for buzzer
if enable_buzzer:
buz_pin = 26
pi.set_mode(buz_pin, pigpio.OUTPUT)
pi.set_PWM_frequency(buz_pin, 4000)
pi.set_PWM_dutycycle(buz_pin, 128) # Startup beep...
# Other PWM init
valve_air_pin = 12
if (flow_control < flow_control_max): valve_air_val_max = valves_init # Min > 0 to not always disable proportional valves control...
else: valve_air_val_max = 100
valve_air_val_max_closed = valve_air_val_max
valve_air_val_max_PEEP = valves_init
valve_air_val = 0
valve_air_val = min(100, max(0, valve_air_val))
valve_O2_pin = 13
if (flow_control < flow_control_max): valve_O2_val_max = valves_init # Min > 0 to not always disable proportional valves control...
else: valve_O2_val_max = 100
valve_O2_val_max_closed = valve_O2_val_max
valve_O2_val_max_PEEP = valves_init
valve_O2_val = 0
valve_O2_val = min(100, max(0, valve_O2_val))
pi.set_mode(valve_air_pin, pigpio.OUTPUT)
pi.set_PWM_frequency(valve_air_pin, valves_pwm_freq)
pi.set_PWM_dutycycle(valve_air_pin, int(min(255, max(0, 255*valve_air_val/100))))
pi.set_mode(valve_O2_pin, pigpio.OUTPUT)
pi.set_PWM_frequency(valve_O2_pin, valves_pwm_freq)
pi.set_PWM_dutycycle(valve_O2_pin, int(min(255, max(0, 255*valve_O2_val/100))))
# Digital outputs (valves)
valve_expi_pin = 0
valve_expi_val_max_PEEP = PEEP_dec_rate
valve_expi_val = 0
valve_expi_val = min(100, max(0, valve_expi_val))
pi.set_mode(valve_expi_pin, pigpio.OUTPUT)
pi.set_PWM_frequency(valve_expi_pin, valves_pwm_freq)
pi.set_PWM_dutycycle(valve_expi_pin, int(min(255, max(0, 255-255*valve_expi_val/100))))
# Digital inputs (buttons)
select = -1 # Index of the selected parameter that should be changed by up/down buttons
parameters = ['mode', 'flow_control', 'O2_percent', 'ramp', 'Ppeak', 'PEEP', 'respi_rate', 'inspi_percent',
'PEEP_dec_rate', 'PEEP_tuning', 'Fl_PEEP', 'O2_PEEP', 'PEEP_inspi_detection_delta', 'vol_inspi_detection_delta', 'inspi_detection_delta_duration', 'flow_thresh']
select_button_pin = 24
pi.set_mode(select_button_pin, pigpio.INPUT)
pi.set_pull_up_down(select_button_pin, pigpio.PUD_UP)
select_button_val = pi.read(select_button_pin)
select_button_val_prev = select_button_val
up_button_pin = 16
pi.set_mode(up_button_pin, pigpio.INPUT)
pi.set_pull_up_down(up_button_pin, pigpio.PUD_UP)
up_button_val = pi.read(up_button_pin)
up_button_val_prev = up_button_val
down_button_pin = 17
pi.set_mode(down_button_pin, pigpio.INPUT)
pi.set_pull_up_down(down_button_pin, pigpio.PUD_UP)
down_button_val = pi.read(down_button_pin)
down_button_val_prev = down_button_val
# Bar02, HSC
if enable_p_ms5837:
p_ms5837 = ms5837.MS5837_02BA(bus = 6)
try:
if not p_ms5837.init():
print('P sensor could not be initialized')
time.sleep(0.1)
if not p_ms5837.init():
print('P sensor could not be initialized')
exit(1)
except:
print('P sensor could not be initialized')
time.sleep(0.1)
if not p_ms5837.init():
print('P sensor could not be initialized')
exit(1)
if enable_p0_ms5837:
p0_ms5837 = ms5837.MS5837_02BA(bus = 3)
try:
if not p0_ms5837.init():
print('P0 sensor could not be initialized')
time.sleep(0.1)
if not p0_ms5837.init():
print('P0 sensor could not be initialized')
exit(1)
except:
print('P0 sensor could not be initialized')
time.sleep(0.1)
if not p0_ms5837.init():
print('P0 sensor could not be initialized')
exit(1)
if enable_p_inspi_hsc:
try:
p_inspi_hsc = hsc.HHSC(bus = 6, addr = 0x48, min_pressure = -160.0, max_pressure = 160.0, unit = 'mbar', transfer = 'A')
except:
print('HSC I sensor could not be initialized')
time.sleep(0.1)
try:
p_inspi_hsc = hsc.HHSC(bus = 6, addr = 0x48, min_pressure = -160.0, max_pressure = 160.0, unit = 'mbar', transfer = 'A')
except:
print('HSC I sensor could not be initialized')
exit(1)
if enable_p_expi_hsc:
try:
p_expi_hsc = hsc.HHSC(bus = 3, addr = 0x48, min_pressure = -160.0, max_pressure = 160.0, unit = 'mbar', transfer = 'A')
except:
print('HSC E sensor could not be initialized')
time.sleep(0.1)
try:
p_expi_hsc = hsc.HHSC(bus = 3, addr = 0x48, min_pressure = -160.0, max_pressure = 160.0, unit = 'mbar', transfer = 'A')
except:
print('HSC E sensor could not be initialized')
exit(1)
# The very first pressure measurements might be wrong...
i = 0
while (i < 5):
if enable_p_ms5837:
if not p_ms5837.read(ms5837.OSR_256):
print('P sensor read failed!')
time.sleep(0.1)
if enable_p0_ms5837:
if not p0_ms5837.read(ms5837.OSR_256):
print('P0 sensor read failed!')
time.sleep(0.1)
if enable_p_inspi_hsc:
try:
p_diff_inspi_hsc, temp_inspi_hsc = p_inspi_hsc.read()
except:
print('HSC I sensor read failed!')
time.sleep(0.1)
if enable_p_expi_hsc:
try:
p_diff_expi_hsc, temp_expi_hsc = p_expi_hsc.read()
except:
print('HSC E sensor read failed!')
time.sleep(0.1)
i = i+1
# Honeywell RSC
pressure_air, temperature_air, flow_air, flow_filtered_air, vol_air, pressure_offset_air, flow_offset_air = 0, 25, 0, 0, 0, 0, 0
pressure_expi, temperature_expi, flow_expi, flow_filtered_expi, vol_expi, pressure_offset_expi, flow_offset_expi = 0, 25, 0, 0, 0, 0, 0
pressure_O2, temperature_O2, flow_O2, flow_filtered_O2, vol_O2, pressure_offset_O2, flow_offset_O2 = 0, 25, 0, 0, 0, 0, 0
if enable_air_rsc:
flow_air_rsc = rsc.HRSC(spi_bus = 0)
flow_air_rsc.sensor_info()
flow_air_rsc.reset()
if enable_expi_rsc:
flow_expi_rsc = rsc.HRSC(spi_bus = 3)
flow_expi_rsc.sensor_info()
flow_expi_rsc.reset()
if enable_O2_rsc:
flow_O2_rsc = rsc.HRSC(spi_bus = 4)
flow_O2_rsc.sensor_info()
flow_O2_rsc.reset()
time.sleep(0.005)
if enable_air_rsc:
flow_air_rsc.adc_configure()
flow_air_rsc.set_speed(speed_rsc)
if enable_expi_rsc:
flow_expi_rsc.adc_configure()
flow_expi_rsc.set_speed(speed_rsc)
if enable_O2_rsc:
flow_O2_rsc.adc_configure()
flow_O2_rsc.set_speed(speed_rsc)
time.sleep(0.005)
if enable_air_rsc:
flow_air_rsc.pressure_request()
if enable_expi_rsc:
flow_expi_rsc.pressure_request()
if enable_O2_rsc:
flow_O2_rsc.pressure_request()
time.sleep(delay_rsc)
if enable_air_rsc:
raw_pressure_air = flow_air_rsc.pressure_reply()
flow_air_rsc.temperature_request()
if enable_expi_rsc:
raw_pressure_expi = flow_expi_rsc.pressure_reply()
flow_expi_rsc.temperature_request()
if enable_O2_rsc:
raw_pressure_O2 = flow_O2_rsc.pressure_reply()
flow_O2_rsc.temperature_request()
time.sleep(delay_rsc)
if enable_air_rsc:
raw_temperature_air = flow_air_rsc.temperature_reply()
raw_zero_pressure_air = raw_pressure_air
raw_zero_temperature_air = raw_temperature_air
pressure_air, temperature_air = flow_air_rsc.comp_readings(raw_pressure_air, raw_temperature_air)
pressure_air = flow_air_rsc.conv_pressure_to_mbar(pressure_air)
pressure_offset_air = pressure_air
if enable_expi_rsc:
raw_temperature_expi = flow_expi_rsc.temperature_reply()
raw_zero_pressure_expi = raw_pressure_expi
raw_zero_temperature_expi = raw_temperature_expi
pressure_expi, temperature_expi = flow_expi_rsc.comp_readings(raw_pressure_expi, raw_temperature_expi)
pressure_expi = flow_expi_rsc.conv_pressure_to_mbar(pressure_expi)
pressure_offset_expi = pressure_expi
if enable_O2_rsc:
raw_temperature_O2 = flow_O2_rsc.temperature_reply()
raw_zero_pressure_O2 = raw_pressure_O2
raw_zero_temperature_O2 = raw_temperature_O2
pressure_O2, temperature_O2 = flow_O2_rsc.comp_readings(raw_pressure_O2, raw_temperature_O2)
pressure_O2 = flow_O2_rsc.conv_pressure_to_mbar(pressure_O2)
pressure_offset_O2 = pressure_O2
# Auto-zero filter proposed by the documentation in the beginning (with valves closed)...
i = 0
while (i < nb_count_auto_zero_filter_rsc):
if enable_air_rsc:
flow_air_rsc.pressure_request()
if enable_expi_rsc:
flow_expi_rsc.pressure_request()
if enable_O2_rsc:
flow_O2_rsc.pressure_request()
time.sleep(delay_rsc)
if enable_air_rsc:
raw_pressure_air = flow_air_rsc.pressure_reply()
flow_air_rsc.temperature_request()
if enable_expi_rsc:
raw_pressure_expi = flow_expi_rsc.pressure_reply()
flow_expi_rsc.temperature_request()
if enable_O2_rsc:
raw_pressure_O2 = flow_O2_rsc.pressure_reply()
flow_O2_rsc.temperature_request()
time.sleep(delay_rsc)
if enable_air_rsc:
raw_temperature_air = flow_air_rsc.temperature_reply()
raw_zero_pressure_air = (1-coef_filter_rsc)*raw_pressure_air+coef_filter_rsc*raw_zero_pressure_air
raw_zero_temperature_air = (1-coef_filter_rsc)*raw_temperature_air+coef_filter_rsc*raw_zero_temperature_air
if enable_expi_rsc:
raw_temperature_expi = flow_expi_rsc.temperature_reply()
raw_zero_pressure_expi = (1-coef_filter_rsc)*raw_pressure_expi+coef_filter_rsc*raw_zero_pressure_expi
raw_zero_temperature_expi = (1-coef_filter_rsc)*raw_temperature_expi+coef_filter_rsc*raw_zero_temperature_expi
if enable_O2_rsc:
raw_temperature_O2 = flow_O2_rsc.temperature_reply()
raw_zero_pressure_O2 = (1-coef_filter_rsc)*raw_pressure_O2+coef_filter_rsc*raw_zero_pressure_O2
raw_zero_temperature_O2 = (1-coef_filter_rsc)*raw_temperature_O2+coef_filter_rsc*raw_zero_temperature_O2
i = i+1
# Store auto-zero info...
if (nb_count_auto_zero_filter_rsc > 0):
if enable_air_rsc:
flow_air_rsc.comp_auto_zero(raw_zero_pressure_air, raw_zero_temperature_air)
if enable_expi_rsc:
flow_expi_rsc.comp_auto_zero(raw_zero_pressure_expi, raw_zero_temperature_expi)
if enable_O2_rsc:
flow_O2_rsc.comp_auto_zero(raw_zero_pressure_O2, raw_zero_temperature_O2)
# Additional filter to estimate the offset in the beginning (with valves closed)...
i = 0
while (i < nb_count_offset_filter_rsc):
if enable_air_rsc:
flow_air_rsc.pressure_request()
if enable_expi_rsc:
flow_expi_rsc.pressure_request()
if enable_O2_rsc:
flow_O2_rsc.pressure_request()
time.sleep(delay_rsc)
if enable_air_rsc:
raw_pressure_air = flow_air_rsc.pressure_reply()
flow_air_rsc.temperature_request()
if enable_expi_rsc:
raw_pressure_expi = flow_expi_rsc.pressure_reply()
flow_expi_rsc.temperature_request()
if enable_O2_rsc:
raw_pressure_O2 = flow_O2_rsc.pressure_reply()
flow_O2_rsc.temperature_request()
time.sleep(delay_rsc)
if enable_air_rsc:
raw_temperature_air = flow_air_rsc.temperature_reply()
pressure_air, temperature_air = flow_air_rsc.comp_readings(raw_pressure_air, raw_temperature_air)
pressure_air = flow_air_rsc.conv_pressure_to_mbar(pressure_air)
pressure_offset_air = (1-coef_filter_rsc)*pressure_air+coef_filter_rsc*pressure_offset_air
if enable_expi_rsc:
raw_temperature_expi = flow_expi_rsc.temperature_reply()
pressure_expi, temperature_expi = flow_expi_rsc.comp_readings(raw_pressure_expi, raw_temperature_expi)
pressure_expi = flow_expi_rsc.conv_pressure_to_mbar(pressure_expi)
pressure_offset_expi = (1-coef_filter_rsc)*pressure_expi+coef_filter_rsc*pressure_offset_expi
if enable_O2_rsc:
raw_temperature_O2 = flow_O2_rsc.temperature_reply()
pressure_O2, temperature_O2 = flow_O2_rsc.comp_readings(raw_pressure_O2, raw_temperature_O2)
pressure_O2 = flow_O2_rsc.conv_pressure_to_mbar(pressure_O2)
pressure_offset_O2 = (1-coef_filter_rsc)*pressure_O2+coef_filter_rsc*pressure_offset_O2
i = i+1
if (nb_count_offset_filter_rsc <= 0):
pressure_offset_air = 0
pressure_offset_expi = 0
pressure_offset_O2 = 0
respi_rate_converted = respi_rate*(1.0/60.0) # In breaths/s
cycle_duration = 1.0/respi_rate_converted
inspi_duration = inspi_percent*0.01*cycle_duration
expi_duration = cycle_duration-inspi_duration
t = timer()
t_prev = t
dt = t-t_prev
t0 = t
t_cycle_start = t
p0 = 1000
p = 1000
p_e = 1000
if enable_p_ms5837: p = p_ms5837.pressure()
if enable_p_inspi_hsc: p = p0+p_diff_inspi_hsc
if enable_p_expi_hsc: p_e = p0+p_diff_expi_hsc
p_prev = p
p_cycle_start = p
p0 = p
if enable_p0_ms5837: p0 = p0_ms5837.pressure()
temperature = 25
temperature_e = 25
if enable_p_ms5837: temperature = p_ms5837.temperature()
if enable_p_inspi_hsc: temperature = temp_inspi_hsc
if enable_p_expi_hsc: temperature_e = temp_expi_hsc
temperature0 = temperature
if enable_p0_ms5837: temperature0 = p0_ms5837.temperature()
Ppeak_reached = False
PEEP_reached = False
inspi_end = False
force_new_cycle = False
t_hist = []
flow_hist = []
vol_hist = []
t_last_not_all_valves_closed = t0
t_all_valves_closed = t0
t_valve_air_closed = t0
t_valve_O2_closed = t0
t_valve_expi_closed = t0
t_alarms = t0
# Stop the startup beep
if enable_buzzer: pi.set_PWM_dutycycle(buz_pin, 0)
# File errors are not critical...
try:
file = open('data.csv', 'a')
file.write('t (in s);t0 (in s);p0 (in mbar);temperature0 (in C);p (in mbar);temperature (in C);p_e (in mbar);temperature_e (in C);alarms;select;'
'mode;flow_control (in L/min);O2_percent (in %);ramp (in %);Ppeak (in mbar);PEEP (in mbar);respi_rate (in breaths/min);inspi_percent (in %);'
'PEEP_dec_rate (in %);PEEP_tuning (in %);Fl_PEEP (in %);O2_PEEP (in %);PEEP_inspi_detection_delta (in mbar);vol_inspi_detection_delta (in ml);inspi_detection_delta_duration (in ms);flow_thresh (in L/min);'
'valve_air_val;valve_O2_val;valve_expi_val;'
'pressure_air (in mbar);pressure_expi (in mbar);pressure_O2 (in mbar);temperature_air (in C);temperature_expi (in C);temperature_O2 (in C);'
'flow_air (in L/min);flow_expi (in L/min);flow_O2 (in L/min);flow_filtered_air (in L/min);flow_filtered_expi (in L/min);flow_filtered_O2 (in L/min);vol_air (in L);vol_expi (in L);vol_O2 (in L);\n')
except:
if enable_alarms:
t_alarms = t
alarms = alarms | OS_ALARM
bExit = False
def signal_handler(sig, frame):
global bExit
#signal.signal(signal.SIGINT, signal.SIG_IGN) # To avoid interruption by other CTRL+C...
bExit = True
signal.signal(signal.SIGINT, signal_handler) # To be able to put actuators in a safe state when using CTRL+C
# Divisions by 0, NAN, INF, int divisions...?
count = 0
while (not bExit):
if (t-t_cycle_start < inspi_duration):
PEEP_reached = False
if ((p-p0 > Ppeak) or (Ppeak_reached == True)): # Should close valves to maintain Ppeak...
Ppeak_reached = True
valve_air_val = 0
valve_O2_val = 0
else:
if ((0.01*ramp*inspi_duration > 0) and (t-t_cycle_start <= 0.01*ramp*inspi_duration)): # Ramp to avoid pressure peak with strong flow...
valve_air_val = max(valve_air_val_max_PEEP, min(100, valve_air_val_max*float(t-t_cycle_start)/(0.01*ramp*inspi_duration))) # Min > 0 to not always disable proportional valves control...
valve_O2_val = max(valve_O2_val_max_PEEP, min(100, valve_O2_val_max*float(t-t_cycle_start)/(0.01*ramp*inspi_duration))) # Min > 0 to not always disable proportional valves control...
else:
valve_air_val = valve_air_val_max
valve_O2_val = valve_O2_val_max
valve_expi_val = 0
else:
Ppeak_reached = False
if ((p-p0 < PEEP) or (PEEP_reached == True)): # Should try to maintain PEEP...
if not PEEP_reached:
t_PEEP_reached = t
PEEP_reached = True
if (Fl_PEEP <= 0):
valve_air_val = 0
valve_O2_val = 0
valve_expi_val = 0
if ((mode == 1) and (t-t_PEEP_reached > inspi_detection_delta_duration*0.001) and (p-p0 < PEEP-PEEP_inspi_detection_delta)): # Attempt to detect inspiration in assist mode...
force_new_cycle = True
print('Inspiration detected at t=%f s (PI dlta)' % (t-t0))
else:
if ((mode == 1) and (t-t_PEEP_reached > inspi_detection_delta_duration*0.001) and (len(t_hist) > 0) and (vol_hist[-1]-vol_hist[0] > float(vol_inspi_detection_delta)/1000000.0)): # Attempt to detect inspiration in assist mode...
force_new_cycle = True
print('Inspiration detected at t=%f s (VI dlta during I dlta)' % (t-t0))
err_pressure_PEEP = (p-p0)-PEEP
err_flow_PEEP_air = flow_control_PEEP*max(0.0, 1.0-O2_percent*0.01*O2_PEEP*0.01)-flow_filtered_air*60000.0 # In L/min, with ideal flow input if there was no pressure limitation...
err_flow_PEEP_O2 = flow_control_PEEP*O2_percent*0.01*O2_PEEP*0.01-flow_filtered_O2*60000.0 # In L/min, with ideal flow input if there was no pressure limitation...
if ((coef_PEEP_flow_control_valve_expi != 0.0) or (err_pressure_PEEP > 0) or ((abs(err_pressure_PEEP) > err_pressure_PEEP_thresh) and (abs(err_flow_PEEP_air) < flow_thresh) and (abs(err_flow_PEEP_O2) < flow_thresh))): # and (err_flow_PEEP_air <= 0) and (err_flow_PEEP_O2 <= 0))):
valve_expi_val_max_PEEP = max(PEEP_tuning, min(PEEP_dec_rate, valve_expi_val_max_PEEP+coef_PEEP_pressure_control_valve_expi*dt*err_pressure_PEEP+coef_PEEP_flow_control_valve_expi*dt*(err_flow_PEEP_air+err_flow_PEEP_O2))) # Should open if pressure is too high or close a little if pressure is too low while the desired air and O2 flows have been reached...
valve_expi_val = valve_expi_val_max_PEEP
flow_control_PEEP_pressure_excess = max(0, min(flow_control_PEEP, flow_control_PEEP_pressure_excess-coef_pressure_excess*(flow_control_PEEP/float(flow_control_max))*dt*err_pressure_PEEP)) # To limit flow if pressure is too high...
err_flow_PEEP_air = flow_control_PEEP_pressure_excess*max(0.0, 1.0-O2_percent*0.01*O2_PEEP*0.01)-flow_filtered_air*60000.0 # In L/min
if ((err_flow_PEEP_air < 0) or (t-t_valve_air_closed > valves_delay)): # Avoid accumulating error when flow is low just due to valve delay...
valve_air_val_max_PEEP = max(1, min(100, valve_air_val_max_PEEP+coef_PEEP_flow_control_valve_air*dt*err_flow_PEEP_air-coef_PEEP_pressure_control_valve_air*(flow_control_PEEP/float(flow_control_max))*dt*err_pressure_PEEP)) # Min > 0 to not always disable proportional valves control...
valve_air_val = valve_air_val_max_PEEP
err_flow_PEEP_O2 = flow_control_PEEP_pressure_excess*O2_percent*0.01*O2_PEEP*0.01-flow_filtered_O2*60000.0 # In L/min
if ((err_flow_PEEP_O2 < 0) or (t-t_valve_O2_closed > valves_delay)): # Avoid accumulating error when flow is low just due to valve delay...
valve_O2_val_max_PEEP = max(1, min(100, valve_O2_val_max_PEEP+coef_PEEP_flow_control_valve_O2*dt*err_flow_PEEP_O2-coef_PEEP_pressure_control_valve_O2*(flow_control_PEEP/float(flow_control_max))*dt*err_pressure_PEEP)) # Min > 0 to not always disable proportional valves control...
valve_O2_val = valve_O2_val_max_PEEP
else:
valve_air_val = 0
valve_O2_val = 0
valve_expi_val = PEEP_dec_rate
if (mode == 2):
# Override to only make O2:Air mix...
# Balloon not handled...
if (p-p0 > Ppeak+P_err): # Allow some error since a control should be made later to limit the flow to stay below Ppeak...
valve_air_val = 0
valve_air_val_max = valves_init
valve_O2_val = 0
valve_O2_val_max = valves_init
else:
valve_air_val = valve_air_val_max
valve_O2_val = valve_O2_val_max
valve_expi_val = 100
# Pressure should never be too low or too high...
if (((p-p0 > Ppeak+P_err) or (p-p0 > P_absolute_max) or (p-p0 < P_absolute_min)) or \
((enable_p_expi_hsc) and ((p_e-p0 > Ppeak+P_err) or (p_e-p0 > P_absolute_max) or (p_e-p0 < P_absolute_min)))):
if enable_alarms:
t_alarms = t
alarms = alarms | PRESSURE_ALARM
valve_air_val = 0
valve_O2_val = 0
valve_expi_val = 100
if (mode != 2):
if ((inspi_duration-2*dt <= t-t_cycle_start) and (t-t_cycle_start <= inspi_duration) and (not Ppeak_reached)):
if enable_alarms:
t_alarms = t
alarms = alarms | PPEAK_ALARM
if ((inspi_duration+expi_duration-2*dt <= t-t_cycle_start) and (t-t_cycle_start <= inspi_duration+expi_duration) and (not PEEP_reached)):
if enable_alarms:
t_alarms = t
alarms = alarms | PEEP_ALARM
if (alarms != 0):
if (t-t_alarms > 5): # Clear alarms and stop buzzer after some time if no recent alarm
alarms = 0
if enable_buzzer: pi.set_PWM_dutycycle(buz_pin, 0)
else:
if enable_buzzer: pi.set_PWM_dutycycle(buz_pin, 128)
# Actuators
valve_air_val = min(100, max(0, valve_air_val))
if (valve_air_val <= valves_closed):
t_valve_air_closed = t
valve_air_val_max_closed = valve_air_val_max
valve_O2_val = min(100, max(0, valve_O2_val))
if (valve_O2_val <= valves_closed):
t_valve_O2_closed = t
valve_O2_val_max_closed = valve_O2_val_max
pi.set_PWM_dutycycle(valve_air_pin, int(min(255, max(0, 255*valve_air_val/100))))
pi.set_PWM_dutycycle(valve_O2_pin, int(min(255, max(0, 255*valve_O2_val/100))))
valve_expi_val = min(100, max(0, valve_expi_val))
if (valve_expi_val <= valves_closed): t_valve_expi_closed = t
pi.set_PWM_dutycycle(valve_expi_pin, int(min(255, max(0, 255-255*valve_expi_val/100))))
# Buttons to set parameters
select_button_val = pi.read(select_button_pin)
if (select_button_val != select_button_val_prev):
select_button_val_prev = select_button_val
if (select_button_val > 0):
select = select + 1
if (select >= len(parameters)): select = -1
if (select >= 0) and (select < len(parameters)):
up_button_val = pi.read(up_button_pin)
if (up_button_val != up_button_val_prev):
up_button_val_prev = up_button_val
if (up_button_val > 0):
param_id = 0
if (select == param_id):
mode = mode + mode_step
if (mode > mode_max): mode = mode_max
param_id = param_id+1
if (select == param_id):
flow_control = flow_control + flow_control_step
if (flow_control > flow_control_max): flow_control = flow_control_max
param_id = param_id+1
if (select == param_id):
O2_percent = O2_percent + O2_percent_step
if (O2_percent > O2_percent_max): O2_percent = O2_percent_max
param_id = param_id+1
if (select == param_id):
ramp = ramp + ramp_step
if (ramp > ramp_max): ramp = ramp_max
param_id = param_id+1
if (select == param_id):
Ppeak = Ppeak + Ppeak_step
if (Ppeak > Ppeak_max): Ppeak = Ppeak_max
param_id = param_id+1
if (select == param_id):
PEEP = PEEP + PEEP_step
if (PEEP > PEEP_max): PEEP = PEEP_max
param_id = param_id+1
if (select == param_id):
respi_rate = respi_rate + respi_rate_step
if (respi_rate > respi_rate_max): respi_rate = respi_rate_max
param_id = param_id+1
if (select == param_id):
inspi_percent = inspi_percent + inspi_percent_step
if (inspi_percent > inspi_percent_max): inspi_percent = inspi_percent_max
param_id = param_id+1
if (select == param_id):
PEEP_dec_rate = PEEP_dec_rate + PEEP_dec_rate_step
if (PEEP_dec_rate > PEEP_dec_rate_max): PEEP_dec_rate = PEEP_dec_rate_max
param_id = param_id+1
if (select == param_id):
PEEP_tuning = PEEP_tuning + PEEP_tuning_step
if (PEEP_tuning > PEEP_tuning_max): PEEP_tuning = PEEP_tuning_max
param_id = param_id+1
if (select == param_id):
Fl_PEEP = Fl_PEEP + Fl_PEEP_step
if (Fl_PEEP > Fl_PEEP_max): Fl_PEEP = Fl_PEEP_max
param_id = param_id+1
if (select == param_id):
O2_PEEP = O2_PEEP + O2_PEEP_step
if (O2_PEEP > O2_PEEP_max): O2_PEEP = O2_PEEP_max
param_id = param_id+1
if (select == param_id):
PEEP_inspi_detection_delta = PEEP_inspi_detection_delta + PEEP_inspi_detection_delta_step
if (PEEP_inspi_detection_delta > PEEP_inspi_detection_delta_max): PEEP_inspi_detection_delta = PEEP_inspi_detection_delta_max
param_id = param_id+1
if (select == param_id):
vol_inspi_detection_delta = vol_inspi_detection_delta + vol_inspi_detection_delta_step
if (vol_inspi_detection_delta > vol_inspi_detection_delta_max): vol_inspi_detection_delta = vol_inspi_detection_delta_max
param_id = param_id+1
if (select == param_id):
inspi_detection_delta_duration = inspi_detection_delta_duration + inspi_detection_delta_duration_step
if (inspi_detection_delta_duration > inspi_detection_delta_duration_max): inspi_detection_delta_duration = inspi_detection_delta_duration_max
param_id = param_id+1
if (select == param_id):
flow_thresh = flow_thresh + flow_thresh_step
if (flow_thresh > flow_thresh_max): flow_thresh = flow_thresh_max
param_id = param_id+1
down_button_val = pi.read(down_button_pin)
if (down_button_val != down_button_val_prev):
down_button_val_prev = down_button_val
if (down_button_val > 0):
param_id = 0
if (select == param_id):
mode = mode - mode_step
if (mode < mode_min): mode = mode_min
param_id = param_id+1
if (select == param_id):
flow_control = flow_control - flow_control_step
if (flow_control < flow_control_min): flow_control = flow_control_min
param_id = param_id+1
if (select == param_id):
O2_percent = O2_percent - O2_percent_step
if (O2_percent < O2_percent_min): O2_percent = O2_percent_min
param_id = param_id+1
if (select == param_id):
ramp = ramp - ramp_step
if (ramp < ramp_min): ramp = ramp_min
param_id = param_id+1
if (select == param_id):
Ppeak = Ppeak - Ppeak_step
if (Ppeak < Ppeak_min): Ppeak = Ppeak_min
param_id = param_id+1
if (select == param_id):
PEEP = PEEP - PEEP_step
if (PEEP < PEEP_min): PEEP = PEEP_min
param_id = param_id+1
if (select == param_id):
respi_rate = respi_rate - respi_rate_step
if (respi_rate < respi_rate_min): respi_rate = respi_rate_min
param_id = param_id+1
if (select == param_id):
inspi_percent = inspi_percent - inspi_percent_step
if (inspi_percent < inspi_percent_min): inspi_percent = inspi_percent_min
param_id = param_id+1
if (select == param_id):
PEEP_dec_rate = PEEP_dec_rate - PEEP_dec_rate_step
if (PEEP_dec_rate < PEEP_dec_rate_min): PEEP_dec_rate = PEEP_dec_rate_min
param_id = param_id+1
if (select == param_id):
PEEP_tuning = PEEP_tuning - PEEP_tuning_step
if (PEEP_tuning < PEEP_tuning_min): PEEP_tuning = PEEP_tuning_min
param_id = param_id+1
if (select == param_id):
Fl_PEEP = Fl_PEEP - Fl_PEEP_step
if (Fl_PEEP < Fl_PEEP_min): Fl_PEEP = Fl_PEEP_min
param_id = param_id+1
if (select == param_id):
O2_PEEP = O2_PEEP - O2_PEEP_step
if (O2_PEEP < O2_PEEP_min): O2_PEEP = O2_PEEP_min
param_id = param_id+1
if (select == param_id):
PEEP_inspi_detection_delta = PEEP_inspi_detection_delta - PEEP_inspi_detection_delta_step
if (PEEP_inspi_detection_delta < PEEP_inspi_detection_delta_min): PEEP_inspi_detection_delta = PEEP_inspi_detection_delta_min
param_id = param_id+1
if (select == param_id):
vol_inspi_detection_delta = vol_inspi_detection_delta - vol_inspi_detection_delta_step
if (vol_inspi_detection_delta < vol_inspi_detection_delta_min): vol_inspi_detection_delta = vol_inspi_detection_delta_min
param_id = param_id+1
if (select == param_id):
inspi_detection_delta_duration = inspi_detection_delta_duration - inspi_detection_delta_duration_step
if (inspi_detection_delta_duration < inspi_detection_delta_duration_min): inspi_detection_delta_duration = inspi_detection_delta_duration_min
param_id = param_id+1
if (select == param_id):
flow_thresh = flow_thresh - flow_thresh_step
if (flow_thresh < flow_thresh_min): flow_thresh = flow_thresh_min
param_id = param_id+1
# Sensors
if enable_air_rsc:
try:
flow_air_rsc.pressure_request()
except:
print('RSC A sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
flow_air_rsc.pressure_request()
if enable_expi_rsc:
try:
flow_expi_rsc.pressure_request()
except:
print('RSC E sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
flow_expi_rsc.pressure_request()
if enable_O2_rsc:
try:
flow_O2_rsc.pressure_request()
except:
print('RSC O sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
flow_O2_rsc.pressure_request()
time.sleep(max(0, delay_rsc-0.005))
if enable_p_ms5837:
try:
if not p_ms5837.read(ms5837.OSR_256):
print('P sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
if not p_ms5837.read(ms5837.OSR_256):
print('P sensor read failed!')
exit(1)
except:
print('P sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
if not p_ms5837.read(ms5837.OSR_256):
print('P sensor read failed!')
exit(1)
elif enable_p_inspi_hsc:
try:
p_diff_inspi_hsc, temp_inspi_hsc = p_inspi_hsc.read()
except:
print('HSC I sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
p_diff_inspi_hsc, temp_inspi_hsc = p_inspi_hsc.read()
time.sleep(0.005)
else:
time.sleep(0.005)
if enable_p_expi_hsc:
try:
p_diff_expi_hsc, temp_expi_hsc = p_expi_hsc.read()
except:
print('HSC E sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
p_diff_expi_hsc, temp_expi_hsc = p_expi_hsc.read()
if enable_air_rsc:
try:
raw_pressure_air = flow_air_rsc.pressure_reply()
except:
print('RSC A sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
raw_pressure_air = flow_air_rsc.pressure_reply()
if enable_expi_rsc:
try:
raw_pressure_expi = flow_expi_rsc.pressure_reply()
except:
print('RSC E sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
raw_pressure_expi = flow_expi_rsc.pressure_reply()
if enable_O2_rsc:
try:
raw_pressure_O2 = flow_O2_rsc.pressure_reply()
except:
print('RSC O sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
raw_pressure_O2 = flow_O2_rsc.pressure_reply()
if enable_air_rsc:
try:
flow_air_rsc.temperature_request()
except:
print('RSC A sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
flow_air_rsc.temperature_request()
if enable_expi_rsc:
try:
flow_expi_rsc.temperature_request()
except:
print('RSC E sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
flow_expi_rsc.temperature_request()
if enable_O2_rsc:
try:
flow_O2_rsc.temperature_request()
except:
print('RSC O sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
flow_O2_rsc.temperature_request()
time.sleep(delay_rsc)
if enable_air_rsc:
try:
raw_temperature_air = flow_air_rsc.temperature_reply()
except:
print('RSC A sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
raw_temperature_air = flow_air_rsc.temperature_reply()
if enable_expi_rsc:
try:
raw_temperature_expi = flow_expi_rsc.temperature_reply()
except:
print('RSC E sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
raw_temperature_expi = flow_expi_rsc.temperature_reply()
if enable_O2_rsc:
try:
raw_temperature_O2 = flow_O2_rsc.temperature_reply()
except:
print('RSC O sensor read failed!')
if enable_alarms:
t_alarms = t
alarms = alarms | HARDWARE_ALARM
time.sleep(0.1)
raw_temperature_O2 = flow_O2_rsc.temperature_reply()
if enable_air_rsc:
pressure_air, temperature_air = flow_air_rsc.comp_readings(raw_pressure_air, raw_temperature_air)
pressure_air = flow_air_rsc.conv_pressure_to_mbar(pressure_air)
pressure_air = pressure_air-pressure_offset_air
if (temperature_air > -273.15): rho_air = rho0_air*(273.15/(273.15+temperature_air)) # In kg/m3
vel_air = np.sign(pressure_air)*math.sqrt(2*(abs(pressure_air)*100.0)/(rho_air*((float(A1_air)/float(A2_air))**2-1)))
flow_air = A1_air*vel_air
if enable_expi_rsc:
pressure_expi, temperature_expi = flow_expi_rsc.comp_readings(raw_pressure_expi, raw_temperature_expi)
pressure_expi = flow_expi_rsc.conv_pressure_to_mbar(pressure_expi)
pressure_expi = pressure_expi-pressure_offset_expi
if (temperature_expi > -273.15): rho_expi = rho0_expi*(273.15/(273.15+temperature_expi)) # In kg/m3
vel_expi = np.sign(pressure_expi)*math.sqrt(2*(abs(pressure_expi)*100.0)/(rho_expi*((float(A1_expi)/float(A2_expi))**2-1)))
flow_expi = A1_expi*vel_expi
if enable_O2_rsc:
pressure_O2, temperature_O2 = flow_O2_rsc.comp_readings(raw_pressure_O2, raw_temperature_O2)
pressure_O2 = flow_O2_rsc.conv_pressure_to_mbar(pressure_O2)
pressure_O2 = pressure_O2-pressure_offset_O2
if (temperature_O2 > -273.15): rho_O2 = rho0_O2*(273.15/(273.15+temperature_O2)) # In kg/m3
vel_O2 = np.sign(pressure_O2)*math.sqrt(2*(abs(pressure_O2)*100.0)/(rho_O2*((float(A1_O2)/float(A2_O2))**2-1)))
flow_O2 = A1_O2*vel_O2
# Filters
# Offset...
#if ((valve_air_val <= 0) and (valve_O2_val <= 0) and (valve_expi_val <= 0)):
# t_all_valves_closed = t
# if (t_all_valves_closed-t_last_not_all_valves_closed > valves_delay):