-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaserControlAsync.py
More file actions
executable file
·604 lines (549 loc) · 25.5 KB
/
laserControlAsync.py
File metadata and controls
executable file
·604 lines (549 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
import json
import httpx
import asyncio
class carbide:
def __init__(self, endpoint):
self.carbideEndPoint = endpoint
self.requestHeaders = {
"Content-Type": "application/json",
"Accept": "application/json",
}
self.actualattenuatorpercentage = None
self.actualattenuatorpercentage_float = None
self.actualoutputenergy = None
self.actualoutputfrequency = None
self.actualoutputpower = None
self.actualpulseduration = None
self.actualPpdivider = None
self.actualshutterstate = None
self.actualrafrequency = None
self.actualstateid = None
self.actualharmonic = None
self.serialnumber = None
self.isoutputenabled = None
self.lastexecutedpresetindex = None
self.currentpreset = None
self.enableoutput = None
self.closeoutput = None
self.gotostandby = None
self.actualstatename = None
self.wavelength = None
self.pulsepickerstatus = None
self.currentaomtriggersource = None
self.powerlockstatus = None
loop = asyncio.get_event_loop()
loop.create_task(self.serialNumber())
loop.create_task(self.isOutputEnabled())
loop.create_task(self.generalStatus())
loop.create_task(self.actualValues())
print("Laser control initialised")
async def generalStatus(self):
"""Fetch general status asynchronously"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Basic/GeneralStatus"
)
return response
async def getInfo(self):
"""Gets text-based status, warnings, and errors asynchronously"""
async with httpx.AsyncClient() as client:
# Fetch general status, warnings, and errors concurrently
general_status, warnings, errors = await asyncio.gather(
self.generalStatus(),
client.get(f"{self.carbideEndPoint}/v1/Basic/Warnings"),
client.get(f"{self.carbideEndPoint}/v1/Basic/Errors"),
)
# Handle General Status
if general_status.status_code == 200:
print(general_status.text)
# Handle Warnings
if warnings.status_code == 200:
print(warnings.text)
# Handle Errors
if errors.status_code == 200:
print(errors.text)
async def actualValues(self):
"""Fetch and process actual values asynchronously using httpx"""
endpoints = {
"actualattenuatorpercentage": f"{self.carbideEndPoint}/v1/Basic/ActualAttenuatorPercentage",
"actualoutputenergy": f"{self.carbideEndPoint}/v1/Basic/ActualOutputEnergy",
"actualoutputfrequency": f"{self.carbideEndPoint}/v1/Basic/ActualOutputFrequency",
"actualoutputpower": f"{self.carbideEndPoint}/v1/Basic/ActualOutputPower",
"actualpulseduration": f"{self.carbideEndPoint}/v1/Basic/ActualPulseDuration",
"actualPpdivider": f"{self.carbideEndPoint}/v1/Basic/ActualPpDivider",
"actualshutterstate": f"{self.carbideEndPoint}/v1/Basic/ActualShutterState",
"actualrafrequency": f"{self.carbideEndPoint}/v1/Advanced/ActualRaFrequency",
"actualstateid": f"{self.carbideEndPoint}/v1/Advanced/ActualStateId",
"actualharmonic": f"{self.carbideEndPoint}/v1/Basic/ActualHarmonic",
}
async with httpx.AsyncClient() as client:
# Fetch all data concurrently
responses = await asyncio.gather(
*(client.get(url) for url in endpoints.values())
)
# Process and store responses
for key, response in zip(endpoints.keys(), responses):
if response.status_code == 200:
value = response.text.strip()
setattr(self, key, value) # Dynamically set attribute on `self`
# Log or process specific keys
if key == "actualattenuatorpercentage":
self.actualattenuatorpercentage_float = float(value)
print(f"Attenuator percentage: {value}%")
elif key == "actualoutputenergy":
print(f"Output energy: {value} uJ")
elif key == "actualoutputfrequency":
print(f"Output frequency: {value} kHz")
elif key == "actualoutputpower":
print(f"Output power: {value} W")
elif key == "actualpulseduration":
print(f"Pulse duration: {value} fs")
elif key == "actualPpdivider":
print(f"Pulse picker divider: {value}")
elif key == "actualshutterstate":
print(f"Shutter state: {value}")
elif key == "actualrafrequency":
print(f"RA frequency: {value} Hz")
elif key == "actualstateid":
print(f"State ID no.: {value}")
elif key == "actualharmonic":
try:
self.wavelength = int(1030 / int(value))
print(f"Using harmonic {value}, {self.wavelength}nm")
except ValueError:
print("Harmonic is likely 0 as not running")
else:
print(f"Unable to fetch {key}: {response.status_code}")
async def serialNumber(self):
"""Fetch and display the laser serial number asynchronously using httpx"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{self.carbideEndPoint}/v1/Basic/SerialNumber")
if response.status_code == 200:
self.serialnumber = response.text.strip()
print(f"Laser Serial Number: {self.serialnumber}")
else:
print("Not sure about serial number")
async def isOutputEnabled(self):
"""Check if the output is enabled asynchronously using httpx"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Basic/IsOutputEnabled"
)
if response.status_code == 200:
self.isoutputenabled = response.text.strip()
else:
self.isoutputenabled = "unknown"
async def getLastExecutedPresetIndex(self):
"""Fetch the last executed preset index asynchronously using httpx"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Basic/LastExecutedPresetIndex"
)
if response.status_code == 200:
self.lastexecutedpresetindex = response.text.strip() # Store the value
print(f"Last executed preset index was {self.lastexecutedpresetindex}")
else:
print("Error getting last executed preset index")
async def selectAndApplyPreset(self, preset="1"):
"""Select preset, apply preset, and wait for the laser to become operational
Args:
preset (str, optional): Preset to use as set in GUI. Defaults to "1".
"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Basic/SelectedPresetIndex"
)
if response.status_code == 200:
self.currentpreset = response.text.strip()
print(f"Current preset is {self.currentpreset}")
else:
print("No idea what current preset is")
print(f"Requesting preset {preset}")
presetNumber = int(preset)
async with httpx.AsyncClient() as client:
select_response = await client.put(
f"{self.carbideEndPoint}/v1/Basic/SelectedPresetIndex",
data=json.dumps(presetNumber),
headers=self.requestHeaders,
)
if select_response.status_code == 200:
print(f"Preset {preset} has been set, applying...")
async with httpx.AsyncClient() as client:
apply_response = await client.post(
f"{self.carbideEndPoint}/v1/Basic/ApplySelectedPreset",
headers=self.requestHeaders,
)
await asyncio.sleep(2)
if apply_response.status_code == 200:
await self.waitForLaserOperational()
print("Successfully applied!")
elif apply_response.status_code == 403:
print("Could not apply preset")
else:
print("Error applying preset")
elif select_response.status_code == 403:
print("Preset doesn't exist. Check available presets")
else:
print("Error setting preset")
async def changeOutput(self, state="close"):
"""_summary_
Args:
state (str, optional): _description_. Defaults to "close".
"""
print(f"Requesting output {state}")
async with httpx.AsyncClient() as client:
if state == "enable":
response = await client.post(
f"{self.carbideEndPoint}/v1/Basic/EnableOutput",
headers=self.requestHeaders,
)
self.enableoutput = response
if response.status_code == 200:
print("Output enabled")
elif response.status_code == 403:
print("Cannot enable output, check state")
else:
print("Set output enable error")
elif state == "close":
response = await client.post(
f"{self.carbideEndPoint}/v1/Basic/CloseOutput",
headers=self.requestHeaders,
)
self.closeoutput = response
if response.status_code == 200:
print("Output closed")
elif response.status_code == 403:
print("Laser not running anyway")
else:
print("Set output close error")
async def closeOutput(self):
async with httpx.AsyncClient() as client:
self.closeoutput = await client.post(
f"{self.carbideEndPoint}/v1/Basic/CloseOutput",
headers=self.requestHeaders,
)
async def waitForLaserOperational(self):
"""Wait for laser to reach operational state, break if in failure state"""
await self.actualStateName()
while self.actualstatename.text.strip() != '"Operational"':
print(self.actualstatename.text.strip())
await asyncio.sleep(1)
await self.actualStateName()
if self.actualstatename.text.strip() == '"Failure"':
print("Laser in failure state. Stopping...")
break
if self.actualstatename.text.strip() == '"Operational"':
print("Laser in operational state, ready to enable output")
async def actualStateName(self):
"""Get current state of laser"""
async with httpx.AsyncClient() as client:
self.actualstatename = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualStateName"
)
async def goToStandby(self):
"""_summary_"""
async with httpx.AsyncClient() as client:
self.gotostandby = await client.post(
f"{self.carbideEndPoint}/v1/Basic/GoToStandby",
headers=self.requestHeaders,
)
if self.gotostandby.status_code == 200:
await self.actualStateName()
while self.actualstatename.text.strip() != '"StandingBy"':
await asyncio.sleep(1)
await self.actualStateName()
if self.actualstatename.text.strip() == '"StandingBy"':
print("Laser in standby")
else:
print("Laser not in standby, please check state manually")
async def targetAttenuatorPercentage(self, percentage="0"):
"""_summary_
Args:
percentage (str, optional): percentage to request. Defaults to "0".
"""
async with httpx.AsyncClient() as client:
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualAttenuatorPercentage"
)
if response_actual.status_code == 200:
self.actualattenuatorpercentage_float = float(response_actual.text)
response_set = await client.put(
f"{self.carbideEndPoint}/v1/Basic/TargetAttenuatorPercentage",
data=json.dumps(percentage),
headers=self.requestHeaders,
)
if response_set.status_code == 200:
response_target = await client.get(
f"{self.carbideEndPoint}/v1/Basic/TargetAttenuatorPercentage"
)
if response_target.status_code == 200:
while int(response_target.text) != int(response_actual.text):
print(
f"Target attenuator percentage is {response_target.text}, currently at {response_actual.text}"
)
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualAttenuatorPercentage"
)
else:
print("Cannot get requested attenuator percentage")
elif response_set.status_code == 403:
print("Cannot set to this value, likely out of bounds")
else:
print("Error setting target attenuator percentage")
async def targetPulseDuration(self, length="290"):
"""_summary_
Args:
length (str, optional): pulse length to request (-ve = negative chirp, otherwise positive).
Defaults to "290".
"""
async with httpx.AsyncClient() as client:
response_set = await client.put(
f"{self.carbideEndPoint}/v1/Basic/TargetPulseDuration",
data=json.dumps(length),
headers=self.requestHeaders,
)
if response_set.status_code == 200:
response_target = await client.get(
f"{self.carbideEndPoint}/v1/Basic/TargetPulseDuration"
)
if response_target.status_code == 200:
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualPulseDuration"
)
while int(response_target.text) != int(response_actual.text):
print(
f"Target pulse duration is {response_target.text}, currently at {response_actual.text}"
)
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualPulseDuration"
)
else:
print("Cannot get requested pulse duration")
elif response_set.status_code == 403:
print("Cannot set to this value, likely out of bounds")
else:
print("Error setting pulse duration")
async def targetPpDivider(self, divider="1000"):
async with httpx.AsyncClient() as client:
response_set = await client.put(
f"{self.carbideEndPoint}/v1/Basic/TargetPpDivider",
data=json.dumps(divider),
headers=self.requestHeaders,
)
if response_set.status_code == 200:
response_get = await client.get(
f"{self.carbideEndPoint}/v1/Basic/TargetPpDivider"
)
if response_get.status_code == 200:
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualPpDivider"
)
while int(response_get.text) != int(response_actual.text):
print(
f"Target pulse picker divider is {response_get.text}, currently at {response_actual.text}"
)
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualPpDivider"
)
else:
print("Cannot get requested pulse duration")
elif response_set.status_code == 403:
print("Cannot set to this value, likely out of bounds")
else:
print("Error setting divider")
async def targetRaFrequency(self, frequency="60"):
async with httpx.AsyncClient() as client:
response_set = await client.put(
f"{self.carbideEndPoint}/v1/Basic/TargetRaFrequency",
data=json.dumps(frequency),
headers=self.requestHeaders,
)
if response_set.status_code == 200:
response_get = await client.get(
f"{self.carbideEndPoint}/v1/Basic/TargetRaFrequency"
)
if response_get.status_code == 200:
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualRaFrequency"
)
while int(response_get.text) != int(response_actual.text):
print(
f"Target RA frequency is {response_get.text}, currently at {response_actual.text}"
)
response_actual = await client.get(
f"{self.carbideEndPoint}/v1/Basic/ActualRaFrequency"
)
else:
print("Cannot get requested RA frequency")
elif response_set.status_code == 403:
print("Cannot set RA frequency to this value, likely out of bounds")
else:
print("Error setting RA frequency")
async def isRemoteInterlockActive(self):
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Advanced/IsRemoteInterlockActive"
)
if response.status_code == 200:
if response.text == "true":
print("Remote interlock armed")
elif response.text == "false":
print("Remote interlock is NOT armed")
else:
print("Cannot get remote interlock state")
async def resetRemoteInterlock(self):
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.carbideEndPoint}/v1/Advanced/ResetRemoteInterlock",
headers=self.requestHeaders,
)
if response.status_code == 200:
print("Remote interlock reset")
await self.isRemoteInterlockActive()
elif response.status_code == 403:
print("Cannot reset remote interlock")
await self.isRemoteInterlockActive()
else:
print("Error resetting remote interlock")
async def isPpEnabled(self):
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Advanced/IsPpEnabled"
)
if response.status_code == 200:
if response.text == "true":
print("Pulse picker is enabled")
self.pulsepickerstatus = True
elif response.text == "false":
print("Pulse picker not enabled")
self.pulsepickerstatus = False
else:
print("Can't get pulse picker status")
else:
print("Error getting pulse picker status")
async def togglePulsePicker(self, toggle="off"):
async with httpx.AsyncClient() as client:
if toggle == "off":
response = await client.post(
f"{self.carbideEndPoint}/v1/Advanced/EnablePp",
headers=self.requestHeaders,
)
if response.status_code == 200:
print("Enabling pulse picker")
await self.isPpEnabled()
elif response.status_code == 403:
print("Could not enable pulse picker")
await self.isPpEnabled()
else:
print("Error enabling pulse picker")
await self.isPpEnabled()
elif toggle == "on":
response = await client.post(
f"{self.carbideEndPoint}/v1/Advanced/DisablePp",
headers=self.requestHeaders,
)
if response.status_code == 200:
print("Disabling pulse picker")
await self.isPpEnabled()
elif response.status_code == 403:
print("Could not disable pulse picker (probably already disabled)")
await self.isPpEnabled()
else:
print("Error disabling pulse picker")
await self.isPpEnabled()
else:
print(f"Unknown request of pulse picker state: {toggle}")
async def getAomTriggerSource(self):
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/ExternalControl/AomTriggerSource"
)
if response.status_code == 200:
print(f"AOM trigger source is set to {response.text}")
if response.text == '"Internal"':
self.currentaomtriggersource = 1
elif response.text == '"ExternalLow"':
self.currentaomtriggersource = 2
elif response.text == '"ExternalHigh"':
self.currentaomtriggersource = 3
else:
print("Unknown current trigger source")
self.currentaomtriggersource = 0
else:
print("Cannot get AOM trigger source")
async def setAomTriggerSource(self, source="Internal"):
async with httpx.AsyncClient() as client:
response = await client.put(
f"{self.carbideEndPoint}/v1/ExternalControl/AomTriggerSource",
data=json.dumps(source),
headers=self.requestHeaders,
)
if response.status_code == 200:
await self.getAomTriggerSource()
elif response.status_code == 403:
print("Could not set trigger source")
await self.getAomTriggerSource()
else:
print("Error setting trigger source")
async def isPowerlockEnabled(self):
await self.actualValues()
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.carbideEndPoint}/v1/Basic/IsPowerlockEnabled"
)
if response.status_code == 200:
if response.text == "true":
self.powerlockstatus = "enabled"
elif response.text == "false":
self.powerlockstatus = "disabled"
print(f"Powerlock is currently {self.powerlockstatus}")
async def powerlockControl(self, state="enable"):
async with httpx.AsyncClient() as client:
if state == "enable":
response = await client.post(
f"{self.carbideEndPoint}/v1/Basic/EnablePowerlock",
headers=self.requestHeaders,
)
if response.status_code == 200:
print("Enabling powerlock...")
while self.powerlockstatus != "enabled":
await asyncio.sleep(1)
await self.isPowerlockEnabled()
elif response.status_code == 403:
print("Unable to enable powerlock")
else:
print("Error enabling powerlock")
elif state == "disable":
response = await client.post(
f"{self.carbideEndPoint}/v1/Basic/DisablePowerlock",
headers=self.requestHeaders,
)
if response.status_code == 200:
print("Disabling powerlock...")
while self.powerlockstatus != "disabled":
await asyncio.sleep(1)
await self.isPowerlockEnabled()
elif response.status_code == 403:
print("Unable to disable powerlock")
else:
print("Error disabling powerlock")
async def reduceLeak(self):
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.carbideEndPoint}/v1/Advanced/ReduceLeak"
)
if response.status_code == 200:
print("Reducing leak...")
await asyncio.sleep(3)
print("Leak reduction successful")
elif response.status_code == 403:
print("Unable to run leak reduction")
async def main():
run = carbide(endpoint="http://172.23.17.123:20010")
await asyncio.gather(
run.isOutputEnabled(), run.selectAndApplyPreset("5"), run.changeOutput("enable")
)
await asyncio.sleep(5)
await run.changeOutput()
if __name__ == "__main__":
asyncio.run(main())