-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlLoopIGHN.py
150 lines (128 loc) · 5.18 KB
/
ControlLoopIGHN.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
import serial
import time
import datetime
import igh
import ilm
import qweb
#### Read/Write/Log Functions ####
def readEverything(ctrl):
state = ''
state += 'ighn_temp_1k=' + str(ctrl.getOneKPotTemp())
state += ';ighn_temp_sorb=' + str(ctrl.getSorbTemp())
state += ';ighn_temp_mix=' + str(ctrl.getMixChTemp())
state += ';ighn_power_mix=' + str(ctrl.getMixChPower())
state += ';ighn_power_still=' + str(ctrl.getStillPower())
state += ';ighn_power_sorb=' + str(ctrl.getSorbPower())
state += ';ighn_pres_g1=' + str(ctrl.getG1())
state += ';ighn_pres_g2=' + str(ctrl.getG2())
state += ';ighn_pres_g3=' + str(ctrl.getG3())
state += ';ighn_pres_p1=' + str(ctrl.getP1())
state += ';ighn_pres_p2=' + str(ctrl.getP2())
state += ';ighn_nv=' + str(ctrl.getNV())
state += ';ighn_v6=' + str(ctrl.getV6())
state += ';ighn_v12a=' + str(ctrl.getV12A())
state += ';ighn_valves=' + str(ctrl.getStatus())
state += ';ighn_power_mix_range=' + str(ctrl.MixPowerRange)
ctrl.getMixChResistance() # not logging this?
return state
def readILM(ctrl):
ctrl.getHeliumLevel()
ctrl.getNitrogenLevel()
def log(igh_ctrl, ilm_ctrl):
response = qweb.getLoggableInfoForNow('igh')
sensors = str.split(response, '\n')
for sensor in sensors:
if sensor != '':
found = True
loggable_name=''
props = str.split(sensor, ';')
for prop in props:
keyvals = str.split(prop, '=')
if keyvals[0]=='loggable_name':
loggable_name = keyvals[1]
# IGH North
if loggable_name == 'ighn_temp_sorb':
val = igh_ctrl.SorbTemp
elif loggable_name == 'ighn_temp_1k':
val = igh_ctrl.OneKPotTemp
elif loggable_name == 'ighn_temp_mix':
val = igh_ctrl.MixChTemp
elif loggable_name == 'ighn_power_mix':
val = igh_ctrl.MixChPower
elif loggable_name == 'ighn_power_still':
val = igh_ctrl.StillPower
elif loggable_name == 'ighn_power_sorb':
val = igh_ctrl.SorbPower
elif loggable_name == 'ighn_pres_g1':
val = igh_ctrl.G1
elif loggable_name == 'ighn_pres_g2':
val = igh_ctrl.G2
elif loggable_name == 'ighn_pres_g3':
val = igh_ctrl.G3
elif loggable_name == 'ighn_pres_p1':
val = igh_ctrl.P1
elif loggable_name == 'ighn_pres_p2':
val = igh_ctrl.P2
elif loggable_name == 'ighn_nv':
val = igh_ctrl.NV
elif loggable_name == 'ighn_res_mix':
val = igh_ctrl.MixChResistance
#ILM
elif loggable_name == 'bluedewar_he_level':
val = ilm_ctrl.HeliumLevel
elif loggable_name == 'bluedewar_ni_level':
val = ilm_ctrl.NitrogenLevel
else:
found = False
if found:
qweb.makeLogEntry(loggable_name, val)
# print(str(datetime.datetime.now()) + ': logged new data')
def executeCommands(ctrl):
response = qweb.getCommands(port_id, 'C')
cmdrows = str.split(response, '\n')
for cmdrow in cmdrows:
if cmdrow != '':
props = str.split(cmdrow, ';')
for prop in props:
keyvals = str.split(prop, '=')
if keyvals[0]=='command_id':
command_id=keyvals[1]
elif keyvals[0]=='cmd':
cmd=keyvals[1]
print(datetime.datetime.now(), ' COMMAND :: ', cmdrow)
try:
response = ctrl.runCommand(cmd)
print(datetime.datetime.now(), ' RESPONSE :: ', response)
qweb.setCommandStatus(command_id, 'P')
qweb.setCommandResponse(command_id, response)
except Exception as e:
qweb.setCommandStatus(command_id, 'F')
print(datetime.datetime.now(), 'CMD ERROR: ', e)
#### Main Control Loop ####
if __name__ == "__main__":
port_id=2 # port_id for the qdot-server database (IGHN = 2)
#setup period for logging/executing IGH commands
period_all = 0.500 #seconds
period_log = 10
period_command = 5
t_all = 0
t_log = 0
t_command = 0
igh_ctrl = igh.IGH('/dev/ttyS6', 9600, 5)
ilm_ctrl = ilm.ILM('/dev/ttyS6', 9600, 6)
while True:
try:
if time.time() - t_all >=period_all:
state = readEverything(igh_ctrl)
readILM(ilm_ctrl)
t_all = time.time()
qweb.setCurrentState(2, state)
if time.time() - t_log >=period_log:
log(igh_ctrl, ilm_ctrl)
t_log = time.time()
if time.time() - t_command >= period_command and period_command > 0:
executeCommands(igh_ctrl)
t_command = time.time()
except Exception as e:
print(datetime.datetime.now(), ': ',e)
time.sleep(0.1)