-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulators.py
259 lines (212 loc) · 7.47 KB
/
simulators.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
import threading
import time
import random
# class that simulates the channel of the caen by giving a random value to its attributes vmon, imon and
class ChannelSimulator:
def __init__(self, trip_probability=0.02):
# private attributes (they do not exist in the real device)
self._trip_probability = trip_probability
self._vset = 100
# public attributes (they exist in the real device)
self.vset = 100
self.iset = 0.6 # uA # functionality not implemented
self.vmon = self.vset
self.imon = self.vset / 10e3 # (uA) lets say there is a resistance of 10 MOhm
self.imdec = 3 # channel imon number of decimal digits (2 HR, 3 LR)
self.rup = 50 # V/s
self.rdw = 10 # V/s
self.pdwn = "RAMP" # 'KILL' or 'RAMP'
self.stat = {
"ON": True,
"RUP": False,
"RDW": False,
"OVC": False,
"OVV": False,
"UNV": False,
"MAXV": False,
"TRIP": False,
"OVP": False,
"OVT": False,
"DIS": False,
"KILL": False,
"ILK": False,
"NOCAL": False,
}
def _randomize(self):
if self.stat["KILL"] or self.stat["DIS"]:
self.stat["ON"] = False
self.vmon = 0
self.imon = 0
return
if self.stat["TRIP"] or self.stat["ILK"]:
self.stat["ON"] = False
if self.pdwn == "KILL":
self.stat["KILL"] = (
True # not sure if it behaves like this in KILL mode
)
self.vmon = 0
self.imon = 0
return
self.vmon = random.gauss(self._vset, 1.0 / 3) # 0.3 V standard deviation
self.imon = random.gauss(
self.vmon / 10e3, self.vmon / 100e3
) # (uA) lets say there is a resistance of 10 MOhm and 10% standard deviation
if not self.stat["ON"]:
self._vset -= self.rdw
self.imon = -self.imon * 10
# self.stat["RDW"] = True # not sure
if self._vset <= 0:
self._vset = 0
self.vmon = 0
self.imon = 0
return
if not self.stat["TRIP"]:
self.stat["TRIP"] = random.random() < self._trip_probability
# simulate ramp up and ramp down when the channel is ON
if self._vset < self.vset:
self.stat["RUP"] = True
self.stat["RDW"] = False
self._vset += self.rup
self.imon = self.imon * 10
if self._vset > self.vset:
self._vset = self.vset
elif self._vset > self.vset:
self.stat["RDW"] = True
self.stat["RUP"] = False
self._vset -= self.rdw
self.imon = -self.imon * 10
if self._vset < self.vset:
self._vset = self.vset
else:
self.stat["RUP"] = False
self.stat["RDW"] = False
def turn_on(self):
self.stat["ON"] = True
self.stat["TRIP"] = False
self.stat["KILL"] = False
self.stat["ILK"] = False
def turn_off(self):
self.stat["ON"] = False
self.stat["KILL"] = False
self.stat["TRIP"] = False
self.stat["ILK"] = False # not sure
@property
def on(self):
return self.stat["ON"]
def vset(self, voltage):
self.vset = voltage
class ModuleSimulator:
def __init__(self, n_channels, trip_probability=0.05):
self.name = "N1471H SIMULATOR"
self.number_of_channels = n_channels
self.channels = [
ChannelSimulator(
1 - (1 - trip_probability) ** (1.0 / self.number_of_channels)
)
for i in range(self.number_of_channels)
]
self.board_alarm_status = {
"CH0": False,
"CH1": False,
"CH2": False,
"CH3": False,
"PWFAIL": False,
"OVP": False,
"HVCKFAIL": False,
}
self.interlock_status = False
self.interlock_mode = "CLOSED"
# Start the device reading thread
self.randomize_thread = threading.Thread(
target=self.__continuous_randomize, daemon=True
).start()
def clear_alarm_signal(self):
self.board_alarm_status = {k: False for k in self.board_alarm_status.keys()}
for ch in self.channels:
ch.stat["TRIP"] = False
ch.stat["ILK"] = False
def _randomize(self):
# print(self.board_alarm_status, self.interlock_status)
# check for trips and set the alarm signal
for i, ch in enumerate(self.channels):
if ch.stat["TRIP"]:
self.board_alarm_status["CH" + str(i)] = True
# print(f"Channel {i} trip")
# do the alarm-intlck connection and act the interlock
self.__connection_alarm_intlck()
if self.interlock_status:
for ch in self.channels:
ch.stat["ILK"] = True
# randomize the channels
for ch in self.channels:
ch._randomize()
def __continuous_randomize(self, wait_seconds=1):
while True:
self._randomize()
time.sleep(wait_seconds)
def __connection_alarm_intlck(self):
self.interlock_status = any([v for k, v in self.board_alarm_status.items()])
class SpellmanSimulator:
def __init__(self):
self.server_host = "ip"
self.server_port = 50001
self.name = 'Spellman SL30 SIMULATOR'
self.vset = 0 # V
self.iset = 0
self.vmon = 0 # mA
self.imon = 0
self.stat = {'HV': True, 'ILK': False, 'FAULT': False, 'REMOTE': True, 'ARC': False}
# start the device reading thread
self.randomize_thread = threading.Thread(target=self.__continuous_randomize, daemon=True).start()
def __continuous_randomize(self, wait_seconds=1):
while True:
self._randomize()
time.sleep(wait_seconds)
def _randomize(self):
if not self.stat['REMOTE']:
return
if self.stat['HV']:
self.vmon = random.gauss(self.vset, 0.1)
Rleft = 200 + 80 # MOhm
Rright = 200 + 50 # Mohm
imon_mean = self.vmon * (1 / Rleft + 1 / Rright) *1e-3 # mA
self.imon = random.gauss(imon_mean, imon_mean *0.01)
else:
self.vmon = 0
self.imon = 0
def get_vset(self):
return self.vset
def set_vset(self, voltage_V):
self.vset = voltage_V
def get_iset(self):
return self.iset
def set_iset(self, current_mA):
self.iset = current_mA
def get_vmon(self):
return self.vmon
def get_imon(self):
return self.imon
def get_status(self):
return self.stat
def turn_remote_on(self):
self.stat['REMOTE'] = True
def turn_remote_off(self):
self.stat['REMOTE'] = False
def turn_hv_on(self):
self.stat['HV'] = True
def turn_hv_off(self):
self.stat['HV'] = False
def turn_on(self):
self.turn_hv_on()
def turn_off(self):
self.turn_hv_off()
def status(self):
return self.stat
@property
def on(self):
return self.get_status()['HV']
@property
def remote(self):
return self.get_status()['REMOTE']
def vset(self, voltage):
self.set_vset(voltage)