Compare commits
503 Commits
Author | SHA1 | Date | |
---|---|---|---|
b040abcdf6 | |||
0def8f52e7 | |||
c074d2cbba | |||
d2c9524497 | |||
1c55b7dbf1 | |||
b63dca3b4e | |||
5f1e37a99e | |||
8c606150f5 | |||
efc1694408 | |||
cba15e0237 | |||
41ed82e72e | |||
95ca85fd9c | |||
4f9372066a | |||
be779b118b | |||
feb0566e87 | |||
8b40e91578 | |||
eeb666c5dd | |||
fac29af0ad | |||
d55ee42612 | |||
75cfffb548 | |||
7d02b47c7a | |||
ecfc0b2ab8 | |||
04402e72b6 | |||
61b65220e1 | |||
67030985d6 | |||
ca2ca5d7cb | |||
ba3c64ddac | |||
06bcf94fa2 | |||
f6f433e6b2 | |||
2a165c5013 | |||
8e70b97ef7 | |||
e1cda7d263 | |||
959a09957e | |||
f68886a4ed | |||
eeea754181 | |||
ae7bf3ce96 | |||
affe3c161c | |||
5b6a9de96e | |||
34793f8bd0 | |||
7333ccd7a6 | |||
2d42eab5fa | |||
c88a71985a | |||
ae2a731161 | |||
ec664d9268 | |||
4ca7bf0a7d | |||
715fcf4d36 | |||
a429852c80 | |||
3eb5f90ce6 | |||
f1115c937b | |||
91d0f2c635 | |||
614a49f3e3 | |||
1e56f2f39e | |||
82e9b599ad | |||
c4781d3d70 | |||
c0b928f2f6 | |||
73bb0cff1e | |||
6454e5f96e | |||
3f53823c07 | |||
fe60a8ebd8 | |||
fb0a689786 | |||
2ee12a62f0 | |||
de970ece3a | |||
59810aa748 | |||
d2e7ce7242 | |||
72fbed289d | |||
60c6553ee7 | |||
cf10590245 | |||
44c59bd818 | |||
c355999e85 | |||
4f0daf3424 | |||
be7c9eec8c | |||
ef5f4cd2f3 | |||
1da16f12c4 | |||
2658dd8090 | |||
fa5d5654f8 | |||
bf8d5101b0 | |||
31b5928725 | |||
110855200a | |||
21f9921269 | |||
bce88ff343 | |||
5359f0351f | |||
ab2e001093 | |||
ff0e77a166 | |||
195a48f366 | |||
2110f8d83b | |||
4c6c7c7950 | |||
7d7972b73f | |||
fc40f5e0c9 | |||
a19b085c30 | |||
09772eb746 | |||
c249b1648d | |||
480913dabe | |||
c2a080360d | |||
923da8ca15 | |||
7a2e764262 | |||
d726fd9fa0 | |||
267acf644c | |||
0bc8accaaa | |||
0351ebd98d | |||
3a235f6380 | |||
500c5f8c5d | |||
6b8e6498fa | |||
4fc6ef52da | |||
fcb5052565 | |||
acc7580dc6 | |||
4f28e5555c | |||
ec2bc57ade | |||
7142cee0db | |||
a3d7520da9 | |||
9d5ed822e8 | |||
b6c2cf8d3a | |||
a26ae0da4e | |||
a7cbb35455 | |||
e866ae7034 | |||
f35dae5893 | |||
2fc9eccfd5 | |||
ca57f46ed2 | |||
1776d7b4b6 | |||
34f46ab0b0 | |||
98a73d7882 | |||
e7148a30cb | |||
da948468b4 | |||
75df438d69 | |||
10fe42babf | |||
8124ed3294 | |||
aa7910c28c | |||
ff6a98af92 | |||
0647b34bb8 | |||
271bb27699 | |||
2cbe3f19b4 | |||
228902a6ee | |||
bee591c861 | |||
8e7e70c50e | |||
e36ff9bc41 | |||
e02c9b1378 | |||
5a19103d0e | |||
e201e7dce9 | |||
532e4a7ab5 | |||
c9ccaa484a | |||
a5dfc82b95 | |||
e22da0db67 | |||
e89bc07759 | |||
e405783716 | |||
dc59906c8a | |||
161fb7b2ca | |||
04940b1a0b | |||
6f6f07b0f9 | |||
a44d232dea | |||
32b1d6412d | |||
87830a1473 | |||
fcca3801a4 | |||
00b57e3d2c | |||
720d010c47 | |||
9e52665b88 | |||
a529cb9ab1 | |||
33fb90af05 | |||
da122ad961 | |||
11d1ad546f | |||
37d28c9f35 | |||
dc0cc590ed | |||
0771cb6899 | |||
564620e9e3 | |||
e1b30bf37e | |||
439c9d34c1 | |||
ac90999bdd | |||
85dd905cfd | |||
04b9bed7c9 | |||
1171245704 | |||
ab1fdbae16 | |||
2d628e151c | |||
329c8d999a | |||
af920aafdf | |||
98162a59b9 | |||
64b4cba67c | |||
d99f2dfbc3 | |||
e673dfe92b | |||
d18e368ab9 | |||
a334cc4f0a | |||
42c5e97122 | |||
f069b31d5a | |||
47000f7a9f | |||
6f95c0d825 | |||
a9d479ba0a | |||
9329d2b0f4 | |||
4f69899fbe | |||
2dad4b4ee1 | |||
5174321b6a | |||
32edc4c049 | |||
d986134781 | |||
bd4330f9fd | |||
f260bd25b1 | |||
d0af831b1f | |||
df8bc1c203 | |||
c101af99d3 | |||
7baacb0f9e | |||
155efb06bf | |||
2f730ab444 | |||
41f3a2ecd4 | |||
10d2d16453 | |||
087a5ef476 | |||
67bbe9d9a5 | |||
0b501ba444 | |||
981513df10 | |||
95e5102137 | |||
d498fa9230 | |||
eff8ac8c62 | |||
39f0180f40 | |||
dd6ee398f2 | |||
c3796aa6eb | |||
4cf54dcce8 | |||
79277e86f1 | |||
361f9ac4fc | |||
11a3bed8b8 | |||
5db84b3fa1 | |||
dccd329435 | |||
0d265b9752 | |||
9cab6670b9 | |||
8c6f1a21ab | |||
a1271b7268 | |||
9bf187758b | |||
349c510555 | |||
31b1a916f5 | |||
9755040ac2 | |||
c0704b3d4f | |||
58ff438f46 | |||
98ab5a5785 | |||
58f7ef6de4 | |||
431a1c20ab | |||
2e698af78c | |||
5c2b4c6491 | |||
80b1932f39 | |||
644bfa85f7 | |||
bd06abc060 | |||
efad0e2079 | |||
c3bfc0a930 | |||
2eef2f1d7f | |||
d8621bdc2c | |||
23c9aa6038 | |||
0464d60f68 | |||
d4b80eabd6 | |||
952cbe76a5 | |||
746df2eb94 | |||
494960a2ac | |||
0382313fc7 | |||
66faf89d7f | |||
79da402ceb | |||
b0b85a2711 | |||
5fb1e649ab | |||
7a3cfe9836 | |||
e12f2bacb1 | |||
d6c641e67b | |||
ec15a35977 | |||
05415e79b1 | |||
908f2d4c23 | |||
cdd6d7f1a2 | |||
8831e7142f | |||
e5c99c36e7 | |||
bb0619f212 | |||
0e5f62d849 | |||
3aaa12eee1 | |||
dbf4ad373e | |||
595c32783c | |||
b48b965070 | |||
2cf073e7d8 | |||
ef1664e1a2 | |||
05cf4a791a | |||
c8f30582a5 | |||
0acb80380a | |||
b5205d7c7a | |||
819026659a | |||
09f6149822 | |||
0ce5a495f0 | |||
a66f2ac21b | |||
244d84aa56 | |||
f1ea85bfa7 | |||
4c577cf83d | |||
ca4297e3a6 | |||
31706093fb | |||
f4fac1f3dd | |||
c9500bcd96 | |||
65c5277a79 | |||
e0132ed201 | |||
e0090a5918 | |||
ee9f8536b2 | |||
a1141c87c9 | |||
b75d89ad16 | |||
af32fd308c | |||
355eaeb864 | |||
a2458005f1 | |||
1004512caf | |||
9f54d89efa | |||
72f9f242d7 | |||
4857ebf043 | |||
25ff6018c4 | |||
b3fbf3b107 | |||
8651a5fb83 | |||
231f5942f8 | |||
1d893974cb | |||
6b32c4bb81 | |||
9b801e9487 | |||
23b1f59320 | |||
3a2a90e712 | |||
ad7ff7f7f3 | |||
8799710b38 | |||
e1d5170a90 | |||
a39db9a35d | |||
5c874483a9 | |||
65480cd773 | |||
7d59345f43 | |||
bc553f889c | |||
cae225df41 | |||
3a52f9d31c | |||
4ebb15ca6c | |||
240c4f027b | |||
f7e24f13bb | |||
c3d8068e02 | |||
084e890645 | |||
edd3437682 | |||
c2728c8340 | |||
725820fafd | |||
9f653b4e6f | |||
ae7d3514a1 | |||
6b9d4a8140 | |||
af295b12b0 | |||
69dd011260 | |||
d62076128a | |||
38ae301cda | |||
2ee9ea65da | |||
0dfaa79f77 | |||
866fe73f2e | |||
f5c96214b0 | |||
4d28abe141 | |||
c75d7f17f7 | |||
a384664639 | |||
ee708f7b02 | |||
edc942cb24 | |||
dcd79506a9 | |||
f4e974f46c | |||
d889401697 | |||
85295a7d72 | |||
82957c287d | |||
05593d80f6 | |||
7a870aa56c | |||
09d48ea913 | |||
8850edbc2d | |||
59cc981566 | |||
3cc9a75174 | |||
52b77ba9e6 | |||
db3b190c26 | |||
c522c41654 | |||
929e41ffff | |||
8d99a8c536 | |||
a14c282993 | |||
d09634a55d | |||
b17030afa2 | |||
37c9efb27b | |||
4167ce7b00 | |||
2b7b2267d2 | |||
8071c21819 | |||
e16ef3ae87 | |||
51147d8e09 | |||
6909eb8541 | |||
5d6b208671 | |||
b3eebb6c6a | |||
7f166a5b8c | |||
c1eb764b09 | |||
a9d798fabc | |||
a928c95efd | |||
7df4584150 | |||
b7cebe2cd8 | |||
b8b2dafaf8 | |||
f66411dded | |||
ce4bbec766 | |||
355810a887 | |||
aa98604f88 | |||
b0051ca3f0 | |||
92edbb27ea | |||
20fc48ddf0 | |||
340c031f46 | |||
3ac8b4e255 | |||
6ffc73a1e1 | |||
4ef0b0c01d | |||
1fc805a5a2 | |||
05edf98dfe | |||
e09c365ea5 | |||
64cb297a06 | |||
eaefa1ce87 | |||
71aaf7187a | |||
0a28192c15 | |||
1b9cac04b1 | |||
2b1986ad8f | |||
270152d503 | |||
1922578dfa | |||
43880346d6 | |||
d0794a7803 | |||
dc76ac92de | |||
8579368259 | |||
cd90385e6c | |||
3acea5f7c7 | |||
c564ae392c | |||
1357ead435 | |||
6a0261c728 | |||
d717a481d7 | |||
4c94580cb9 | |||
8eee7ab3b0 | |||
478075c545 | |||
b7d16d2e16 | |||
d3379d5e95 | |||
d6ad5f058d | |||
a35134978a | |||
7891c281e1 | |||
6460e51920 | |||
b40b0e75b1 | |||
af983287e7 | |||
8d23503bbd | |||
bb097ac3ba | |||
16a9550080 | |||
9858973ba1 | |||
e6d6179925 | |||
60c62a340d | |||
3c0c60615a | |||
fda1939324 | |||
8767be2aac | |||
39a3e79eb3 | |||
c1d42f0f02 | |||
3fe44d32b1 | |||
f58ab263e7 | |||
e0fe7e46d1 | |||
b423235c5d | |||
aa82bc580d | |||
c2596a9629 | |||
bf1761bbc4 | |||
99588fc815 | |||
bbc4663266 | |||
c1307cdd03 | |||
b911bc1838 | |||
26a0f2e078 | |||
4f7083bc98 | |||
0909f92e12 | |||
f3450375ce | |||
eb2e8f5f74 | |||
2ef0da68e8 | |||
9b38db7706 | |||
8f7fb1e45b | |||
f13e29aad2 | |||
c5d228ffc4 | |||
071ba38b60 | |||
b29b1e1b36 | |||
c91d726f9d | |||
1d75d192e5 | |||
4bf3acab98 | |||
eebc9232cd | |||
c16f159599 | |||
796be752b7 | |||
b8e8d24b50 | |||
45dd14a72c | |||
ad7cfe4ea0 | |||
47d09e9b08 | |||
d76d79aebb | |||
a64eb7f33b | |||
3d0d779d81 | |||
3b7cc33f64 | |||
41489a4a24 | |||
3140d454ae | |||
7bd166a8a1 | |||
f3978385b4 | |||
bc6a99e11b | |||
6b610f1e25 | |||
9a60de9c1c | |||
026e657799 | |||
35f08bf4ad | |||
76ae75a926 | |||
27ac70b1da | |||
a343b07f1d | |||
09411f36f3 | |||
b0f0a48e51 | |||
e4261ecfe1 | |||
598dd07888 | |||
f2b330a3f0 | |||
a85201ad7d | |||
2cf6e167a8 | |||
ffc2c495fb | |||
7ea4e3955a | |||
cc1632e07d | |||
1e17d0c6b9 | |||
e568c665a8 | |||
d0f895ed44 | |||
69f5692951 | |||
980499ba41 | |||
6cde8177d5 | |||
23779c8f8c | |||
899a07aec8 | |||
6a32ecf342 | |||
1464a6bce5 | |||
bb6f692c6b | |||
1a8ddbc696 | |||
ed02131a37 | |||
a19425684c | |||
24cffad4df | |||
05fec236da | |||
b1a88440ef | |||
c9721649a3 | |||
d2c3370a40 |
22
calibtest.py
22
calibtest.py
@ -1,22 +0,0 @@
|
|||||||
import sys
|
|
||||||
import os
|
|
||||||
from glob import glob
|
|
||||||
from frappy_psi.calcurve import CalCurve
|
|
||||||
|
|
||||||
os.chdir('/Users/zolliker/gitpsi/calcurves')
|
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
calib = sys.argv[1]
|
|
||||||
c = CalCurve(calib)
|
|
||||||
else:
|
|
||||||
for file in sorted(glob('*.*')):
|
|
||||||
if file.endswith('.md') or file.endswith('.std'):
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
c = CalCurve(file)
|
|
||||||
xy = c.export()
|
|
||||||
print('%9.4g %12.7g %9.4g %9.4g %s' % (tuple(c.extx) + tuple(c.exty) + (file,)))
|
|
||||||
except Exception as e:
|
|
||||||
print(file, e)
|
|
||||||
calib = file
|
|
||||||
|
|
@ -6,7 +6,7 @@ Node('QnwTC1test.psi.ch',
|
|||||||
Mod('io',
|
Mod('io',
|
||||||
'frappy_psi.qnw.QnwIO',
|
'frappy_psi.qnw.QnwIO',
|
||||||
'connection for Quantum northwest',
|
'connection for Quantum northwest',
|
||||||
uri='tcp://ldmcc01-ts:3004',
|
uri='tcp://ldm-fi-ts:3001',
|
||||||
)
|
)
|
||||||
|
|
||||||
Mod('T',
|
Mod('T',
|
||||||
|
@ -6,7 +6,7 @@ Node('TFA10.psi.ch',
|
|||||||
Mod('io',
|
Mod('io',
|
||||||
'frappy_psi.thermofisher.ThermFishIO',
|
'frappy_psi.thermofisher.ThermFishIO',
|
||||||
'connection for ThermoFisher A10',
|
'connection for ThermoFisher A10',
|
||||||
uri='tcp://ldmse-d910-ts:3001',
|
uri='tcp://ldm-fi-ts:3002',
|
||||||
)
|
)
|
||||||
|
|
||||||
Mod('T',
|
Mod('T',
|
||||||
|
@ -24,7 +24,6 @@ Mod('ts_low',
|
|||||||
minrange=13,
|
minrange=13,
|
||||||
range=22,
|
range=22,
|
||||||
tolerance = 0.1,
|
tolerance = 0.1,
|
||||||
vexc = 3,
|
|
||||||
htrrng=4,
|
htrrng=4,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,8 +32,7 @@ Mod('ts_high',
|
|||||||
'sample Cernox',
|
'sample Cernox',
|
||||||
channel = 1,
|
channel = 1,
|
||||||
switcher = 'lsc_channel',
|
switcher = 'lsc_channel',
|
||||||
minrange=11,
|
minrange=9,
|
||||||
vexc = 5,
|
|
||||||
range=22,
|
range=22,
|
||||||
tolerance = 0.1,
|
tolerance = 0.1,
|
||||||
htrrng=5,
|
htrrng=5,
|
||||||
@ -47,8 +45,6 @@ Mod('ts',
|
|||||||
value=Param(unit='K'),
|
value=Param(unit='K'),
|
||||||
low='ts_low',
|
low='ts_low',
|
||||||
high='ts_high',
|
high='ts_high',
|
||||||
#min_high=0.6035,
|
|
||||||
#max_low=1.6965,
|
|
||||||
min_high=0.6,
|
min_high=0.6,
|
||||||
max_low=1.7,
|
max_low=1.7,
|
||||||
tolerance=0.1,
|
tolerance=0.1,
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
Node('attocube_test.psi.ch',
|
|
||||||
'a single attocube axis',
|
|
||||||
interface='tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('r',
|
|
||||||
'frappy_psi.attocube.Axis',
|
|
||||||
'ANRv220-F3-02882',
|
|
||||||
axis = 1,
|
|
||||||
value = Param(unit='deg'),
|
|
||||||
tolerance = 0.1,
|
|
||||||
target_min = 0,
|
|
||||||
target_max = 360,
|
|
||||||
steps_fwd = 45,
|
|
||||||
steps_bwd = 85,
|
|
||||||
step_mode = True,
|
|
||||||
# gear = 1.2,
|
|
||||||
)
|
|
||||||
|
|
231
cfg/dilsc_cfg.py
231
cfg/dilsc_cfg.py
@ -1,231 +0,0 @@
|
|||||||
Node('cfg/dilsc1.cfg',
|
|
||||||
'triton test',
|
|
||||||
interface='5000',
|
|
||||||
name='dilsc1',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('triton',
|
|
||||||
'frappy_psi.mercury.IO',
|
|
||||||
'connection to triton software',
|
|
||||||
uri='tcp://192.168.2.33:33576',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_mix',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'mix. chamber temperature',
|
|
||||||
slot='T8',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_pt2head',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'PTR2 head temperature',
|
|
||||||
slot='T1',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_pt2plate',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'PTR2 plate temperature',
|
|
||||||
slot='T2',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_still',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'still temperature',
|
|
||||||
slot='T3',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('htr_still',
|
|
||||||
'frappy_psi.triton.HeaterOutput',
|
|
||||||
'still heater',
|
|
||||||
slot='H2',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_coldpl',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'cold plate temperature',
|
|
||||||
slot='T4',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_mixcx',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'mix. chamber cernox',
|
|
||||||
slot='T5',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_pt1head',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'PTR1 head temperature',
|
|
||||||
slot='T6',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_pt1plate',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'PTR1 plate temperature',
|
|
||||||
slot='T7',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_pucksensor',
|
|
||||||
'frappy_psi.triton.TemperatureLoop',
|
|
||||||
'puck sensor temperature',
|
|
||||||
output_module='htr_pucksensor',
|
|
||||||
slot='TA',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('htr_pucksensor',
|
|
||||||
'frappy_psi.triton.HeaterOutputWithRange',
|
|
||||||
'mix. chamber heater',
|
|
||||||
slot='H1,TA',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('T_magnet',
|
|
||||||
'frappy_psi.triton.TemperatureSensor',
|
|
||||||
'magnet temperature',
|
|
||||||
slot='T13',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('action',
|
|
||||||
'frappy_psi.triton.Action',
|
|
||||||
'higher level scripts',
|
|
||||||
io='triton',
|
|
||||||
slot='DR',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_dump',
|
|
||||||
'frappy_psi.mercury.PressureSensor',
|
|
||||||
'dump pressure',
|
|
||||||
slot='P1',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_cond',
|
|
||||||
'frappy_psi.mercury.PressureSensor',
|
|
||||||
'condenser pressure',
|
|
||||||
slot='P2',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_still',
|
|
||||||
'frappy_psi.mercury.PressureSensor',
|
|
||||||
'still pressure',
|
|
||||||
slot='P3',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_fore',
|
|
||||||
'frappy_psi.mercury.PressureSensor',
|
|
||||||
'pressure on the pump side',
|
|
||||||
slot='P5',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_back',
|
|
||||||
'frappy_psi.mercury.PressureSensor',
|
|
||||||
'pressure on the back side of the pump',
|
|
||||||
slot='P4',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('p_ovc',
|
|
||||||
'frappy_psi.mercury.PressureSensor',
|
|
||||||
'outer vacuum pressure',
|
|
||||||
slot='P6',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('V1',
|
|
||||||
'frappy_psi.triton.Valve',
|
|
||||||
'valve V1',
|
|
||||||
slot='V1',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('V2',
|
|
||||||
'frappy_psi.triton.Valve',
|
|
||||||
'valve V2',
|
|
||||||
slot='V2',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('V4',
|
|
||||||
'frappy_psi.triton.Valve',
|
|
||||||
'valve V4',
|
|
||||||
slot='V4',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('V5',
|
|
||||||
'frappy_psi.triton.Valve',
|
|
||||||
'valve V5',
|
|
||||||
slot='V5',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('V9',
|
|
||||||
'frappy_psi.triton.Valve',
|
|
||||||
'valve V9',
|
|
||||||
slot='V9',
|
|
||||||
io='triton',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('ips',
|
|
||||||
'frappy_psi.mercury.IO',
|
|
||||||
'IPS for magnet',
|
|
||||||
uri='192.168.127.254:3001',
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('mf',
|
|
||||||
'frappy_psi.dilsc.VectorField',
|
|
||||||
'vector field',
|
|
||||||
x='mfx',
|
|
||||||
y='mfy',
|
|
||||||
z='mfz',
|
|
||||||
sphere_radius=0.6,
|
|
||||||
cylinders=((0.23, 5.2), (0.45, 0.8)),
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('mfx',
|
|
||||||
'frappy_psi.ips_mercury.SimpleField',
|
|
||||||
'magnetic field, x-axis',
|
|
||||||
slot='GRPX',
|
|
||||||
io='ips',
|
|
||||||
tolerance=0.0001,
|
|
||||||
wait_stable_field=0.0,
|
|
||||||
nunits=2,
|
|
||||||
target=Param(max=0.6),
|
|
||||||
ramp=0.225,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('mfy',
|
|
||||||
'frappy_psi.ips_mercury.SimpleField',
|
|
||||||
'magnetic field, y axis',
|
|
||||||
slot='GRPY',
|
|
||||||
io='ips',
|
|
||||||
tolerance=0.0001,
|
|
||||||
wait_stable_field=0.0,
|
|
||||||
nunits=2,
|
|
||||||
target=Param(max=0.6),
|
|
||||||
ramp=0.225,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('mfz',
|
|
||||||
'frappy_psi.ips_mercury.Field',
|
|
||||||
'magnetic field, z-axis',
|
|
||||||
slot='GRPZ',
|
|
||||||
io='ips',
|
|
||||||
tolerance=0.0001,
|
|
||||||
target=Param(max=5.2),
|
|
||||||
mode='DRIVEN',
|
|
||||||
ramp=0.52,
|
|
||||||
)
|
|
@ -1,52 +0,0 @@
|
|||||||
Node('flowsas.psi.ch',
|
|
||||||
'flowsas test motors',
|
|
||||||
'tcp://5000',
|
|
||||||
)
|
|
||||||
|
|
||||||
#Mod('mot_io',
|
|
||||||
# 'frappy_psi.phytron.PhytronIO',
|
|
||||||
# 'io for motor control',
|
|
||||||
# uri = 'serial:///dev/ttyUSB0',
|
|
||||||
# )
|
|
||||||
|
|
||||||
#Mod('hmot',
|
|
||||||
# 'frappy_psi.phytron.Motor',
|
|
||||||
# 'horizontal axis',
|
|
||||||
# axis = 'X',
|
|
||||||
# io = 'mot_io',
|
|
||||||
# encoder_mode= 'NO',
|
|
||||||
# )
|
|
||||||
|
|
||||||
#Mod('vmot',
|
|
||||||
# 'frappy_psi.phytron.Motor',
|
|
||||||
# 'vertical axis',
|
|
||||||
# axis = 'Y',
|
|
||||||
# io = 'mot_io',
|
|
||||||
# encoder_mode= 'NO',
|
|
||||||
# )
|
|
||||||
|
|
||||||
Mod('syr_io',
|
|
||||||
'frappy_psi.cetoni_pump.LabCannBus',
|
|
||||||
'Module for bus',
|
|
||||||
deviceconfig = "/home/l_samenv/frappy/cetoniSDK/CETONI_SDK_Raspi_64bit_v20220627/config/dual_pumps",
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('syr1',
|
|
||||||
'frappy_psi.cetoni_pump.SyringePump',
|
|
||||||
'First syringe pump',
|
|
||||||
io='syr_io',
|
|
||||||
pump_name = "Nemesys_S_1_Pump",
|
|
||||||
valve_name = "Nemesys_S_1_Valve",
|
|
||||||
inner_diameter_set = 10,
|
|
||||||
piston_stroke_set = 60,
|
|
||||||
)
|
|
||||||
|
|
||||||
Mod('syr2',
|
|
||||||
'frappy_psi.cetoni_pump.SyringePump',
|
|
||||||
'Second syringe pump',
|
|
||||||
io='syr_io',
|
|
||||||
pump_name = "Nemesys_S_2_Pump",
|
|
||||||
valve_name = "Nemesys_S_2_Valve",
|
|
||||||
inner_diameter_set = 1,
|
|
||||||
piston_stroke_set = 60,
|
|
||||||
)
|
|
16
cfg/lockin830_cfg.py
Normal file
16
cfg/lockin830_cfg.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Node('lockin830test.psi.ch',
|
||||||
|
'lockin830 test',
|
||||||
|
'tcp://5000',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('io',
|
||||||
|
'frappy_psi.SR830.SR830_IO',
|
||||||
|
'lockin communication',
|
||||||
|
uri='tcp://linse-976d-ts:3002',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('XY',
|
||||||
|
'frappy_psi.SR830.XY',
|
||||||
|
'XY channels',
|
||||||
|
io='io',
|
||||||
|
)
|
34
cfg/multimeter_cfg.py
Normal file
34
cfg/multimeter_cfg.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
Node('multimetertest.psi.ch',
|
||||||
|
'multimeter test',
|
||||||
|
'tcp://5000',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('io',
|
||||||
|
'frappy_psi.HP.HP_IO',
|
||||||
|
'multimeter communication',
|
||||||
|
uri='/dev/cu.usbserial-21410',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('Voltage',
|
||||||
|
'frappy_psi.HP.Voltage',
|
||||||
|
'voltage',
|
||||||
|
io='io',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('Current',
|
||||||
|
'frappy_psi.HP.Current',
|
||||||
|
'current',
|
||||||
|
io='io',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('Resistance',
|
||||||
|
'frappy_psi.HP.Resistance',
|
||||||
|
'resistivity',
|
||||||
|
io='io',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('Frequency',
|
||||||
|
'frappy_psi.HP.Frequency',
|
||||||
|
'resistivity',
|
||||||
|
io='io',
|
||||||
|
)
|
67
cfg/sim921_cfg.py
Normal file
67
cfg/sim921_cfg.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
Node('bridge.psi.ch',
|
||||||
|
'ac resistance bridge',
|
||||||
|
'tcp://5000',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('io',
|
||||||
|
'frappy_psi.bridge.BridgeIO',
|
||||||
|
'communication to sim900',
|
||||||
|
uri='serial:///dev/cu.usbserial-14340',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('res1',
|
||||||
|
'frappy_psi.bridge.Resistance',
|
||||||
|
'module communication',
|
||||||
|
io='io',
|
||||||
|
port=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('res2',
|
||||||
|
'frappy_psi.bridge.Resistance',
|
||||||
|
'module communication',
|
||||||
|
io='io',
|
||||||
|
port=3,
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('res3',
|
||||||
|
'frappy_psi.bridge.Resistance',
|
||||||
|
'module communication',
|
||||||
|
io='io',
|
||||||
|
port=5,
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('phase1',
|
||||||
|
'frappy_psi.bridge.Phase',
|
||||||
|
'module communication',
|
||||||
|
resistance='res1',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('phase2',
|
||||||
|
'frappy_psi.bridge.Phase',
|
||||||
|
'module communication',
|
||||||
|
resistance='res2',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('phase3',
|
||||||
|
'frappy_psi.bridge.Phase',
|
||||||
|
'module communication',
|
||||||
|
resistance='res3',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('dev1',
|
||||||
|
'frappy_psi.bridge.Deviation',
|
||||||
|
'module communication',
|
||||||
|
resistance='res1',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('dev2',
|
||||||
|
'frappy_psi.bridge.Deviation',
|
||||||
|
'module communication',
|
||||||
|
resistance='res1',
|
||||||
|
)
|
||||||
|
|
||||||
|
Mod('dev3',
|
||||||
|
'frappy_psi.bridge.Deviation',
|
||||||
|
'module communication',
|
||||||
|
resistance='res3',
|
||||||
|
)
|
@ -138,6 +138,13 @@ Mod('T_one_K',
|
|||||||
io='itc',
|
io='itc',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Mod('htr_one_K',
|
||||||
|
'frappy_psi.mercury.HeaterOutput',
|
||||||
|
'1 K plate warmup heater',
|
||||||
|
slot='DB3.H1',
|
||||||
|
io='itc',
|
||||||
|
)
|
||||||
|
|
||||||
Mod('T_mix_wup',
|
Mod('T_mix_wup',
|
||||||
'frappy_psi.mercury.TemperatureLoop',
|
'frappy_psi.mercury.TemperatureLoop',
|
||||||
'mix. chamber warmup temperature',
|
'mix. chamber warmup temperature',
|
||||||
|
@ -282,7 +282,6 @@ class SecopClient(ProxyClient):
|
|||||||
self.nodename = uri
|
self.nodename = uri
|
||||||
self._lock = RLock()
|
self._lock = RLock()
|
||||||
self._shutdown = Event()
|
self._shutdown = Event()
|
||||||
self.cleanup = []
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
try:
|
||||||
@ -298,10 +297,6 @@ class SecopClient(ProxyClient):
|
|||||||
with self._lock:
|
with self._lock:
|
||||||
if self.io:
|
if self.io:
|
||||||
return
|
return
|
||||||
self.txq = queue.Queue(30)
|
|
||||||
self.pending = queue.Queue(30)
|
|
||||||
self.active_requests.clear()
|
|
||||||
self.cleanup.clear()
|
|
||||||
if self.online:
|
if self.online:
|
||||||
self._set_state(True, 'reconnecting')
|
self._set_state(True, 'reconnecting')
|
||||||
else:
|
else:
|
||||||
@ -373,12 +368,6 @@ class SecopClient(ProxyClient):
|
|||||||
noactivity = 0
|
noactivity = 0
|
||||||
try:
|
try:
|
||||||
while self._running:
|
while self._running:
|
||||||
while self.cleanup:
|
|
||||||
entry = self.cleanup.pop()
|
|
||||||
for key, prev in self.active_requests.items():
|
|
||||||
if prev is entry:
|
|
||||||
self.active_requests.pop(key)
|
|
||||||
break
|
|
||||||
# may raise ConnectionClosed
|
# may raise ConnectionClosed
|
||||||
reply = self.io.readline()
|
reply = self.io.readline()
|
||||||
if reply is None:
|
if reply is None:
|
||||||
@ -416,14 +405,6 @@ class SecopClient(ProxyClient):
|
|||||||
self.updateValue(module, param, value, timestamp, readerror)
|
self.updateValue(module, param, value, timestamp, readerror)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass # ignore updates of unknown parameters
|
pass # ignore updates of unknown parameters
|
||||||
except Exception as e:
|
|
||||||
self.log.debug(f'error when updating %s:%s %r', module, param, value)
|
|
||||||
try:
|
|
||||||
# catch errors in callback functions
|
|
||||||
self.updateValue(module, param, None, timestamp,
|
|
||||||
type(e)(f'{e} - raised on client side'))
|
|
||||||
except Exception as ee:
|
|
||||||
self.log.warn(f'can not handle error update %r for %s:%s: %r', e, module, param, ee)
|
|
||||||
if action in (EVENTREPLY, ERRORPREFIX + EVENTREPLY):
|
if action in (EVENTREPLY, ERRORPREFIX + EVENTREPLY):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
@ -610,10 +591,8 @@ class SecopClient(ProxyClient):
|
|||||||
def get_reply(self, entry):
|
def get_reply(self, entry):
|
||||||
"""wait for reply and return it"""
|
"""wait for reply and return it"""
|
||||||
if not entry[1].wait(10): # event
|
if not entry[1].wait(10): # event
|
||||||
self.cleanup.append(entry)
|
|
||||||
raise TimeoutError('no response within 10s')
|
raise TimeoutError('no response within 10s')
|
||||||
if not entry[2]: # reply
|
if not entry[2]: # reply
|
||||||
# no cleanup needed as self.active_requests will be cleared on connect
|
|
||||||
raise ConnectionError('connection closed before reply')
|
raise ConnectionError('connection closed before reply')
|
||||||
action, _, data = entry[2] # pylint: disable=unpacking-non-sequence
|
action, _, data = entry[2] # pylint: disable=unpacking-non-sequence
|
||||||
if action.startswith(ERRORPREFIX):
|
if action.startswith(ERRORPREFIX):
|
||||||
|
@ -232,7 +232,7 @@ class ReadFailedError(SECoPError):
|
|||||||
|
|
||||||
|
|
||||||
class OutOfRangeError(SECoPError):
|
class OutOfRangeError(SECoPError):
|
||||||
"""The value read from the hardware is out of sensor or calibration range"""
|
"""The requested parameter can not be read just now"""
|
||||||
name = 'OutOfRange'
|
name = 'OutOfRange'
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,304 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
|
||||||
#
|
|
||||||
# *****************************************************************************
|
|
||||||
"""extended parameters
|
|
||||||
|
|
||||||
special parameter classes with some automatic functionality
|
|
||||||
"""
|
|
||||||
|
|
||||||
import re
|
|
||||||
from frappy.core import Parameter, Property
|
|
||||||
from frappy.datatypes import BoolType, DataType, DataTypeType, EnumType, \
|
|
||||||
FloatRange, StringType, StructOf, ValueType
|
|
||||||
from frappy.errors import ProgrammingError
|
|
||||||
|
|
||||||
|
|
||||||
class StructParam(Parameter):
|
|
||||||
"""convenience class to create a struct Parameter together with individual params
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
|
|
||||||
class Controller(Drivable):
|
|
||||||
|
|
||||||
...
|
|
||||||
|
|
||||||
ctrlpars = StructParam('ctrlpars struct', [
|
|
||||||
('pid_p', 'p', Parameter('control parameter p', FloatRange())),
|
|
||||||
('pid_i', 'i', Parameter('control parameter i', FloatRange())),
|
|
||||||
('pid_d', 'd', Parameter('control parameter d', FloatRange())),
|
|
||||||
], readonly=False)
|
|
||||||
|
|
||||||
...
|
|
||||||
|
|
||||||
then implement either read_ctrlpars and write_ctrlpars or
|
|
||||||
read_pid_p, read_pid_i, read_pid_d, write_pid_p, write_pid_i and write_pid_d
|
|
||||||
|
|
||||||
the methods not implemented will be created automatically
|
|
||||||
"""
|
|
||||||
|
|
||||||
# use properties, as simple attributes are not considered on copy()
|
|
||||||
paramdict = Property('dict <parametername> of Parameter(...)', ValueType())
|
|
||||||
hasStructRW = Property('has a read_<struct param> or write_<struct param> method',
|
|
||||||
BoolType(), default=False)
|
|
||||||
|
|
||||||
insideRW = 0 # counter for avoiding multiple superfluous updates
|
|
||||||
|
|
||||||
def __init__(self, description=None, paramdict=None, prefix_or_map='', *, datatype=None, readonly=False, **kwds):
|
|
||||||
"""create a struct parameter together with individual parameters
|
|
||||||
|
|
||||||
in addition to normal Parameter arguments:
|
|
||||||
|
|
||||||
:param paramdict: dict <member name> of Parameter(...)
|
|
||||||
:param prefix_or_map: either a prefix for the parameter name to add to the member name
|
|
||||||
or a dict <member name> or <parameter name>
|
|
||||||
"""
|
|
||||||
if isinstance(paramdict, DataType):
|
|
||||||
raise ProgrammingError('second argument must be a dict of Param')
|
|
||||||
if datatype is None and paramdict is not None: # omit the following on Parameter.copy()
|
|
||||||
if isinstance(prefix_or_map, str):
|
|
||||||
prefix_or_map = {m: prefix_or_map + m for m in paramdict}
|
|
||||||
for membername, param in paramdict.items():
|
|
||||||
param.name = prefix_or_map[membername]
|
|
||||||
datatype = StructOf(**{m: p.datatype for m, p in paramdict.items()})
|
|
||||||
kwds['influences'] = [p.name for p in paramdict.values()]
|
|
||||||
self.updateEnable = {}
|
|
||||||
if paramdict:
|
|
||||||
kwds['paramdict'] = paramdict
|
|
||||||
super().__init__(description, datatype, readonly=readonly, **kwds)
|
|
||||||
|
|
||||||
def __set_name__(self, owner, name):
|
|
||||||
# names of access methods of structed param (e.g. ctrlpars)
|
|
||||||
struct_read_name = f'read_{name}' # e.g. 'read_ctrlpars'
|
|
||||||
struct_write_name = f'write_{name}' # e.h. 'write_ctrlpars'
|
|
||||||
self.hasStructRW = hasattr(owner, struct_read_name) or hasattr(owner, struct_write_name)
|
|
||||||
|
|
||||||
for membername, param in self.paramdict.items():
|
|
||||||
pname = param.name
|
|
||||||
changes = {
|
|
||||||
'readonly': self.readonly,
|
|
||||||
'influences': set(param.influences) | {name},
|
|
||||||
}
|
|
||||||
param.ownProperties.update(changes)
|
|
||||||
param.init(changes)
|
|
||||||
setattr(owner, pname, param)
|
|
||||||
param.__set_name__(owner, param.name)
|
|
||||||
|
|
||||||
if self.hasStructRW:
|
|
||||||
rname = f'read_{pname}'
|
|
||||||
|
|
||||||
if not hasattr(owner, rname):
|
|
||||||
def rfunc(self, membername=membername, struct_read_name=struct_read_name):
|
|
||||||
return getattr(self, struct_read_name)()[membername]
|
|
||||||
|
|
||||||
rfunc.poll = False # read_<struct param> is polled only
|
|
||||||
setattr(owner, rname, rfunc)
|
|
||||||
|
|
||||||
if not self.readonly:
|
|
||||||
wname = f'write_{pname}'
|
|
||||||
if not hasattr(owner, wname):
|
|
||||||
def wfunc(self, value, membername=membername,
|
|
||||||
name=name, rname=rname, struct_write_name=struct_write_name):
|
|
||||||
valuedict = dict(getattr(self, name))
|
|
||||||
valuedict[membername] = value
|
|
||||||
getattr(self, struct_write_name)(valuedict)
|
|
||||||
return getattr(self, rname)()
|
|
||||||
|
|
||||||
setattr(owner, wname, wfunc)
|
|
||||||
|
|
||||||
if not self.hasStructRW:
|
|
||||||
if not hasattr(owner, struct_read_name):
|
|
||||||
def struct_read_func(self, name=name, flist=tuple(
|
|
||||||
(m, f'read_{p.name}') for m, p in self.paramdict.items())):
|
|
||||||
pobj = self.parameters[name]
|
|
||||||
# disable updates generated from the callbacks of individual params
|
|
||||||
pobj.insideRW += 1 # guarded by self.accessLock
|
|
||||||
try:
|
|
||||||
return {m: getattr(self, f)() for m, f in flist}
|
|
||||||
finally:
|
|
||||||
pobj.insideRW -= 1
|
|
||||||
|
|
||||||
setattr(owner, struct_read_name, struct_read_func)
|
|
||||||
|
|
||||||
if not (self.readonly or hasattr(owner, struct_write_name)):
|
|
||||||
|
|
||||||
def struct_write_func(self, value, name=name, funclist=tuple(
|
|
||||||
(m, f'write_{p.name}') for m, p in self.paramdict.items())):
|
|
||||||
pobj = self.parameters[name]
|
|
||||||
pobj.insideRW += 1 # guarded by self.accessLock
|
|
||||||
try:
|
|
||||||
return {m: getattr(self, f)(value[m]) for m, f in funclist}
|
|
||||||
finally:
|
|
||||||
pobj.insideRW -= 1
|
|
||||||
|
|
||||||
setattr(owner, struct_write_name, struct_write_func)
|
|
||||||
|
|
||||||
super().__set_name__(owner, name)
|
|
||||||
|
|
||||||
def finish(self, modobj=None):
|
|
||||||
"""register callbacks for consistency"""
|
|
||||||
super().finish(modobj)
|
|
||||||
if modobj:
|
|
||||||
|
|
||||||
if self.hasStructRW:
|
|
||||||
def cb(value, modobj=modobj, structparam=self):
|
|
||||||
for membername, param in structparam.paramdict.items():
|
|
||||||
setattr(modobj, param.name, value[membername])
|
|
||||||
|
|
||||||
modobj.addCallback(self.name, cb)
|
|
||||||
else:
|
|
||||||
for membername, param in self.paramdict.items():
|
|
||||||
def cb(value, modobj=modobj, structparam=self, membername=membername):
|
|
||||||
if not structparam.insideRW:
|
|
||||||
prev = dict(getattr(modobj, structparam.name))
|
|
||||||
prev[membername] = value
|
|
||||||
setattr(modobj, structparam.name, prev)
|
|
||||||
|
|
||||||
modobj.addCallback(param.name, cb)
|
|
||||||
|
|
||||||
|
|
||||||
class FloatEnumParam(Parameter):
|
|
||||||
"""combine enum and float parameter
|
|
||||||
|
|
||||||
Example Usage:
|
|
||||||
|
|
||||||
vrange = FloatEnumParam('sensor range', ['500uV', '20mV', '1V'], 'V')
|
|
||||||
|
|
||||||
The following will be created automatically:
|
|
||||||
|
|
||||||
- the parameter vrange will get a datatype FloatRange(5e-4, 1, unit='V')
|
|
||||||
- an additional parameter `vrange_idx` will be created with an enum type
|
|
||||||
{'500uV': 0, '20mV': 1, '1V': 2}
|
|
||||||
- the method `write_vrange` will be created automatically
|
|
||||||
|
|
||||||
However, the methods `write_vrange_idx` and `read_vrange_idx`, if needed,
|
|
||||||
have to implemented by the programmer.
|
|
||||||
|
|
||||||
Writing to the float parameter involves 'rounding' to the closest allowed value.
|
|
||||||
|
|
||||||
Customization:
|
|
||||||
|
|
||||||
The individual labels might be customized by defining them as a tuple
|
|
||||||
(<index>, <label>, <float value>) where either the index or the float value
|
|
||||||
may be omitted.
|
|
||||||
|
|
||||||
When the index is omitted, the element will be the previous index + 1 or
|
|
||||||
0 when it is the first element.
|
|
||||||
|
|
||||||
Omitted values will be determined from the label, assuming that they use
|
|
||||||
one of the predefined unit prefixes together with the given unit.
|
|
||||||
|
|
||||||
The name of the index parameter is by default '<name>_idx' but might be
|
|
||||||
changed with the idx_name argument.
|
|
||||||
"""
|
|
||||||
# use properties, as simple attributes are not considered on copy()
|
|
||||||
idx_name = Property('name of attached index parameter', StringType(), default='')
|
|
||||||
valuedict = Property('dict <index> of <value>', ValueType(dict))
|
|
||||||
enumtype = Property('dict <label> of <index', DataTypeType())
|
|
||||||
|
|
||||||
# TODO: factor out unit handling, at the latest when needed elsewhere
|
|
||||||
PREFIXES = {'q': -30, 'r': -27, 'y': -24, 'z': -21, 'a': -18, 'f': -15,
|
|
||||||
'p': -12, 'n': -9, 'u': -6, 'µ': -6, 'm': -3,
|
|
||||||
'': 0, 'k': 3, 'M': 6, 'G': 9, 'T': 12,
|
|
||||||
'P': 15, 'E': 18, 'Z': 21, 'Y': 24, 'R': 25, 'Q': 30}
|
|
||||||
|
|
||||||
def __init__(self, description=None, labels=None, unit='',
|
|
||||||
*, datatype=None, readonly=False, **kwds):
|
|
||||||
if labels is None:
|
|
||||||
# called on Parameter.copy()
|
|
||||||
super().__init__(description, datatype, readonly=readonly, **kwds)
|
|
||||||
return
|
|
||||||
if isinstance(labels, DataType):
|
|
||||||
raise ProgrammingError('second argument must be a list of labels, not a datatype')
|
|
||||||
nextidx = 0
|
|
||||||
try:
|
|
||||||
edict = {}
|
|
||||||
vdict = {}
|
|
||||||
for elem in labels:
|
|
||||||
if isinstance(elem, str):
|
|
||||||
idx, label = [nextidx, elem]
|
|
||||||
else:
|
|
||||||
if isinstance(elem[0], str):
|
|
||||||
elem = [nextidx] + list(elem)
|
|
||||||
idx, label, *tail = elem
|
|
||||||
if tail:
|
|
||||||
vdict[idx], = tail
|
|
||||||
edict[label] = idx
|
|
||||||
nextidx = idx + 1
|
|
||||||
except (ValueError, TypeError) as e:
|
|
||||||
raise ProgrammingError('labels must be a list of labels or tuples '
|
|
||||||
'([index], label, [value])') from e
|
|
||||||
pat = re.compile(rf'([+-]?\d*\.?\d*) *({"|".join(self.PREFIXES)}){unit}$')
|
|
||||||
try:
|
|
||||||
# determine missing values from labels
|
|
||||||
for label, idx in edict.items():
|
|
||||||
if idx not in vdict:
|
|
||||||
value, prefix = pat.match(label).groups()
|
|
||||||
vdict[idx] = float(f'{value}e{self.PREFIXES[prefix]}')
|
|
||||||
except (AttributeError, ValueError) as e:
|
|
||||||
raise ProgrammingError(f"{label!r} has not the form '<float><prefix>{unit}'") from e
|
|
||||||
try:
|
|
||||||
enumtype = EnumType(**edict)
|
|
||||||
except TypeError as e:
|
|
||||||
raise ProgrammingError(str(e)) from e
|
|
||||||
datatype = FloatRange(min(vdict.values()), max(vdict.values()), unit=unit)
|
|
||||||
super().__init__(description, datatype, enumtype=enumtype, valuedict=vdict,
|
|
||||||
readonly=readonly, **kwds)
|
|
||||||
|
|
||||||
def __set_name__(self, owner, name):
|
|
||||||
super().__set_name__(owner, name)
|
|
||||||
if not self.idx_name:
|
|
||||||
self.idx_name = name + '_idx'
|
|
||||||
iname = self.idx_name
|
|
||||||
idx_param = Parameter(f'index of {name}', self.enumtype,
|
|
||||||
readonly=self.readonly, influences={name})
|
|
||||||
idx_param.init({})
|
|
||||||
setattr(owner, iname, idx_param)
|
|
||||||
idx_param.__set_name__(owner, iname)
|
|
||||||
|
|
||||||
self.setProperty('influences', {iname})
|
|
||||||
|
|
||||||
if not hasattr(owner, f'write_{name}'):
|
|
||||||
|
|
||||||
# customization (like rounding up or down) might be
|
|
||||||
# achieved by adding write_<name>. if not, the default
|
|
||||||
# is rounding to the closest value
|
|
||||||
|
|
||||||
def wfunc(mobj, value, vdict=self.valuedict, fname=name, wfunc_iname=f'write_{iname}'):
|
|
||||||
getattr(mobj, wfunc_iname)(
|
|
||||||
min(vdict, key=lambda i: abs(vdict[i] - value)))
|
|
||||||
return getattr(mobj, fname)
|
|
||||||
|
|
||||||
setattr(owner, f'write_{name}', wfunc)
|
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
|
||||||
"""getter for value"""
|
|
||||||
if instance is None:
|
|
||||||
return self
|
|
||||||
return self.valuedict[instance.parameters[self.idx_name].value]
|
|
||||||
|
|
||||||
def trigger_setter(self, modobj, _):
|
|
||||||
# trigger update of float parameter on change of enum parameter
|
|
||||||
modobj.announceUpdate(self.name, getattr(modobj, self.name))
|
|
||||||
|
|
||||||
def finish(self, modobj=None):
|
|
||||||
"""register callbacks for consistency"""
|
|
||||||
super().finish(modobj)
|
|
||||||
if modobj:
|
|
||||||
modobj.addCallback(self.idx_name, self.trigger_setter, modobj)
|
|
@ -1,10 +1,6 @@
|
|||||||
import sys
|
from frappy.gui.qt import QCheckBox, QComboBox, QLineEdit, pyqtSignal
|
||||||
|
|
||||||
from frappy.gui.qt import QCheckBox, QComboBox, QDoubleSpinBox, QLineEdit, \
|
from frappy.datatypes import BoolType, EnumType
|
||||||
QSpinBox, pyqtSignal
|
|
||||||
|
|
||||||
from frappy.datatypes import BoolType, EnumType, FloatRange, IntRange, \
|
|
||||||
StringType, TextType
|
|
||||||
|
|
||||||
# ArrayOf, BLOBType, FloatRange, IntRange, StringType, StructOf, TextType, TupleOf
|
# ArrayOf, BLOBType, FloatRange, IntRange, StringType, StructOf, TextType, TupleOf
|
||||||
|
|
||||||
@ -13,24 +9,11 @@ def get_input_widget(datatype, parent=None):
|
|||||||
return {
|
return {
|
||||||
EnumType: EnumInput,
|
EnumType: EnumInput,
|
||||||
BoolType: BoolInput,
|
BoolType: BoolInput,
|
||||||
IntRange: IntInput,
|
|
||||||
StringType: StringInput,
|
|
||||||
TextType: StringInput,
|
|
||||||
}.get(datatype.__class__, GenericInput)(datatype, parent)
|
}.get(datatype.__class__, GenericInput)(datatype, parent)
|
||||||
|
|
||||||
|
|
||||||
class InputBase:
|
class GenericInput(QLineEdit):
|
||||||
submitted = pyqtSignal()
|
submitted = pyqtSignal()
|
||||||
input_feedback = pyqtSignal(str)
|
|
||||||
|
|
||||||
def get_input(self):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def submit(self):
|
|
||||||
self.submitted.emit()
|
|
||||||
|
|
||||||
|
|
||||||
class GenericInput(InputBase, QLineEdit):
|
|
||||||
def __init__(self, datatype, parent=None):
|
def __init__(self, datatype, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.datatype = datatype
|
self.datatype = datatype
|
||||||
@ -40,28 +23,12 @@ class GenericInput(InputBase, QLineEdit):
|
|||||||
def get_input(self):
|
def get_input(self):
|
||||||
return self.datatype.from_string(self.text())
|
return self.datatype.from_string(self.text())
|
||||||
|
|
||||||
|
def submit(self):
|
||||||
class StringInput(GenericInput):
|
self.submitted.emit()
|
||||||
def __init__(self, datatype, parent=None):
|
|
||||||
super().__init__(datatype, parent)
|
|
||||||
|
|
||||||
|
|
||||||
class IntInput(InputBase, QSpinBox):
|
class EnumInput(QComboBox):
|
||||||
def __init__(self, datatype, parent=None):
|
submitted = pyqtSignal()
|
||||||
super().__init__(parent)
|
|
||||||
self.datatype = datatype
|
|
||||||
# we dont use setMaximum and setMinimum because it is quite restrictive
|
|
||||||
# when typing, so set it as high as possible
|
|
||||||
self.setMaximum(2147483647)
|
|
||||||
self.setMinimum(-2147483648)
|
|
||||||
|
|
||||||
self.lineEdit().returnPressed.connect(self.submit)
|
|
||||||
|
|
||||||
def get_input(self):
|
|
||||||
return self.datatype(self.value())
|
|
||||||
|
|
||||||
|
|
||||||
class EnumInput(InputBase, QComboBox):
|
|
||||||
def __init__(self, datatype, parent=None):
|
def __init__(self, datatype, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.setPlaceholderText('choose value')
|
self.setPlaceholderText('choose value')
|
||||||
@ -78,11 +45,18 @@ class EnumInput(InputBase, QComboBox):
|
|||||||
def get_input(self):
|
def get_input(self):
|
||||||
return self._map[self.currentIndex()].value
|
return self._map[self.currentIndex()].value
|
||||||
|
|
||||||
|
def submit(self):
|
||||||
|
self.submitted.emit()
|
||||||
|
|
||||||
class BoolInput(InputBase, QCheckBox):
|
|
||||||
|
class BoolInput(QCheckBox):
|
||||||
|
submitted = pyqtSignal()
|
||||||
def __init__(self, datatype, parent=None):
|
def __init__(self, datatype, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.datatype = datatype
|
self.datatype = datatype
|
||||||
|
|
||||||
def get_input(self):
|
def get_input(self):
|
||||||
return self.isChecked()
|
return self.isChecked()
|
||||||
|
|
||||||
|
def submit(self):
|
||||||
|
self.submitted.emit()
|
||||||
|
@ -24,9 +24,9 @@ from frappy.gui.qt import QColor, QDialog, QHBoxLayout, QIcon, QLabel, \
|
|||||||
QLineEdit, QMessageBox, QPropertyAnimation, QPushButton, Qt, QToolButton, \
|
QLineEdit, QMessageBox, QPropertyAnimation, QPushButton, Qt, QToolButton, \
|
||||||
QWidget, pyqtProperty, pyqtSignal
|
QWidget, pyqtProperty, pyqtSignal
|
||||||
|
|
||||||
from frappy.gui.inputwidgets import get_input_widget
|
|
||||||
from frappy.gui.util import Colors, loadUi
|
from frappy.gui.util import Colors, loadUi
|
||||||
from frappy.gui.valuewidgets import get_widget
|
from frappy.gui.valuewidgets import get_widget
|
||||||
|
from frappy.gui.inputwidgets import get_input_widget
|
||||||
|
|
||||||
|
|
||||||
class CommandDialog(QDialog):
|
class CommandDialog(QDialog):
|
||||||
@ -54,11 +54,7 @@ class CommandDialog(QDialog):
|
|||||||
self.resize(self.sizeHint())
|
self.resize(self.sizeHint())
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
try:
|
return True, self.widgets[0].get_value()
|
||||||
return self.widgets[0].get_value()
|
|
||||||
except Exception as e:
|
|
||||||
QMessageBox.warning(self.parent(), 'Operation failed', str(e))
|
|
||||||
return None
|
|
||||||
|
|
||||||
def exec(self):
|
def exec(self):
|
||||||
if super().exec():
|
if super().exec():
|
||||||
@ -99,9 +95,8 @@ class CommandButton(QPushButton):
|
|||||||
if self._argintype:
|
if self._argintype:
|
||||||
dlg = CommandDialog(self._cmdname, self._argintype)
|
dlg = CommandDialog(self._cmdname, self._argintype)
|
||||||
args = dlg.exec()
|
args = dlg.exec()
|
||||||
if args is not None:
|
if args: # not 'Cancel' clicked
|
||||||
# no errors when converting value and 'Cancel' wasn't clicked
|
self._cb(self._cmdname, args[1])
|
||||||
self._cb(self._cmdname, args)
|
|
||||||
else:
|
else:
|
||||||
# no need for arguments
|
# no need for arguments
|
||||||
self._cb(self._cmdname, None)
|
self._cb(self._cmdname, None)
|
||||||
@ -447,8 +442,8 @@ class ModuleWidget(QWidget):
|
|||||||
self.paramDetails.emit(self._name, param)
|
self.paramDetails.emit(self._name, param)
|
||||||
|
|
||||||
def _button_pressed(self, param):
|
def _button_pressed(self, param):
|
||||||
try:
|
|
||||||
target = self._paramInputs[param].get_input()
|
target = self._paramInputs[param].get_input()
|
||||||
|
try:
|
||||||
self._node.setParameter(self._name, param, target)
|
self._node.setParameter(self._name, param, target)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
QMessageBox.warning(self.parent(), 'Operation failed', str(e))
|
QMessageBox.warning(self.parent(), 'Operation failed', str(e))
|
||||||
|
@ -42,10 +42,10 @@ try:
|
|||||||
QDialogButtonBox, QDoubleSpinBox, QFileDialog, QFrame, QGridLayout, \
|
QDialogButtonBox, QDoubleSpinBox, QFileDialog, QFrame, QGridLayout, \
|
||||||
QGroupBox, QHBoxLayout, QInputDialog, QLabel, QLineEdit, QMainWindow, \
|
QGroupBox, QHBoxLayout, QInputDialog, QLabel, QLineEdit, QMainWindow, \
|
||||||
QMenu, QMessageBox, QPlainTextEdit, QPushButton, QRadioButton, \
|
QMenu, QMessageBox, QPlainTextEdit, QPushButton, QRadioButton, \
|
||||||
QScrollArea, QSizePolicy, QSlider, QSpacerItem, QSpinBox, QStyle, \
|
QScrollArea, QSizePolicy, QSpacerItem, QSpinBox, QStyle, \
|
||||||
QStyleOptionTab, QStylePainter, QTabBar, QTabWidget, QTextEdit, \
|
QStyleOptionTab, QStylePainter, QTabBar, QTabWidget, QTextEdit, \
|
||||||
QToolButton, QTreeView, QTreeWidget, QTreeWidgetItem, QVBoxLayout, \
|
QToolButton, QTreeView, QTreeWidget, QTreeWidgetItem, QVBoxLayout, \
|
||||||
QWidget
|
QWidget,QSlider
|
||||||
|
|
||||||
import frappy.gui.resources_qt6
|
import frappy.gui.resources_qt6
|
||||||
|
|
||||||
@ -62,9 +62,9 @@ except ImportError as e:
|
|||||||
QDialog, QDialogButtonBox, QDoubleSpinBox, QFileDialog, QFrame, \
|
QDialog, QDialogButtonBox, QDoubleSpinBox, QFileDialog, QFrame, \
|
||||||
QGridLayout, QGroupBox, QHBoxLayout, QInputDialog, QLabel, QLineEdit, \
|
QGridLayout, QGroupBox, QHBoxLayout, QInputDialog, QLabel, QLineEdit, \
|
||||||
QMainWindow, QMenu, QMessageBox, QPlainTextEdit, QPushButton, \
|
QMainWindow, QMenu, QMessageBox, QPlainTextEdit, QPushButton, \
|
||||||
QRadioButton, QScrollArea, QShortcut, QSizePolicy, QSlider, \
|
QRadioButton, QScrollArea, QShortcut, QSizePolicy, QSpacerItem, \
|
||||||
QSpacerItem, QSpinBox, QStyle, QStyleOptionTab, QStylePainter, \
|
QSpinBox, QStyle, QStyleOptionTab, QStylePainter, QTabBar, \
|
||||||
QTabBar, QTabWidget, QTextEdit, QToolButton, QTreeView, QTreeWidget, \
|
QTabWidget, QTextEdit, QToolButton, QTreeView, QTreeWidget, \
|
||||||
QTreeWidgetItem, QVBoxLayout, QWidget
|
QTreeWidgetItem, QVBoxLayout, QWidget, QSlider
|
||||||
|
|
||||||
import frappy.gui.resources_qt5
|
import frappy.gui.resources_qt5
|
||||||
|
@ -61,6 +61,7 @@ class HasIO(Module):
|
|||||||
ioname = opts.get('io') or f'{name}_io'
|
ioname = opts.get('io') or f'{name}_io'
|
||||||
io = self.ioClass(ioname, srv.log.getChild(ioname), opts, srv) # pylint: disable=not-callable
|
io = self.ioClass(ioname, srv.log.getChild(ioname), opts, srv) # pylint: disable=not-callable
|
||||||
io.callingModule = []
|
io.callingModule = []
|
||||||
|
srv.modules[ioname] = io
|
||||||
srv.secnode.add_module(io, ioname)
|
srv.secnode.add_module(io, ioname)
|
||||||
self.ioDict[self.uri] = ioname
|
self.ioDict[self.uri] = ioname
|
||||||
self.io = ioname
|
self.io = ioname
|
||||||
|
@ -141,7 +141,6 @@ class SequencerMixin:
|
|||||||
return self.Status.IDLE, ''
|
return self.Status.IDLE, ''
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop sequence"""
|
|
||||||
if self.seq_is_alive():
|
if self.seq_is_alive():
|
||||||
self._seq_stopflag = True
|
self._seq_stopflag = True
|
||||||
|
|
||||||
|
@ -83,14 +83,15 @@ class HasAccessibles(HasProperties):
|
|||||||
override_values.pop(key, None)
|
override_values.pop(key, None)
|
||||||
elif key in accessibles:
|
elif key in accessibles:
|
||||||
override_values[key] = value
|
override_values[key] = value
|
||||||
# remark: merged_properties contain already the properties of accessibles of cls
|
|
||||||
for aname, aobj in list(accessibles.items()):
|
for aname, aobj in list(accessibles.items()):
|
||||||
if aname in override_values:
|
if aname in override_values:
|
||||||
|
aobj = aobj.copy()
|
||||||
value = override_values[aname]
|
value = override_values[aname]
|
||||||
if value is None:
|
if value is None:
|
||||||
accessibles.pop(aname)
|
accessibles.pop(aname)
|
||||||
continue
|
continue
|
||||||
aobj = aobj.create_from_value(merged_properties[aname], value)
|
aobj.merge(merged_properties[aname])
|
||||||
|
aobj.override(value)
|
||||||
# replace the bare value by the created accessible
|
# replace the bare value by the created accessible
|
||||||
setattr(cls, aname, aobj)
|
setattr(cls, aname, aobj)
|
||||||
else:
|
else:
|
||||||
@ -333,7 +334,8 @@ class Module(HasAccessibles):
|
|||||||
self.secNode = srv.secnode
|
self.secNode = srv.secnode
|
||||||
self.log = logger
|
self.log = logger
|
||||||
self.name = name
|
self.name = name
|
||||||
self.paramCallbacks = {}
|
self.valueCallbacks = {}
|
||||||
|
self.errorCallbacks = {}
|
||||||
self.earlyInitDone = False
|
self.earlyInitDone = False
|
||||||
self.initModuleDone = False
|
self.initModuleDone = False
|
||||||
self.startModuleDone = False
|
self.startModuleDone = False
|
||||||
@ -467,7 +469,8 @@ class Module(HasAccessibles):
|
|||||||
apply default when no value is given (in cfg or as Parameter argument)
|
apply default when no value is given (in cfg or as Parameter argument)
|
||||||
or complain, when cfg is needed
|
or complain, when cfg is needed
|
||||||
"""
|
"""
|
||||||
self.paramCallbacks[pname] = []
|
self.valueCallbacks[pname] = []
|
||||||
|
self.errorCallbacks[pname] = []
|
||||||
if isinstance(pobj, Limit):
|
if isinstance(pobj, Limit):
|
||||||
basepname = pname.rpartition('_')[0]
|
basepname = pname.rpartition('_')[0]
|
||||||
baseparam = self.parameters.get(basepname)
|
baseparam = self.parameters.get(basepname)
|
||||||
@ -532,46 +535,68 @@ class Module(HasAccessibles):
|
|||||||
err.report_error = False
|
err.report_error = False
|
||||||
return # no updates for repeated errors
|
return # no updates for repeated errors
|
||||||
err = secop_error(err)
|
err = secop_error(err)
|
||||||
value_err = value, err
|
elif not changed and timestamp < (pobj.timestamp or 0) + pobj.omit_unchanged_within:
|
||||||
else:
|
|
||||||
if not changed and timestamp < (pobj.timestamp or 0) + pobj.omit_unchanged_within:
|
|
||||||
# no change within short time -> omit
|
# no change within short time -> omit
|
||||||
return
|
return
|
||||||
value_err = (value,)
|
|
||||||
pobj.timestamp = timestamp or time.time()
|
pobj.timestamp = timestamp or time.time()
|
||||||
pobj.readerror = err
|
if err:
|
||||||
for cbfunc, cbargs in self.paramCallbacks[pname]:
|
callbacks = self.errorCallbacks
|
||||||
try:
|
pobj.readerror = arg = err
|
||||||
cbfunc(*cbargs, *value_err)
|
else:
|
||||||
except Exception:
|
callbacks = self.valueCallbacks
|
||||||
pass
|
arg = value
|
||||||
|
pobj.readerror = None
|
||||||
if pobj.export:
|
if pobj.export:
|
||||||
self.updateCallback(self, pobj)
|
self.updateCallback(self, pobj)
|
||||||
|
cblist = callbacks[pname]
|
||||||
def addCallback(self, pname, callback_function, *args):
|
for cb in cblist:
|
||||||
self.paramCallbacks[pname].append((callback_function, args))
|
try:
|
||||||
|
cb(arg)
|
||||||
|
except Exception:
|
||||||
|
# print(formatExtendedTraceback())
|
||||||
|
pass
|
||||||
|
|
||||||
def registerCallbacks(self, modobj, autoupdate=()):
|
def registerCallbacks(self, modobj, autoupdate=()):
|
||||||
"""register callbacks to another module <modobj>
|
"""register callbacks to another module <modobj>
|
||||||
|
|
||||||
whenever a self.<param> changes or changes its error state:
|
- whenever a self.<param> changes:
|
||||||
<modobj>.update_param(<value> [, <exc>]) is called,
|
<modobj>.update_<param> is called with the new value as argument.
|
||||||
where <value> is the new value and <exc> is given only in case of error.
|
If this method raises an exception, <modobj>.<param> gets into an error state.
|
||||||
if the method does not exist, and <param> is in autoupdate
|
If the method does not exist and <param> is in autoupdate,
|
||||||
<modobj>.announceUpdate(<pname>, <value>, <exc>) is called
|
<modobj>.<param> is updated to self.<param>
|
||||||
with <exc> being None in case of no error.
|
- whenever <self>.<param> gets into an error state:
|
||||||
|
<modobj>.error_update_<param> is called with the exception as argument.
|
||||||
Remark: when <modobj>.update_<param> does not accept the <exc> argument,
|
If this method raises an error, <modobj>.<param> gets into an error state.
|
||||||
nothing happens (the callback is catched by try / except).
|
If this method does not exist, and <param> is in autoupdate,
|
||||||
Any exceptions raised by the callback function are silently ignored.
|
<modobj>.<param> gets into the same error state as self.<param>
|
||||||
"""
|
"""
|
||||||
autoupdate = set(autoupdate)
|
|
||||||
for pname in self.parameters:
|
for pname in self.parameters:
|
||||||
cbfunc = getattr(modobj, 'update_' + pname, None)
|
errfunc = getattr(modobj, 'error_update_' + pname, None)
|
||||||
if cbfunc:
|
if errfunc:
|
||||||
self.addCallback(pname, cbfunc)
|
def errcb(err, p=pname, efunc=errfunc):
|
||||||
|
try:
|
||||||
|
efunc(err)
|
||||||
|
except Exception as e:
|
||||||
|
modobj.announceUpdate(p, err=e)
|
||||||
|
self.errorCallbacks[pname].append(errcb)
|
||||||
|
else:
|
||||||
|
def errcb(err, p=pname):
|
||||||
|
modobj.announceUpdate(p, err=err)
|
||||||
|
if pname in autoupdate:
|
||||||
|
self.errorCallbacks[pname].append(errcb)
|
||||||
|
|
||||||
|
updfunc = getattr(modobj, 'update_' + pname, None)
|
||||||
|
if updfunc:
|
||||||
|
def cb(value, ufunc=updfunc, efunc=errcb):
|
||||||
|
try:
|
||||||
|
ufunc(value)
|
||||||
|
except Exception as e:
|
||||||
|
efunc(e)
|
||||||
|
self.valueCallbacks[pname].append(cb)
|
||||||
elif pname in autoupdate:
|
elif pname in autoupdate:
|
||||||
self.addCallback(pname, modobj.announceUpdate, pname)
|
def cb(value, p=pname):
|
||||||
|
modobj.announceUpdate(p, value)
|
||||||
|
self.valueCallbacks[pname].append(cb)
|
||||||
|
|
||||||
def isBusy(self, status=None):
|
def isBusy(self, status=None):
|
||||||
"""helper function for treating substates of BUSY correctly"""
|
"""helper function for treating substates of BUSY correctly"""
|
||||||
@ -589,10 +614,6 @@ class Module(HasAccessibles):
|
|||||||
# enablePoll == False: we still need the poll thread for writing values from writeDict
|
# enablePoll == False: we still need the poll thread for writing values from writeDict
|
||||||
if hasattr(self, 'io'):
|
if hasattr(self, 'io'):
|
||||||
self.io.polledModules.append(self)
|
self.io.polledModules.append(self)
|
||||||
if not self.io.triggerPoll:
|
|
||||||
# when self.io.enablePoll is False, triggerPoll is not
|
|
||||||
# created for self.io in the else clause below
|
|
||||||
self.io.triggerPoll = threading.Event()
|
|
||||||
else:
|
else:
|
||||||
self.triggerPoll = threading.Event()
|
self.triggerPoll = threading.Event()
|
||||||
self.polledModules.append(self)
|
self.polledModules.append(self)
|
||||||
@ -692,8 +713,8 @@ class Module(HasAccessibles):
|
|||||||
for mobj in polled_modules:
|
for mobj in polled_modules:
|
||||||
pinfo = mobj.pollInfo = PollInfo(mobj.pollinterval, self.triggerPoll)
|
pinfo = mobj.pollInfo = PollInfo(mobj.pollinterval, self.triggerPoll)
|
||||||
# trigger a poll interval change when self.pollinterval changes.
|
# trigger a poll interval change when self.pollinterval changes.
|
||||||
if 'pollinterval' in mobj.paramCallbacks:
|
if 'pollinterval' in mobj.valueCallbacks:
|
||||||
mobj.addCallback('pollinterval', pinfo.update_interval)
|
mobj.valueCallbacks['pollinterval'].append(pinfo.update_interval)
|
||||||
|
|
||||||
for pname, pobj in mobj.parameters.items():
|
for pname, pobj in mobj.parameters.items():
|
||||||
rfunc = getattr(mobj, 'read_' + pname)
|
rfunc = getattr(mobj, 'read_' + pname)
|
||||||
|
@ -36,7 +36,6 @@ from .modulebase import Module
|
|||||||
|
|
||||||
class Readable(Module):
|
class Readable(Module):
|
||||||
"""basic readable module"""
|
"""basic readable module"""
|
||||||
# pylint: disable=invalid-name
|
|
||||||
Status = Enum('Status',
|
Status = Enum('Status',
|
||||||
IDLE=StatusType.IDLE,
|
IDLE=StatusType.IDLE,
|
||||||
WARN=StatusType.WARN,
|
WARN=StatusType.WARN,
|
||||||
@ -93,7 +92,7 @@ class Drivable(Writable):
|
|||||||
|
|
||||||
@Command(None, result=None)
|
@Command(None, result=None)
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""not implemented - this is a no-op"""
|
"""cease driving, go to IDLE state"""
|
||||||
|
|
||||||
|
|
||||||
class Communicator(HasComlog, Module):
|
class Communicator(HasComlog, Module):
|
||||||
|
@ -57,17 +57,13 @@ class Accessible(HasProperties):
|
|||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
return self.propertyValues
|
return self.propertyValues
|
||||||
|
|
||||||
def create_from_value(self, properties, value):
|
def override(self, value):
|
||||||
"""return a clone with given value and inherited properties"""
|
"""override with a bare value"""
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def clone(self, properties, **kwds):
|
|
||||||
"""return a clone of ourselfs with inherited properties"""
|
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
"""return a (deep) copy of ourselfs"""
|
"""return a (deep) copy of ourselfs"""
|
||||||
return self.clone(self.propertyValues)
|
raise NotImplementedError
|
||||||
|
|
||||||
def updateProperties(self, merged_properties):
|
def updateProperties(self, merged_properties):
|
||||||
"""update merged_properties with our own properties"""
|
"""update merged_properties with our own properties"""
|
||||||
@ -98,16 +94,6 @@ class Accessible(HasProperties):
|
|||||||
props.append(f'{k}={v!r}')
|
props.append(f'{k}={v!r}')
|
||||||
return f"{self.__class__.__name__}({', '.join(props)})"
|
return f"{self.__class__.__name__}({', '.join(props)})"
|
||||||
|
|
||||||
def fixExport(self):
|
|
||||||
if self.export is True:
|
|
||||||
predefined_cls = PREDEFINED_ACCESSIBLES.get(self.name)
|
|
||||||
if predefined_cls is None:
|
|
||||||
self.export = '_' + self.name
|
|
||||||
elif isinstance(self, predefined_cls):
|
|
||||||
self.export = self.name
|
|
||||||
else:
|
|
||||||
raise ProgrammingError(f'can not use {self.name!r} as name of a {type(self).__name__}')
|
|
||||||
|
|
||||||
|
|
||||||
class Parameter(Accessible):
|
class Parameter(Accessible):
|
||||||
"""defines a parameter
|
"""defines a parameter
|
||||||
@ -235,17 +221,26 @@ class Parameter(Accessible):
|
|||||||
self.name = name
|
self.name = name
|
||||||
if isinstance(self.datatype, EnumType):
|
if isinstance(self.datatype, EnumType):
|
||||||
self.datatype.set_name(name)
|
self.datatype.set_name(name)
|
||||||
self.fixExport()
|
|
||||||
|
|
||||||
def clone(self, properties, **kwds):
|
if self.export is True:
|
||||||
"""return a clone of ourselfs with inherited properties"""
|
predefined_cls = PREDEFINED_ACCESSIBLES.get(self.name, None)
|
||||||
res = type(self)(**kwds)
|
if predefined_cls is Parameter:
|
||||||
|
self.export = self.name
|
||||||
|
elif predefined_cls is None:
|
||||||
|
self.export = '_' + self.name
|
||||||
|
else:
|
||||||
|
raise ProgrammingError(f'can not use {self.name!r} as name of a Parameter')
|
||||||
|
if 'export' in self.ownProperties:
|
||||||
|
# avoid export=True overrides export=<name>
|
||||||
|
self.ownProperties['export'] = self.export
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
"""return a (deep) copy of ourselfs"""
|
||||||
|
res = type(self)()
|
||||||
res.name = self.name
|
res.name = self.name
|
||||||
res.init(properties)
|
res.init(self.propertyValues)
|
||||||
res.init(res.ownProperties)
|
|
||||||
if 'datatype' in self.propertyValues:
|
if 'datatype' in self.propertyValues:
|
||||||
res.datatype = res.datatype.copy()
|
res.datatype = res.datatype.copy()
|
||||||
res.finish()
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def updateProperties(self, merged_properties):
|
def updateProperties(self, merged_properties):
|
||||||
@ -258,9 +253,9 @@ class Parameter(Accessible):
|
|||||||
merged_properties.pop(key)
|
merged_properties.pop(key)
|
||||||
merged_properties.update(self.ownProperties)
|
merged_properties.update(self.ownProperties)
|
||||||
|
|
||||||
def create_from_value(self, properties, value):
|
def override(self, value):
|
||||||
"""return a clone with given value and inherited properties"""
|
"""override default"""
|
||||||
return self.clone(properties, value=self.datatype(value))
|
self.value = self.datatype(value)
|
||||||
|
|
||||||
def merge(self, merged_properties):
|
def merge(self, merged_properties):
|
||||||
"""merge with inherited properties
|
"""merge with inherited properties
|
||||||
@ -279,7 +274,7 @@ class Parameter(Accessible):
|
|||||||
|
|
||||||
:param modobj: final call, called from Module.__init__
|
:param modobj: final call, called from Module.__init__
|
||||||
"""
|
"""
|
||||||
self.fixExport()
|
|
||||||
if self.constant is not None:
|
if self.constant is not None:
|
||||||
constant = self.datatype(self.constant)
|
constant = self.datatype(self.constant)
|
||||||
# The value of the `constant` property should be the
|
# The value of the `constant` property should be the
|
||||||
@ -395,7 +390,7 @@ class Command(Accessible):
|
|||||||
else:
|
else:
|
||||||
# goodie: allow @Command instead of @Command()
|
# goodie: allow @Command instead of @Command()
|
||||||
self.func = argument # this is the wrapped method!
|
self.func = argument # this is the wrapped method!
|
||||||
if argument.__doc__ is not None:
|
if argument.__doc__:
|
||||||
self.description = inspect.cleandoc(argument.__doc__)
|
self.description = inspect.cleandoc(argument.__doc__)
|
||||||
self.name = self.func.__name__ # this is probably not needed
|
self.name = self.func.__name__ # this is probably not needed
|
||||||
self._inherit = inherit # save for __set_name__
|
self._inherit = inherit # save for __set_name__
|
||||||
@ -406,8 +401,18 @@ class Command(Accessible):
|
|||||||
if self.func is None:
|
if self.func is None:
|
||||||
raise ProgrammingError(f'Command {owner.__name__}.{name} must be used as a method decorator')
|
raise ProgrammingError(f'Command {owner.__name__}.{name} must be used as a method decorator')
|
||||||
|
|
||||||
self.fixExport()
|
|
||||||
self.datatype = CommandType(self.argument, self.result)
|
self.datatype = CommandType(self.argument, self.result)
|
||||||
|
if self.export is True:
|
||||||
|
predefined_cls = PREDEFINED_ACCESSIBLES.get(name, None)
|
||||||
|
if predefined_cls is Command:
|
||||||
|
self.export = name
|
||||||
|
elif predefined_cls is None:
|
||||||
|
self.export = '_' + name
|
||||||
|
else:
|
||||||
|
raise ProgrammingError(f'can not use {name!r} as name of a Command') from None
|
||||||
|
if 'export' in self.ownProperties:
|
||||||
|
# avoid export=True overrides export=<name>
|
||||||
|
self.ownProperties['export'] = self.export
|
||||||
if not self._inherit:
|
if not self._inherit:
|
||||||
for key, pobj in self.properties.items():
|
for key, pobj in self.properties.items():
|
||||||
if key not in self.propertyValues:
|
if key not in self.propertyValues:
|
||||||
@ -434,38 +439,38 @@ class Command(Accessible):
|
|||||||
f' members!: {params} != {members}')
|
f' members!: {params} != {members}')
|
||||||
self.argument.optional = [p for p,v in sig.parameters.items()
|
self.argument.optional = [p for p,v in sig.parameters.items()
|
||||||
if v.default is not inspect.Parameter.empty]
|
if v.default is not inspect.Parameter.empty]
|
||||||
if 'description' not in self.ownProperties and func.__doc__ is not None:
|
if 'description' not in self.propertyValues and func.__doc__:
|
||||||
self.description = inspect.cleandoc(func.__doc__)
|
self.description = inspect.cleandoc(func.__doc__)
|
||||||
self.ownProperties['description'] = self.description
|
self.ownProperties['description'] = self.description
|
||||||
self.func = func
|
self.func = func
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def clone(self, properties, **kwds):
|
def copy(self):
|
||||||
"""return a clone of ourselfs with inherited properties"""
|
"""return a (deep) copy of ourselfs"""
|
||||||
res = type(self)(**kwds)
|
res = type(self)()
|
||||||
res.name = self.name
|
res.name = self.name
|
||||||
self.fixExport()
|
|
||||||
res.func = self.func
|
res.func = self.func
|
||||||
res.init(properties)
|
res.init(self.propertyValues)
|
||||||
res.init(res.ownProperties)
|
|
||||||
if res.argument:
|
if res.argument:
|
||||||
res.argument = res.argument.copy()
|
res.argument = res.argument.copy()
|
||||||
if res.result:
|
if res.result:
|
||||||
res.result = res.result.copy()
|
res.result = res.result.copy()
|
||||||
res.finish()
|
self.finish()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def updateProperties(self, merged_properties):
|
def updateProperties(self, merged_properties):
|
||||||
"""update merged_properties with our own properties"""
|
"""update merged_properties with our own properties"""
|
||||||
merged_properties.update(self.ownProperties)
|
merged_properties.update(self.ownProperties)
|
||||||
|
|
||||||
def create_from_value(self, properties, value):
|
def override(self, value):
|
||||||
"""return a clone with given value and inherited properties
|
"""override method
|
||||||
|
|
||||||
this is needed when the @Command is missing on a method overriding a command"""
|
this is needed when the @Command is missing on a method overriding a command"""
|
||||||
if not callable(value):
|
if not callable(value):
|
||||||
raise ProgrammingError(f'{self.name} = {value!r} is overriding a Command')
|
raise ProgrammingError(f'{self.name} = {value!r} is overriding a Command')
|
||||||
return self.clone(properties)(value)
|
self.func = value
|
||||||
|
if value.__doc__:
|
||||||
|
self.description = inspect.cleandoc(value.__doc__)
|
||||||
|
|
||||||
def merge(self, merged_properties):
|
def merge(self, merged_properties):
|
||||||
"""merge with inherited properties
|
"""merge with inherited properties
|
||||||
|
@ -84,7 +84,9 @@ class PersistentMixin(Module):
|
|||||||
flag = getattr(pobj, 'persistent', False)
|
flag = getattr(pobj, 'persistent', False)
|
||||||
if flag:
|
if flag:
|
||||||
if flag == 'auto':
|
if flag == 'auto':
|
||||||
self.addCallback(pname, self.saveParameters)
|
def cb(value, m=self):
|
||||||
|
m.saveParameters()
|
||||||
|
self.valueCallbacks[pname].append(cb)
|
||||||
self.initData[pname] = pobj.value
|
self.initData[pname] = pobj.value
|
||||||
if not pobj.given:
|
if not pobj.given:
|
||||||
if pname in loaded:
|
if pname in loaded:
|
||||||
@ -127,18 +129,16 @@ class PersistentMixin(Module):
|
|||||||
self.writeInitParams()
|
self.writeInitParams()
|
||||||
return loaded
|
return loaded
|
||||||
|
|
||||||
def saveParameters(self, _=None):
|
def saveParameters(self):
|
||||||
"""save persistent parameters
|
"""save persistent parameters
|
||||||
|
|
||||||
- to be called regularly explicitly by the module
|
- to be called regularly explicitly by the module
|
||||||
- the caller has to make sure that this is not called after
|
- the caller has to make sure that this is not called after
|
||||||
a power down of the connected hardware before loadParameters
|
a power down of the connected hardware before loadParameters
|
||||||
|
|
||||||
dummy argument to avoid closure for callback
|
|
||||||
"""
|
"""
|
||||||
if self.writeDict:
|
if self.writeDict:
|
||||||
# do not save before all values are written to the hw, as potentially
|
# do not save before all values are written to the hw, as potentially
|
||||||
# factory default values were read in the meantime
|
# factory default values were read in the mean time
|
||||||
return
|
return
|
||||||
self.__save_params()
|
self.__save_params()
|
||||||
|
|
||||||
|
@ -47,11 +47,9 @@ def make_update(modulename, pobj):
|
|||||||
if pobj.readerror:
|
if pobj.readerror:
|
||||||
return (ERRORPREFIX + EVENTREPLY, f'{modulename}:{pobj.export}',
|
return (ERRORPREFIX + EVENTREPLY, f'{modulename}:{pobj.export}',
|
||||||
# error-report !
|
# error-report !
|
||||||
[pobj.readerror.name, str(pobj.readerror),
|
[pobj.readerror.name, str(pobj.readerror), {'t': pobj.timestamp}])
|
||||||
{'t': pobj.timestamp} if pobj.timestamp else {}])
|
|
||||||
return (EVENTREPLY, f'{modulename}:{pobj.export}',
|
return (EVENTREPLY, f'{modulename}:{pobj.export}',
|
||||||
[pobj.export_value(),
|
[pobj.export_value(), {'t': pobj.timestamp}])
|
||||||
{'t': pobj.timestamp} if pobj.timestamp else {}])
|
|
||||||
|
|
||||||
|
|
||||||
class Dispatcher:
|
class Dispatcher:
|
||||||
|
@ -1,237 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
|
||||||
#
|
|
||||||
# *****************************************************************************
|
|
||||||
"""The common parts of the SECNodes outside interfaces"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import threading
|
|
||||||
|
|
||||||
from frappy.errors import SECoPError
|
|
||||||
from frappy.lib import formatException, formatExtendedStack, \
|
|
||||||
formatExtendedTraceback
|
|
||||||
from frappy.protocol.messages import ERRORPREFIX, HELPREPLY, HELPREQUEST, \
|
|
||||||
HelpMessage
|
|
||||||
|
|
||||||
|
|
||||||
class DecodeError(Exception):
|
|
||||||
def __init__(self, message, raw_msg):
|
|
||||||
super().__init__(message)
|
|
||||||
self._raw_msg = raw_msg
|
|
||||||
|
|
||||||
@property
|
|
||||||
def raw_msg(self):
|
|
||||||
return self._raw_msg
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionClose(Exception):
|
|
||||||
"""Indicates that receive quit due to an error."""
|
|
||||||
|
|
||||||
|
|
||||||
class RequestHandler:
|
|
||||||
"""Base class for the request handlers.
|
|
||||||
|
|
||||||
This is an extended copy of the BaseRequestHandler from socketserver.
|
|
||||||
|
|
||||||
To make a new interface, implement these methods:
|
|
||||||
ingest, next_message, decode_message, receive, send_reply and format
|
|
||||||
and extend (override) setup() and finish() if needed.
|
|
||||||
|
|
||||||
For an example, have a look at TCPRequestHandler.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Methods from BaseRequestHandler
|
|
||||||
def __init__(self, request, client_address, server):
|
|
||||||
self.request = request
|
|
||||||
self.client_address = client_address
|
|
||||||
self.server = server
|
|
||||||
self.log = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.setup()
|
|
||||||
self.handle()
|
|
||||||
except Exception:
|
|
||||||
if self.log:
|
|
||||||
self.log.error(formatException())
|
|
||||||
else:
|
|
||||||
server.log.error(formatException())
|
|
||||||
finally:
|
|
||||||
self.finish()
|
|
||||||
|
|
||||||
def setup(self):
|
|
||||||
self.log = self.server.log
|
|
||||||
self.log.info("new connection %s", self.format())
|
|
||||||
# notify dispatcher of us
|
|
||||||
self.server.dispatcher.add_connection(self)
|
|
||||||
self.send_lock = threading.Lock()
|
|
||||||
self.running = True
|
|
||||||
# overwrite this with an appropriate buffer if needed
|
|
||||||
self.data = None
|
|
||||||
|
|
||||||
def handle(self):
|
|
||||||
"""handle a new connection"""
|
|
||||||
# copy state info
|
|
||||||
serverobj = self.server
|
|
||||||
# copy relevant settings from Interface
|
|
||||||
detailed_errors = serverobj.detailed_errors
|
|
||||||
|
|
||||||
# start serving
|
|
||||||
while self.running:
|
|
||||||
try:
|
|
||||||
newdata = self.receive()
|
|
||||||
if newdata is None:
|
|
||||||
# no new data during read, continue
|
|
||||||
continue
|
|
||||||
self.ingest(newdata)
|
|
||||||
except ConnectionClose:
|
|
||||||
# either normal close or error in receive
|
|
||||||
return
|
|
||||||
# put data into (de-) framer,
|
|
||||||
# de-frame data with next_message() and decode it
|
|
||||||
# call dispatcher.handle_request(self, message)
|
|
||||||
# dispatcher will queue the reply before returning
|
|
||||||
while self.running:
|
|
||||||
try:
|
|
||||||
msg = self.next_message()
|
|
||||||
if msg is None:
|
|
||||||
break # no more messages to process
|
|
||||||
except DecodeError as err:
|
|
||||||
# we have to decode 'origin' here
|
|
||||||
# use latin-1, as utf-8 or ascii may lead to encoding errors
|
|
||||||
msg = err.raw_msg.decode('latin-1').split(' ', 3) + [
|
|
||||||
None
|
|
||||||
] # make sure len(msg) > 1
|
|
||||||
result = (
|
|
||||||
ERRORPREFIX + msg[0],
|
|
||||||
msg[1],
|
|
||||||
[
|
|
||||||
'InternalError', str(err),
|
|
||||||
{
|
|
||||||
'exception': formatException(),
|
|
||||||
'traceback': formatExtendedStack()
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)
|
|
||||||
print('--------------------')
|
|
||||||
print(formatException())
|
|
||||||
print('--------------------')
|
|
||||||
print(formatExtendedTraceback(sys.exc_info()))
|
|
||||||
print('====================')
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
if msg[0] == HELPREQUEST:
|
|
||||||
self.handle_help()
|
|
||||||
result = (HELPREPLY, None, None)
|
|
||||||
else:
|
|
||||||
result = serverobj.dispatcher.handle_request(self,
|
|
||||||
msg)
|
|
||||||
except SECoPError as err:
|
|
||||||
result = (
|
|
||||||
ERRORPREFIX + msg[0],
|
|
||||||
msg[1],
|
|
||||||
[
|
|
||||||
err.name,
|
|
||||||
str(err),
|
|
||||||
{
|
|
||||||
'exception': formatException(),
|
|
||||||
'traceback': formatExtendedStack()
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)
|
|
||||||
except Exception as err:
|
|
||||||
# create Error Obj instead
|
|
||||||
result = (
|
|
||||||
ERRORPREFIX + msg[0],
|
|
||||||
msg[1],
|
|
||||||
[
|
|
||||||
'InternalError',
|
|
||||||
repr(err),
|
|
||||||
{
|
|
||||||
'exception': formatException(),
|
|
||||||
'traceback': formatExtendedStack()
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)
|
|
||||||
print('--------------------')
|
|
||||||
print(formatException())
|
|
||||||
print('--------------------')
|
|
||||||
print(formatExtendedTraceback(sys.exc_info()))
|
|
||||||
print('====================')
|
|
||||||
|
|
||||||
if not result:
|
|
||||||
self.log.error('empty result upon msg %s', repr(msg))
|
|
||||||
if result[0].startswith(ERRORPREFIX) and not detailed_errors:
|
|
||||||
# strip extra information
|
|
||||||
result[2][2].clear()
|
|
||||||
self.send_reply(result)
|
|
||||||
|
|
||||||
def handle_help(self):
|
|
||||||
for idx, line in enumerate(HelpMessage.splitlines()):
|
|
||||||
# not sending HELPREPLY here, as there should be only one reply for
|
|
||||||
# every request
|
|
||||||
self.send_reply(('_', f'{idx + 1}', line))
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
"""called when handle() terminates, i.e. the socket closed"""
|
|
||||||
self.log.info('closing connection %s', self.format())
|
|
||||||
# notify dispatcher
|
|
||||||
self.server.dispatcher.remove_connection(self)
|
|
||||||
|
|
||||||
# Methods for implementing in derived classes:
|
|
||||||
def ingest(self, newdata):
|
|
||||||
"""Put the new data into the buffer."""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def next_message(self):
|
|
||||||
"""Get the next decoded message from the buffer.
|
|
||||||
|
|
||||||
Has to return a triple of (MESSAGE, specifier, data) or None, in case
|
|
||||||
there are no further messages in the receive queue.
|
|
||||||
|
|
||||||
If there is an Error during decoding, this method has to raise a
|
|
||||||
DecodeError.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def receive(self):
|
|
||||||
"""Receive data from the link.
|
|
||||||
|
|
||||||
Should return the received data or None if there was nothing new. Has
|
|
||||||
to raise a ConnectionClose on shutdown of the connection or on errors
|
|
||||||
that are not recoverable.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def send_reply(self, data):
|
|
||||||
"""send reply
|
|
||||||
|
|
||||||
stops recv loop on error
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def format(self):
|
|
||||||
"""
|
|
||||||
Format available connection data into something recognizable for
|
|
||||||
logging.
|
|
||||||
|
|
||||||
For example, the remote IP address or a connection identifier.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
# TODO: server baseclass?
|
|
@ -18,77 +18,122 @@
|
|||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""TCP interface to the SECoP Server"""
|
"""provides tcp interface to the SECoP Server"""
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import socketserver
|
import socketserver
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from frappy.datatypes import BoolType, StringType
|
from frappy.datatypes import BoolType, StringType
|
||||||
from frappy.lib import SECoP_DEFAULT_PORT
|
from frappy.errors import SECoPError
|
||||||
|
from frappy.lib import formatException, formatExtendedStack, \
|
||||||
|
formatExtendedTraceback, SECoP_DEFAULT_PORT
|
||||||
from frappy.properties import Property
|
from frappy.properties import Property
|
||||||
from frappy.protocol.interface import decode_msg, encode_msg_frame, get_msg
|
from frappy.protocol.interface import decode_msg, encode_msg_frame, get_msg
|
||||||
from frappy.protocol.interface.handler import ConnectionClose, \
|
from frappy.protocol.messages import ERRORPREFIX, HELPREPLY, HELPREQUEST, \
|
||||||
RequestHandler, DecodeError
|
HelpMessage
|
||||||
from frappy.protocol.messages import HELPREQUEST
|
|
||||||
|
|
||||||
|
|
||||||
MESSAGE_READ_SIZE = 1024
|
MESSAGE_READ_SIZE = 1024
|
||||||
|
HELP = HELPREQUEST.encode()
|
||||||
|
|
||||||
|
|
||||||
def format_address(addr):
|
class TCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
if len(addr) == 2:
|
|
||||||
return '%s:%d' % addr
|
|
||||||
address, port = addr[0:2]
|
|
||||||
if address.startswith('::ffff'):
|
|
||||||
return '%s:%d' % (address[7:], port)
|
|
||||||
return '[%s]:%d' % (address, port)
|
|
||||||
|
|
||||||
|
|
||||||
class TCPRequestHandler(RequestHandler):
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
super().setup()
|
self.log = self.server.log
|
||||||
self.request.settimeout(1)
|
self.running = True
|
||||||
self.data = b''
|
self.send_lock = threading.Lock()
|
||||||
|
|
||||||
def finish(self):
|
def handle(self):
|
||||||
"""called when handle() terminates, i.e. the socket closed"""
|
"""handle a new tcp-connection"""
|
||||||
super().finish()
|
# copy state info
|
||||||
# close socket
|
mysocket = self.request
|
||||||
|
clientaddr = self.client_address
|
||||||
|
serverobj = self.server
|
||||||
|
|
||||||
|
self.log.info("handling new connection from %s", format_address(clientaddr))
|
||||||
|
data = b''
|
||||||
|
|
||||||
|
# notify dispatcher of us
|
||||||
|
serverobj.dispatcher.add_connection(self)
|
||||||
|
|
||||||
|
# copy relevant settings from Interface
|
||||||
|
detailed_errors = serverobj.detailed_errors
|
||||||
|
|
||||||
|
mysocket.settimeout(1)
|
||||||
|
# start serving
|
||||||
|
while self.running:
|
||||||
try:
|
try:
|
||||||
self.request.shutdown(socket.SHUT_RDWR)
|
newdata = mysocket.recv(MESSAGE_READ_SIZE)
|
||||||
except Exception:
|
if not newdata:
|
||||||
pass
|
# no timeout error, but no new data -> connection closed
|
||||||
finally:
|
return
|
||||||
self.request.close()
|
data = data + newdata
|
||||||
|
|
||||||
def ingest(self, newdata):
|
|
||||||
self.data += newdata
|
|
||||||
|
|
||||||
def next_message(self):
|
|
||||||
try:
|
|
||||||
message, self.data = get_msg(self.data)
|
|
||||||
if message is None:
|
|
||||||
return None
|
|
||||||
if message.strip() == b'':
|
|
||||||
return (HELPREQUEST, None, None)
|
|
||||||
return decode_msg(message)
|
|
||||||
except Exception as e:
|
|
||||||
raise DecodeError('exception in receive', raw_msg=message) from e
|
|
||||||
|
|
||||||
def receive(self):
|
|
||||||
try:
|
|
||||||
data = self.request.recv(MESSAGE_READ_SIZE)
|
|
||||||
if not data:
|
|
||||||
raise ConnectionClose('socket was closed')
|
|
||||||
return data
|
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
return None
|
continue
|
||||||
except socket.error as e:
|
except socket.error as e:
|
||||||
self.log.exception(e)
|
self.log.exception(e)
|
||||||
raise ConnectionClose() from e
|
return
|
||||||
|
if not data:
|
||||||
|
continue
|
||||||
|
# put data into (de-) framer,
|
||||||
|
# put frames into (de-) coder and if a message appear,
|
||||||
|
# call dispatcher.handle_request(self, message)
|
||||||
|
# dispatcher will queue the reply before returning
|
||||||
|
while self.running:
|
||||||
|
origin, data = get_msg(data)
|
||||||
|
if origin is None:
|
||||||
|
break # no more messages to process
|
||||||
|
origin = origin.strip()
|
||||||
|
if origin in (HELP, b''): # empty string -> send help message
|
||||||
|
for idx, line in enumerate(HelpMessage.splitlines()):
|
||||||
|
# not sending HELPREPLY here, as there should be only one reply for every request
|
||||||
|
self.send_reply(('_', f'{idx + 1}', line))
|
||||||
|
# ident matches request
|
||||||
|
self.send_reply((HELPREPLY, None, None))
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
msg = decode_msg(origin)
|
||||||
|
except Exception as err:
|
||||||
|
# we have to decode 'origin' here
|
||||||
|
# use latin-1, as utf-8 or ascii may lead to encoding errors
|
||||||
|
msg = origin.decode('latin-1').split(' ', 3) + [None] # make sure len(msg) > 1
|
||||||
|
result = (ERRORPREFIX + msg[0], msg[1], ['InternalError', str(err),
|
||||||
|
{'exception': formatException(),
|
||||||
|
'traceback': formatExtendedStack()}])
|
||||||
|
print('--------------------')
|
||||||
|
print(formatException())
|
||||||
|
print('--------------------')
|
||||||
|
print(formatExtendedTraceback(sys.exc_info()))
|
||||||
|
print('====================')
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
result = serverobj.dispatcher.handle_request(self, msg)
|
||||||
|
except SECoPError as err:
|
||||||
|
result = (ERRORPREFIX + msg[0], msg[1], [err.name, str(err),
|
||||||
|
{'exception': formatException(),
|
||||||
|
'traceback': formatExtendedStack()}])
|
||||||
|
except Exception as err:
|
||||||
|
# create Error Obj instead
|
||||||
|
result = (ERRORPREFIX + msg[0], msg[1], ['InternalError', repr(err),
|
||||||
|
{'exception': formatException(),
|
||||||
|
'traceback': formatExtendedStack()}])
|
||||||
|
print('--------------------')
|
||||||
|
print(formatException())
|
||||||
|
print('--------------------')
|
||||||
|
print(formatExtendedTraceback(sys.exc_info()))
|
||||||
|
print('====================')
|
||||||
|
|
||||||
|
if not result:
|
||||||
|
self.log.error('empty result upon msg %s', repr(msg))
|
||||||
|
if result[0].startswith(ERRORPREFIX) and not detailed_errors:
|
||||||
|
# strip extra information
|
||||||
|
result[2][2].clear()
|
||||||
|
self.send_reply(result)
|
||||||
|
|
||||||
def send_reply(self, data):
|
def send_reply(self, data):
|
||||||
"""send reply
|
"""send reply
|
||||||
@ -111,9 +156,18 @@ class TCPRequestHandler(RequestHandler):
|
|||||||
self.log.error('ERROR in send_reply %r', e)
|
self.log.error('ERROR in send_reply %r', e)
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
def format(self):
|
def finish(self):
|
||||||
return f'from {format_address(self.client_address)}'
|
"""called when handle() terminates, i.e. the socket closed"""
|
||||||
|
self.log.info('closing connection from %s', format_address(self.client_address))
|
||||||
|
# notify dispatcher
|
||||||
|
self.server.dispatcher.remove_connection(self)
|
||||||
|
# close socket
|
||||||
|
try:
|
||||||
|
self.request.shutdown(socket.SHUT_RDWR)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
self.request.close()
|
||||||
|
|
||||||
class DualStackTCPServer(socketserver.ThreadingTCPServer):
|
class DualStackTCPServer(socketserver.ThreadingTCPServer):
|
||||||
"""Subclassed to provide IPv6 capabilities as socketserver only uses IPv4"""
|
"""Subclassed to provide IPv6 capabilities as socketserver only uses IPv4"""
|
||||||
@ -176,3 +230,12 @@ class TCPServer(DualStackTCPServer):
|
|||||||
if ntry:
|
if ntry:
|
||||||
self.log.warning('tried again %d times after "Address already in use"', ntry)
|
self.log.warning('tried again %d times after "Address already in use"', ntry)
|
||||||
self.log.info("TCPServer initiated")
|
self.log.info("TCPServer initiated")
|
||||||
|
|
||||||
|
|
||||||
|
def format_address(addr):
|
||||||
|
if len(addr) == 2:
|
||||||
|
return '%s:%d' % addr
|
||||||
|
address, port = addr[0:2]
|
||||||
|
if address.startswith('::ffff'):
|
||||||
|
return '%s:%d' % (address[7:], port)
|
||||||
|
return '[%s]:%d' % (address, port)
|
||||||
|
@ -1,160 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Alexander Zaft <a.zaft@fz-juelich.de>
|
|
||||||
#
|
|
||||||
# *****************************************************************************
|
|
||||||
|
|
||||||
import json
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from websockets.exceptions import ConnectionClosedOK, ConnectionClosedError
|
|
||||||
from websockets.sync.server import CloseCode, serve
|
|
||||||
|
|
||||||
from frappy.protocol.interface.handler import ConnectionClose, \
|
|
||||||
RequestHandler, DecodeError
|
|
||||||
from frappy.protocol.messages import HELPREQUEST
|
|
||||||
|
|
||||||
|
|
||||||
def encode_msg_frame_str(action, specifier=None, data=None):
|
|
||||||
""" encode a msg_triple into an msg_frame, ready to be sent
|
|
||||||
|
|
||||||
action (and optional specifier) are str strings,
|
|
||||||
data may be an json-yfied python object"""
|
|
||||||
msg = (action, specifier or '', '' if data is None else json.dumps(data))
|
|
||||||
return ' '.join(msg).strip()
|
|
||||||
|
|
||||||
|
|
||||||
class WSRequestHandler(RequestHandler):
|
|
||||||
"""Handles a Websocket connection."""
|
|
||||||
|
|
||||||
def __init__(self, conn, server):
|
|
||||||
self.conn = conn
|
|
||||||
client_address = conn.remote_address
|
|
||||||
request = conn.socket
|
|
||||||
super().__init__(request, client_address, server)
|
|
||||||
|
|
||||||
def setup(self):
|
|
||||||
super().setup()
|
|
||||||
self.server.connections.add(self)
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
"""called when handle() terminates, i.e. the socket closed"""
|
|
||||||
super().finish()
|
|
||||||
self.server.connections.discard(self)
|
|
||||||
# this will be called for a second time if the server is shutting down,
|
|
||||||
# but in that case it will be a no-op
|
|
||||||
self.conn.close()
|
|
||||||
|
|
||||||
def ingest(self, newdata):
|
|
||||||
# recv on the websocket connection returns one message, we don't save
|
|
||||||
# anything in data
|
|
||||||
self.data = newdata
|
|
||||||
|
|
||||||
def next_message(self):
|
|
||||||
"""split the string into a message triple."""
|
|
||||||
if self.data is None:
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
message = self.data.strip()
|
|
||||||
if message == '':
|
|
||||||
return HELPREQUEST, None, None
|
|
||||||
res = message.split(' ', 2) + ['', '']
|
|
||||||
action, specifier, data = res[0:3]
|
|
||||||
self.data = None
|
|
||||||
return (
|
|
||||||
action,
|
|
||||||
specifier or None,
|
|
||||||
None if data == '' else json.loads(data)
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
raise DecodeError('exception when reading in message',
|
|
||||||
raw_msg=bytes(message, 'utf-8')) from e
|
|
||||||
|
|
||||||
def receive(self):
|
|
||||||
"""receives one message from the websocket."""
|
|
||||||
try:
|
|
||||||
return self.conn.recv()
|
|
||||||
except TimeoutError:
|
|
||||||
return None
|
|
||||||
except ConnectionClosedOK:
|
|
||||||
raise ConnectionClose from None
|
|
||||||
except ConnectionClosedError as e:
|
|
||||||
self.log.error('No close frame received from %s', self.format())
|
|
||||||
raise ConnectionClose from e
|
|
||||||
except OSError as e:
|
|
||||||
self.log.exception(e)
|
|
||||||
raise ConnectionClose from e
|
|
||||||
|
|
||||||
def send_reply(self, data):
|
|
||||||
"""send reply
|
|
||||||
|
|
||||||
stops recv loop on error (including timeout when output buffer full for
|
|
||||||
more than 1 sec)
|
|
||||||
"""
|
|
||||||
if not data:
|
|
||||||
self.log.error('should not reply empty data!')
|
|
||||||
return
|
|
||||||
outdata = encode_msg_frame_str(*data)
|
|
||||||
with self.send_lock:
|
|
||||||
if self.running:
|
|
||||||
try:
|
|
||||||
self.conn.send(outdata)
|
|
||||||
except (BrokenPipeError, IOError) as e:
|
|
||||||
self.log.debug('send_reply got an %r, connection closed?',
|
|
||||||
e)
|
|
||||||
self.running = False
|
|
||||||
except Exception as e:
|
|
||||||
self.log.error('ERROR in send_reply %r', e)
|
|
||||||
self.running = False
|
|
||||||
|
|
||||||
def format(self):
|
|
||||||
return f'{self.conn.id} from {self.client_address}'
|
|
||||||
|
|
||||||
class WSServer:
|
|
||||||
"""Server for providing a websocket interface.
|
|
||||||
|
|
||||||
Implementation note:
|
|
||||||
The websockets library doesn't provide an option to subclass its server, so
|
|
||||||
we take the returned value as an attribute and provide the neccessary
|
|
||||||
function calls.
|
|
||||||
"""
|
|
||||||
def __init__(self, name, logger, options, srv):
|
|
||||||
self.connections = set() # keep track for shutting down
|
|
||||||
self.dispatcher = srv.dispatcher
|
|
||||||
self.name = name
|
|
||||||
self.log = logger
|
|
||||||
self.port = int(options.pop('uri').split('://', 1)[-1])
|
|
||||||
self.detailed_errors = options.pop('detailed_errors', False)
|
|
||||||
|
|
||||||
handle = partial(WSRequestHandler, server=self)
|
|
||||||
# websockets only gives the serve method without an option to subclass
|
|
||||||
self.ws_server = serve(handle, '', self.port, logger=logger)
|
|
||||||
self.log.info("Websocket server %s binding to port %d", name, self.port)
|
|
||||||
|
|
||||||
def serve_forever(self):
|
|
||||||
self.ws_server.serve_forever()
|
|
||||||
|
|
||||||
def shutdown(self):
|
|
||||||
for c in list(self.connections):
|
|
||||||
c.conn.close(code=CloseCode.GOING_AWAY, reason='shutting down')
|
|
||||||
self.ws_server.shutdown()
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, *args):
|
|
||||||
return self.shutdown()
|
|
@ -71,7 +71,7 @@ class ProxyModule(HasIO, Module):
|
|||||||
pname, pobj = params.popitem()
|
pname, pobj = params.popitem()
|
||||||
props = remoteparams.get(pname, None)
|
props = remoteparams.get(pname, None)
|
||||||
if props is None:
|
if props is None:
|
||||||
if pobj.export and pname != 'status':
|
if pobj.export:
|
||||||
self.log.warning('remote parameter %s:%s does not exist', self.module, pname)
|
self.log.warning('remote parameter %s:%s does not exist', self.module, pname)
|
||||||
continue
|
continue
|
||||||
dt = props['datatype']
|
dt = props['datatype']
|
||||||
@ -108,19 +108,17 @@ class ProxyModule(HasIO, Module):
|
|||||||
# for now, the error message must be enough
|
# for now, the error message must be enough
|
||||||
|
|
||||||
def nodeStateChange(self, online, state):
|
def nodeStateChange(self, online, state):
|
||||||
disconnected = Readable.Status.ERROR, 'disconnected'
|
|
||||||
if online:
|
if online:
|
||||||
if not self._consistency_check_done:
|
if not self._consistency_check_done:
|
||||||
self._check_descriptive_data()
|
self._check_descriptive_data()
|
||||||
self._consistency_check_done = True
|
self._consistency_check_done = True
|
||||||
if self.status == disconnected:
|
|
||||||
self.status = Readable.Status.IDLE, 'connected'
|
|
||||||
else:
|
else:
|
||||||
|
newstatus = Readable.Status.ERROR, 'disconnected'
|
||||||
readerror = CommunicationFailedError('disconnected')
|
readerror = CommunicationFailedError('disconnected')
|
||||||
if self.status != disconnected:
|
if self.status != newstatus:
|
||||||
for pname in set(self.parameters) - set(('module', 'status')):
|
for pname in set(self.parameters) - set(('module', 'status')):
|
||||||
self.announceUpdate(pname, None, readerror)
|
self.announceUpdate(pname, None, readerror)
|
||||||
self.status = disconnected
|
self.announceUpdate('status', newstatus)
|
||||||
|
|
||||||
def checkProperties(self):
|
def checkProperties(self):
|
||||||
pass # skip
|
pass # skip
|
||||||
@ -195,7 +193,7 @@ def proxy_class(remote_class, name=None):
|
|||||||
attrs[aname] = pobj
|
attrs[aname] = pobj
|
||||||
|
|
||||||
def rfunc(self, pname=aname):
|
def rfunc(self, pname=aname):
|
||||||
value, _, readerror = self._secnode.getParameter(self.module, pname, True)
|
value, _, readerror = self._secnode.getParameter(self.name, pname, True)
|
||||||
if readerror:
|
if readerror:
|
||||||
raise readerror
|
raise readerror
|
||||||
return value
|
return value
|
||||||
@ -205,7 +203,7 @@ def proxy_class(remote_class, name=None):
|
|||||||
if not pobj.readonly:
|
if not pobj.readonly:
|
||||||
|
|
||||||
def wfunc(self, value, pname=aname):
|
def wfunc(self, value, pname=aname):
|
||||||
value, _, readerror = self._secnode.setParameter(self.module, pname, value)
|
value, _, readerror = self._secnode.setParameter(self.name, pname, value)
|
||||||
if readerror:
|
if readerror:
|
||||||
raise readerror
|
raise readerror
|
||||||
return value
|
return value
|
||||||
@ -216,7 +214,7 @@ def proxy_class(remote_class, name=None):
|
|||||||
cobj = aobj.copy()
|
cobj = aobj.copy()
|
||||||
|
|
||||||
def cfunc(self, arg=None, cname=aname):
|
def cfunc(self, arg=None, cname=aname):
|
||||||
return self._secnode.execCommand(self.module, cname, arg)[0]
|
return self._secnode.execCommand(self.name, cname, arg)[0]
|
||||||
|
|
||||||
attrs[aname] = cobj(cfunc)
|
attrs[aname] = cobj(cfunc)
|
||||||
|
|
||||||
|
@ -53,7 +53,6 @@ except ImportError:
|
|||||||
class Server:
|
class Server:
|
||||||
INTERFACES = {
|
INTERFACES = {
|
||||||
'tcp': 'protocol.interface.tcp.TCPServer',
|
'tcp': 'protocol.interface.tcp.TCPServer',
|
||||||
'ws': 'protocol.interface.ws.WSServer',
|
|
||||||
}
|
}
|
||||||
_restart = True
|
_restart = True
|
||||||
|
|
||||||
|
@ -239,7 +239,6 @@ class HasStates:
|
|||||||
|
|
||||||
@Command
|
@Command
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop state machine"""
|
|
||||||
self.stop_machine()
|
self.stop_machine()
|
||||||
|
|
||||||
def final_status(self, code=IDLE, text=''):
|
def final_status(self, code=IDLE, text=''):
|
||||||
|
164
frappy/structparam.py
Normal file
164
frappy/structparam.py
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
# *****************************************************************************
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
# Foundation; either version 2 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
#
|
||||||
|
# Module authors:
|
||||||
|
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||||
|
#
|
||||||
|
# *****************************************************************************
|
||||||
|
"""convenience class to create a struct Parameter together with indivdual params
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
class Controller(Drivable):
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
ctrlpars = StructParam('ctrlpars struct', [
|
||||||
|
('pid_p', 'p', Parameter('control parameter p', FloatRange())),
|
||||||
|
('pid_i', 'i', Parameter('control parameter i', FloatRange())),
|
||||||
|
('pid_d', 'd', Parameter('control parameter d', FloatRange())),
|
||||||
|
], readonly=False)
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
then implement either read_ctrlpars and write_ctrlpars or
|
||||||
|
read_pid_p, read_pid_i, read_pid_d, write_pid_p, write_pid_i and write_pid_d
|
||||||
|
|
||||||
|
the methods not implemented will be created automatically
|
||||||
|
"""
|
||||||
|
|
||||||
|
from frappy.core import Parameter, Property
|
||||||
|
from frappy.datatypes import BoolType, DataType, StructOf, ValueType
|
||||||
|
from frappy.errors import ProgrammingError
|
||||||
|
|
||||||
|
|
||||||
|
class StructParam(Parameter):
|
||||||
|
"""create a struct parameter together with individual parameters
|
||||||
|
|
||||||
|
in addition to normal Parameter arguments:
|
||||||
|
|
||||||
|
:param paramdict: dict <member name> of Parameter(...)
|
||||||
|
:param prefix_or_map: either a prefix for the parameter name to add to the member name
|
||||||
|
or a dict <member name> or <paramerter name>
|
||||||
|
"""
|
||||||
|
# use properties, as simple attributes are not considered on copy()
|
||||||
|
paramdict = Property('dict <parametername> of Parameter(...)', ValueType())
|
||||||
|
hasStructRW = Property('has a read_<struct param> or write_<struct param> method',
|
||||||
|
BoolType(), default=False)
|
||||||
|
|
||||||
|
insideRW = 0 # counter for avoiding multiple superfluous updates
|
||||||
|
|
||||||
|
def __init__(self, description=None, paramdict=None, prefix_or_map='', *, datatype=None, readonly=False, **kwds):
|
||||||
|
if isinstance(paramdict, DataType):
|
||||||
|
raise ProgrammingError('second argument must be a dict of Param')
|
||||||
|
if datatype is None and paramdict is not None: # omit the following on Parameter.copy()
|
||||||
|
if isinstance(prefix_or_map, str):
|
||||||
|
prefix_or_map = {m: prefix_or_map + m for m in paramdict}
|
||||||
|
for membername, param in paramdict.items():
|
||||||
|
param.name = prefix_or_map[membername]
|
||||||
|
datatype = StructOf(**{m: p.datatype for m, p in paramdict.items()})
|
||||||
|
kwds['influences'] = [p.name for p in paramdict.values()]
|
||||||
|
self.updateEnable = {}
|
||||||
|
super().__init__(description, datatype, paramdict=paramdict, readonly=readonly, **kwds)
|
||||||
|
|
||||||
|
def __set_name__(self, owner, name):
|
||||||
|
# names of access methods of structed param (e.g. ctrlpars)
|
||||||
|
struct_read_name = f'read_{name}' # e.g. 'read_ctrlpars'
|
||||||
|
struct_write_name = f'write_{name}' # e.h. 'write_ctrlpars'
|
||||||
|
self.hasStructRW = hasattr(owner, struct_read_name) or hasattr(owner, struct_write_name)
|
||||||
|
|
||||||
|
for membername, param in self.paramdict.items():
|
||||||
|
pname = param.name
|
||||||
|
changes = {
|
||||||
|
'readonly': self.readonly,
|
||||||
|
'influences': set(param.influences) | {name},
|
||||||
|
}
|
||||||
|
param.ownProperties.update(changes)
|
||||||
|
param.init(changes)
|
||||||
|
setattr(owner, pname, param)
|
||||||
|
param.__set_name__(owner, param.name)
|
||||||
|
|
||||||
|
if self.hasStructRW:
|
||||||
|
rname = f'read_{pname}'
|
||||||
|
|
||||||
|
if not hasattr(owner, rname):
|
||||||
|
def rfunc(self, membername=membername, struct_read_name=struct_read_name):
|
||||||
|
return getattr(self, struct_read_name)()[membername]
|
||||||
|
|
||||||
|
rfunc.poll = False # read_<struct param> is polled only
|
||||||
|
setattr(owner, rname, rfunc)
|
||||||
|
|
||||||
|
if not self.readonly:
|
||||||
|
wname = f'write_{pname}'
|
||||||
|
if not hasattr(owner, wname):
|
||||||
|
def wfunc(self, value, membername=membername,
|
||||||
|
name=name, rname=rname, struct_write_name=struct_write_name):
|
||||||
|
valuedict = dict(getattr(self, name))
|
||||||
|
valuedict[membername] = value
|
||||||
|
getattr(self, struct_write_name)(valuedict)
|
||||||
|
return getattr(self, rname)()
|
||||||
|
|
||||||
|
setattr(owner, wname, wfunc)
|
||||||
|
|
||||||
|
if not self.hasStructRW:
|
||||||
|
if not hasattr(owner, struct_read_name):
|
||||||
|
def struct_read_func(self, name=name, flist=tuple(
|
||||||
|
(m, f'read_{p.name}') for m, p in self.paramdict.items())):
|
||||||
|
pobj = self.parameters[name]
|
||||||
|
# disable updates generated from the callbacks of individual params
|
||||||
|
pobj.insideRW += 1 # guarded by self.accessLock
|
||||||
|
try:
|
||||||
|
return {m: getattr(self, f)() for m, f in flist}
|
||||||
|
finally:
|
||||||
|
pobj.insideRW -= 1
|
||||||
|
|
||||||
|
setattr(owner, struct_read_name, struct_read_func)
|
||||||
|
|
||||||
|
if not (self.readonly or hasattr(owner, struct_write_name)):
|
||||||
|
|
||||||
|
def struct_write_func(self, value, name=name, funclist=tuple(
|
||||||
|
(m, f'write_{p.name}') for m, p in self.paramdict.items())):
|
||||||
|
pobj = self.parameters[name]
|
||||||
|
pobj.insideRW += 1 # guarded by self.accessLock
|
||||||
|
try:
|
||||||
|
return {m: getattr(self, f)(value[m]) for m, f in funclist}
|
||||||
|
finally:
|
||||||
|
pobj.insideRW -= 1
|
||||||
|
|
||||||
|
setattr(owner, struct_write_name, struct_write_func)
|
||||||
|
|
||||||
|
super().__set_name__(owner, name)
|
||||||
|
|
||||||
|
def finish(self, modobj=None):
|
||||||
|
"""register callbacks for consistency"""
|
||||||
|
super().finish(modobj)
|
||||||
|
if modobj:
|
||||||
|
|
||||||
|
if self.hasStructRW:
|
||||||
|
def cb(value, modobj=modobj, structparam=self):
|
||||||
|
for membername, param in structparam.paramdict.items():
|
||||||
|
setattr(modobj, param.name, value[membername])
|
||||||
|
|
||||||
|
modobj.valueCallbacks[self.name].append(cb)
|
||||||
|
else:
|
||||||
|
for membername, param in self.paramdict.items():
|
||||||
|
def cb(value, modobj=modobj, structparam=self, membername=membername):
|
||||||
|
if not structparam.insideRW:
|
||||||
|
prev = dict(getattr(modobj, structparam.name))
|
||||||
|
prev[membername] = value
|
||||||
|
setattr(modobj, structparam.name, prev)
|
||||||
|
|
||||||
|
modobj.valueCallbacks[param.name].append(cb)
|
@ -120,7 +120,6 @@ class MagneticField(Drivable):
|
|||||||
)
|
)
|
||||||
heatswitch = Attached(Switch, description='name of heat switch device')
|
heatswitch = Attached(Switch, description='name of heat switch device')
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
|
||||||
Status = Enum(Drivable.Status, PERSIST=PERSIST, PREPARE=301, RAMPING=302, FINISH=303)
|
Status = Enum(Drivable.Status, PERSIST=PERSIST, PREPARE=301, RAMPING=302, FINISH=303)
|
||||||
|
|
||||||
status = Parameter(datatype=TupleOf(EnumType(Status), StringType()))
|
status = Parameter(datatype=TupleOf(EnumType(Status), StringType()))
|
||||||
@ -194,7 +193,6 @@ class MagneticField(Drivable):
|
|||||||
self.log.error(self, 'main thread exited unexpectedly!')
|
self.log.error(self, 'main thread exited unexpectedly!')
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop at current value"""
|
|
||||||
self.write_target(self.read_value())
|
self.write_target(self.read_value())
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,12 +36,12 @@ from time import sleep, time as currenttime
|
|||||||
import PyTango
|
import PyTango
|
||||||
|
|
||||||
from frappy.datatypes import ArrayOf, EnumType, FloatRange, IntRange, \
|
from frappy.datatypes import ArrayOf, EnumType, FloatRange, IntRange, \
|
||||||
LimitsType, StatusType, StringType, TupleOf, ValueType
|
LimitsType, StringType, TupleOf, ValueType
|
||||||
from frappy.errors import CommunicationFailedError, ConfigError, \
|
from frappy.errors import CommunicationFailedError, ConfigError, \
|
||||||
HardwareError, ProgrammingError, WrongTypeError
|
HardwareError, ProgrammingError, WrongTypeError
|
||||||
from frappy.lib import lazy_property
|
from frappy.lib import lazy_property
|
||||||
from frappy.modules import Command, Drivable, Module, Parameter, Property, \
|
from frappy.modules import Command, Drivable, Module, Parameter, Readable, \
|
||||||
Readable, Writable
|
StatusType, Writable, Property
|
||||||
|
|
||||||
#####
|
#####
|
||||||
|
|
||||||
@ -641,7 +641,6 @@ class AnalogOutput(PyTangoDevice, Drivable):
|
|||||||
sleep(0.3)
|
sleep(0.3)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""cease driving, go to IDLE state"""
|
|
||||||
self._dev.Stop()
|
self._dev.Stop()
|
||||||
|
|
||||||
|
|
||||||
|
@ -194,19 +194,19 @@ class Nmr(Readable):
|
|||||||
x = val['xval'][:len(val['yval'])]
|
x = val['xval'][:len(val['yval'])]
|
||||||
return (x, val['yval'])
|
return (x, val['yval'])
|
||||||
|
|
||||||
@Command(IntRange(1), result=TupleOf(ArrayOf(string, maxlen=100),
|
@Command(result=TupleOf(ArrayOf(string, maxlen=100),
|
||||||
ArrayOf(floating, maxlen=100)))
|
ArrayOf(floating, maxlen=100)))
|
||||||
def get_amplitude(self, count):
|
def get_amplitude(self):
|
||||||
"""Last <count> amplitude datapoints."""
|
"""Last 20 amplitude datapoints."""
|
||||||
rv = self.cell.cell.nmr_paramlog_get('amplitude', count)
|
rv = self.cell.cell.nmr_paramlog_get('amplitude', 20)
|
||||||
x = [ str(timestamp) for timestamp in rv['xval']]
|
x = [ str(timestamp) for timestamp in rv['xval']]
|
||||||
return (x,rv['yval'])
|
return (x,rv['yval'])
|
||||||
|
|
||||||
@Command(IntRange(1), result=TupleOf(ArrayOf(string, maxlen=100),
|
@Command(result=TupleOf(ArrayOf(string, maxlen=100),
|
||||||
ArrayOf(floating, maxlen=100)))
|
ArrayOf(floating, maxlen=100)))
|
||||||
def get_phase(self, count):
|
def get_phase(self):
|
||||||
"""Last <count> phase datapoints."""
|
"""Last 20 phase datapoints."""
|
||||||
val = self.cell.cell.nmr_paramlog_get('phase', count)
|
val = self.cell.cell.nmr_paramlog_get('phase', 20)
|
||||||
return ([str(timestamp) for timestamp in val['xval']], val['yval'])
|
return ([str(timestamp) for timestamp in val['xval']], val['yval'])
|
||||||
|
|
||||||
|
|
||||||
|
229
frappy_psi/HP.py
Normal file
229
frappy_psi/HP.py
Normal file
@ -0,0 +1,229 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# *****************************************************************************
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
# Foundation; either version 2 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
#
|
||||||
|
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
|
# *****************************************************************************
|
||||||
|
"""Hewlett-Packard HP34401A Multimeter (not finished)"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from frappy.core import HasIO, Readable, Parameter, FloatRange, EnumType, StatusType, IDLE, ERROR, WARN
|
||||||
|
|
||||||
|
|
||||||
|
def string_to_value(value):
|
||||||
|
"""
|
||||||
|
Converting the value to float, removing the units, converting the prefix into the number.
|
||||||
|
:param value: value
|
||||||
|
:return: float value without units
|
||||||
|
"""
|
||||||
|
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
||||||
|
value, pfx = value_with_unit.match(value).groups()
|
||||||
|
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
||||||
|
if pfx in pfx_dict:
|
||||||
|
value = round(float(value) * pfx_dict[pfx], 12)
|
||||||
|
return float(value)
|
||||||
|
|
||||||
|
|
||||||
|
class HP_IO(HasIO):
|
||||||
|
end_of_line = b'\n'
|
||||||
|
identification = [('*IDN?', r'HEWLETT-PACKARD,34401A,0,.*')]
|
||||||
|
|
||||||
|
|
||||||
|
class HP34401A(HP_IO):
|
||||||
|
status = Parameter(datatype=StatusType(Readable, 'BUSY'))
|
||||||
|
autorange = Parameter('autorange_on', EnumType('autorange', off=0, on=1), readonly=False, default=0)
|
||||||
|
|
||||||
|
def comm(self, cmd): # read until \n
|
||||||
|
string = f'{cmd}\n'
|
||||||
|
n_string = string.encode()
|
||||||
|
response = self.communicate(n_string)
|
||||||
|
|
||||||
|
if response:
|
||||||
|
return response
|
||||||
|
|
||||||
|
response = self.communicate(n_string)
|
||||||
|
return response if response else None
|
||||||
|
|
||||||
|
def read_range(self, function):
|
||||||
|
return self.comm(f'{function}:range?')
|
||||||
|
|
||||||
|
def write_range(self, function, range):
|
||||||
|
return self.comm(f'{function}:range {range}')
|
||||||
|
|
||||||
|
def write_autorange(self, function):
|
||||||
|
cmd = f'{function}:range:auto {"on" if self.autorange == 0 else "off"}'
|
||||||
|
self.comm(cmd)
|
||||||
|
return self.comm(f'{function}:range:auto?')
|
||||||
|
|
||||||
|
def read_resolution(self, function):
|
||||||
|
return self.comm(f'{function}:resolution?')
|
||||||
|
|
||||||
|
def write_resolution(self, function, resolution):
|
||||||
|
self.comm(f'{function}:resolution {resolution}')
|
||||||
|
return self.comm(f'{function}:resolution?')
|
||||||
|
|
||||||
|
def read_status(self):
|
||||||
|
stb = int(self.comm('*STB?'))
|
||||||
|
esr = int(self.comm('*ESR?'))
|
||||||
|
|
||||||
|
if esr & (1 << 3):
|
||||||
|
return ERROR, 'self-test/calibration/reading failed'
|
||||||
|
if esr & (1 << 4):
|
||||||
|
return ERROR, 'execution error'
|
||||||
|
if esr & (1 << 5):
|
||||||
|
return ERROR, 'syntax error'
|
||||||
|
if esr & (1 << 2):
|
||||||
|
return ERROR, 'query error'
|
||||||
|
if stb & (1 << 3):
|
||||||
|
return WARN, 'questionable data'
|
||||||
|
if stb & (1 << 5):
|
||||||
|
return WARN, 'standard event register is not empty'
|
||||||
|
if stb & (1 << 6):
|
||||||
|
return WARN, 'requested service'
|
||||||
|
|
||||||
|
if any(stb & (1 << i) for i in range(3) or stb & (1 << 7)):
|
||||||
|
return IDLE, ''
|
||||||
|
if esr & (1 << 6):
|
||||||
|
return IDLE, ''
|
||||||
|
if esr & (1 << 7):
|
||||||
|
return IDLE, ''
|
||||||
|
if stb & (1 << 4):
|
||||||
|
return IDLE, 'message available'
|
||||||
|
if esr & (1 << 0):
|
||||||
|
return IDLE, 'operation complete'
|
||||||
|
if esr & (1 << 1):
|
||||||
|
return IDLE, 'not used'
|
||||||
|
|
||||||
|
|
||||||
|
class Voltage(HP34401A, Readable):
|
||||||
|
value = Parameter('voltage', datatype=FloatRange(0.1, 1000), unit='V')
|
||||||
|
range = Parameter('voltage sensitivity value', FloatRange(), unit='V', default=1, readonly=False)
|
||||||
|
resolution = Parameter('resolution')
|
||||||
|
mode = Parameter('measurement mode: ac/dc', readonly=False)
|
||||||
|
|
||||||
|
ioClass = HP_IO
|
||||||
|
|
||||||
|
MODE_NAMES = {0: 'dc', 1: 'ac'}
|
||||||
|
VOLT_RANGE = ['100mV', '1V', '10V', '100V', '1000V']
|
||||||
|
v_range = Parameter('voltage range', EnumType('voltage index range',
|
||||||
|
{name: idx for idx, name in enumerate(VOLT_RANGE)}), readonly=False)
|
||||||
|
|
||||||
|
acdc = None
|
||||||
|
|
||||||
|
def write_mode(self, mode):
|
||||||
|
"""
|
||||||
|
Set the mode - AC or DC
|
||||||
|
:param mode: AC/DC
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if mode == 1:
|
||||||
|
self.comm(f'configure:voltage:AC {self.range}, {self.resolution}')
|
||||||
|
else:
|
||||||
|
self.comm(f'configure:voltage:DC {self.range}, {self.resolution}')
|
||||||
|
self.acdc = self.MODE_NAMES[mode]
|
||||||
|
return self.comm(f'function?')
|
||||||
|
|
||||||
|
def read_value(self):
|
||||||
|
"""
|
||||||
|
Makes a AC/DC voltage measurement.
|
||||||
|
:return: AC/DC value
|
||||||
|
"""
|
||||||
|
return self.comm(f'measure:voltage:{self.acdc}?')
|
||||||
|
|
||||||
|
def write_autorange_acdc(self, function):
|
||||||
|
full_function = f'{function}:{self.acdc}'
|
||||||
|
return self.write_autorange(full_function)
|
||||||
|
|
||||||
|
def read_range_voltage(self):
|
||||||
|
return self.read_range(f'voltage:{self.acdc}')
|
||||||
|
|
||||||
|
def write_range_voltage(self, range):
|
||||||
|
return self.write_range(f'voltage:{self.acdc}', range)
|
||||||
|
|
||||||
|
def write_autorange_voltage(self):
|
||||||
|
return self.write_autorange_acdc('voltage')
|
||||||
|
|
||||||
|
def read_resolution_voltage(self):
|
||||||
|
return self.read_resolution(f'voltage:{self.acdc}')
|
||||||
|
|
||||||
|
def write_resolution_voltage(self, resolution):
|
||||||
|
return self.write_resolution(f'voltage:{self.acdc}', resolution)
|
||||||
|
|
||||||
|
|
||||||
|
class Current(HP34401A, Readable, Voltage):
|
||||||
|
value = Parameter('current', FloatRange, unit='A')
|
||||||
|
range = Parameter('current range', FloatRange)
|
||||||
|
CURR_RANGE_AC = ['10mA', '100mA', '1A', '3A']
|
||||||
|
CURR_RANGE_DC = ['1A', '3A']
|
||||||
|
|
||||||
|
def read_range_current(self):
|
||||||
|
return self.read_range(f'current:{self.acdc}')
|
||||||
|
|
||||||
|
def write_autorange_current(self):
|
||||||
|
return self.write_autorange_acdc('current')
|
||||||
|
|
||||||
|
def write_range_current(self, range):
|
||||||
|
return self.write_range(f'current:{self.acdc}', range)
|
||||||
|
|
||||||
|
def read_resolution_current(self):
|
||||||
|
return self.read_resolution(f'current:{self.acdc}')
|
||||||
|
|
||||||
|
def write_resolution_current(self, resolution):
|
||||||
|
return self.write_resolution(f'current:{self.acdc}', resolution)
|
||||||
|
|
||||||
|
|
||||||
|
class Resistance(HP34401A, Readable):
|
||||||
|
value = Parameter('resistance')
|
||||||
|
mode = Parameter('measurement mode: 2-/4-wire ohms', EnumType(two_wire=2, four_wire=4), readonly=False)
|
||||||
|
resolution = Parameter('resistance measurement resolution')
|
||||||
|
range = Parameter('resistance measurement range')
|
||||||
|
RESIST_RANGE = ['100Om', '1kOm', '10kOm', '100kOm', '1MOm', '10MOm', '100MOm']
|
||||||
|
FUNCTION_MAP = {2: 'resistance', 4: 'fresistance'}
|
||||||
|
|
||||||
|
def write_range_resistance(self, range):
|
||||||
|
return self.write_range(f'{self.FUNCTION_MAP[self.mode]}', range)
|
||||||
|
|
||||||
|
def read_range_resistance(self):
|
||||||
|
return self.read_range(f'{self.FUNCTION_MAP[self.mode]}')
|
||||||
|
|
||||||
|
def write_mode(self, mode):
|
||||||
|
if mode == 2:
|
||||||
|
self.comm(f'configure:resistance {self.range},{self.resolution}')
|
||||||
|
elif mode == 4:
|
||||||
|
self.comm(f'configure:fresistance {self.range}, {self.resolution}')
|
||||||
|
return self.comm('configure?')
|
||||||
|
|
||||||
|
def write_autorange_resistance(self):
|
||||||
|
return self.write_autorange(self.FUNCTION_MAP[self.mode])
|
||||||
|
|
||||||
|
def read_resolution_resistance(self):
|
||||||
|
return self.read_resolution(f'{self.FUNCTION_MAP[self.mode]}')
|
||||||
|
|
||||||
|
def write_resolution_resistance(self, resolution):
|
||||||
|
return self.write_resolution(f'{self.FUNCTION_MAP[self.mode]}', resolution)
|
||||||
|
|
||||||
|
|
||||||
|
class Frequency(HP34401A, Readable):
|
||||||
|
value = Parameter('frequency', FloatRange(3, 300e3), unit='Hz')
|
||||||
|
|
||||||
|
def write_autorange_frequency(self):
|
||||||
|
return self.write_autorange('frequency')
|
||||||
|
|
||||||
|
def read_resolution_frequency(self):
|
||||||
|
return self.read_resolution('frequency')
|
||||||
|
|
||||||
|
def write_resolution_frequency(self, resolution):
|
||||||
|
return self.write_resolution('frequency', resolution)
|
@ -15,6 +15,7 @@
|
|||||||
#
|
#
|
||||||
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
|
"""Stanford Research Systems SR830 DS Lock-in Amplifier"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
@ -25,6 +26,11 @@ from frappy.errors import IsBusyError
|
|||||||
|
|
||||||
|
|
||||||
def string_to_value(value):
|
def string_to_value(value):
|
||||||
|
"""
|
||||||
|
Converting the value to float, removing the units, converting the prefix into the number.
|
||||||
|
:param value: value
|
||||||
|
:return: float value without units
|
||||||
|
"""
|
||||||
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
||||||
value, pfx = value_with_unit.match(value).groups()
|
value, pfx = value_with_unit.match(value).groups()
|
||||||
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
||||||
@ -40,6 +46,13 @@ class SR830_IO(StringIO):
|
|||||||
|
|
||||||
class StanfRes(HasIO, Readable):
|
class StanfRes(HasIO, Readable):
|
||||||
def set_par(self, cmd, *args):
|
def set_par(self, cmd, *args):
|
||||||
|
"""
|
||||||
|
Set parameter.
|
||||||
|
Query commands are the same as setting commands, but they have a question mark.
|
||||||
|
:param cmd: command
|
||||||
|
:param args: value(s)
|
||||||
|
:return: reply
|
||||||
|
"""
|
||||||
head = ','.join([cmd] + [a if isinstance(a, str) else f'{a:g}' for a in args])
|
head = ','.join([cmd] + [a if isinstance(a, str) else f'{a:g}' for a in args])
|
||||||
tail = cmd.replace(' ', '? ')
|
tail = cmd.replace(' ', '? ')
|
||||||
new_tail = re.sub(r'[0-9.]+', '', tail)
|
new_tail = re.sub(r'[0-9.]+', '', tail)
|
||||||
@ -149,6 +162,10 @@ class XY(StanfRes):
|
|||||||
return IDLE, ''
|
return IDLE, ''
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
|
"""
|
||||||
|
Read XY. The manual autorange implemented.
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if self.read_status()[0] == BUSY:
|
if self.read_status()[0] == BUSY:
|
||||||
raise IsBusyError('changing gain')
|
raise IsBusyError('changing gain')
|
||||||
reply = self.get_par('SNAP? 1, 2')
|
reply = self.get_par('SNAP? 1, 2')
|
||||||
@ -166,11 +183,13 @@ class XY(StanfRes):
|
|||||||
return int(self.get_par('SENS?'))
|
return int(self.get_par('SENS?'))
|
||||||
|
|
||||||
def read_range(self):
|
def read_range(self):
|
||||||
|
"""Sensitivity range value"""
|
||||||
idx = self.read_irange()
|
idx = self.read_irange()
|
||||||
name = self.SEN_RANGE[idx]
|
name = self.SEN_RANGE[idx]
|
||||||
return string_to_value(name)
|
return string_to_value(name)
|
||||||
|
|
||||||
def write_irange(self, irange):
|
def write_irange(self, irange):
|
||||||
|
"""Index of sensitivity from the range"""
|
||||||
value = int(irange)
|
value = int(irange)
|
||||||
self.set_par(f'SENS {value}')
|
self.set_par(f'SENS {value}')
|
||||||
self._autogain_started = time.time()
|
self._autogain_started = time.time()
|
||||||
@ -178,6 +197,12 @@ class XY(StanfRes):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def write_range(self, target):
|
def write_range(self, target):
|
||||||
|
"""
|
||||||
|
Setting the sensitivity range.
|
||||||
|
cl_idx/cl_value is the closest index/value from the range to the target
|
||||||
|
:param target:
|
||||||
|
:return: closest value of the sensitivity range
|
||||||
|
"""
|
||||||
target = float(target)
|
target = float(target)
|
||||||
cl_idx = None
|
cl_idx = None
|
||||||
cl_value = float('inf')
|
cl_value = float('inf')
|
||||||
@ -194,6 +219,7 @@ class XY(StanfRes):
|
|||||||
return cl_value
|
return cl_value
|
||||||
|
|
||||||
def read_itc(self):
|
def read_itc(self):
|
||||||
|
"""Time constant index from the range"""
|
||||||
return int(self.get_par(f'OFLT?'))
|
return int(self.get_par(f'OFLT?'))
|
||||||
|
|
||||||
def write_itc(self, itc):
|
def write_itc(self, itc):
|
||||||
@ -201,11 +227,18 @@ class XY(StanfRes):
|
|||||||
return self.set_par(f'OFLT {value}')
|
return self.set_par(f'OFLT {value}')
|
||||||
|
|
||||||
def read_tc(self):
|
def read_tc(self):
|
||||||
|
"""Read time constant value from the range"""
|
||||||
idx = self.read_itc()
|
idx = self.read_itc()
|
||||||
name = self.TIME_CONST[idx]
|
name = self.TIME_CONST[idx]
|
||||||
return string_to_value(name)
|
return string_to_value(name)
|
||||||
|
|
||||||
def write_tc(self, target):
|
def write_tc(self, target):
|
||||||
|
"""
|
||||||
|
Setting the time constant from the range.
|
||||||
|
cl_idx/cl_value is the closest index/value from the range to the target
|
||||||
|
:param target: time constant
|
||||||
|
:return: closest time constant value
|
||||||
|
"""
|
||||||
target = float(target)
|
target = float(target)
|
||||||
cl_idx = None
|
cl_idx = None
|
||||||
cl_value = float('inf')
|
cl_value = float('inf')
|
||||||
|
@ -17,690 +17,248 @@
|
|||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
|
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
import threading
|
from frappy.core import Drivable, Parameter, Command, Property, ERROR, WARN, BUSY, IDLE, Done, nopoll
|
||||||
from frappy.core import Drivable, Parameter, Command, Property, Module, HasIO, \
|
from frappy.features import HasTargetLimits, HasSimpleOffset
|
||||||
ERROR, WARN, BUSY, IDLE, nopoll, Limit
|
|
||||||
from frappy.datatypes import IntRange, FloatRange, StringType, BoolType
|
from frappy.datatypes import IntRange, FloatRange, StringType, BoolType
|
||||||
from frappy.errors import BadValueError, HardwareError, ConfigError
|
from frappy.errors import ConfigError, BadValueError
|
||||||
|
sys.path.append('/home/l_samenv/Documents/anc350/Linux64/userlib/lib')
|
||||||
from PyANC350v4 import Positioner
|
from PyANC350v4 import Positioner
|
||||||
|
|
||||||
|
|
||||||
class IO(Module):
|
DIRECTION_NAME = {1: 'forward', -1: 'backward'}
|
||||||
"""'communication' module for attocube controller
|
|
||||||
|
|
||||||
why an extra class:
|
|
||||||
- HasIO assures that a single common communicator is used
|
class FreezeStatus:
|
||||||
- access must be thread safe
|
"""freeze status for some time
|
||||||
|
|
||||||
|
hardware quite often does not treat status correctly: on a target change it
|
||||||
|
may take some time to return the 'busy' status correctly.
|
||||||
|
|
||||||
|
in classes with this mixin, within :meth:`write_target` call
|
||||||
|
|
||||||
|
self.freeze_status(0.5, BUSY, 'changed target')
|
||||||
|
|
||||||
|
a wrapper around read_status will take care that the status will be the given value,
|
||||||
|
for at least the given delay. This does NOT cover the case when self.status is set
|
||||||
|
directly from an other method.
|
||||||
"""
|
"""
|
||||||
uri = Property('dummy uri, only one controller may exists',
|
__freeze_status_until = 0
|
||||||
StringType())
|
|
||||||
export = False
|
|
||||||
_hw = None
|
|
||||||
_lock = None
|
|
||||||
used_axes = set()
|
|
||||||
|
|
||||||
def initModule(self):
|
def __init_subclass__(cls):
|
||||||
if self._hw is None:
|
def wrapped(self, inner=cls.read_status):
|
||||||
IO._lock = threading.Lock()
|
if time.time() < self.__freeze_status_until:
|
||||||
IO._hw = Positioner()
|
return Done
|
||||||
super().initModule()
|
return inner(self)
|
||||||
|
|
||||||
def shutdownModule(self):
|
cls.read_status = wrapped
|
||||||
if IO._hw:
|
super().__init_subclass__()
|
||||||
IO._hw.disconnect()
|
|
||||||
IO._hw = None
|
|
||||||
|
|
||||||
def configureAQuadBIn(self, axisNo, enable, resolution):
|
def freeze_status(self, delay, code=BUSY, text='changed target'):
|
||||||
"""Enables and configures the A-Quad-B (quadrature) input for the target position.
|
"""freezze status to the given value for the given delay"""
|
||||||
Parameters
|
self.__freeze_status_until = time.time() + delay
|
||||||
axisNo Axis number (0 ... 2)
|
self.status = code, text
|
||||||
enable Enable (1) or disable (0) A-Quad-B input
|
|
||||||
resolution A-Quad-B step width in m. Internal resolution is 1 nm.
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureAQuadBIn(axisNo, enable, resolution)
|
|
||||||
|
|
||||||
def configureAQuadBOut(self, axisNo, enable, resolution, clock):
|
|
||||||
"""Enables and configures the A-Quad-B output of the current position.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
enable Enable (1) or disable (0) A-Quad-B output
|
|
||||||
resolution A-Quad-B step width in m; internal resolution is 1 nm
|
|
||||||
clock Clock of the A-Quad-B output [s]. Allowed range is 40ns ... 1.3ms; internal resulution is 20ns.
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureAQuadBOut(axisNo, enable, resolution, clock)
|
|
||||||
|
|
||||||
def configureExtTrigger(self, axisNo, mode):
|
|
||||||
"""Enables the input trigger for steps.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
mode Disable (0), Quadratur (1), Trigger(2) for external triggering
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureExtTrigger(axisNo, mode)
|
|
||||||
|
|
||||||
def configureNslTriggerAxis(self, axisNo):
|
|
||||||
"""Selects Axis for NSL Trigger.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureNslTriggerAxis(axisNo)
|
|
||||||
|
|
||||||
def configureRngTrigger(self, axisNo, lower, upper):
|
|
||||||
"""Configure lower position for range Trigger.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
lower Lower position for range trigger (nm)
|
|
||||||
upper Upper position for range trigger (nm)
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureRngTrigger(axisNo, lower, upper)
|
|
||||||
|
|
||||||
def configureRngTriggerEps(self, axisNo, epsilon):
|
|
||||||
"""Configure hysteresis for range Trigger.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
epsilon hysteresis in nm / mdeg
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureRngTriggerEps(axisNo, epsilon)
|
|
||||||
|
|
||||||
def configureRngTriggerPol(self, axisNo, polarity):
|
|
||||||
"""Configure lower position for range Trigger.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
polarity Polarity of trigger signal when position is between lower and upper Low(0) and High(1)
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.configureRngTriggerPol(axisNo, polarity)
|
|
||||||
|
|
||||||
def getActuatorName(self, axisNo):
|
|
||||||
"""Get the name of the currently selected actuator
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
name Name of the actuator
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.getActuatorName(axisNo)
|
|
||||||
|
|
||||||
def getActuatorType(self, axisNo):
|
|
||||||
"""Get the type of the currently selected actuator
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
type_ Type of the actuator {0: linear, 1: goniometer, 2: rotator}
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.getActuatorType(axisNo)
|
|
||||||
|
|
||||||
def getAmplitude(self, axisNo):
|
|
||||||
"""Reads back the amplitude parameter of an axis.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
amplitude Amplitude V
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.getAmplitude(axisNo)
|
|
||||||
|
|
||||||
def getAxisStatus(self, axisNo):
|
|
||||||
"""Reads status information about an axis of the device.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
connected Output: If the axis is connected to a sensor.
|
|
||||||
enabled Output: If the axis voltage output is enabled.
|
|
||||||
moving Output: If the axis is moving.
|
|
||||||
target Output: If the target is reached in automatic positioning
|
|
||||||
eotFwd Output: If end of travel detected in forward direction.
|
|
||||||
eotBwd Output: If end of travel detected in backward direction.
|
|
||||||
error Output: If the axis' sensor is in error state.
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.getAxisStatus(axisNo)
|
|
||||||
|
|
||||||
def getFrequency(self, axisNo):
|
|
||||||
"""Reads back the frequency parameter of an axis.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
frequency Output: Frequency in Hz
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.getFrequency(axisNo)
|
|
||||||
|
|
||||||
def getPosition(self, axisNo):
|
|
||||||
"""Retrieves the current actuator position. For linear type actuators the position unit is m; for goniometers and rotators it is degree.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
position Output: Current position [m] or [°]
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.getPosition(axisNo)
|
|
||||||
|
|
||||||
def measureCapacitance(self, axisNo):
|
|
||||||
"""Performs a measurement of the capacitance of the piezo motor and returns the result. If no motor is connected, the result will be 0. The function doesn't return before the measurement is complete; this will take a few seconds of time.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
Returns
|
|
||||||
cap Output: Capacitance [F]
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.measureCapacitance(axisNo)
|
|
||||||
|
|
||||||
def selectActuator(self, axisNo, actuator):
|
|
||||||
"""Selects the actuator to be used for the axis from actuator presets.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
actuator Actuator selection (0 ... 255)
|
|
||||||
0: ANPg101res
|
|
||||||
1: ANGt101res
|
|
||||||
2: ANPx51res
|
|
||||||
3: ANPx101res
|
|
||||||
4: ANPx121res
|
|
||||||
5: ANPx122res
|
|
||||||
6: ANPz51res
|
|
||||||
7: ANPz101res
|
|
||||||
8: ANR50res
|
|
||||||
9: ANR51res
|
|
||||||
10: ANR101res
|
|
||||||
11: Test
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.selectActuator(axisNo, actuator)
|
|
||||||
|
|
||||||
def setAmplitude(self, axisNo, amplitude):
|
|
||||||
"""Sets the amplitude parameter for an axis
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
amplitude Amplitude in V, internal resolution is 1 mV
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.setAmplitude(axisNo, amplitude)
|
|
||||||
|
|
||||||
def setAxisOutput(self, axisNo, enable, autoDisable):
|
|
||||||
"""Enables or disables the voltage output of an axis.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
enable Enables (1) or disables (0) the voltage output.
|
|
||||||
autoDisable If the voltage output is to be deactivated automatically when end of travel is detected.
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.setAxisOutput(axisNo, enable, autoDisable)
|
|
||||||
|
|
||||||
def setDcVoltage(self, axisNo, voltage):
|
|
||||||
"""Sets the DC level on the voltage output when no sawtooth based motion is active.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
voltage DC output voltage [V], internal resolution is 1 mV
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.setDcVoltage(axisNo, voltage)
|
|
||||||
|
|
||||||
def setFrequency(self, axisNo, frequency):
|
|
||||||
"""Sets the frequency parameter for an axis
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
frequency Frequency in Hz, internal resolution is 1 Hz
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.setFrequency(axisNo, frequency)
|
|
||||||
|
|
||||||
def setTargetPosition(self, axisNo, target):
|
|
||||||
"""Sets the target position for automatic motion, see ANC_startAutoMove. For linear type actuators the position unit is m, for goniometers and rotators it is degree.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
target Target position [m] or [°]. Internal resulution is 1 nm or 1 µ°.
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.setTargetPosition(axisNo, target)
|
|
||||||
|
|
||||||
def setTargetRange(self, axisNo, targetRg):
|
|
||||||
"""Defines the range around the target position where the target is considered to be reached.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
targetRg Target range [m] or [°]. Internal resulution is 1 nm or 1 µ°.
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.setTargetRange(axisNo, targetRg)
|
|
||||||
|
|
||||||
def startAutoMove(self, axisNo, enable, relative):
|
|
||||||
"""Switches automatic moving (i.e. following the target position) on or off
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
enable Enables (1) or disables (0) automatic motion
|
|
||||||
relative If the target position is to be interpreted absolute (0) or relative to the current position (1)
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.startAutoMove(axisNo, enable, relative)
|
|
||||||
|
|
||||||
def startContinuousMove(self, axisNo, start, backward):
|
|
||||||
"""Starts or stops continous motion in forward direction. Other kinds of motions are stopped.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
start Starts (1) or stops (0) the motion
|
|
||||||
backward If the move direction is forward (0) or backward (1)
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.startContinuousMove(axisNo, start, backward)
|
|
||||||
|
|
||||||
def startSingleStep(self, axisNo, backward):
|
|
||||||
"""Triggers a single step in desired direction.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
axisNo Axis number (0 ... 2)
|
|
||||||
backward If the step direction is forward (0) or backward (1)
|
|
||||||
Returns
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
with self._lock:
|
|
||||||
return self._hw.startSingleStep(axisNo, backward)
|
|
||||||
|
|
||||||
|
|
||||||
class Stopped(RuntimeError):
|
class Axis(HasTargetLimits, FreezeStatus, Drivable):
|
||||||
"""thread was stopped"""
|
|
||||||
|
|
||||||
|
|
||||||
class Axis(HasIO, Drivable):
|
|
||||||
axis = Property('axis number', IntRange(0, 2), 0)
|
axis = Property('axis number', IntRange(0, 2), 0)
|
||||||
value = Parameter('axis position', FloatRange(unit='deg'))
|
value = Parameter('axis position', FloatRange(unit='deg'))
|
||||||
frequency = Parameter('frequency', FloatRange(1, unit='Hz'), readonly=False)
|
frequency = Parameter('frequency', FloatRange(1, unit='Hz'), readonly=False)
|
||||||
amplitude = Parameter('amplitude', FloatRange(0, unit='V'), readonly=False)
|
amplitude = Parameter('amplitude', FloatRange(0, unit='V'), readonly=False)
|
||||||
gear = Parameter('gear factor', FloatRange(), readonly=False, value=1)
|
gear = Parameter('gear factor', FloatRange(), readonly=False, default=1, initwrite=True)
|
||||||
tolerance = Parameter('positioning tolerance', FloatRange(0, unit='$'),
|
tolerance = Parameter('positioning tolerance', FloatRange(0, unit='$'), readonly=False, default=0.01)
|
||||||
readonly=False, default=0.01)
|
output = Parameter('enable output', BoolType(), readonly=False)
|
||||||
sensor_connected = Parameter('a sensor is connected', BoolType())
|
|
||||||
info = Parameter('axis info', StringType())
|
info = Parameter('axis info', StringType())
|
||||||
statusbits = Parameter('status bits', StringType())
|
statusbits = Parameter('status bits', StringType())
|
||||||
step_mode = Parameter('step mode (soft closed loop)', BoolType(),
|
|
||||||
default=False, readonly=False, group='step_mode')
|
|
||||||
timeout = Parameter('timeout after no progress detected', FloatRange(0),
|
|
||||||
default=1, readonly=False, group='step_mode')
|
|
||||||
steps_fwd = Parameter('forward steps / main unit', FloatRange(0), unit='$/s',
|
|
||||||
default=0, readonly=False, group='step_mode')
|
|
||||||
steps_bwd = Parameter('backward steps / main unit', FloatRange(0, unit='$/s'),
|
|
||||||
default=0, readonly=False, group='step_mode')
|
|
||||||
delay = Parameter('delay between tries within loop', FloatRange(0, unit='s'),
|
|
||||||
readonly=False, default=0.05, group='step_mode')
|
|
||||||
maxstep = Parameter('max. step duration', FloatRange(0, unit='s'),
|
|
||||||
default=0.25, readonly=False, group='step_mode')
|
|
||||||
prop = Parameter('factor for control loop', FloatRange(0, 1),
|
|
||||||
readonly=False, default=0.8, group='step_mode')
|
|
||||||
uri = 'ANC'
|
|
||||||
ioClass = IO
|
|
||||||
target_min = Limit()
|
|
||||||
target_max = Limit()
|
|
||||||
|
|
||||||
fast_interval = 0.25
|
_hw = Positioner()
|
||||||
|
|
||||||
_hw = None
|
|
||||||
_scale = 1 # scale for custom units
|
_scale = 1 # scale for custom units
|
||||||
|
_move_steps = 0 # number of steps to move (used by move command)
|
||||||
SCALES = {'deg': 1, 'm': 1, 'mm': 1000, 'um': 1000000, 'µm': 1000000}
|
SCALES = {'deg': 1, 'm': 1, 'mm': 1000, 'um': 1000000, 'µm': 1000000}
|
||||||
_thread = None
|
_direction = 1 # move direction
|
||||||
_moving_since = 0
|
_idle_status = IDLE, ''
|
||||||
_status = IDLE, ''
|
_error_state = '' # empty string: no error
|
||||||
_calib_range = None
|
_history = None
|
||||||
_try_cnt = 0
|
_check_sensor = False
|
||||||
_at_target = False
|
_try_count = 0
|
||||||
|
|
||||||
def initModule(self):
|
def __init__(self, name, logger, opts, srv):
|
||||||
super().initModule()
|
unit = opts.pop('unit', 'deg')
|
||||||
self._stopped = threading.Event()
|
opts['value.unit'] = unit
|
||||||
if self.axis in IO.used_axes:
|
|
||||||
raise ConfigError(f'a module with axisNo={self.axis} already exists')
|
|
||||||
IO.used_axes.add(self.axis)
|
|
||||||
|
|
||||||
def initialReads(self):
|
|
||||||
self.read_info()
|
|
||||||
super().initialReads()
|
|
||||||
|
|
||||||
def shutdownModule(self):
|
|
||||||
IO.used_axes.discard(self.axis)
|
|
||||||
|
|
||||||
def read_value(self):
|
|
||||||
if self._thread:
|
|
||||||
return self.value
|
|
||||||
try:
|
try:
|
||||||
return self._read_pos()
|
self._scale = self.SCALES[unit] * opts.get('gear', 1)
|
||||||
except Stopped:
|
except KeyError as e:
|
||||||
return self.value
|
raise ConfigError('unsupported unit: %s' % unit)
|
||||||
|
super().__init__(name, logger, opts, srv)
|
||||||
|
|
||||||
def write_gear(self, value):
|
def write_gear(self, value):
|
||||||
self._scale = self.SCALES[self.parameters['value'].datatype.unit] * self.gear
|
self._scale = self.SCALES[self.parameters['value'].datatype.unit] * self.gear
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def startModule(self, start_events):
|
||||||
|
super().startModule(start_events)
|
||||||
|
start_events.queue(self.read_info)
|
||||||
|
|
||||||
|
def check_value(self, value):
|
||||||
|
"""check if value allows moving in current direction"""
|
||||||
|
if self._direction > 0:
|
||||||
|
if value > self.target_limits[1]:
|
||||||
|
raise BadValueError('above upper limit')
|
||||||
|
elif value < self.target_limits[0]:
|
||||||
|
raise BadValueError('below lower limit')
|
||||||
|
|
||||||
|
def read_value(self):
|
||||||
|
pos = self._hw.getPosition(self.axis) * self._scale
|
||||||
|
if self.isBusy():
|
||||||
|
try:
|
||||||
|
self.check_value(pos)
|
||||||
|
except BadValueError as e:
|
||||||
|
self._stop()
|
||||||
|
self._idle_status = ERROR, str(e)
|
||||||
|
return pos
|
||||||
|
|
||||||
def read_frequency(self):
|
def read_frequency(self):
|
||||||
return self.io.getFrequency(self.axis)
|
return self._hw.getFrequency(self.axis)
|
||||||
|
|
||||||
def write_frequency(self, value):
|
def write_frequency(self, value):
|
||||||
self.io.setFrequency(self.axis, value)
|
self._hw.setFrequency(self.axis, value)
|
||||||
return self.io.getFrequency(self.axis)
|
return self._hw.getFrequency(self.axis)
|
||||||
|
|
||||||
def read_amplitude(self):
|
def read_amplitude(self):
|
||||||
return self.io.getAmplitude(self.axis)
|
return self._hw.getAmplitude(self.axis)
|
||||||
|
|
||||||
def write_amplitude(self, value):
|
def write_amplitude(self, value):
|
||||||
self.io.setAmplitude(self.axis, value)
|
self._hw.setAmplitude(self.axis, value)
|
||||||
return self.io.getAmplitude(self.axis)
|
return self._hw.getAmplitude(self.axis)
|
||||||
|
|
||||||
def read_statusbits(self):
|
def write_tolerance(self, value):
|
||||||
self._get_status()
|
self._hw.setTargetRange(self.axis, value / self._scale)
|
||||||
return self.statusbits
|
return value
|
||||||
|
|
||||||
def _get_status(self):
|
def write_output(self, value):
|
||||||
"""get axis status
|
self._hw.setAxisOutput(self.axis, enable=value, autoDisable=0)
|
||||||
|
return value
|
||||||
- update self.sensor_connected and self.statusbits
|
|
||||||
- return <moving flag>, <error flag>, <reason>
|
|
||||||
|
|
||||||
<moving flag> is True whn moving
|
|
||||||
<in_error> is True when in error
|
|
||||||
<reason> is an error text, when in error, 'at target' or '' otherwise
|
|
||||||
"""
|
|
||||||
statusbits = self.io.getAxisStatus(self.axis)
|
|
||||||
self.sensor_connected, self._output, moving, at_target, fwd_stuck, bwd_stuck, error = statusbits
|
|
||||||
self.statusbits = ''.join(k for k, v in zip('OTFBE', (self._output,) + statusbits[3:]) if v)
|
|
||||||
if error:
|
|
||||||
return ERROR, 'other error'
|
|
||||||
if bwd_stuck:
|
|
||||||
return ERROR, 'end of travel backward'
|
|
||||||
if fwd_stuck:
|
|
||||||
return ERROR, 'end of travel forward'
|
|
||||||
target_reached = at_target > self._at_target
|
|
||||||
self._at_target = at_target
|
|
||||||
if self._moving_since:
|
|
||||||
if target_reached:
|
|
||||||
return IDLE, 'at target'
|
|
||||||
if time.time() < self._moving_since + 0.25:
|
|
||||||
return BUSY, 'started'
|
|
||||||
if at_target:
|
|
||||||
return IDLE, 'at target'
|
|
||||||
if moving and self._output:
|
|
||||||
return BUSY, 'moving'
|
|
||||||
return WARN, 'stopped by unknown reason'
|
|
||||||
if self._moving_since is False:
|
|
||||||
return IDLE, 'stopped'
|
|
||||||
if not self.step_mode and at_target:
|
|
||||||
return IDLE, 'at target'
|
|
||||||
return IDLE, ''
|
|
||||||
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
status = self._get_status()
|
statusbits = self._hw.getAxisStatus(self.axis)
|
||||||
if self.step_mode:
|
sensor, self.output, moving, attarget, eot_fwd, eot_bwd, sensor_error = statusbits
|
||||||
return self._status
|
self.statusbits = ''.join((k for k, v in zip('SOMTFBE', statusbits) if v))
|
||||||
if self._moving_since:
|
if self._move_steps:
|
||||||
if status[0] != BUSY:
|
if not (eot_fwd or eot_bwd):
|
||||||
self._moving_since = 0
|
return BUSY, 'moving by steps'
|
||||||
self.setFastPoll(False)
|
if not sensor:
|
||||||
return status
|
self._error_state = 'no sensor connected'
|
||||||
|
elif sensor_error:
|
||||||
def _wait(self, delay):
|
self._error_state = 'sensor error'
|
||||||
if self._stopped.wait(delay):
|
elif eot_fwd:
|
||||||
raise Stopped()
|
self._error_state = 'end of travel forward'
|
||||||
|
elif eot_bwd:
|
||||||
def _read_pos(self):
|
self._error_state = 'end of travel backward'
|
||||||
if not self.sensor_connected:
|
|
||||||
return 0
|
|
||||||
poslist = []
|
|
||||||
for i in range(9):
|
|
||||||
if i:
|
|
||||||
self._wait(0.001)
|
|
||||||
poslist.append(self.io.getPosition(self.axis) * self._scale)
|
|
||||||
self._poslist = sorted(poslist)
|
|
||||||
return self._poslist[len(poslist) // 2] # median
|
|
||||||
|
|
||||||
def _run_drive(self, target):
|
|
||||||
self.value = self._read_pos()
|
|
||||||
self.status = self._status = BUSY, 'drive by steps'
|
|
||||||
deadline = time.time() + self.timeout
|
|
||||||
max_steps = self.maxstep * self.frequency
|
|
||||||
while True:
|
|
||||||
for _ in range(2):
|
|
||||||
dif = target - self.value
|
|
||||||
steps_per_unit = self.steps_bwd if dif < 0 else self.steps_fwd
|
|
||||||
tol = max(self.tolerance, 0.6 / steps_per_unit) # avoid a tolerance less than 60% of a step
|
|
||||||
if abs(dif) > tol * 3:
|
|
||||||
break
|
|
||||||
# extra wait time when already close
|
|
||||||
self._wait(2 * self.delay)
|
|
||||||
self.read_value()
|
|
||||||
status = None
|
|
||||||
if abs(dif) < tol:
|
|
||||||
status = IDLE, 'in tolerance'
|
|
||||||
elif self._poslist[2] <= target <= self._poslist[-3]: # target within noise
|
|
||||||
status = IDLE, 'within noise'
|
|
||||||
elif dif > 0:
|
|
||||||
steps = min(max_steps, min(dif, (dif + tol) * self.prop) * steps_per_unit)
|
|
||||||
else:
|
else:
|
||||||
steps = max(-max_steps, max(dif, (dif - tol) * self.prop) * steps_per_unit)
|
if self._error_state and not DIRECTION_NAME[self._direction] in self._error_state:
|
||||||
if status or steps == 0:
|
self._error_state = ''
|
||||||
self._status = status
|
status_text = 'moving' if self._try_count == 0 else 'moving (retry %d)' % self._try_count
|
||||||
break
|
if moving and self._history is not None: # history None: moving by steps
|
||||||
if round(steps) == 0: # this should not happen
|
self._history.append(self.value)
|
||||||
self._status = WARN, 'steps=0'
|
if len(self._history) < 5:
|
||||||
break
|
return BUSY, status_text
|
||||||
self._move_steps(steps)
|
beg = self._history.pop(0)
|
||||||
if self._step_size > self.prop * 0.25 / steps_per_unit:
|
if abs(beg - self.target) < self.tolerance:
|
||||||
# some progress happened
|
# reset normal tolerance
|
||||||
deadline = time.time() + self.timeout
|
self._stop()
|
||||||
elif time.time() > deadline:
|
self._idle_status = IDLE, 'in tolerance'
|
||||||
self._status = WARN, 'timeout - no progress'
|
return self._idle_status
|
||||||
break
|
# self._hw.setTargetRange(self.axis, self.tolerance / self._scale)
|
||||||
self.read_status()
|
if (self.value - beg) * self._direction > 0:
|
||||||
|
return BUSY, status_text
|
||||||
|
self._try_count += 1
|
||||||
|
if self._try_count < 10:
|
||||||
|
self.log.warn('no progress retry %d', self._try_count)
|
||||||
|
return BUSY, status_text
|
||||||
|
self._idle_status = WARN, 'no progress'
|
||||||
|
if self._error_state:
|
||||||
|
self._try_count += 1
|
||||||
|
if self._try_count < 10 and self._history is not None:
|
||||||
|
self.log.warn('end of travel retry %d', self._try_count)
|
||||||
|
self.write_target(self.target)
|
||||||
|
return Done
|
||||||
|
self._idle_status = WARN, self._error_state
|
||||||
|
if self.status[0] != IDLE:
|
||||||
|
self._stop()
|
||||||
|
return self._idle_status
|
||||||
|
|
||||||
def _thread_wrapper(self, func, *args):
|
def write_target(self, value):
|
||||||
|
if value == self.read_value():
|
||||||
|
return value
|
||||||
|
self.check_limits(value)
|
||||||
|
self._try_count = 0
|
||||||
|
self._direction = 1 if value > self.value else -1
|
||||||
|
# if self._error_state and DIRECTION_NAME[-self._direction] not in self._error_state:
|
||||||
|
# raise BadValueError('can not move (%s)' % self._error_state)
|
||||||
|
self._move_steps = 0
|
||||||
|
self.write_output(1)
|
||||||
|
# try first with 50 % of tolerance
|
||||||
|
self._hw.setTargetRange(self.axis, self.tolerance * 0.5 / self._scale)
|
||||||
|
for itry in range(5):
|
||||||
try:
|
try:
|
||||||
func(*args)
|
self._hw.setTargetPosition(self.axis, value / self._scale)
|
||||||
except Stopped as e:
|
self._hw.startAutoMove(self.axis, enable=1, relative=0)
|
||||||
self._status = IDLE, str(e)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._status = ERROR, f'{type(e).__name__} - {e}'
|
if itry == 4:
|
||||||
finally:
|
raise
|
||||||
self.io.setAxisOutput(self.axis, enable=0, autoDisable=0)
|
self.log.warn('%r', e)
|
||||||
self.setFastPoll(False)
|
self._history = [self.value]
|
||||||
self._stopped.clear()
|
self._idle_status = IDLE, ''
|
||||||
self._thread = None
|
self.freeze_status(1, BUSY, 'changed target')
|
||||||
|
self.setFastPoll(True, 1)
|
||||||
|
return value
|
||||||
|
|
||||||
def _stop_thread(self):
|
def doPoll(self):
|
||||||
if self._thread:
|
if self._move_steps == 0:
|
||||||
self._stopped.set()
|
super().doPoll()
|
||||||
self._thread.join()
|
|
||||||
|
|
||||||
def _start_thread(self, *args):
|
|
||||||
self._stop_thread()
|
|
||||||
thread = threading.Thread(target=self._thread_wrapper, args=args)
|
|
||||||
self._thread = thread
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
def write_target(self, target):
|
|
||||||
if not self.sensor_connected:
|
|
||||||
raise HardwareError('no sensor connected')
|
|
||||||
self._stop_thread()
|
|
||||||
self.io.setTargetRange(self.axis, self.tolerance / self._scale)
|
|
||||||
if self.step_mode:
|
|
||||||
self.status = BUSY, 'changed target'
|
|
||||||
self._start_thread(self._run_drive, target)
|
|
||||||
else:
|
|
||||||
self._try_cnt = 0
|
|
||||||
self.setFastPoll(True, self.fast_interval)
|
|
||||||
self.io.setTargetPosition(self.axis, target / self._scale)
|
|
||||||
self.io.setAxisOutput(self.axis, enable=1, autoDisable=0)
|
|
||||||
self.io.startAutoMove(self.axis, enable=1, relative=0)
|
|
||||||
self._moving_since = time.time()
|
|
||||||
self.status = self._get_status()
|
|
||||||
return target
|
|
||||||
|
|
||||||
@Command()
|
|
||||||
def stop(self):
|
|
||||||
if self.step_mode:
|
|
||||||
self._stop_thread()
|
|
||||||
self._status = IDLE, 'stopped'
|
|
||||||
elif self._moving_since:
|
|
||||||
self._moving_since = False # indicate stop
|
|
||||||
self.read_status()
|
|
||||||
|
|
||||||
@Command(IntRange())
|
|
||||||
def move(self, steps):
|
|
||||||
"""relative move by number of steps"""
|
|
||||||
self._stop_thread()
|
|
||||||
self.read_value()
|
|
||||||
if steps > 0:
|
|
||||||
if self.value > self.target_max:
|
|
||||||
raise BadValueError('above upper limit')
|
|
||||||
elif self.value < self.target_min:
|
|
||||||
raise BadValueError('below lower limit')
|
|
||||||
self.status = self._status = BUSY, 'moving relative'
|
|
||||||
self._start_thread(self._run_move, steps)
|
|
||||||
|
|
||||||
def _run_move(self, steps):
|
|
||||||
self.setFastPoll(True, self.fast_interval)
|
|
||||||
self._move_steps(steps)
|
|
||||||
self.status = self._status = IDLE, ''
|
|
||||||
|
|
||||||
def _move_steps(self, steps):
|
|
||||||
steps = round(steps)
|
|
||||||
if not steps:
|
|
||||||
return
|
return
|
||||||
previous = self._read_pos()
|
self._hw.startSingleStep(self.axis, self._direction < 0)
|
||||||
self.io.setAxisOutput(self.axis, enable=1, autoDisable=0)
|
self._move_steps -= self._direction
|
||||||
# wait for output is really on
|
if self._move_steps % int(self.frequency) == 0: # poll value and status every second
|
||||||
for i in range(100):
|
super().doPoll()
|
||||||
self._wait(0.001)
|
|
||||||
self._get_status()
|
|
||||||
if self._output:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise ValueError('can not switch on output')
|
|
||||||
for cnt in range(abs(steps)):
|
|
||||||
self.io.setAxisOutput(self.axis, enable=1, autoDisable=0)
|
|
||||||
if not self._thread:
|
|
||||||
raise Stopped('stopped')
|
|
||||||
self.io.startSingleStep(self.axis, steps < 0)
|
|
||||||
self._wait(1 / self.frequency)
|
|
||||||
self._get_status()
|
|
||||||
if cnt and not self._output:
|
|
||||||
steps = cnt
|
|
||||||
break
|
|
||||||
self._wait(self.delay)
|
|
||||||
self.value = self._read_pos()
|
|
||||||
self._step_size = (self.value - previous) / steps
|
|
||||||
|
|
||||||
@Command(IntRange(0))
|
|
||||||
def calib_steps(self, delta):
|
|
||||||
"""calibrate steps_fwd and steps_bwd using <delta> steps forwards and backwards"""
|
|
||||||
if not self.sensor_connected:
|
|
||||||
raise HardwareError('no sensor connected')
|
|
||||||
self._stop_thread()
|
|
||||||
self._status = BUSY, 'calibrate step size'
|
|
||||||
self.read_status()
|
|
||||||
self._start_thread(self._run_calib, delta)
|
|
||||||
|
|
||||||
def _run_calib(self, steps):
|
|
||||||
self.value = self._read_pos()
|
|
||||||
if self._calib_range is None or abs(self.target - self.value) > self._calib_range:
|
|
||||||
self.target = self.value
|
|
||||||
maxfwd = 0
|
|
||||||
maxbwd = 0
|
|
||||||
cntfwd = 0
|
|
||||||
cntbwd = 0
|
|
||||||
self._calib_range = 0
|
|
||||||
for i in range(10):
|
|
||||||
if self.value <= self.target:
|
|
||||||
self._status = BUSY, 'move forwards'
|
|
||||||
self.read_status()
|
|
||||||
self._move_steps(steps)
|
|
||||||
while True:
|
|
||||||
self._move_steps(steps)
|
|
||||||
if self._step_size and self._output:
|
|
||||||
maxfwd = max(maxfwd, self._step_size)
|
|
||||||
cntfwd += 1
|
|
||||||
if self.value > self.target:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
self._status = BUSY, 'move backwards'
|
|
||||||
self.read_status()
|
|
||||||
self._move_steps(-steps)
|
|
||||||
while True:
|
|
||||||
self._move_steps(-steps)
|
|
||||||
if self._step_size:
|
|
||||||
maxbwd = max(maxbwd, self._step_size)
|
|
||||||
cntbwd += 1
|
|
||||||
if self.value < self.target:
|
|
||||||
break
|
|
||||||
# keep track how far we had to go for calibration
|
|
||||||
self._calib_range = max(self._calib_range, abs(self.value - self.target))
|
|
||||||
if cntfwd >= 3 and cntbwd >= 3:
|
|
||||||
self.steps_fwd = 1 / maxfwd
|
|
||||||
self.steps_bwd = 1 / maxbwd
|
|
||||||
self._status = IDLE, 'calib step size done'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
self._status = WARN, 'calib step size failed'
|
|
||||||
self.read_status()
|
|
||||||
|
|
||||||
@nopoll
|
@nopoll
|
||||||
def read_info(self):
|
def read_info(self):
|
||||||
"""read info from controller"""
|
"""read info from controller"""
|
||||||
axistype = ['linear', 'gonio', 'rotator'][self.io.getActuatorType(self.axis)]
|
cap = self._hw.measureCapacitance(self.axis) * 1e9
|
||||||
name = self.io.getActuatorName(self.axis)
|
axistype = ['linear', 'gonio', 'rotator'][self._hw.getActuatorType(self.axis)]
|
||||||
cap = self.io.measureCapacitance(self.axis) * 1e9
|
return '%s %s %.3gnF' % (self._hw.getActuatorName(self.axis), axistype, cap)
|
||||||
return f'{name} {axistype} {cap:.3g}nF'
|
|
||||||
|
def _stop(self):
|
||||||
|
self._move_steps = 0
|
||||||
|
self._history = None
|
||||||
|
for _ in range(5):
|
||||||
|
try:
|
||||||
|
self._hw.startAutoMove(self.axis, enable=0, relative=0)
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
if itry == 4:
|
||||||
|
raise
|
||||||
|
self.log.warn('%r', e)
|
||||||
|
self._hw.setTargetRange(self.axis, self.tolerance / self._scale)
|
||||||
|
self.setFastPoll(False)
|
||||||
|
|
||||||
|
@Command()
|
||||||
|
def stop(self):
|
||||||
|
self._idle_status = IDLE, 'stopped' if self.isBusy() else ''
|
||||||
|
self._stop()
|
||||||
|
self.status = self._idle_status
|
||||||
|
|
||||||
|
@Command(IntRange())
|
||||||
|
def move(self, value):
|
||||||
|
"""relative move by number of steps"""
|
||||||
|
self._direction = 1 if value > 0 else -1
|
||||||
|
self.check_value(self.value)
|
||||||
|
self._history = None
|
||||||
|
if DIRECTION_NAME[self._direction] in self._error_state:
|
||||||
|
raise BadValueError('can not move (%s)' % self._error_state)
|
||||||
|
self._move_steps = value
|
||||||
|
self._idle_status = IDLE, ''
|
||||||
|
self.read_status()
|
||||||
|
self.setFastPoll(True, 1/self.frequency)
|
||||||
|
279
frappy_psi/bridge.py
Normal file
279
frappy_psi/bridge.py
Normal file
@ -0,0 +1,279 @@
|
|||||||
|
# *****************************************************************************
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
# Foundation; either version 2 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
#
|
||||||
|
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
|
# *****************************************************************************
|
||||||
|
"""Stanford Research Systems SIM900 Mainframe"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from frappy.core import StringIO, HasIO, Readable, \
|
||||||
|
Parameter, FloatRange, IntRange, EnumType, \
|
||||||
|
Property, Attached, IDLE, ERROR, WARN
|
||||||
|
|
||||||
|
|
||||||
|
def string_to_value(value):
|
||||||
|
"""
|
||||||
|
Converting the value to float, removing the units, converting the prefix into the number.
|
||||||
|
:param value: value
|
||||||
|
:return: float value without units
|
||||||
|
"""
|
||||||
|
value_with_unit = re.compile(r'(\d+)([pnumkMG]?)')
|
||||||
|
value, pfx = value_with_unit.match(value).groups()
|
||||||
|
pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9}
|
||||||
|
if pfx in pfx_dict:
|
||||||
|
value = round(float(value) * pfx_dict[pfx], 12)
|
||||||
|
return float(value)
|
||||||
|
|
||||||
|
|
||||||
|
def find_idx(list_of_values, target):
|
||||||
|
"""
|
||||||
|
Search for the nearest value and index from the given range for the given target.
|
||||||
|
:param list_of_values: range of values
|
||||||
|
:param target: target
|
||||||
|
:return: closest index and closest value
|
||||||
|
"""
|
||||||
|
target = float(target)
|
||||||
|
cl_idx = None
|
||||||
|
cl_value = float('inf')
|
||||||
|
|
||||||
|
for idx, value in enumerate(list_of_values):
|
||||||
|
if value >= target:
|
||||||
|
diff = value - target
|
||||||
|
|
||||||
|
if diff < cl_value:
|
||||||
|
cl_value = value
|
||||||
|
cl_idx = idx
|
||||||
|
|
||||||
|
return cl_idx, cl_value
|
||||||
|
|
||||||
|
|
||||||
|
class BridgeIO(StringIO):
|
||||||
|
"""_\n is placed at the beginning of each command to distinguish
|
||||||
|
the previous response with a possible asynchronous response from the actual value returned by the method. """
|
||||||
|
|
||||||
|
end_of_line = '\n'
|
||||||
|
identification = [('_\n*IDN?', r'Stanford_Research_Systems,.*')]
|
||||||
|
|
||||||
|
|
||||||
|
class Base(HasIO):
|
||||||
|
port = Property('modules port', IntRange(0, 15))
|
||||||
|
|
||||||
|
def communicate(self, command):
|
||||||
|
"""
|
||||||
|
Connection to the particular module x.
|
||||||
|
:param command: command
|
||||||
|
:return: return
|
||||||
|
"""
|
||||||
|
return self.io.communicate(f'_\nconn {self.port:x},"_\n"\n{command}')
|
||||||
|
|
||||||
|
def query(self, command):
|
||||||
|
"""converting to float"""
|
||||||
|
return float(self.communicate(command))
|
||||||
|
|
||||||
|
|
||||||
|
class Resistance(Base, Readable):
|
||||||
|
value = Parameter('resistance', datatype=FloatRange, unit='ohm')
|
||||||
|
output_offset = Parameter('resistance deviation', datatype=FloatRange, unit='Ohm', readonly=False)
|
||||||
|
phase_hold = Parameter('phase hold', EnumType('phase hold', off=0, on=1))
|
||||||
|
|
||||||
|
RES_RANGE = ['20mOhm', '200mOhm', '2Ohm', '20Ohm', '200Ohm', '2kOhm', '20kOhm', '200kOhm',
|
||||||
|
'2MOhm', '20MOhm']
|
||||||
|
irange = Parameter('resistance range index', EnumType('resistance range index',
|
||||||
|
{name: idx for idx, name in enumerate(RES_RANGE)}),
|
||||||
|
readonly=False)
|
||||||
|
range = Parameter('resistance range value', FloatRange(2e-2, 2e7), unit='Om', readonly=False)
|
||||||
|
|
||||||
|
TIME_CONST = ['0.3s', '1s', '3s', '10s', '30s', '100s', '300s']
|
||||||
|
itc = Parameter('time constant index',
|
||||||
|
EnumType('time const. index range',
|
||||||
|
{name: value for value, name in enumerate(TIME_CONST)}), readonly=False)
|
||||||
|
tc = Parameter('time constant value', FloatRange(1e-1, 3e2), unit='s', readonly=False)
|
||||||
|
|
||||||
|
EXCT_RANGE = ['0', '3uV', '10uV', '30uV', '100uV', '300uV', '1mV', '3mV', '10mV', '30mV']
|
||||||
|
iexct = Parameter('excitation index',
|
||||||
|
EnumType('excitation index range', {name: idx for idx, name in enumerate(EXCT_RANGE, start=-1)}),
|
||||||
|
readonly=False)
|
||||||
|
exct = Parameter('excitation value', FloatRange(0, 3e-2), unit='s', default=300, readonly=False)
|
||||||
|
|
||||||
|
autorange = Parameter('autorange_on', EnumType('autorange', off=0, on=1),
|
||||||
|
readonly=False, default=0)
|
||||||
|
|
||||||
|
RES_RANGE_values = [string_to_value(value) for value in RES_RANGE]
|
||||||
|
TIME_CONST_values = [string_to_value(value) for value in TIME_CONST]
|
||||||
|
EXCT_RANGE_values = [string_to_value(value) for value in EXCT_RANGE]
|
||||||
|
|
||||||
|
ioClass = BridgeIO
|
||||||
|
|
||||||
|
def doPoll(self):
|
||||||
|
super().doPoll()
|
||||||
|
max_res = abs(self.value)
|
||||||
|
if self.autorange == 1:
|
||||||
|
if max_res >= 0.9 * self.range and self.irange < 9:
|
||||||
|
self.write_irange(self.irange + 1)
|
||||||
|
elif max_res <= 0.3 * self.range and self.irange > 0:
|
||||||
|
self.write_irange(self.irange - 1)
|
||||||
|
|
||||||
|
def read_status(self):
|
||||||
|
"""
|
||||||
|
Both the mainframe (SR900) and the module (SR921) have the same commands for the status,
|
||||||
|
here implemented commands are made for module status, not the frame!
|
||||||
|
|
||||||
|
:return: status type and message
|
||||||
|
"""
|
||||||
|
esr = int(self.communicate('*esr?')) # standart event status byte
|
||||||
|
ovsr = int(self.communicate('ovsr?')) # overload status
|
||||||
|
cesr = int(self.communicate('cesr?')) # communication error status
|
||||||
|
|
||||||
|
if esr & (1 << 1):
|
||||||
|
return ERROR, 'input error, cleared'
|
||||||
|
if esr & (1 << 2):
|
||||||
|
return ERROR, 'query error'
|
||||||
|
if esr & (1 << 4):
|
||||||
|
return ERROR, 'execution error'
|
||||||
|
if esr & (1 << 5):
|
||||||
|
return ERROR, 'command error'
|
||||||
|
if cesr & (1 << 0):
|
||||||
|
return ERROR, 'parity error'
|
||||||
|
if cesr & (1 << 2):
|
||||||
|
return ERROR, 'noise error'
|
||||||
|
if cesr & (1 << 4):
|
||||||
|
return ERROR, 'input overflow, cleared'
|
||||||
|
if cesr & (1 << 3):
|
||||||
|
return ERROR, 'hardware overflow'
|
||||||
|
if ovsr & (1 << 0):
|
||||||
|
return ERROR, 'output overload'
|
||||||
|
if cesr & (1 << 7):
|
||||||
|
return WARN, 'device clear'
|
||||||
|
if ovsr & (1 << 2):
|
||||||
|
return WARN, 'current saturation'
|
||||||
|
if ovsr & (1 << 3):
|
||||||
|
return WARN, 'under servo'
|
||||||
|
if ovsr & (1 << 4):
|
||||||
|
return WARN, 'over servo'
|
||||||
|
return IDLE, ''
|
||||||
|
|
||||||
|
def read_value(self):
|
||||||
|
return self.query('rval?')
|
||||||
|
|
||||||
|
def read_irange(self):
|
||||||
|
"""index of the resistance value according to the range"""
|
||||||
|
return self.query('rang?')
|
||||||
|
|
||||||
|
def write_irange(self, idx):
|
||||||
|
value = int(idx)
|
||||||
|
self.query(f'rang {value}; rang?')
|
||||||
|
self.read_range()
|
||||||
|
return value
|
||||||
|
|
||||||
|
def read_range(self):
|
||||||
|
"""value of the resistance range"""
|
||||||
|
idx = self.read_irange()
|
||||||
|
name = self.RES_RANGE[idx]
|
||||||
|
return string_to_value(name)
|
||||||
|
|
||||||
|
def write_range(self, target):
|
||||||
|
cl_idx, cl_value = find_idx(self.RES_RANGE_values, target)
|
||||||
|
self.query(f'rang {cl_idx}; rang?')
|
||||||
|
return cl_value
|
||||||
|
|
||||||
|
def read_output_offset(self):
|
||||||
|
"""Output offset, can be set by user. This is the value subtracted from the measured value"""
|
||||||
|
return self.query('rset?')
|
||||||
|
|
||||||
|
def write_output_offset(self, output_offset):
|
||||||
|
self.query(f'rset {output_offset};rset?')
|
||||||
|
|
||||||
|
def read_itc(self):
|
||||||
|
"""index of the temperature constant value according to the range"""
|
||||||
|
return self.query('tcon?')
|
||||||
|
|
||||||
|
def write_itc(self, itc):
|
||||||
|
self.read_itc()
|
||||||
|
value = int(itc)
|
||||||
|
return self.query(f'tcon {value}; tcon?')
|
||||||
|
|
||||||
|
def read_tc(self):
|
||||||
|
idx = self.read_itc()
|
||||||
|
name = self.TIME_CONST[idx]
|
||||||
|
return string_to_value(name)
|
||||||
|
|
||||||
|
def write_tc(self, target):
|
||||||
|
cl_idx, cl_value = find_idx(self.TIME_CONST_values, target)
|
||||||
|
self.query(f'tcon {cl_idx};tcon?')
|
||||||
|
return cl_value
|
||||||
|
|
||||||
|
def read_autorange(self):
|
||||||
|
return self.autorange
|
||||||
|
|
||||||
|
def write_autorange(self, value):
|
||||||
|
self.query(f'agai {value:d};agai?')
|
||||||
|
return value
|
||||||
|
|
||||||
|
def read_iexct(self):
|
||||||
|
"""index of the excitation value according to the range"""
|
||||||
|
return int(self.query('exci?'))
|
||||||
|
|
||||||
|
def write_iexct(self, iexct):
|
||||||
|
value = int(iexct)
|
||||||
|
return self.query(f'exci {value};exci?')
|
||||||
|
|
||||||
|
def write_exct(self, target):
|
||||||
|
target = float(target)
|
||||||
|
cl_idx = None
|
||||||
|
cl_value = float('inf')
|
||||||
|
min_diff = float('inf')
|
||||||
|
|
||||||
|
for idx, value in enumerate(self.EXCT_RANGE_values):
|
||||||
|
diff = abs(value - target)
|
||||||
|
|
||||||
|
if diff < min_diff:
|
||||||
|
min_diff = diff
|
||||||
|
cl_value = value
|
||||||
|
cl_idx = idx
|
||||||
|
|
||||||
|
self.write_iexct(cl_idx)
|
||||||
|
return cl_value
|
||||||
|
|
||||||
|
def read_exct(self):
|
||||||
|
idx = int(self.read_iexct())
|
||||||
|
name = self.EXCT_RANGE[idx + 1]
|
||||||
|
return string_to_value(name)
|
||||||
|
|
||||||
|
def read_phase_hold(self):
|
||||||
|
"""
|
||||||
|
Set the phase hold mode (if on - phase is assumed to be zero).
|
||||||
|
:return: 0 - off, 1 - on
|
||||||
|
"""
|
||||||
|
return int(self.communicate('phld?'))
|
||||||
|
|
||||||
|
def write_phase_hold(self, phase_hold):
|
||||||
|
self.communicate(f'phld {phase_hold}')
|
||||||
|
return self.read_phase_hold()
|
||||||
|
|
||||||
|
|
||||||
|
class Phase(Readable):
|
||||||
|
resistance = Attached()
|
||||||
|
value = Parameter('phase', FloatRange, default=0, unit='deg')
|
||||||
|
|
||||||
|
def read_value(self):
|
||||||
|
return self.resistance.query('phas?')
|
||||||
|
|
||||||
|
|
||||||
|
class Deviation(Readable):
|
||||||
|
resistance = Attached()
|
||||||
|
value = Parameter('resistance deviation', FloatRange(), unit='Ohm')
|
||||||
|
|
||||||
|
def read_value(self):
|
||||||
|
return self.resistance.query('rdev?')
|
@ -1,576 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# *****************************************************************************
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
|
||||||
# *****************************************************************************
|
|
||||||
"""Software calibration"""
|
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
from os.path import basename, dirname, exists, join
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
from scipy.interpolate import PchipInterpolator, CubicSpline, PPoly # pylint: disable=import-error
|
|
||||||
|
|
||||||
|
|
||||||
from frappy.errors import ProgrammingError, RangeError
|
|
||||||
from frappy.lib import clamp
|
|
||||||
|
|
||||||
to_scale = {
|
|
||||||
'lin': lambda x: x,
|
|
||||||
'log': lambda x: np.log10(x),
|
|
||||||
}
|
|
||||||
from_scale = {
|
|
||||||
'lin': lambda x: x,
|
|
||||||
'log': lambda x: 10 ** np.array(x),
|
|
||||||
}
|
|
||||||
TYPES = [ # lakeshore type, inp-type, loglog
|
|
||||||
('DT', 'si', False), # Si diode
|
|
||||||
('TG', 'gaalas', False), # GaAlAs diode
|
|
||||||
('PT', 'pt250', False), # platinum, 250 Ohm range
|
|
||||||
('PT', 'pt500', False), # platinum, 500 Ohm range
|
|
||||||
('PT', 'pt2500', False), # platinum, 2500 Ohm range
|
|
||||||
('RF', 'rhfe', False), # rhodium iron
|
|
||||||
('CC', 'c', True), # carbon, LakeShore acronym unknown
|
|
||||||
('CX', 'cernox', True), # Cernox
|
|
||||||
('RX', 'ruox', True), # rutheniumm oxide
|
|
||||||
('GE', 'ge', True), # germanium, LakeShore acronym unknown
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
OPTION_TYPE = {
|
|
||||||
'loglog': 0, # boolean
|
|
||||||
'extrange': 2, # tuple(min T, max T for extrapolation
|
|
||||||
'calibrange': 2, # tuple(min T, max T)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class HasOptions:
|
|
||||||
def insert_option(self, key, value):
|
|
||||||
key = key.strip()
|
|
||||||
argtype = OPTION_TYPE.get(key, 1)
|
|
||||||
if argtype == 1: # one number or string
|
|
||||||
try:
|
|
||||||
value = float(value)
|
|
||||||
except ValueError:
|
|
||||||
value = value.strip()
|
|
||||||
elif argtype == 0:
|
|
||||||
if value.strip().lower() in ('false', '0'):
|
|
||||||
value = False
|
|
||||||
else:
|
|
||||||
value = True
|
|
||||||
else:
|
|
||||||
value = [float(f) for f in value.split(',')]
|
|
||||||
self.options[key] = value
|
|
||||||
|
|
||||||
|
|
||||||
class StdParser(HasOptions):
|
|
||||||
"""parser used for reading columns"""
|
|
||||||
def __init__(self, **options):
|
|
||||||
"""keys of options may be either 'x' or 'logx' and either 'y' or 'logy'
|
|
||||||
|
|
||||||
default is x=0, y=1
|
|
||||||
"""
|
|
||||||
if 'logx' in options:
|
|
||||||
self.xscale = 'log'
|
|
||||||
self.xcol = options.pop('logx')
|
|
||||||
else:
|
|
||||||
self.xscale = 'lin'
|
|
||||||
self.xcol = options.pop('x', 0)
|
|
||||||
if 'logy' in options:
|
|
||||||
self.yscale = 'log'
|
|
||||||
self.ycol = options.pop('logy')
|
|
||||||
else:
|
|
||||||
self.yscale = 'lin'
|
|
||||||
self.ycol = options.pop('y', 1)
|
|
||||||
self.xdata, self.ydata = [], []
|
|
||||||
self.options = options
|
|
||||||
self.invalid_lines = []
|
|
||||||
|
|
||||||
def parse(self, line):
|
|
||||||
"""get numbers from a line and put them to self.xdata / self.ydata"""
|
|
||||||
row = line.split()
|
|
||||||
try:
|
|
||||||
self.xdata.append(float(row[self.xcol]))
|
|
||||||
self.ydata.append(float(row[self.ycol]))
|
|
||||||
except (IndexError, ValueError):
|
|
||||||
self.invalid_lines.append(line)
|
|
||||||
return
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class InpParser(StdParser):
|
|
||||||
"""M. Zollikers *.inp calcurve format"""
|
|
||||||
HEADERLINE = re.compile(r'#?(?:(\w+)\s*=\s*([^!# \t\n]*)|curv.*)')
|
|
||||||
INP_TYPES = {ityp: (ltyp, loglog) for ltyp, ityp, loglog in TYPES}
|
|
||||||
|
|
||||||
def __init__(self, **options):
|
|
||||||
options.update(x=0, y=1)
|
|
||||||
super().__init__(**options)
|
|
||||||
self.header = True
|
|
||||||
|
|
||||||
def parse(self, line):
|
|
||||||
"""scan header"""
|
|
||||||
if self.header:
|
|
||||||
match = self.HEADERLINE.match(line)
|
|
||||||
if match:
|
|
||||||
key, value = match.groups()
|
|
||||||
if key is None:
|
|
||||||
self.header = False
|
|
||||||
else:
|
|
||||||
key = key.lower()
|
|
||||||
value = value.strip()
|
|
||||||
if key == 'type':
|
|
||||||
type_, loglog = self.INP_TYPES.get(value.lower(), (None, None))
|
|
||||||
if type_ is not None:
|
|
||||||
self.options['type'] = type_
|
|
||||||
if loglog is not None:
|
|
||||||
self.options['loglog'] = loglog
|
|
||||||
else:
|
|
||||||
self.insert_option(key, value)
|
|
||||||
return
|
|
||||||
elif line.startswith('!'):
|
|
||||||
return
|
|
||||||
super().parse(line)
|
|
||||||
|
|
||||||
|
|
||||||
class Parser340(StdParser):
|
|
||||||
"""parser for LakeShore *.340 files"""
|
|
||||||
HEADERLINE = re.compile(r'([^:]*):\s*([^(]*)')
|
|
||||||
CALIBHIGH = dict(L=325, M=420, H=500, B=40)
|
|
||||||
|
|
||||||
def __init__(self, **options):
|
|
||||||
options.update(x=1, y=2)
|
|
||||||
super().__init__(**options)
|
|
||||||
self.header = True
|
|
||||||
|
|
||||||
def parse(self, line):
|
|
||||||
"""scan header"""
|
|
||||||
if self.header:
|
|
||||||
match = self.HEADERLINE.match(line)
|
|
||||||
if match:
|
|
||||||
key, value = match.groups()
|
|
||||||
key = ''.join(key.split()).lower()
|
|
||||||
value = value.strip()
|
|
||||||
if key == 'dataformat':
|
|
||||||
if value[0:1] == '4':
|
|
||||||
self.xscale, self.yscale = 'log', 'lin' # logOhm
|
|
||||||
self.options['loglog'] = True
|
|
||||||
elif value[0:1] == '5':
|
|
||||||
self.xscale, self.yscale = 'log', 'log' # logOhm, logK
|
|
||||||
self.options['loglog'] = True
|
|
||||||
elif value[0:1] in ('1', '2', '3'):
|
|
||||||
self.options['loglog'] = False
|
|
||||||
else:
|
|
||||||
raise ValueError('invalid Data Format')
|
|
||||||
self.options['scale'] = self.xscale + self.yscale
|
|
||||||
self.insert_option(key, value)
|
|
||||||
return
|
|
||||||
if 'No.' in line:
|
|
||||||
self.header = False
|
|
||||||
return
|
|
||||||
if len(line.split()) != 3:
|
|
||||||
return
|
|
||||||
super().parse(line)
|
|
||||||
if self.header and self.xdata:
|
|
||||||
# valid line detected
|
|
||||||
self.header = False
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
model = self.options.get('sensormodel', '').split('-')
|
|
||||||
if model[0]:
|
|
||||||
self.options['type'] = model[0]
|
|
||||||
if 'calibrange' not in self.options:
|
|
||||||
if len(model) > 2:
|
|
||||||
try: # e.g. model[-1] == 1.4M -> calibrange = 1.4, 420
|
|
||||||
self.options['calibrange'] = float(model[-1][:-1]), self.CALIBHIGH[model[-1][-1]]
|
|
||||||
return
|
|
||||||
except (ValueError, KeyError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class CaldatParser(StdParser):
|
|
||||||
"""parser for format from sea/tcl/startup/calib_ext.tcl"""
|
|
||||||
|
|
||||||
def __init__(self, options):
|
|
||||||
options.update(x=1, y=2)
|
|
||||||
super().__init__(options)
|
|
||||||
|
|
||||||
|
|
||||||
PARSERS = {
|
|
||||||
"340": Parser340,
|
|
||||||
"inp": InpParser,
|
|
||||||
"caldat": CaldatParser,
|
|
||||||
"dat": StdParser, # lakeshore raw data *.dat format
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def check(x, y, islog):
|
|
||||||
# check interpolation error
|
|
||||||
yi = y[:-2] + (x[1:-1] - x[:-2]) * (y[2:] - y[:-2]) / (x[2:] - x[:-2])
|
|
||||||
if islog:
|
|
||||||
return sum((yi - y[1:-1]) ** 2)
|
|
||||||
return sum((np.log10(yi) - np.log10(y[1:-1])) ** 2)
|
|
||||||
|
|
||||||
|
|
||||||
def get_curve(newscale, curves):
|
|
||||||
"""get curve from curve cache (converts not existing ones)
|
|
||||||
|
|
||||||
:param newscale: the new scale to get
|
|
||||||
:param curves: a dict <scale> of <array> storing available scales
|
|
||||||
:return: the retrieved or converted curve
|
|
||||||
"""
|
|
||||||
if newscale in curves:
|
|
||||||
return curves[newscale]
|
|
||||||
for scale, array in curves.items():
|
|
||||||
curves[newscale] = curve = to_scale[newscale](from_scale[scale](array))
|
|
||||||
return curve
|
|
||||||
|
|
||||||
|
|
||||||
class CalCurve(HasOptions):
|
|
||||||
EXTRAPOLATION_AMOUNT = 0.1
|
|
||||||
MAX_EXTRAPOLATION_FACTOR = 2
|
|
||||||
|
|
||||||
def __init__(self, calibspec=None, *, x=None, y=None, cubic_spline=True, **options):
|
|
||||||
"""calibration curve
|
|
||||||
|
|
||||||
:param calibspec: a string with name or filename, options
|
|
||||||
lookup path for files in env. variable FRAPPY_CALIB_PATH
|
|
||||||
calibspec format:
|
|
||||||
[<full path> | <name>][,<key>=<value> ...]
|
|
||||||
for <key>/<value> as in parser arguments
|
|
||||||
:param x, y: x and y arrays (given instead of calibspec)
|
|
||||||
:param cubic_split: set to False for always using Pchip interpolation
|
|
||||||
:param options: options for parsers
|
|
||||||
"""
|
|
||||||
self.options = options
|
|
||||||
if calibspec is None:
|
|
||||||
parser = StdParser()
|
|
||||||
parser.xdata = x
|
|
||||||
parser.ydata = y
|
|
||||||
else:
|
|
||||||
if x or y:
|
|
||||||
raise ProgrammingError('can not give both calibspec and x,y ')
|
|
||||||
sensopt = calibspec.split(',')
|
|
||||||
calibname = sensopt.pop(0)
|
|
||||||
_, dot, ext = basename(calibname).rpartition('.')
|
|
||||||
kind = None
|
|
||||||
pathlist = os.environ.get('FRAPPY_CALIB_PATH', '').split(':')
|
|
||||||
pathlist.append(join(dirname(__file__), 'calcurves'))
|
|
||||||
for path in pathlist:
|
|
||||||
# first try without adding kind
|
|
||||||
filename = join(path.strip(), calibname)
|
|
||||||
if exists(filename):
|
|
||||||
kind = ext if dot else None
|
|
||||||
break
|
|
||||||
# then try adding all kinds as extension
|
|
||||||
for nam in calibname, calibname.upper(), calibname.lower():
|
|
||||||
for kind in PARSERS:
|
|
||||||
filename = join(path.strip(), '%s.%s' % (nam, kind))
|
|
||||||
if exists(filename):
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise FileNotFoundError(calibname)
|
|
||||||
sensopt = iter(sensopt)
|
|
||||||
for opt in sensopt:
|
|
||||||
key, _, value = opt.lower().partition('=')
|
|
||||||
if OPTION_TYPE.get(key) == 2:
|
|
||||||
self.options[key] = float(value), float(next(sensopt))
|
|
||||||
else:
|
|
||||||
self.insert_option(key, value)
|
|
||||||
kind = self.options.pop('kind', kind)
|
|
||||||
cls = PARSERS.get(kind, StdParser)
|
|
||||||
try:
|
|
||||||
parser = cls(**self.options)
|
|
||||||
with open(filename, encoding='utf-8') as f:
|
|
||||||
for line in f:
|
|
||||||
parser.parse(line)
|
|
||||||
parser.finish()
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError('error parsing calib curve %s %r' % (calibspec, e)) from e
|
|
||||||
# take defaults from parser options
|
|
||||||
self.options = dict(parser.options, **self.options)
|
|
||||||
|
|
||||||
x = np.asarray(parser.xdata)
|
|
||||||
y = np.asarray(parser.ydata)
|
|
||||||
if len(x) < 2:
|
|
||||||
raise ValueError('calib file %s has less than 2 points' % calibspec)
|
|
||||||
|
|
||||||
if x[0] > x[-1]:
|
|
||||||
x = np.flip(np.array(x))
|
|
||||||
y = np.flip(np.array(y))
|
|
||||||
else:
|
|
||||||
x = np.array(x)
|
|
||||||
y = np.array(y)
|
|
||||||
not_incr_idx = np.argwhere(x[1:] <= x[:-1])
|
|
||||||
if len(not_incr_idx):
|
|
||||||
raise RangeError('x not monotonic at x=%.4g' % x[not_incr_idx[0]])
|
|
||||||
|
|
||||||
self.x = {parser.xscale: x}
|
|
||||||
self.y = {parser.yscale: y}
|
|
||||||
self.lin_forced = [parser.yscale == 'lin' and (y[0] <= 0 or y[-1] <= 0),
|
|
||||||
parser.xscale == 'lin' and x[0] <= 0]
|
|
||||||
if sum(self.lin_forced):
|
|
||||||
self.loglog = False
|
|
||||||
else:
|
|
||||||
self.loglog = self.options.get('loglog', y[0] > y[-1]) # loglog defaults to True for NTC
|
|
||||||
newscale = 'log' if self.loglog else 'lin'
|
|
||||||
self.scale = newscale
|
|
||||||
x = get_curve(newscale, self.x)
|
|
||||||
y = get_curve(newscale, self.y)
|
|
||||||
self.convert_x = to_scale[newscale]
|
|
||||||
self.convert_y = from_scale[newscale]
|
|
||||||
self.calibrange = self.options.get('calibrange')
|
|
||||||
dirty = set()
|
|
||||||
self.extra_points = False
|
|
||||||
self.cutted = False
|
|
||||||
if self.calibrange:
|
|
||||||
self.calibrange = sorted(self.calibrange)
|
|
||||||
# determine indices (ibeg, iend) of first and last well calibrated point
|
|
||||||
ylin = get_curve('lin', self.y)
|
|
||||||
beg, end = self.calibrange
|
|
||||||
if y[0] > y[-1]:
|
|
||||||
ylin = -ylin
|
|
||||||
beg, end = -end, -beg
|
|
||||||
|
|
||||||
ibeg, iend = np.searchsorted(ylin, (beg, end))
|
|
||||||
if ibeg > 0 and abs(ylin[ibeg-1] - beg) < 0.1 * (ylin[ibeg] - ylin[ibeg-1]):
|
|
||||||
# add previous point, if close
|
|
||||||
ibeg -= 1
|
|
||||||
if iend < len(ylin) and abs(ylin[iend] - end) < 0.1 * (ylin[iend] - ylin[iend-1]):
|
|
||||||
# add next point, if close
|
|
||||||
iend += 1
|
|
||||||
if self.options.get('cut', False):
|
|
||||||
self.cutted = True
|
|
||||||
x = x[ibeg:iend]
|
|
||||||
y = y[ibeg:iend]
|
|
||||||
self.x = {newscale: x}
|
|
||||||
self.y = {newscale: y}
|
|
||||||
ibeg = 0
|
|
||||||
iend = len(x)
|
|
||||||
dirty.add('xy')
|
|
||||||
else:
|
|
||||||
self.extra_points = ibeg, len(x) - iend
|
|
||||||
else:
|
|
||||||
ibeg = 0
|
|
||||||
iend = len(x)
|
|
||||||
ylin = get_curve('lin', self.y)
|
|
||||||
self.calibrange = tuple(sorted([ylin[0], ylin[-1]]))
|
|
||||||
|
|
||||||
if cubic_spline:
|
|
||||||
# fit well calibrated part with spline
|
|
||||||
# determine slopes of calibrated part with CubicSpline
|
|
||||||
spline = CubicSpline(x[ibeg:iend], y[ibeg:iend])
|
|
||||||
roots = spline.derivative().roots(extrapolate=False)
|
|
||||||
if len(roots):
|
|
||||||
cubic_spline = False
|
|
||||||
|
|
||||||
self.cubic_spline = cubic_spline
|
|
||||||
if cubic_spline:
|
|
||||||
coeff = spline.c
|
|
||||||
if self.extra_points:
|
|
||||||
p = PchipInterpolator(x, y).c
|
|
||||||
# use Pchip outside left and right of calibrange
|
|
||||||
# remark: first derivative at end of calibrange is not continuous
|
|
||||||
coeff = np.concatenate((p[:, :ibeg], coeff, p[:, iend-1:]), axis=1)
|
|
||||||
else:
|
|
||||||
spline = PchipInterpolator(x, y)
|
|
||||||
coeff = spline.c
|
|
||||||
# extrapolation extension
|
|
||||||
# linear extrapolation is more robust than spline extrapolation
|
|
||||||
x1, x2 = x[0], x[-1]
|
|
||||||
# take slope at end of calibrated range for extrapolation
|
|
||||||
slopes = spline([x[ibeg], x[iend-1]], 1)
|
|
||||||
for i, j in enumerate([ibeg, iend-2]):
|
|
||||||
# slope of last interval in calibrange
|
|
||||||
si = (y[j+1] - y[j])/(x[j+1] - x[j])
|
|
||||||
# make sure slope is not more than a factor 2 different
|
|
||||||
# from the slope calculated at the outermost calibrated intervals
|
|
||||||
slopes[i] = clamp(slopes[i], 2*si, 0.5 * si)
|
|
||||||
dx = 0.1 if self.loglog else (x2 - x1) * 0.1
|
|
||||||
xe = np.concatenate(([x1 - dx], x, [x2 + dx]))
|
|
||||||
# x3 = np.append(x, x2 + dx)
|
|
||||||
# y3 = np.append(y, y[-1] + slope * dx)
|
|
||||||
y0 = y[0] - slopes[0] * dx
|
|
||||||
coeff = np.concatenate(([[0], [0], [slopes[0]], [y0]], coeff, [[0], [0], [slopes[1]], [y[-1]]]), axis=1)
|
|
||||||
self.spline = PPoly(coeff, xe)
|
|
||||||
# ranges without extrapolation:
|
|
||||||
self.xrange = get_curve('lin', self.x)[[0, -1]]
|
|
||||||
self.yrange = sorted(get_curve('lin', self.y)[[0, -1]])
|
|
||||||
self.calibrange = [max(self.calibrange[0], self.yrange[0]),
|
|
||||||
min(self.calibrange[1], self.yrange[1])]
|
|
||||||
self.set_extrapolation()
|
|
||||||
|
|
||||||
# check
|
|
||||||
# ys = self.spline(xe)
|
|
||||||
# ye = np.concatenate(([y0], y, [y[-1] + slope2 * dx]))
|
|
||||||
# assert np.all(np.abs(ys - ye) < 1e-5 * (0.1 + np.abs(ys + ye)))
|
|
||||||
|
|
||||||
def set_extrapolation(self, extleft=None, extright=None):
|
|
||||||
"""set default extrapolation range for export method
|
|
||||||
|
|
||||||
:param extleft: y value for the lower end of the extrapolation
|
|
||||||
:param extright: y value for the upper end of the extrapolation
|
|
||||||
|
|
||||||
if arguments omitted or None are replaced by a default extrapolation scheme
|
|
||||||
|
|
||||||
on return self.extx and self.exty are set to the extrapolated ranges
|
|
||||||
"""
|
|
||||||
yc1, yc2 = self.calibrange
|
|
||||||
y1, y2 = to_scale[self.scale]([yc1, yc2])
|
|
||||||
d = (y2 - y1) * self.EXTRAPOLATION_AMOUNT
|
|
||||||
yex1, yex2 = tuple(from_scale[self.scale]([y1 - d, y2 + d]))
|
|
||||||
t1, t2 = tuple(from_scale[self.scale]([y1, y2]))
|
|
||||||
|
|
||||||
# raw units, excluding extrapolation points at end
|
|
||||||
xrng = self.spline.x[1], self.spline.x[-2]
|
|
||||||
# first and last point
|
|
||||||
yp1, yp2 = sorted(from_scale[self.scale](self.spline(xrng)))
|
|
||||||
xrng = from_scale[self.scale](xrng)
|
|
||||||
|
|
||||||
# limit by maximal factor
|
|
||||||
f = self.MAX_EXTRAPOLATION_FACTOR
|
|
||||||
# but ext range should be at least to the points in curve
|
|
||||||
self.exty = [min(yp1, max(yex1, min(t1 / f, t1 * f))),
|
|
||||||
max(yp2, min(yex2, max(t2 * f, t2 / f)))]
|
|
||||||
if extleft is not None:
|
|
||||||
self.exty[0] = min(extleft, yp1)
|
|
||||||
if extright is not None:
|
|
||||||
self.exty[1] = max(extright, yp2)
|
|
||||||
self.extx = sorted(self.invert(*yd) for yd in zip(self.exty, xrng))
|
|
||||||
# check that sensor range is not extended by more than a factor f
|
|
||||||
extnew = [max(self.extx[0], min(xrng[0] / f, xrng[0] * f)),
|
|
||||||
min(self.extx[1], max(xrng[1] / f, xrng[1] * f))]
|
|
||||||
if extnew != self.extx:
|
|
||||||
# need further reduction
|
|
||||||
self.extx = extnew
|
|
||||||
self.exty = sorted(self(extnew))
|
|
||||||
|
|
||||||
def convert(self, value):
|
|
||||||
"""convert a single value
|
|
||||||
|
|
||||||
return a tuple (converted value, boolean: was it clamped?)
|
|
||||||
"""
|
|
||||||
x = clamp(value, *self.extx)
|
|
||||||
return self(x), x == value
|
|
||||||
|
|
||||||
def __call__(self, value):
|
|
||||||
"""convert value or numpy array without checking extrapolation range"""
|
|
||||||
return self.convert_y(self.spline(self.convert_x(value)))
|
|
||||||
|
|
||||||
def invert(self, y, defaultx=None, xscale=True, yscale=True):
|
|
||||||
"""invert y, return defaultx if no solution is found"""
|
|
||||||
if yscale:
|
|
||||||
y = to_scale[self.scale](y)
|
|
||||||
r = self.spline.solve(y)
|
|
||||||
try:
|
|
||||||
if xscale:
|
|
||||||
return from_scale[self.scale](r[0])
|
|
||||||
return r[0]
|
|
||||||
except IndexError:
|
|
||||||
return defaultx
|
|
||||||
|
|
||||||
def export(self, logformat=False, nmax=199, yrange=None, extrapolate=True, xlimits=None):
|
|
||||||
"""export curve for downloading to hardware
|
|
||||||
|
|
||||||
:param nmax: max number of points. if the number of given points is bigger,
|
|
||||||
the points with the lowest interpolation error are omitted
|
|
||||||
:param logformat: a list with two elements of None, True or False
|
|
||||||
True: use log, False: use line, None: use log if self.loglog
|
|
||||||
values None are replaced with the effectively used format
|
|
||||||
False / True are replaced by [False, False] / [True, True]
|
|
||||||
default is False
|
|
||||||
:param yrange: to reduce or extrapolate to this interval (extrapolate is ignored when given)
|
|
||||||
:param extrapolate: a flag indicating whether the curves should be extrapolated
|
|
||||||
to the preset extrapolation range
|
|
||||||
:param xlimits: max x range
|
|
||||||
:return: numpy array with 2 dimensions returning the curve
|
|
||||||
"""
|
|
||||||
|
|
||||||
if logformat in (True, False):
|
|
||||||
logformat = [logformat, logformat]
|
|
||||||
try:
|
|
||||||
scales = []
|
|
||||||
for idx, logfmt in enumerate(logformat):
|
|
||||||
if logfmt and self.lin_forced[idx]:
|
|
||||||
raise ValueError('%s must contain positive values only' % 'xy'[idx])
|
|
||||||
logformat[idx] = linlog = self.loglog if logfmt is None else logfmt
|
|
||||||
scales.append('log' if linlog else 'lin')
|
|
||||||
xscale, yscale = scales
|
|
||||||
except (TypeError, AssertionError):
|
|
||||||
raise ValueError('logformat must be a 2 element list or a boolean')
|
|
||||||
|
|
||||||
x = self.spline.x[1:-1] # raw units, excluding extrapolated points
|
|
||||||
x1, x2 = xmin, xmax = x[0], x[-1]
|
|
||||||
y1, y2 = sorted(self.spline([x1, x2]))
|
|
||||||
|
|
||||||
if extrapolate and not yrange:
|
|
||||||
yrange = self.exty
|
|
||||||
if yrange is not None:
|
|
||||||
xmin, xmax = sorted(self.invert(*yd, xscale=False) for yd in zip(yrange, [x1, x2]))
|
|
||||||
if xlimits is not None:
|
|
||||||
lim = to_scale[self.scale](xlimits)
|
|
||||||
xmin = clamp(xmin, *lim)
|
|
||||||
xmax = clamp(xmax, *lim)
|
|
||||||
if xmin != x1 or xmax != x2:
|
|
||||||
ibeg, iend = np.searchsorted(x, (xmin, xmax))
|
|
||||||
if abs(x[ibeg] - xmin) < 0.1 * (x[ibeg + 1] - x[ibeg]):
|
|
||||||
# remove first point, if close
|
|
||||||
ibeg += 1
|
|
||||||
if abs(x[iend - 1] - xmax) < 0.1 * (x[iend - 1] - x[iend - 2]):
|
|
||||||
# remove last point, if close
|
|
||||||
iend -= 1
|
|
||||||
x = np.concatenate(([xmin], x[ibeg:iend], [xmax]))
|
|
||||||
y = self.spline(x)
|
|
||||||
|
|
||||||
# convert to exported scale
|
|
||||||
if xscale != self.scale:
|
|
||||||
x = to_scale[xscale](from_scale[self.scale](x))
|
|
||||||
if yscale != self.scale:
|
|
||||||
y = to_scale[yscale](from_scale[self.scale](y))
|
|
||||||
|
|
||||||
# reduce number of points, if needed
|
|
||||||
n = len(x)
|
|
||||||
i, j = 1, n - 1 # index range for calculating interpolation deviation
|
|
||||||
deviation = np.zeros(n)
|
|
||||||
while True:
|
|
||||||
# calculate interpolation error when a single point is omitted
|
|
||||||
ym = y[i-1:j-1] + (x[i:j] - x[i-1:j-1]) * (y[i+1:j+1] - y[i-1:j-1]) / (x[i+1:j+1] - x[i-1:j-1])
|
|
||||||
if yscale == 'log':
|
|
||||||
deviation[i:j] = np.abs(ym - y[i:j])
|
|
||||||
else:
|
|
||||||
deviation[i:j] = np.abs(ym - y[i:j]) / (np.abs(ym + y[i:j]) + 1e-10)
|
|
||||||
if n <= nmax:
|
|
||||||
break
|
|
||||||
idx = np.argmin(deviation[1:-1]) + 1 # find index of the smallest error
|
|
||||||
y = np.delete(y, idx)
|
|
||||||
x = np.delete(x, idx)
|
|
||||||
deviation = np.delete(deviation, idx)
|
|
||||||
n -= 1
|
|
||||||
# index range to recalculate
|
|
||||||
i, j = max(1, idx - 1), min(n - 1, idx + 1)
|
|
||||||
self.deviation = deviation # for debugging purposes
|
|
||||||
return np.stack([x, y], axis=1)
|
|
@ -1,116 +0,0 @@
|
|||||||
libpath = '/home/l_samenv/frappy/cetoniSDK/CETONI_SDK_Raspi_64bit_v20220627/python/src/'
|
|
||||||
import sys
|
|
||||||
if libpath not in sys.path:
|
|
||||||
sys.path.append(libpath)
|
|
||||||
|
|
||||||
from frappy.core import Drivable, Readable, StringIO, HasIO, FloatRange, IntRange, StringType, BoolType, EnumType, \
|
|
||||||
Parameter, Property, PersistentParam, Command, IDLE, BUSY, ERROR, Attached
|
|
||||||
from qmixsdk import qmixbus
|
|
||||||
from qmixsdk import qmixpump
|
|
||||||
from qmixsdk import qmixvalve
|
|
||||||
from qmixsdk.qmixbus import UnitPrefix, TimeUnit
|
|
||||||
|
|
||||||
class LabCannBus(Readable):
|
|
||||||
deviceconfig = Property('config files', StringType(),default="/home/l_samenv/frappy/cetoniSDK/CETONI_SDK_Raspi_64bit_v20220627/config/dual_pumps")
|
|
||||||
|
|
||||||
def earlyInit(self):
|
|
||||||
super().earlyInit()
|
|
||||||
self.bus = qmixbus.Bus()
|
|
||||||
self.bus.open(self.deviceconfig, "")
|
|
||||||
|
|
||||||
def initModule(self):
|
|
||||||
super().initModule()
|
|
||||||
self.bus.start()
|
|
||||||
|
|
||||||
def shutdownModule(self):
|
|
||||||
"""Not so gracefully close the connection"""
|
|
||||||
self.bus.stop()
|
|
||||||
self.bus.close()
|
|
||||||
|
|
||||||
class SyringePump(Drivable):
|
|
||||||
io = Attached()
|
|
||||||
pump_name = Property('name of pump', StringType(),default="Nemesys_S_1_Pump")
|
|
||||||
valve_name = Property('name of valve', StringType(),default="Nemesys_S_1_Valve")
|
|
||||||
|
|
||||||
inner_diameter_set = Property('inner diameter', FloatRange(), default=1)
|
|
||||||
piston_stroke_set = Property('piston stroke', FloatRange(), default=60)
|
|
||||||
|
|
||||||
value = PersistentParam('volume', FloatRange(unit='mL'))
|
|
||||||
status = PersistentParam()
|
|
||||||
|
|
||||||
max_flow_rate = Parameter('max flow rate', FloatRange(0,100000, unit='mL/min',), readonly=True)
|
|
||||||
max_volume = Parameter('max volume', FloatRange(0,100000, unit='mL',), readonly=True)
|
|
||||||
target_flow_rate = Parameter('target flow rate', FloatRange(unit='mL/min'), readonly=False)
|
|
||||||
real_flow_rate = Parameter('actual flow rate', FloatRange(unit='mL/min'), readonly=True)
|
|
||||||
target = Parameter('target volume', FloatRange(unit='mL'), readonly=False)
|
|
||||||
|
|
||||||
no_of_valve_pos = Property('number of valve positions', IntRange(0,10), default=1)
|
|
||||||
valve_pos = Parameter('valve position', EnumType('valve', CLOSED=0, APP=1, RES=2, OPEN=3), readonly=False)
|
|
||||||
|
|
||||||
def initModule(self):
|
|
||||||
super().initModule()
|
|
||||||
|
|
||||||
self.pump = qmixpump.Pump()
|
|
||||||
self.pump.lookup_by_name(self.pump_name)
|
|
||||||
|
|
||||||
self.valve = qmixvalve.Valve()
|
|
||||||
self.valve.lookup_by_name(self.valve_name)
|
|
||||||
|
|
||||||
|
|
||||||
def initialReads(self):
|
|
||||||
if self.pump.is_in_fault_state():
|
|
||||||
self.pump.clear_fault()
|
|
||||||
|
|
||||||
if not self.pump.is_enabled():
|
|
||||||
self.pump.enable(True)
|
|
||||||
|
|
||||||
self.pump.set_syringe_param(self.inner_diameter_set, self.piston_stroke_set)
|
|
||||||
|
|
||||||
self.pump.set_volume_unit(qmixpump.UnitPrefix.milli, qmixpump.VolumeUnit.litres)
|
|
||||||
|
|
||||||
self.pump.set_flow_unit(qmixpump.UnitPrefix.milli, qmixpump.VolumeUnit.litres, qmixpump.TimeUnit.per_minute)
|
|
||||||
|
|
||||||
self.max_flow_rate = self.pump.get_flow_rate_max()
|
|
||||||
self.max_volume = self.pump.get_volume_max()
|
|
||||||
self.no_of_valve_pos = self.valve.number_of_valve_positions()
|
|
||||||
self.valve_pos = self.valve.actual_valve_position()
|
|
||||||
|
|
||||||
self.target_flow_rate = self.max_flow_rate * 0.5
|
|
||||||
self.target = self.pump.get_fill_level()
|
|
||||||
|
|
||||||
def read_value(self):
|
|
||||||
return self.pump.get_fill_level()
|
|
||||||
|
|
||||||
def write_target(self, target):
|
|
||||||
self.pump.set_fill_level(target, self.target_flow_rate)
|
|
||||||
self.status = BUSY, 'Target changed'
|
|
||||||
return target
|
|
||||||
|
|
||||||
def write_target_flow_rate(self, rate):
|
|
||||||
self.pump.target_flow_rate = rate
|
|
||||||
return rate
|
|
||||||
|
|
||||||
def read_real_flow_rate(self):
|
|
||||||
return self.pump.get_flow_is()
|
|
||||||
|
|
||||||
def read_valve_pos(self):
|
|
||||||
return self.valve.actual_valve_position()
|
|
||||||
|
|
||||||
def write_valve_pos(self, target_pos):
|
|
||||||
self.valve.switch_valve_to_position(target_pos)
|
|
||||||
return target_pos
|
|
||||||
|
|
||||||
def read_status(self):
|
|
||||||
fault_state = self.pump.is_in_fault_state()
|
|
||||||
pumping = self.pump.is_pumping()
|
|
||||||
if fault_state == True:
|
|
||||||
return ERROR, 'Pump in fault state'
|
|
||||||
elif pumping == True:
|
|
||||||
return BUSY, 'Pumping'
|
|
||||||
else:
|
|
||||||
return IDLE, ''
|
|
||||||
|
|
||||||
@Command
|
|
||||||
def stop(self):
|
|
||||||
self.pump.stop_pumping()
|
|
||||||
self.target = self.pump.get_fill_level()
|
|
@ -1,4 +1,3 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
@ -19,78 +18,79 @@
|
|||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""vector field"""
|
"""vector field"""
|
||||||
|
|
||||||
import math
|
from frappy.core import Drivable, Done, BUSY, IDLE, WARN, ERROR
|
||||||
from frappy.core import Drivable, Done, BUSY, IDLE, ERROR, Parameter, TupleOf, ArrayOf, FloatRange
|
from frappy.errors import BadValueError
|
||||||
from frappy.errors import RangeError
|
|
||||||
from frappy_psi.vector import Vector
|
from frappy_psi.vector import Vector
|
||||||
from frappy.states import HasStates, Retry, status_code
|
|
||||||
|
|
||||||
|
|
||||||
DECREASE = 1
|
DECREASE = 1
|
||||||
INCREASE = 2
|
INCREASE = 2
|
||||||
|
|
||||||
|
|
||||||
class VectorField(HasStates, Vector, Drivable):
|
class VectorField(Vector, Drivable):
|
||||||
sphere_radius = Parameter('max. sphere', datatype=FloatRange(0, 0.7, unit='T'), readonly=True, default=0.6)
|
_state = None
|
||||||
cylinders = Parameter('allowed cylinders (list of radius and height)',
|
|
||||||
datatype=ArrayOf(TupleOf(FloatRange(0, 0.6, unit='T'), FloatRange(0, 5.2, unit='T')), 1, 9),
|
|
||||||
readonly=True, default=((0.23, 5.2), (0.45, 0.8)))
|
|
||||||
|
|
||||||
def initModule(self):
|
def doPoll(self):
|
||||||
super().initModule()
|
"""periodically called method"""
|
||||||
# override check_limits of the components with a check for restrictions on the vector
|
try:
|
||||||
for idx, component in enumerate(self.components):
|
if self._starting:
|
||||||
|
# first decrease components
|
||||||
def outer_check(target, vector=self, i=idx, inner_check=component.check_target):
|
driving = False
|
||||||
inner_check(target)
|
for target, component in zip(self.target, self.components):
|
||||||
value = [c.value - math.copysign(c.tolerance, c.value)
|
if target * component.value < 0:
|
||||||
for c in vector.components]
|
|
||||||
value[i] = target
|
|
||||||
vector._check_limits(value)
|
|
||||||
|
|
||||||
component.check_target = outer_check
|
|
||||||
|
|
||||||
def merge_status(self):
|
|
||||||
return self.status
|
|
||||||
|
|
||||||
def _check_limits(self, value):
|
|
||||||
"""check if value is within one of the safe shapes"""
|
|
||||||
if sum((v ** 2 for v in value)) <= self.sphere_radius ** 2:
|
|
||||||
return
|
|
||||||
for r, h in self.cylinders:
|
|
||||||
if sum(v ** 2 for v in value[0:2]) <= r ** 2 and abs(value[2]) <= h:
|
|
||||||
return
|
|
||||||
raise RangeError('vector %s does not fit in any limiting shape' % repr(value))
|
|
||||||
|
|
||||||
def write_target(self, target):
|
|
||||||
"""initiate target change"""
|
|
||||||
# check limits first
|
|
||||||
for component_target, component in zip(target, self.components):
|
|
||||||
# check against limits of individual components
|
|
||||||
component.check_target(component_target, vector=None) # no outer check here!
|
|
||||||
self._check_limits(target)
|
|
||||||
for component_target, component in zip(target, self.components):
|
|
||||||
if component_target * component.value < 0:
|
|
||||||
# change sign: drive to zero first
|
# change sign: drive to zero first
|
||||||
component_target = 0
|
target = 0
|
||||||
if abs(component_target) > abs(component.value):
|
if abs(target) < abs(component.target):
|
||||||
continue # do not drive yet
|
if target != component.target:
|
||||||
component.write_target(component_target)
|
|
||||||
self.start_machine(self.ramp_down, target=target)
|
|
||||||
return target
|
|
||||||
|
|
||||||
@status_code(BUSY)
|
|
||||||
def ramp_down(self, state):
|
|
||||||
for target, component in zip(state.target, self.components):
|
|
||||||
if component.isDriving():
|
|
||||||
return Retry()
|
|
||||||
for target, component in zip(state.target, self.components):
|
|
||||||
component.write_target(target)
|
component.write_target(target)
|
||||||
return self.final_ramp
|
if component.isDriving():
|
||||||
|
driving = True
|
||||||
@status_code(BUSY)
|
if driving:
|
||||||
def final_ramp(self, state):
|
return
|
||||||
|
# now we can go to the final targets
|
||||||
|
for target, component in zip(self.target, self.components):
|
||||||
|
component.write_target(target)
|
||||||
|
self._starting = False
|
||||||
|
else:
|
||||||
for component in self.components:
|
for component in self.components:
|
||||||
if component.isDriving():
|
if component.isDriving():
|
||||||
return Retry()
|
return
|
||||||
return self.final_status()
|
self.setFastPoll(False)
|
||||||
|
finally:
|
||||||
|
super().doPoll()
|
||||||
|
|
||||||
|
def merge_status(self):
|
||||||
|
names = [c.name for c in self.components if c.status[0] >= ERROR]
|
||||||
|
if names:
|
||||||
|
return ERROR, 'error in %s' % ', '.join(names)
|
||||||
|
names = [c.name for c in self.components if c.isDriving()]
|
||||||
|
if self._state:
|
||||||
|
# self.log.info('merge %r', [c.status for c in self.components])
|
||||||
|
if names:
|
||||||
|
direction = 'down ' if self._state == DECREASE else ''
|
||||||
|
return BUSY, 'ramping %s%s' % (direction, ', '.join(names))
|
||||||
|
if self.status[0] == BUSY:
|
||||||
|
return self.status
|
||||||
|
return BUSY, 'driving'
|
||||||
|
if names:
|
||||||
|
return WARN, 'moving %s directly' % ', '.join(names)
|
||||||
|
names = [c.name for c in self.components if c.status[0] >= WARN]
|
||||||
|
if names:
|
||||||
|
return WARN, 'warnings in %s' % ', '.join(names)
|
||||||
|
return IDLE, ''
|
||||||
|
|
||||||
|
def write_target(self, value):
|
||||||
|
"""initiate target change"""
|
||||||
|
# first make sure target is valid
|
||||||
|
for target, component in zip(self.target, self.components):
|
||||||
|
# check against limits if individual components
|
||||||
|
component.check_limits(target)
|
||||||
|
if sum(v * v for v in value) > 1:
|
||||||
|
raise BadValueError('norm of vector too high')
|
||||||
|
self.log.info('decrease')
|
||||||
|
self.setFastPoll(True)
|
||||||
|
self.target = value
|
||||||
|
self._state = DECREASE
|
||||||
|
self.doPoll()
|
||||||
|
self.log.info('done write_target %r', value)
|
||||||
|
return Done
|
||||||
|
@ -1,123 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
|
||||||
# *****************************************************************************
|
|
||||||
|
|
||||||
import time
|
|
||||||
import math
|
|
||||||
from frappy.core import Parameter, FloatRange, IntRange, Property
|
|
||||||
from frappy.errors import ProgrammingError
|
|
||||||
|
|
||||||
|
|
||||||
class FrozenParam(Parameter):
|
|
||||||
"""workaround for lazy hardware
|
|
||||||
|
|
||||||
Some hardware does not react nicely: when a parameter is changed,
|
|
||||||
and read back immediately, still the old value is returned.
|
|
||||||
This special parameter helps fixing this problem.
|
|
||||||
|
|
||||||
Mechanism:
|
|
||||||
|
|
||||||
- after a call to write_<param> for a short time (<n_polls> * <interval>)
|
|
||||||
the hardware is polled until the readback changes before the 'changed'
|
|
||||||
message is replied to the client
|
|
||||||
- if there is no change yet within short time, the 'changed' message is
|
|
||||||
set with the given value and further calls to read_<param> return also
|
|
||||||
this given value until the readback value has changed or until
|
|
||||||
<timeout> sec have passed.
|
|
||||||
|
|
||||||
For float parameters, the behaviour for small changes is improved
|
|
||||||
when the write_<param> method tries to return the (may be rounded) value,
|
|
||||||
as if it would be returned by the hardware. If this behaviour is not
|
|
||||||
known, or the programmer is too lazy to implement it, write_<param>
|
|
||||||
should return None or the given value.
|
|
||||||
Also it will help to adjust the datatype properties
|
|
||||||
'absolute_resolution' and 'relative_resolution' to reasonable values.
|
|
||||||
"""
|
|
||||||
timeout = Property('timeout for freezing readback value',
|
|
||||||
FloatRange(0, unit='s'), default=30)
|
|
||||||
n_polls = Property("""number polls within write method""",
|
|
||||||
IntRange(0), default=1)
|
|
||||||
interval = Property("""interval for polls within write method
|
|
||||||
|
|
||||||
the product n_polls * interval should not be more than a fraction of a second
|
|
||||||
in order not to block the connection for too long
|
|
||||||
""",
|
|
||||||
FloatRange(0, unit='s'), default=0.05)
|
|
||||||
new_value = None
|
|
||||||
previous_value = None
|
|
||||||
expire = 0
|
|
||||||
is_float = True # assume float. will be fixed later
|
|
||||||
|
|
||||||
def isclose(self, v1, v2):
|
|
||||||
if v1 == v2:
|
|
||||||
return True
|
|
||||||
if self.is_float:
|
|
||||||
dt = self.datatype
|
|
||||||
try:
|
|
||||||
return math.isclose(v1, v2, abs_tol=dt.absolute_tolerance,
|
|
||||||
rel_tol=dt.relative_tolerance)
|
|
||||||
except AttributeError:
|
|
||||||
# fix once for ever when datatype is not a float
|
|
||||||
self.is_float = False
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __set_name__(self, owner, name):
|
|
||||||
try:
|
|
||||||
rfunc = getattr(owner, f'read_{name}')
|
|
||||||
wfunc = getattr(owner, f'write_{name}')
|
|
||||||
except AttributeError:
|
|
||||||
raise ProgrammingError(f'FrozenParam: methods read_{name} and write_{name} must exist') from None
|
|
||||||
|
|
||||||
super().__set_name__(owner, name)
|
|
||||||
|
|
||||||
def read_wrapper(self, pname=name, rfunc=rfunc):
|
|
||||||
pobj = self.parameters[pname]
|
|
||||||
value = rfunc(self)
|
|
||||||
if pobj.new_value is None:
|
|
||||||
return value
|
|
||||||
if not pobj.isclose(value, pobj.new_value):
|
|
||||||
if value == pobj.previous_value:
|
|
||||||
if time.time() < pobj.expire:
|
|
||||||
return pobj.new_value
|
|
||||||
self.log.warning('%s readback did not change within %g sec',
|
|
||||||
pname, pobj.timeout)
|
|
||||||
else:
|
|
||||||
# value has changed, but is not matching new value
|
|
||||||
self.log.warning('%s readback changed from %r to %r but %r was given',
|
|
||||||
pname, pobj.previous_value, value, pobj.new_value)
|
|
||||||
# readback value has changed or returned value is roughly equal to the new value
|
|
||||||
pobj.new_value = None
|
|
||||||
return value
|
|
||||||
|
|
||||||
def write_wrapper(self, value, wfunc=wfunc, rfunc=rfunc, read_wrapper=read_wrapper, pname=name):
|
|
||||||
pobj = self.parameters[pname]
|
|
||||||
pobj.previous_value = rfunc(self)
|
|
||||||
pobj.new_value = wfunc(self, value)
|
|
||||||
if pobj.new_value is None: # as wfunc is the unwrapped write_* method, the return value may be None
|
|
||||||
pobj.new_value = value
|
|
||||||
pobj.expire = time.time() + pobj.timeout
|
|
||||||
for cnt in range(pobj.n_polls):
|
|
||||||
if cnt: # we may be lucky, and the readback value has already changed
|
|
||||||
time.sleep(pobj.interval)
|
|
||||||
value = read_wrapper(self)
|
|
||||||
if pobj.new_value is None:
|
|
||||||
return value
|
|
||||||
return pobj.new_value
|
|
||||||
|
|
||||||
setattr(owner, f'read_{name}', read_wrapper)
|
|
||||||
setattr(owner, f'write_{name}', write_wrapper)
|
|
@ -15,6 +15,8 @@
|
|||||||
#
|
#
|
||||||
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
|
"""Thermo Haake Phoenix P1 Bath Circulator"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
from frappy.core import StringIO, HasIO, Parameter, FloatRange, BoolType, \
|
from frappy.core import StringIO, HasIO, Parameter, FloatRange, BoolType, \
|
||||||
@ -22,7 +24,13 @@ from frappy.core import StringIO, HasIO, Parameter, FloatRange, BoolType, \
|
|||||||
from frappy_psi.convergence import HasConvergence
|
from frappy_psi.convergence import HasConvergence
|
||||||
from frappy.errors import CommunicationFailedError
|
from frappy.errors import CommunicationFailedError
|
||||||
|
|
||||||
|
|
||||||
def convert(string):
|
def convert(string):
|
||||||
|
"""
|
||||||
|
Converts reply to a number
|
||||||
|
:param string: reply from the command
|
||||||
|
:return: number
|
||||||
|
"""
|
||||||
number = re.sub(r'[^0-9.-]', '', string)
|
number = re.sub(r'[^0-9.-]', '', string)
|
||||||
return float(number)
|
return float(number)
|
||||||
|
|
||||||
@ -55,11 +63,21 @@ class TemperatureLoop(HasIO, HasConvergence, Drivable):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def get_values_status(self):
|
def get_values_status(self):
|
||||||
|
"""
|
||||||
|
Supplementary command for the operating status method.
|
||||||
|
Removes the extra symbol and converts each status value into integer.
|
||||||
|
|
||||||
|
:return: array of integers
|
||||||
|
"""
|
||||||
reply = self.communicate('B')
|
reply = self.communicate('B')
|
||||||
string = reply.rstrip('$')
|
string = reply.rstrip('$')
|
||||||
return [int(val) for val in string]
|
return [int(val) for val in string]
|
||||||
|
|
||||||
def read_status(self): # control_active update
|
def read_status(self): # control_active update
|
||||||
|
"""
|
||||||
|
Operating status.
|
||||||
|
:return: statu type and message
|
||||||
|
"""
|
||||||
values_str = self.get_values_status()
|
values_str = self.get_values_status()
|
||||||
self.read_control_active()
|
self.read_control_active()
|
||||||
|
|
||||||
@ -72,6 +90,10 @@ class TemperatureLoop(HasIO, HasConvergence, Drivable):
|
|||||||
return IDLE, ''
|
return IDLE, ''
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
|
"""
|
||||||
|
F1 - internal temperature, F2 - external temperature
|
||||||
|
:return: float temperature value
|
||||||
|
"""
|
||||||
if self.mode == 1:
|
if self.mode == 1:
|
||||||
value = self.communicate('F1')
|
value = self.communicate('F1')
|
||||||
else:
|
else:
|
||||||
@ -79,6 +101,11 @@ class TemperatureLoop(HasIO, HasConvergence, Drivable):
|
|||||||
return convert(value)
|
return convert(value)
|
||||||
|
|
||||||
def write_control_active(self, value):
|
def write_control_active(self, value):
|
||||||
|
"""
|
||||||
|
Turning on/off the heating, pump and regulation
|
||||||
|
:param value: 0 is OFF, 1 is ON
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if value is True:
|
if value is True:
|
||||||
self.communicate('GO') # heating and pump run
|
self.communicate('GO') # heating and pump run
|
||||||
self.communicate('W SR') # regulation
|
self.communicate('W SR') # regulation
|
||||||
@ -99,6 +126,11 @@ class TemperatureLoop(HasIO, HasConvergence, Drivable):
|
|||||||
return convert(string)
|
return convert(string)
|
||||||
|
|
||||||
def write_target(self, target):
|
def write_target(self, target):
|
||||||
|
"""
|
||||||
|
Selecting Celsius, setting the target
|
||||||
|
:param target: target
|
||||||
|
:return: target
|
||||||
|
"""
|
||||||
self.write_control_active(True)
|
self.write_control_active(True)
|
||||||
self.read_status()
|
self.read_status()
|
||||||
self.communicate('W TE C')
|
self.communicate('W TE C')
|
||||||
@ -106,6 +138,11 @@ class TemperatureLoop(HasIO, HasConvergence, Drivable):
|
|||||||
return target
|
return target
|
||||||
|
|
||||||
def write_mode(self, mode):
|
def write_mode(self, mode):
|
||||||
|
"""
|
||||||
|
Switching to internal or external control
|
||||||
|
:param mode: internal/external
|
||||||
|
:return: selected mode
|
||||||
|
"""
|
||||||
if mode == 1:
|
if mode == 1:
|
||||||
self.communicate('W IN')
|
self.communicate('W IN')
|
||||||
self.communicate('W EX')
|
self.communicate('W EX')
|
||||||
@ -113,7 +150,7 @@ class TemperatureLoop(HasIO, HasConvergence, Drivable):
|
|||||||
|
|
||||||
@Command
|
@Command
|
||||||
def clear_errors(self):
|
def clear_errors(self):
|
||||||
""" Reset after error"""
|
""" Reset after error. Otherwise the status will not be updated"""
|
||||||
if self.read_status()[0] == ERROR:
|
if self.read_status()[0] == ERROR:
|
||||||
try:
|
try:
|
||||||
self.communicate('ER')
|
self.communicate('ER')
|
||||||
|
@ -231,17 +231,14 @@ class ResChannel(Channel):
|
|||||||
def _read_value(self):
|
def _read_value(self):
|
||||||
"""read value, without update"""
|
"""read value, without update"""
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
if now - 0.5 < max(self._last_range_change, self.switcher._start_switch) + self.pause:
|
if now + 0.5 < max(self._last_range_change, self.switcher._start_switch) + self.pause:
|
||||||
return None
|
return None
|
||||||
result = float(self.communicate('RDGR?%d' % self.channel))
|
result = float(self.communicate('RDGR?%d' % self.channel))
|
||||||
if result == 0:
|
if result == 0:
|
||||||
if self.autorange:
|
|
||||||
rng = int(max(self.minrange, self.range)) # convert from enum to int
|
|
||||||
self.write_range(min(self.MAX_RNG, rng + 1))
|
|
||||||
return None
|
return None
|
||||||
if self.autorange:
|
if self.autorange:
|
||||||
self.fix_autorange()
|
self.fix_autorange()
|
||||||
if now - 0.5 > self._last_range_change + self.pause:
|
if now + 0.5 > self._last_range_change + self.pause:
|
||||||
rng = int(max(self.minrange, self.range)) # convert from enum to int
|
rng = int(max(self.minrange, self.range)) # convert from enum to int
|
||||||
if self.status[0] < self.Status.ERROR:
|
if self.status[0] < self.Status.ERROR:
|
||||||
if abs(result) > self.RES_SCALE[rng]:
|
if abs(result) > self.RES_SCALE[rng]:
|
||||||
@ -254,10 +251,8 @@ class ResChannel(Channel):
|
|||||||
lim -= 0.05 # not more than 4 steps at once
|
lim -= 0.05 # not more than 4 steps at once
|
||||||
# effectively: <0.16 %: 4 steps, <1%: 3 steps, <5%: 2 steps, <20%: 1 step
|
# effectively: <0.16 %: 4 steps, <1%: 3 steps, <5%: 2 steps, <20%: 1 step
|
||||||
elif rng < self.MAX_RNG:
|
elif rng < self.MAX_RNG:
|
||||||
self.log.debug('increase range due to error %d', rng)
|
|
||||||
rng = min(self.MAX_RNG, rng + 1)
|
rng = min(self.MAX_RNG, rng + 1)
|
||||||
if rng != self.range:
|
if rng != self.range:
|
||||||
self.log.debug('range change to %d', rng)
|
|
||||||
self.write_range(rng)
|
self.write_range(rng)
|
||||||
self._last_range_change = now
|
self._last_range_change = now
|
||||||
return result
|
return result
|
||||||
@ -386,10 +381,6 @@ class TemperatureLoop(HasConvergence, TemperatureChannel, Drivable):
|
|||||||
htrrng = Parameter('', EnumType(HTRRNG), readonly=False)
|
htrrng = Parameter('', EnumType(HTRRNG), readonly=False)
|
||||||
_control_active = False
|
_control_active = False
|
||||||
|
|
||||||
def doPoll(self):
|
|
||||||
super().doPoll()
|
|
||||||
self.set_htrrng()
|
|
||||||
|
|
||||||
@Command
|
@Command
|
||||||
def control_off(self):
|
def control_off(self):
|
||||||
"""switch control off"""
|
"""switch control off"""
|
||||||
|
@ -198,10 +198,6 @@ class MotorValve(PersistentMixin, Drivable):
|
|||||||
|
|
||||||
@Command
|
@Command
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop at current position
|
|
||||||
|
|
||||||
state will probably be undefined
|
|
||||||
"""
|
|
||||||
self._state.stop()
|
self._state.stop()
|
||||||
self.motor.stop()
|
self.motor.stop()
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
"""modules to access parameters"""
|
"""modules to access parameters"""
|
||||||
|
|
||||||
from frappy.core import Drivable, EnumType, IDLE, Attached, StringType, Property, \
|
from frappy.core import Drivable, EnumType, IDLE, Attached, StringType, Property, \
|
||||||
Parameter, BoolType, FloatRange, Readable, ERROR, nopoll
|
Parameter, FloatRange, Readable, ERROR
|
||||||
from frappy.errors import ConfigError
|
from frappy.errors import ConfigError
|
||||||
from frappy_psi.convergence import HasConvergence
|
from frappy_psi.convergence import HasConvergence
|
||||||
from frappy_psi.mixins import HasRamp
|
from frappy_psi.mixins import HasRamp
|
||||||
@ -72,21 +72,19 @@ class Driv(Drivable):
|
|||||||
raise ConfigError('illegal recursive read/write module')
|
raise ConfigError('illegal recursive read/write module')
|
||||||
super().checkProperties()
|
super().checkProperties()
|
||||||
|
|
||||||
def registerUpdates(self):
|
#def registerUpdates(self):
|
||||||
self.read.addCallback(self.read_param, self.announceUpdate, 'value')
|
# self.read.valueCallbacks[self.read_param].append(self.update_value)
|
||||||
self.write.addCallback(self.write_param, self.announceUpdate, 'target')
|
# self.write.valueCallbacks[self.write_param].append(self.update_target)
|
||||||
|
#
|
||||||
|
#def startModule(self, start_events):
|
||||||
|
# start_events.queue(self.registerUpdates)
|
||||||
|
# super().startModule(start_events)
|
||||||
|
|
||||||
def startModule(self, start_events):
|
|
||||||
start_events.queue(self.registerUpdates)
|
|
||||||
super().startModule(start_events)
|
|
||||||
|
|
||||||
@nopoll
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return getattr(self.read, f'read_{self.read_param}')()
|
return getattr(self.read, f'{self.read_param}')
|
||||||
|
|
||||||
@nopoll
|
|
||||||
def read_target(self):
|
def read_target(self):
|
||||||
return getattr(self.write, f'read_{self.write_param}')()
|
return getattr(self.write, f'{self.write_param}')
|
||||||
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
return IDLE, ''
|
return IDLE, ''
|
||||||
@ -132,7 +130,7 @@ def set_enabled(modobj, value):
|
|||||||
|
|
||||||
|
|
||||||
def get_value(obj, default):
|
def get_value(obj, default):
|
||||||
"""get the value of given module. if not valid, return the default"""
|
"""get the value of given module. if not valid, return the limit (min_high or max_low)"""
|
||||||
if not getattr(obj, 'enabled', True):
|
if not getattr(obj, 'enabled', True):
|
||||||
return default
|
return default
|
||||||
# consider also that a value 0 is invalid
|
# consider also that a value 0 is invalid
|
||||||
@ -150,14 +148,14 @@ class SwitchDriv(HasConvergence, Drivable):
|
|||||||
max_low = Parameter('maximum low target', FloatRange(unit='$'), readonly=False)
|
max_low = Parameter('maximum low target', FloatRange(unit='$'), readonly=False)
|
||||||
# disable_other = Parameter('whether to disable unused channel', BoolType(), readonly=False)
|
# disable_other = Parameter('whether to disable unused channel', BoolType(), readonly=False)
|
||||||
selected = Parameter('selected module', EnumType(low=LOW, high=HIGH), readonly=False, default=0)
|
selected = Parameter('selected module', EnumType(low=LOW, high=HIGH), readonly=False, default=0)
|
||||||
autoswitch = Parameter('switch sensor automatically', BoolType(), readonly=False, default=True)
|
_switch_target = None # if not None, switch to selection mhen mid range is reached
|
||||||
_switch_target = None # if not None, switch to selection when mid range is reached
|
|
||||||
|
|
||||||
# TODO: copy units from attached module
|
# TODO: copy units from attached module
|
||||||
# TODO: callbacks for updates
|
# TODO: callbacks for updates
|
||||||
|
|
||||||
def doPoll(self):
|
def doPoll(self):
|
||||||
super().doPoll()
|
super().doPoll()
|
||||||
|
if self.isBusy():
|
||||||
if self._switch_target is not None:
|
if self._switch_target is not None:
|
||||||
mid = (self.min_high + self.max_low) * 0.5
|
mid = (self.min_high + self.max_low) * 0.5
|
||||||
if self._switch_target == HIGH:
|
if self._switch_target == HIGH:
|
||||||
@ -168,43 +166,33 @@ class SwitchDriv(HasConvergence, Drivable):
|
|||||||
self.write_target(self.target)
|
self.write_target(self.target)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
high = get_value(self.high, mid) # return mid when high is invalid
|
high = get_value(self.high, mid) # return mid then high is invalid
|
||||||
if high < mid: # change to self.max_low
|
if high < mid:
|
||||||
self.value = self.high.value
|
self.value = self.high.value
|
||||||
self._switch_target = None
|
self._switch_target = None
|
||||||
self.write_target(self.target)
|
self.write_target(self.target)
|
||||||
return
|
return
|
||||||
if not self.isBusy() and self.autoswitch:
|
else:
|
||||||
low = get_value(self.low, self.max_low)
|
low = get_value(self.low, self.max_low)
|
||||||
high = get_value(self.high, self.min_high)
|
high = get_value(self.high, self.min_high)
|
||||||
low_valid = low < self.max_low
|
low_valid = low < self.max_low
|
||||||
high_valid = high > self.min_high
|
high_valid = high > self.min_high
|
||||||
if high_valid and high > self.max_low:
|
if high_valid and high > self.max_low:
|
||||||
if not low_valid and not self.low.control_active:
|
if not low_valid:
|
||||||
set_enabled(self.low, False)
|
set_enabled(self.low, False)
|
||||||
return
|
return
|
||||||
if low_valid and low < self.min_high:
|
if low_valid and low < self.min_high:
|
||||||
if not high_valid and not self.high.control_active:
|
if not high_valid:
|
||||||
set_enabled(self.high, False)
|
set_enabled(self.high, False)
|
||||||
return
|
return
|
||||||
# keep only one channel on
|
set_enabled(self.low, True)
|
||||||
#set_enabled(self.low, True)
|
set_enabled(self.high, True)
|
||||||
#set_enabled(self.high, True)
|
|
||||||
|
|
||||||
def get_selected(self):
|
|
||||||
low = get_value(self.low, self.max_low)
|
|
||||||
high = get_value(self.high, self.min_high)
|
|
||||||
if low < self.min_high:
|
|
||||||
return 0
|
|
||||||
if high > self.max_low:
|
|
||||||
return 1
|
|
||||||
return self.selected
|
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return self.low.value if self.get_selected() == LOW else self.high.value
|
return self.low.value if self.selected == LOW else self.high.value
|
||||||
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
status = self.low.status if self.get_selected() == LOW else self.high.status
|
status = self.low.status if self.selected == LOW else self.high.status
|
||||||
if status[0] >= ERROR:
|
if status[0] >= ERROR:
|
||||||
return status
|
return status
|
||||||
return super().read_status() # convergence status
|
return super().read_status() # convergence status
|
||||||
@ -214,22 +202,21 @@ class SwitchDriv(HasConvergence, Drivable):
|
|||||||
selected = self.selected
|
selected = self.selected
|
||||||
target1 = target
|
target1 = target
|
||||||
self._switch_target = None
|
self._switch_target = None
|
||||||
if target > self.max_low * 0.75 + self.min_high * 0.25:
|
if target > self.max_low:
|
||||||
if self.value < self.min_high:
|
if self.value < self.min_high:
|
||||||
target1 = min(target, self.max_low)
|
target1 = self.max_low
|
||||||
self._switch_target = HIGH
|
self._switch_target = HIGH
|
||||||
selected = LOW
|
selected = LOW
|
||||||
else:
|
else:
|
||||||
this, other = other, this
|
this, other = other, this
|
||||||
selected = HIGH
|
selected = HIGH
|
||||||
elif target < self.min_high * 0.75 + self.max_low * 0.25:
|
elif target < self.min_high:
|
||||||
# reinstall this with higher threshold (e.g. 4 K)?
|
if self.value > self.max_low:
|
||||||
#if self.value > self.max_low:
|
target1 = self.min_high
|
||||||
# target1 = max(self.min_high, target)
|
self._switch_target = LOW
|
||||||
# self._switch_target = LOW
|
this, other = other, this
|
||||||
# this, other = other, this
|
selected = HIGH
|
||||||
# selected = HIGH
|
else:
|
||||||
#else:
|
|
||||||
selected = LOW
|
selected = LOW
|
||||||
elif self.selected == HIGH:
|
elif self.selected == HIGH:
|
||||||
this, other = other, this
|
this, other = other, this
|
||||||
|
@ -483,10 +483,6 @@ class Temp(PpmsDrivable):
|
|||||||
self._expected_target_time = time.time() + abs(target - self.value) * 60.0 / max(0.1, ramp)
|
self._expected_target_time = time.time() + abs(target - self.value) * 60.0 / max(0.1, ramp)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""set setpoint to current value
|
|
||||||
|
|
||||||
but restrict to values between last target and current target
|
|
||||||
"""
|
|
||||||
if not self.isDriving():
|
if not self.isDriving():
|
||||||
return
|
return
|
||||||
if self.status[0] != StatusType.STABILIZING:
|
if self.status[0] != StatusType.STABILIZING:
|
||||||
@ -616,7 +612,6 @@ class Field(PpmsDrivable):
|
|||||||
# do not execute FIELD command, as this would trigger a ramp up of leads current
|
# do not execute FIELD command, as this would trigger a ramp up of leads current
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop at current driven Field"""
|
|
||||||
if not self.isDriving():
|
if not self.isDriving():
|
||||||
return
|
return
|
||||||
newtarget = clamp(self._last_target, self.value, self.target)
|
newtarget = clamp(self._last_target, self.value, self.target)
|
||||||
@ -719,7 +714,6 @@ class Position(PpmsDrivable):
|
|||||||
return value # do not execute MOVE command, as this would trigger an unnecessary move
|
return value # do not execute MOVE command, as this would trigger an unnecessary move
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop motor"""
|
|
||||||
if not self.isDriving():
|
if not self.isDriving():
|
||||||
return
|
return
|
||||||
newtarget = clamp(self._last_target, self.value, self.target)
|
newtarget = clamp(self._last_target, self.value, self.target)
|
||||||
|
74
frappy_psi/pulse.py
Normal file
74
frappy_psi/pulse.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
# *****************************************************************************
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
# Foundation; either version 2 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
#
|
||||||
|
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
|
# *****************************************************************************
|
||||||
|
"""Keithley Instruments 2601B-PULSE System (not finished)"""
|
||||||
|
|
||||||
|
from frappy.core import StringIO, HasIO, Readable, Writable, \
|
||||||
|
Parameter, FloatRange, EnumType
|
||||||
|
|
||||||
|
|
||||||
|
class PulseIO(StringIO):
|
||||||
|
end_of_line = '\n'
|
||||||
|
identification = [('*IDN?', 'Keithley Instruments, Model 2601B-PULSE,.*')]
|
||||||
|
|
||||||
|
|
||||||
|
class Base(HasIO):
|
||||||
|
|
||||||
|
def set_source(self):
|
||||||
|
"""
|
||||||
|
Set the source -always current
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return self.communicate(f'smua.source.func = smua.OUTPUT_DCAMPS')
|
||||||
|
|
||||||
|
def get_par(self, cmd):
|
||||||
|
return self.communicate(f'reading = smua.measure.{cmd}()')
|
||||||
|
|
||||||
|
def auto_onof(self, val):
|
||||||
|
if val == 1:
|
||||||
|
return f'smua.AUTORANGE_ON'
|
||||||
|
if val == 0:
|
||||||
|
return f'smua.AUTORANGE_OFF'
|
||||||
|
|
||||||
|
def set_measure(self, cmd, val):
|
||||||
|
return self.communicate(f'smua.measure.{cmd} = {val}')
|
||||||
|
|
||||||
|
|
||||||
|
class Create_Pulse(Base, Readable, Writable):
|
||||||
|
target = Parameter('source target', FloatRange, unit='A', readonly=False)
|
||||||
|
width = Parameter('pulse width', FloatRange, unit="s", readonly=False)
|
||||||
|
resistance = Parameter('resistance', FloatRange)
|
||||||
|
|
||||||
|
SOURCE_RANGE = ['100nA', '1uA', '10uA', '100uA', '1mA', '10mA', '100mA', '1A', '3A']
|
||||||
|
range = Parameter('source range', EnumType('source range',
|
||||||
|
{name: idx for idx, name in enumerate(SOURCE_RANGE)}), readonly=False)
|
||||||
|
|
||||||
|
def read_range(self):
|
||||||
|
return self.range
|
||||||
|
|
||||||
|
def write_range(self, range):
|
||||||
|
self.communicate(f'smua.source.rangei = {range}')
|
||||||
|
return self.range
|
||||||
|
|
||||||
|
def write_target(self, target):
|
||||||
|
return self.communicate(f'smua.source.leveli = {target}')
|
||||||
|
|
||||||
|
def read_resistance(self):
|
||||||
|
return self.communicate('reading = smua.measure.r()')
|
||||||
|
|
||||||
|
|
||||||
|
class Script(Create_Pulse):
|
@ -16,7 +16,7 @@
|
|||||||
# Module authors:
|
# Module authors:
|
||||||
# Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
# Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
|
"""Temperature Controller TC1 Quantum NorthWest"""
|
||||||
|
|
||||||
from frappy.core import Readable, Parameter, FloatRange, IDLE, ERROR, BoolType,\
|
from frappy.core import Readable, Parameter, FloatRange, IDLE, ERROR, BoolType,\
|
||||||
StringIO, HasIO, Property, WARN, Drivable, BUSY, StringType, Done
|
StringIO, HasIO, Property, WARN, Drivable, BUSY, StringType, Done
|
||||||
@ -31,10 +31,17 @@ class QnwIO(StringIO):
|
|||||||
|
|
||||||
class SensorTC1(HasIO, Readable):
|
class SensorTC1(HasIO, Readable):
|
||||||
ioClass = QnwIO
|
ioClass = QnwIO
|
||||||
value = Parameter(unit='degC', min=-15, max=120)
|
value = Parameter(unit='degC', min=-55, max=150)
|
||||||
channel = Property('channel name', StringType())
|
channel = Property('channel name', StringType())
|
||||||
|
|
||||||
def set_param(self, adr, value=None):
|
def set_param(self, adr, value=None):
|
||||||
|
"""
|
||||||
|
Set parameter.
|
||||||
|
Every command starts with "[F1", and the end of line is "]".
|
||||||
|
:param adr: second part of the command
|
||||||
|
:param value: value to set
|
||||||
|
:return: value converted to float
|
||||||
|
"""
|
||||||
short = adr.split()[0]
|
short = adr.split()[0]
|
||||||
# try 3 times in case we got an asynchronous message
|
# try 3 times in case we got an asynchronous message
|
||||||
for _ in range(3):
|
for _ in range(3):
|
||||||
@ -66,7 +73,7 @@ class SensorTC1(HasIO, Readable):
|
|||||||
|
|
||||||
class TemperatureLoopTC1(SensorTC1, Drivable):
|
class TemperatureLoopTC1(SensorTC1, Drivable):
|
||||||
value = Parameter('temperature', unit='degC')
|
value = Parameter('temperature', unit='degC')
|
||||||
target = Parameter('setpoint', unit='degC', min=-5, max=110)
|
target = Parameter('setpoint', unit='degC', min=-55, max=150)
|
||||||
control = Parameter('temperature control flag', BoolType(), readonly=False)
|
control = Parameter('temperature control flag', BoolType(), readonly=False)
|
||||||
ramp = Parameter('ramping value', FloatRange, unit='degC/min', readonly=False)
|
ramp = Parameter('ramping value', FloatRange, unit='degC/min', readonly=False)
|
||||||
ramp_used = Parameter('ramping status', BoolType(), default=False, readonly=False)
|
ramp_used = Parameter('ramping status', BoolType(), default=False, readonly=False)
|
||||||
@ -80,6 +87,16 @@ class TemperatureLoopTC1(SensorTC1, Drivable):
|
|||||||
return self.get_param('MT')
|
return self.get_param('MT')
|
||||||
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
|
"""
|
||||||
|
the device returns 4 symbols according to the current status. These symbols are:
|
||||||
|
”0” or “1” - number of unreported errors
|
||||||
|
”+” or “-” - stirrer is on/off
|
||||||
|
”+” or ”-” - temperature control is on/off
|
||||||
|
”S” or “C” - current sample holder tempeerature is stable/changing
|
||||||
|
There could be the fifth status symbol:
|
||||||
|
”+” or “-” or “W” - rampping is on/off/waiting
|
||||||
|
:return: status messages
|
||||||
|
"""
|
||||||
status = super().read_status()
|
status = super().read_status()
|
||||||
if status[0] == ERROR:
|
if status[0] == ERROR:
|
||||||
return status
|
return status
|
||||||
@ -141,6 +158,5 @@ class TemperatureLoopTC1(SensorTC1, Drivable):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop at current value (does nothing if ramp is not used)"""
|
|
||||||
if self.control and self.ramp_used:
|
if self.control and self.ramp_used:
|
||||||
self.write_target(self.value)
|
self.write_target(self.value)
|
||||||
|
@ -39,13 +39,12 @@ from os.path import expanduser, join, exists
|
|||||||
from frappy.client import ProxyClient
|
from frappy.client import ProxyClient
|
||||||
from frappy.datatypes import ArrayOf, BoolType, \
|
from frappy.datatypes import ArrayOf, BoolType, \
|
||||||
EnumType, FloatRange, IntRange, StringType
|
EnumType, FloatRange, IntRange, StringType
|
||||||
from frappy.core import IDLE, BUSY, ERROR
|
from frappy.errors import ConfigError, HardwareError, secop_error, CommunicationFailedError
|
||||||
from frappy.errors import ConfigError, HardwareError, CommunicationFailedError
|
|
||||||
from frappy.lib import generalConfig, mkthread
|
from frappy.lib import generalConfig, mkthread
|
||||||
from frappy.lib.asynconn import AsynConn, ConnectionClosed
|
from frappy.lib.asynconn import AsynConn, ConnectionClosed
|
||||||
from frappy.modulebase import Done
|
from frappy.modules import Attached, Command, Done, Drivable, \
|
||||||
from frappy.modules import Attached, Command, Drivable, \
|
|
||||||
Module, Parameter, Property, Readable, Writable
|
Module, Parameter, Property, Readable, Writable
|
||||||
|
from frappy.protocol.dispatcher import make_update
|
||||||
|
|
||||||
|
|
||||||
CFG_HEADER = """Node('%(config)s.sea.psi.ch',
|
CFG_HEADER = """Node('%(config)s.sea.psi.ch',
|
||||||
@ -108,6 +107,7 @@ class SeaClient(ProxyClient, Module):
|
|||||||
service = Property("main/stick/addons", StringType(), default='')
|
service = Property("main/stick/addons", StringType(), default='')
|
||||||
visibility = 'expert'
|
visibility = 'expert'
|
||||||
default_json_file = {}
|
default_json_file = {}
|
||||||
|
_connect_thread = None
|
||||||
_instance = None
|
_instance = None
|
||||||
_last_connect = 0
|
_last_connect = 0
|
||||||
|
|
||||||
@ -124,8 +124,6 @@ class SeaClient(ProxyClient, Module):
|
|||||||
self.shutdown = False
|
self.shutdown = False
|
||||||
self.path2param = {}
|
self.path2param = {}
|
||||||
self._write_lock = threading.Lock()
|
self._write_lock = threading.Lock()
|
||||||
self._connect_thread = None
|
|
||||||
self._connected = False
|
|
||||||
config = opts.get('config')
|
config = opts.get('config')
|
||||||
if isinstance(config, dict):
|
if isinstance(config, dict):
|
||||||
config = config['value']
|
config = config['value']
|
||||||
@ -137,11 +135,14 @@ class SeaClient(ProxyClient, Module):
|
|||||||
Module.__init__(self, name, log, opts, srv)
|
Module.__init__(self, name, log, opts, srv)
|
||||||
|
|
||||||
def doPoll(self):
|
def doPoll(self):
|
||||||
if not self._connected and time.time() > self._last_connect + 10:
|
if not self.asynio and time.time() > self._last_connect + 10:
|
||||||
|
with self._write_lock:
|
||||||
|
# make sure no more connect thread is running
|
||||||
|
if self._connect_thread and self._connect_thread.isAlive():
|
||||||
|
return
|
||||||
if not self._last_connect:
|
if not self._last_connect:
|
||||||
self.log.info('reconnect to SEA %s', self.service)
|
self.log.info('reconnect to SEA %s', self.service)
|
||||||
if self._connect_thread is None:
|
self._connect_thread = mkthread(self._connect, None)
|
||||||
self._connect_thread = mkthread(self._connect)
|
|
||||||
|
|
||||||
def register_obj(self, module, obj):
|
def register_obj(self, module, obj):
|
||||||
self.objects.add(obj)
|
self.objects.add(obj)
|
||||||
@ -149,13 +150,15 @@ class SeaClient(ProxyClient, Module):
|
|||||||
self.path2param.setdefault(k, []).extend(v)
|
self.path2param.setdefault(k, []).extend(v)
|
||||||
self.register_callback(module.name, module.updateEvent)
|
self.register_callback(module.name, module.updateEvent)
|
||||||
|
|
||||||
def _connect(self):
|
def _connect(self, started_callback):
|
||||||
try:
|
self.asynio = None
|
||||||
if self.syncio:
|
if self.syncio:
|
||||||
|
# trigger syncio reconnect in self.request()
|
||||||
try:
|
try:
|
||||||
self.syncio.disconnect()
|
self.syncio.disconnect()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
self.syncio = None
|
||||||
self._last_connect = time.time()
|
self._last_connect = time.time()
|
||||||
if self._instance:
|
if self._instance:
|
||||||
try:
|
try:
|
||||||
@ -176,34 +179,32 @@ class SeaClient(ProxyClient, Module):
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise CommunicationFailedError('reply %r should be "Login OK"' % reply)
|
raise CommunicationFailedError('reply %r should be "Login OK"' % reply)
|
||||||
self.syncio = AsynConn(self.uri)
|
result = self.request('frappy_config %s %s' % (self.service, self.config))
|
||||||
assert self.syncio.readline() == b'OK'
|
|
||||||
self.syncio.writeline(b'seauser seaser')
|
|
||||||
assert self.syncio.readline() == b'Login OK'
|
|
||||||
self.log.info('connected to %s', self.uri)
|
|
||||||
|
|
||||||
result = self.raw_request('frappy_config %s %s' % (self.service, self.config))
|
|
||||||
if result.startswith('ERROR:'):
|
if result.startswith('ERROR:'):
|
||||||
raise CommunicationFailedError(f'reply from frappy_config: {result}')
|
raise CommunicationFailedError(f'reply from frappy_config: {result}')
|
||||||
# frappy_async_client switches to the json protocol (better for updates)
|
# frappy_async_client switches to the json protocol (better for updates)
|
||||||
self.asynio.writeline(b'frappy_async_client')
|
self.asynio.writeline(b'frappy_async_client')
|
||||||
self.asynio.writeline(('get_all_param ' + ' '.join(self.objects)).encode())
|
self.asynio.writeline(('get_all_param ' + ' '.join(self.objects)).encode())
|
||||||
self._connected = True
|
|
||||||
mkthread(self._rxthread)
|
|
||||||
finally:
|
|
||||||
self._connect_thread = None
|
self._connect_thread = None
|
||||||
|
mkthread(self._rxthread, started_callback)
|
||||||
|
|
||||||
def request(self, command, quiet=False):
|
def request(self, command, quiet=False):
|
||||||
|
"""send a request and wait for reply"""
|
||||||
with self._write_lock:
|
with self._write_lock:
|
||||||
if not self._connected:
|
if not self.syncio or not self.syncio.connection:
|
||||||
if self._connect_thread is None:
|
if not self.asynio or not self.asynio.connection:
|
||||||
|
try:
|
||||||
|
self._connect_thread.join()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
# let doPoll do the reconnect
|
# let doPoll do the reconnect
|
||||||
self.pollInfo.trigger(True)
|
self.pollInfo.trigger(True)
|
||||||
raise ConnectionClosed('disconnected - reconnect is tried later')
|
raise ConnectionClosed('disconnected - reconnect later')
|
||||||
return self.raw_request(command, quiet)
|
self.syncio = AsynConn(self.uri)
|
||||||
|
assert self.syncio.readline() == b'OK'
|
||||||
def raw_request(self, command, quiet=False):
|
self.syncio.writeline(b'seauser seaser')
|
||||||
"""send a request and wait for reply"""
|
assert self.syncio.readline() == b'Login OK'
|
||||||
|
self.log.info('connected to %s', self.uri)
|
||||||
try:
|
try:
|
||||||
self.syncio.flush_recv()
|
self.syncio.flush_recv()
|
||||||
ft = 'fulltransAct' if quiet else 'fulltransact'
|
ft = 'fulltransAct' if quiet else 'fulltransact'
|
||||||
@ -232,22 +233,16 @@ class SeaClient(ProxyClient, Module):
|
|||||||
result = [reply.split('=', 1)[-1]]
|
result = [reply.split('=', 1)[-1]]
|
||||||
else:
|
else:
|
||||||
result.append(reply)
|
result.append(reply)
|
||||||
raise TimeoutError('no response within 10s')
|
|
||||||
except ConnectionClosed:
|
except ConnectionClosed:
|
||||||
self.close_connections()
|
|
||||||
raise
|
|
||||||
|
|
||||||
def close_connections(self):
|
|
||||||
connections = self.syncio, self.asynio
|
|
||||||
self._connected = False
|
|
||||||
self.syncio = self.asynio = None
|
|
||||||
for conn in connections:
|
|
||||||
try:
|
try:
|
||||||
conn.disconnect()
|
self.syncio.disconnect()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
self.syncio = None
|
||||||
|
raise
|
||||||
|
raise TimeoutError('no response within 10s')
|
||||||
|
|
||||||
def _rxthread(self):
|
def _rxthread(self, started_callback):
|
||||||
recheck = None
|
recheck = None
|
||||||
while not self.shutdown:
|
while not self.shutdown:
|
||||||
if recheck and time.time() > recheck:
|
if recheck and time.time() > recheck:
|
||||||
@ -263,7 +258,11 @@ class SeaClient(ProxyClient, Module):
|
|||||||
if reply is None:
|
if reply is None:
|
||||||
continue
|
continue
|
||||||
except ConnectionClosed:
|
except ConnectionClosed:
|
||||||
self.close_connections()
|
try:
|
||||||
|
self.asynio.disconnect()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.asynio = None
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
msg = json.loads(reply)
|
msg = json.loads(reply)
|
||||||
@ -290,6 +289,9 @@ class SeaClient(ProxyClient, Module):
|
|||||||
data = msg['data']
|
data = msg['data']
|
||||||
if flag == 'finish' and obj == 'get_all_param':
|
if flag == 'finish' and obj == 'get_all_param':
|
||||||
# first updates have finished
|
# first updates have finished
|
||||||
|
if started_callback:
|
||||||
|
started_callback()
|
||||||
|
started_callback = None
|
||||||
continue
|
continue
|
||||||
if flag != 'hdbevent':
|
if flag != 'hdbevent':
|
||||||
if obj not in ('frappy_async_client', 'get_all_param'):
|
if obj not in ('frappy_async_client', 'get_all_param'):
|
||||||
@ -350,7 +352,7 @@ class SeaClient(ProxyClient, Module):
|
|||||||
class SeaConfigCreator(SeaClient):
|
class SeaConfigCreator(SeaClient):
|
||||||
def startModule(self, start_events):
|
def startModule(self, start_events):
|
||||||
"""save objects (and sub-objects) description and exit"""
|
"""save objects (and sub-objects) description and exit"""
|
||||||
self._connect()
|
self._connect(None)
|
||||||
reply = self.request('describe_all')
|
reply = self.request('describe_all')
|
||||||
reply = ''.join('' if line.startswith('WARNING') else line for line in reply.split('\n'))
|
reply = ''.join('' if line.startswith('WARNING') else line for line in reply.split('\n'))
|
||||||
description, reply = json.loads(reply)
|
description, reply = json.loads(reply)
|
||||||
@ -642,7 +644,22 @@ class SeaModule(Module):
|
|||||||
if upd:
|
if upd:
|
||||||
upd(value, timestamp, readerror)
|
upd(value, timestamp, readerror)
|
||||||
return
|
return
|
||||||
self.announceUpdate(parameter, value, readerror, timestamp)
|
try:
|
||||||
|
pobj = self.parameters[parameter]
|
||||||
|
except KeyError:
|
||||||
|
self.log.error('do not know %s:%s', self.name, parameter)
|
||||||
|
raise
|
||||||
|
pobj.timestamp = timestamp
|
||||||
|
# should be done here: deal with clock differences
|
||||||
|
if not readerror:
|
||||||
|
try:
|
||||||
|
pobj.value = value # store the value even in case of a validation error
|
||||||
|
pobj.value = pobj.datatype(value)
|
||||||
|
except Exception as e:
|
||||||
|
readerror = secop_error(e)
|
||||||
|
pobj.readerror = readerror
|
||||||
|
if pobj.export:
|
||||||
|
self.secNode.srv.dispatcher.broadcast_event(make_update(self.name, pobj))
|
||||||
|
|
||||||
def initModule(self):
|
def initModule(self):
|
||||||
self.io.register_obj(self, self.sea_object)
|
self.io.register_obj(self, self.sea_object)
|
||||||
@ -653,35 +670,20 @@ class SeaModule(Module):
|
|||||||
|
|
||||||
|
|
||||||
class SeaReadable(SeaModule, Readable):
|
class SeaReadable(SeaModule, Readable):
|
||||||
_readerror = None
|
|
||||||
_status = IDLE, ''
|
|
||||||
|
|
||||||
def update_value(self, value, timestamp, readerror):
|
|
||||||
# make sure status is always ERROR when reading value fails
|
|
||||||
self._readerror = readerror
|
|
||||||
if readerror:
|
|
||||||
self.read_status() # forced ERROR status
|
|
||||||
self.announceUpdate('value', value, readerror, timestamp)
|
|
||||||
else: # order is important
|
|
||||||
self.value = value # includes announceUpdate
|
|
||||||
self.read_status() # send event for ordinary self._status
|
|
||||||
|
|
||||||
def update_status(self, value, timestamp, readerror):
|
def update_status(self, value, timestamp, readerror):
|
||||||
if readerror:
|
if readerror:
|
||||||
value = f'{readerror.name} - {readerror}'
|
value = repr(readerror)
|
||||||
if value == '':
|
if value == '':
|
||||||
self._status = IDLE, ''
|
self.status = (self.Status.IDLE, '')
|
||||||
else:
|
else:
|
||||||
self._status = ERROR, value
|
self.status = (self.Status.ERROR, value)
|
||||||
self.read_status()
|
|
||||||
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
if self._readerror:
|
return self.status
|
||||||
return ERROR, f'{self._readerror.name} - {self._readerror}'
|
|
||||||
return self._status
|
|
||||||
|
|
||||||
|
|
||||||
class SeaWritable(SeaReadable, Writable):
|
class SeaWritable(SeaModule, Writable):
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return self.target
|
return self.target
|
||||||
|
|
||||||
@ -691,13 +693,20 @@ class SeaWritable(SeaReadable, Writable):
|
|||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
|
|
||||||
class SeaDrivable(SeaReadable, Drivable):
|
class SeaDrivable(SeaModule, Drivable):
|
||||||
|
_sea_status = ''
|
||||||
_is_running = 0
|
_is_running = 0
|
||||||
|
|
||||||
def earlyInit(self):
|
def earlyInit(self):
|
||||||
super().earlyInit()
|
super().earlyInit()
|
||||||
self._run_event = threading.Event()
|
self._run_event = threading.Event()
|
||||||
|
|
||||||
|
def read_status(self):
|
||||||
|
return self.status
|
||||||
|
|
||||||
|
# def read_target(self):
|
||||||
|
# return self.target
|
||||||
|
|
||||||
def write_target(self, value):
|
def write_target(self, value):
|
||||||
self._run_event.clear()
|
self._run_event.clear()
|
||||||
self.io.query(f'run {self.sea_object} {value}')
|
self.io.query(f'run {self.sea_object} {value}')
|
||||||
@ -705,20 +714,25 @@ class SeaDrivable(SeaReadable, Drivable):
|
|||||||
self.log.warn('target changed but is_running stays 0')
|
self.log.warn('target changed but is_running stays 0')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def update_status(self, value, timestamp, readerror):
|
||||||
|
if not readerror:
|
||||||
|
self._sea_status = value
|
||||||
|
self.updateStatus()
|
||||||
|
|
||||||
def update_is_running(self, value, timestamp, readerror):
|
def update_is_running(self, value, timestamp, readerror):
|
||||||
if not readerror:
|
if not readerror:
|
||||||
self._is_running = value
|
self._is_running = value
|
||||||
self.read_status()
|
self.updateStatus()
|
||||||
if value:
|
if value:
|
||||||
self._run_event.set()
|
self._run_event.set()
|
||||||
|
|
||||||
def read_status(self):
|
def updateStatus(self):
|
||||||
status = super().read_status()
|
if self._sea_status:
|
||||||
if self._is_running:
|
self.status = (self.Status.ERROR, self._sea_status)
|
||||||
if status[0] >= ERROR:
|
elif self._is_running:
|
||||||
return ERROR, 'BUSY + ' + status[1]
|
self.status = (self.Status.BUSY, 'driving')
|
||||||
return BUSY, 'driving'
|
else:
|
||||||
return status
|
self.status = (self.Status.IDLE, '')
|
||||||
|
|
||||||
def updateTarget(self, module, parameter, value, timestamp, readerror):
|
def updateTarget(self, module, parameter, value, timestamp, readerror):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
|
@ -27,7 +27,7 @@ import numpy as np
|
|||||||
from scipy.interpolate import splev, splrep # pylint: disable=import-error
|
from scipy.interpolate import splev, splrep # pylint: disable=import-error
|
||||||
|
|
||||||
from frappy.core import Attached, BoolType, Parameter, Readable, StringType, \
|
from frappy.core import Attached, BoolType, Parameter, Readable, StringType, \
|
||||||
FloatRange, nopoll
|
FloatRange
|
||||||
|
|
||||||
|
|
||||||
def linear(x):
|
def linear(x):
|
||||||
@ -195,40 +195,35 @@ class Sensor(Readable):
|
|||||||
if self.description == '_':
|
if self.description == '_':
|
||||||
self.description = f'{self.rawsensor!r} calibrated with curve {self.calib!r}'
|
self.description = f'{self.rawsensor!r} calibrated with curve {self.calib!r}'
|
||||||
|
|
||||||
|
def doPoll(self):
|
||||||
|
self.read_status()
|
||||||
|
|
||||||
def write_calib(self, value):
|
def write_calib(self, value):
|
||||||
self._calib = CalCurve(value)
|
self._calib = CalCurve(value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def _get_value(self, rawvalue):
|
def update_value(self, value):
|
||||||
if self.abs:
|
if self.abs:
|
||||||
rawvalue = abs(float(rawvalue))
|
value = abs(float(value))
|
||||||
return self._calib(rawvalue)
|
self.value = self._calib(value)
|
||||||
|
self._value_error = None
|
||||||
|
|
||||||
def _get_status(self, rawstatus):
|
def error_update_value(self, err):
|
||||||
return rawstatus if self._value_error is None else (self.Status.ERROR, self._value_error)
|
|
||||||
|
|
||||||
def update_value(self, rawvalue, err=None):
|
|
||||||
if err:
|
|
||||||
if self.abs and str(err) == 'R_UNDER': # hack: ignore R_UNDER from ls370
|
if self.abs and str(err) == 'R_UNDER': # hack: ignore R_UNDER from ls370
|
||||||
self._value_error = None
|
self._value_error = None
|
||||||
return
|
return None
|
||||||
err = repr(err)
|
self._value_error = repr(err)
|
||||||
|
raise err
|
||||||
|
|
||||||
|
def update_status(self, value):
|
||||||
|
if self._value_error is None:
|
||||||
|
self.status = value
|
||||||
else:
|
else:
|
||||||
try:
|
self.status = self.Status.ERROR, self._value_error
|
||||||
self.value = self._get_value(rawvalue)
|
|
||||||
except Exception as e:
|
|
||||||
err = repr(e)
|
|
||||||
if err != self._value_error:
|
|
||||||
self._value_error = err
|
|
||||||
self.status = self._get_status(self.rawsensor.status)
|
|
||||||
|
|
||||||
def update_status(self, rawstatus):
|
|
||||||
self.status = self._get_status(rawstatus)
|
|
||||||
|
|
||||||
@nopoll
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return self._get_value(self.rawsensor.read_value())
|
return self._calib(self.rawsensor.read_value())
|
||||||
|
|
||||||
@nopoll
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
return self._get_status(self.rawsensor.read_status())
|
self.update_status(self.rawsensor.status)
|
||||||
|
return self.status
|
||||||
|
@ -26,7 +26,6 @@ from frappy.datatypes import EnumType, FloatRange, StringType
|
|||||||
from frappy.lib.enum import Enum
|
from frappy.lib.enum import Enum
|
||||||
from frappy_psi.mercury import MercuryChannel, Mapped, off_on, HasInput
|
from frappy_psi.mercury import MercuryChannel, Mapped, off_on, HasInput
|
||||||
from frappy_psi import mercury
|
from frappy_psi import mercury
|
||||||
from frappy_psi.frozenparam import FrozenParam
|
|
||||||
|
|
||||||
actions = Enum(none=0, condense=1, circulate=2, collect=3)
|
actions = Enum(none=0, condense=1, circulate=2, collect=3)
|
||||||
open_close = Mapped(CLOSE=0, OPEN=1)
|
open_close = Mapped(CLOSE=0, OPEN=1)
|
||||||
@ -40,23 +39,22 @@ class Action(MercuryChannel, Writable):
|
|||||||
mix_channel = Property('mix channel', StringType(), 'T5')
|
mix_channel = Property('mix channel', StringType(), 'T5')
|
||||||
still_channel = Property('cool down channel', StringType(), 'T4')
|
still_channel = Property('cool down channel', StringType(), 'T4')
|
||||||
value = Parameter('running action', EnumType(actions))
|
value = Parameter('running action', EnumType(actions))
|
||||||
target = FrozenParam('action to do', EnumType(none=0, condense=1, collect=3), readonly=False)
|
target = Parameter('action to do', EnumType(none=0, condense=1, collect=3), readonly=False)
|
||||||
_target = 0
|
_target = 0
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return self.query('SYS:DR:ACTN', actions_map)
|
return self.query('SYS:DR:ACTN', actions_map)
|
||||||
|
|
||||||
# as target is a FrozenParam, value might be still lag behind target
|
def read_target(self):
|
||||||
# but will be updated when changed from an other source
|
return self._target
|
||||||
read_target = read_value
|
|
||||||
|
|
||||||
def write_target(self, value):
|
def write_target(self, value):
|
||||||
|
self._target = value
|
||||||
self.change('SYS:DR:CHAN:COOL', self.cooldown_channel, str)
|
self.change('SYS:DR:CHAN:COOL', self.cooldown_channel, str)
|
||||||
self.change('SYS:DR:CHAN:STIL', self.still_channel, str)
|
self.change('SYS:DR:CHAN:STIL', self.still_channel, str)
|
||||||
self.change('SYS:DR:CHAN:MC', self.mix_channel, str)
|
self.change('SYS:DR:CHAN:MC', self.mix_channel, str)
|
||||||
self.change('DEV:T5:TEMP:MEAS:ENAB', 'ON', str)
|
self.change('DEV:T5:TEMP:MEAS:ENAB', 'ON', str)
|
||||||
self.change('SYS:DR:ACTN', value, actions_map)
|
return self.change('SYS:DR:ACTN', value, actions_map)
|
||||||
return value
|
|
||||||
|
|
||||||
# actions:
|
# actions:
|
||||||
# NONE (no action)
|
# NONE (no action)
|
||||||
@ -76,7 +74,7 @@ class Action(MercuryChannel, Writable):
|
|||||||
class Valve(MercuryChannel, Drivable):
|
class Valve(MercuryChannel, Drivable):
|
||||||
kind = 'VALV'
|
kind = 'VALV'
|
||||||
value = Parameter('valve state', EnumType(closed=0, opened=1))
|
value = Parameter('valve state', EnumType(closed=0, opened=1))
|
||||||
target = FrozenParam('valve target', EnumType(close=0, open=1))
|
target = Parameter('valve target', EnumType(close=0, open=1))
|
||||||
|
|
||||||
_try_count = None
|
_try_count = None
|
||||||
|
|
||||||
@ -110,10 +108,6 @@ class Valve(MercuryChannel, Drivable):
|
|||||||
self.change('DEV::VALV:SIG:STATE', self.target, open_close)
|
self.change('DEV::VALV:SIG:STATE', self.target, open_close)
|
||||||
return BUSY, 'waiting'
|
return BUSY, 'waiting'
|
||||||
|
|
||||||
# as target is a FrozenParam, value might be still lag behind target
|
|
||||||
# but will be updated when changed from an other source
|
|
||||||
read_target = read_value
|
|
||||||
|
|
||||||
def write_target(self, value):
|
def write_target(self, value):
|
||||||
if value != self.read_value():
|
if value != self.read_value():
|
||||||
self._try_count = 0
|
self._try_count = 0
|
||||||
@ -126,18 +120,13 @@ class Valve(MercuryChannel, Drivable):
|
|||||||
class Pump(MercuryChannel, Writable):
|
class Pump(MercuryChannel, Writable):
|
||||||
kind = 'PUMP'
|
kind = 'PUMP'
|
||||||
value = Parameter('pump state', EnumType(off=0, on=1))
|
value = Parameter('pump state', EnumType(off=0, on=1))
|
||||||
target = FrozenParam('pump target', EnumType(off=0, on=1))
|
target = Parameter('pump target', EnumType(off=0, on=1))
|
||||||
|
|
||||||
def read_value(self):
|
def read_value(self):
|
||||||
return self.query('DEV::PUMP:SIG:STATE', off_on)
|
return self.query('DEV::PUMP:SIG:STATE', off_on)
|
||||||
|
|
||||||
# as target is a FrozenParam, value might be still lag behind target
|
|
||||||
# but will be updated when changed from an other source
|
|
||||||
read_target = read_value
|
|
||||||
|
|
||||||
def write_target(self, value):
|
def write_target(self, value):
|
||||||
self.change('DEV::PUMP:SIG:STATE', value, off_on)
|
return self.change('DEV::PUMP:SIG:STATE', value, off_on)
|
||||||
return value
|
|
||||||
|
|
||||||
def read_status(self):
|
def read_status(self):
|
||||||
return IDLE, ''
|
return IDLE, ''
|
||||||
|
@ -411,7 +411,6 @@ class Uniax(PersistentMixin, Drivable):
|
|||||||
|
|
||||||
@Command()
|
@Command()
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""stop motor and control"""
|
|
||||||
if self.motor.isBusy():
|
if self.motor.isBusy():
|
||||||
self.log.info('stop motor')
|
self.log.info('stop motor')
|
||||||
self.motor_stop()
|
self.motor_stop()
|
||||||
|
@ -5,8 +5,6 @@ mlzlog >=0.2.0
|
|||||||
# daemonizing
|
# daemonizing
|
||||||
psutil
|
psutil
|
||||||
python-daemon >=2.0
|
python-daemon >=2.0
|
||||||
# websocket interface:
|
|
||||||
websockets>=11.0
|
|
||||||
# for zmq interface
|
# for zmq interface
|
||||||
#pyzmq>=13.1.0
|
#pyzmq>=13.1.0
|
||||||
#for ppms on windows
|
#for ppms on windows
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
|
|
||||||
from frappy.io import HasIO
|
|
||||||
from frappy.modules import Module, Attached
|
from frappy.modules import Module, Attached
|
||||||
from frappy.protocol.dispatcher import Dispatcher
|
from frappy.protocol.dispatcher import Dispatcher
|
||||||
|
|
||||||
@ -30,9 +29,6 @@ class LoggerStub:
|
|||||||
info = warning = exception = debug
|
info = warning = exception = debug
|
||||||
handlers = []
|
handlers = []
|
||||||
|
|
||||||
def getChild(self, name):
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
logger = LoggerStub()
|
logger = LoggerStub()
|
||||||
|
|
||||||
@ -55,7 +51,6 @@ class ServerStub:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.secnode = SecNodeStub()
|
self.secnode = SecNodeStub()
|
||||||
self.dispatcher = Dispatcher('dispatcher', logger, {}, self)
|
self.dispatcher = Dispatcher('dispatcher', logger, {}, self)
|
||||||
self.log = logger
|
|
||||||
|
|
||||||
|
|
||||||
def test_attach():
|
def test_attach():
|
||||||
@ -69,22 +64,3 @@ def test_attach():
|
|||||||
srv.secnode.add_module(a, 'a')
|
srv.secnode.add_module(a, 'a')
|
||||||
srv.secnode.add_module(m, 'm')
|
srv.secnode.add_module(m, 'm')
|
||||||
assert m.att == a
|
assert m.att == a
|
||||||
|
|
||||||
|
|
||||||
def test_attach_hasio_uri():
|
|
||||||
class TestIO(Module):
|
|
||||||
def __init__(self, name, logger, cfgdict, srv):
|
|
||||||
self._uri = cfgdict.pop('uri')
|
|
||||||
super().__init__(name, logger, cfgdict, srv)
|
|
||||||
|
|
||||||
class HasIOTest(HasIO):
|
|
||||||
ioClass = TestIO
|
|
||||||
|
|
||||||
srv = ServerStub()
|
|
||||||
m = HasIOTest('m', logger, {'description': '', 'uri': 'abc'}, srv)
|
|
||||||
assert srv.secnode.modules['m_io']._uri == 'abc'
|
|
||||||
assert m.io == srv.secnode.modules['m_io']
|
|
||||||
# two modules with the same IO should use the same io module
|
|
||||||
m2 = HasIOTest('m', logger, {'description': '', 'uri': 'abc'}, srv)
|
|
||||||
assert m2.io == srv.secnode.modules['m_io']
|
|
||||||
|
|
||||||
|
@ -1,155 +0,0 @@
|
|||||||
# *****************************************************************************
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License as published by the Free Software
|
|
||||||
# Foundation; either version 2 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
#
|
|
||||||
# Module authors:
|
|
||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
|
||||||
#
|
|
||||||
# *****************************************************************************
|
|
||||||
"""test parameter callbacks"""
|
|
||||||
|
|
||||||
from test.test_modules import LoggerStub, ServerStub
|
|
||||||
import pytest
|
|
||||||
from frappy.core import Module, Parameter, FloatRange
|
|
||||||
from frappy.errors import WrongTypeError
|
|
||||||
|
|
||||||
|
|
||||||
WRONG_TYPE = WrongTypeError()
|
|
||||||
|
|
||||||
|
|
||||||
class Mod(Module):
|
|
||||||
a = Parameter('', FloatRange())
|
|
||||||
b = Parameter('', FloatRange())
|
|
||||||
c = Parameter('', FloatRange())
|
|
||||||
|
|
||||||
def read_a(self):
|
|
||||||
raise WRONG_TYPE
|
|
||||||
|
|
||||||
def read_b(self):
|
|
||||||
raise WRONG_TYPE
|
|
||||||
|
|
||||||
def read_c(self):
|
|
||||||
raise WRONG_TYPE
|
|
||||||
|
|
||||||
|
|
||||||
class Dbl(Module):
|
|
||||||
a = Parameter('', FloatRange())
|
|
||||||
b = Parameter('', FloatRange())
|
|
||||||
c = Parameter('', FloatRange())
|
|
||||||
_error_a = None
|
|
||||||
_value_b = None
|
|
||||||
_error_c = None
|
|
||||||
|
|
||||||
def update_a(self, value, err=None):
|
|
||||||
# treat error updates
|
|
||||||
try:
|
|
||||||
self.a = value * 2
|
|
||||||
except TypeError: # value is None -> err
|
|
||||||
self.announceUpdate('a', None, err)
|
|
||||||
|
|
||||||
def update_b(self, value):
|
|
||||||
self._value_b = value
|
|
||||||
# error updates are ignored
|
|
||||||
self.b = value * 2
|
|
||||||
|
|
||||||
|
|
||||||
def make(cls):
|
|
||||||
logger = LoggerStub()
|
|
||||||
srv = ServerStub({})
|
|
||||||
return cls('mod1', logger, {'description': ''}, srv)
|
|
||||||
|
|
||||||
|
|
||||||
def test_simple_callback():
|
|
||||||
mod1 = make(Mod)
|
|
||||||
result = []
|
|
||||||
|
|
||||||
def cbfunc(arg1, arg2, value):
|
|
||||||
result[:] = arg1, arg2, value
|
|
||||||
|
|
||||||
mod1.addCallback('a', cbfunc, 'ARG1', 'arg2')
|
|
||||||
|
|
||||||
mod1.a = 1.5
|
|
||||||
assert result == ['ARG1', 'arg2', 1.5]
|
|
||||||
|
|
||||||
result.clear()
|
|
||||||
|
|
||||||
with pytest.raises(WrongTypeError):
|
|
||||||
mod1.read_a()
|
|
||||||
|
|
||||||
assert not result # callback function is NOT called
|
|
||||||
|
|
||||||
|
|
||||||
def test_combi_callback():
|
|
||||||
mod1 = make(Mod)
|
|
||||||
result = []
|
|
||||||
|
|
||||||
def cbfunc(arg1, arg2, value, err=None):
|
|
||||||
result[:] = arg1, arg2, value, err
|
|
||||||
|
|
||||||
mod1.addCallback('a', cbfunc, 'ARG1', 'arg2')
|
|
||||||
|
|
||||||
mod1.a = 1.5
|
|
||||||
assert result == ['ARG1', 'arg2', 1.5, None]
|
|
||||||
|
|
||||||
result.clear()
|
|
||||||
|
|
||||||
with pytest.raises(WrongTypeError):
|
|
||||||
mod1.read_a()
|
|
||||||
|
|
||||||
assert result[:3] == ['ARG1', 'arg2', None] # callback function called with value None
|
|
||||||
assert isinstance(result[3], WrongTypeError)
|
|
||||||
|
|
||||||
|
|
||||||
def test_autoupdate():
|
|
||||||
mod1 = make(Mod)
|
|
||||||
mod2 = make(Dbl)
|
|
||||||
mod1.registerCallbacks(mod2, autoupdate=['c'])
|
|
||||||
|
|
||||||
result = {}
|
|
||||||
|
|
||||||
def cbfunc(pname, *args):
|
|
||||||
result[pname] = args
|
|
||||||
|
|
||||||
for param in 'a', 'b', 'c':
|
|
||||||
mod2.addCallback(param, cbfunc, param)
|
|
||||||
|
|
||||||
# test update_a without error
|
|
||||||
mod1.a = 5
|
|
||||||
assert mod2.a == 10
|
|
||||||
assert result.pop('a') == (10,)
|
|
||||||
|
|
||||||
# test update_a with error
|
|
||||||
with pytest.raises(WrongTypeError):
|
|
||||||
mod1.read_a()
|
|
||||||
|
|
||||||
assert result.pop('a') == (None, WRONG_TYPE)
|
|
||||||
|
|
||||||
# test that update_b is ignored in case of error
|
|
||||||
mod1.b = 3
|
|
||||||
assert mod2.b == 6 # no error
|
|
||||||
assert result.pop('b') == (6,)
|
|
||||||
|
|
||||||
with pytest.raises(WrongTypeError):
|
|
||||||
mod1.read_b()
|
|
||||||
assert 'b' not in result
|
|
||||||
|
|
||||||
# test autoupdate
|
|
||||||
mod1.c = 3
|
|
||||||
assert mod2.c == 3
|
|
||||||
assert result['c'] == (3,)
|
|
||||||
|
|
||||||
with pytest.raises(WrongTypeError):
|
|
||||||
mod1.read_c()
|
|
||||||
assert result['c'] == (None, WRONG_TYPE)
|
|
@ -18,14 +18,12 @@
|
|||||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||||
#
|
#
|
||||||
# *****************************************************************************
|
# *****************************************************************************
|
||||||
"""test frappy.extparams"""
|
"""test frappy.mixins.HasCtrlPars"""
|
||||||
|
|
||||||
|
|
||||||
from test.test_modules import LoggerStub, ServerStub
|
from test.test_modules import LoggerStub, ServerStub
|
||||||
import pytest
|
|
||||||
from frappy.core import FloatRange, Module, Parameter
|
from frappy.core import FloatRange, Module, Parameter
|
||||||
from frappy.extparams import StructParam, FloatEnumParam
|
from frappy.structparam import StructParam
|
||||||
from frappy.errors import ProgrammingError
|
|
||||||
|
|
||||||
|
|
||||||
def test_with_read_ctrlpars():
|
def test_with_read_ctrlpars():
|
||||||
@ -132,76 +130,3 @@ def test_order_dependence1():
|
|||||||
def test_order_dependence2():
|
def test_order_dependence2():
|
||||||
test_with_read_ctrlpars()
|
test_with_read_ctrlpars()
|
||||||
test_without_read_ctrlpars()
|
test_without_read_ctrlpars()
|
||||||
|
|
||||||
|
|
||||||
def test_float_enum():
|
|
||||||
class Mod(Module):
|
|
||||||
vrange = FloatEnumParam('voltage range', [
|
|
||||||
(1, '50uV'), '200 µV', '1mV', ('5mV', 0.006), (9, 'max', 0.024)], 'V')
|
|
||||||
gain = FloatEnumParam('gain factor', ('1', '2', '4', '8'), idx_name='igain')
|
|
||||||
dist = FloatEnumParam('distance', ('1m', '1mm', '1µm'), unit='m')
|
|
||||||
|
|
||||||
_vrange_idx = None
|
|
||||||
|
|
||||||
def write_vrange_idx(self, value):
|
|
||||||
self._vrange_idx = value
|
|
||||||
|
|
||||||
logger = LoggerStub()
|
|
||||||
updates = {}
|
|
||||||
srv = ServerStub(updates)
|
|
||||||
|
|
||||||
m = Mod('m', logger, {'description': ''}, srv)
|
|
||||||
|
|
||||||
assert m.write_vrange_idx(1) == 1
|
|
||||||
assert m._vrange_idx == '50uV'
|
|
||||||
assert m._vrange_idx == 1
|
|
||||||
assert m.vrange == 5e-5
|
|
||||||
|
|
||||||
assert m.write_vrange_idx(2) == 2
|
|
||||||
assert m._vrange_idx == '200 µV'
|
|
||||||
assert m._vrange_idx == 2
|
|
||||||
assert m.vrange == 2e-4
|
|
||||||
|
|
||||||
assert m.write_vrange(6e-5) == 5e-5 # round to the next value
|
|
||||||
assert m._vrange_idx == '50uV'
|
|
||||||
assert m._vrange_idx == 1
|
|
||||||
assert m.write_vrange(20e-3) == 24e-3 # round to the next value
|
|
||||||
assert m._vrange_idx == 'max'
|
|
||||||
assert m._vrange_idx == 9
|
|
||||||
|
|
||||||
for idx in range(4):
|
|
||||||
value = 2 ** idx
|
|
||||||
updates.clear()
|
|
||||||
assert m.write_igain(idx) == idx
|
|
||||||
assert updates == {'m': {'igain': idx, 'gain': value}}
|
|
||||||
assert m.igain == idx
|
|
||||||
assert m.igain == str(value)
|
|
||||||
assert m.gain == value
|
|
||||||
|
|
||||||
for idx in range(4):
|
|
||||||
value = 2 ** idx
|
|
||||||
assert m.write_gain(value) == value
|
|
||||||
assert m.igain == idx
|
|
||||||
assert m.igain == str(value)
|
|
||||||
|
|
||||||
for idx in range(3):
|
|
||||||
value = 10 ** (-3 * idx)
|
|
||||||
assert m.write_dist(value) == value
|
|
||||||
assert m.dist_idx == idx
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('labels, unit, error', [
|
|
||||||
(FloatRange(), '', 'not a datatype'), # 2nd arg must not be a datatype
|
|
||||||
([(1, 2, 3)], '', 'must be strings'), # label is not a string
|
|
||||||
([(1, '1V', 3, 4)], 'V', 'labels or tuples'), # 4-tuple
|
|
||||||
([('1A', 3, 4)], 'A', 'labels or tuples'), # two values after label
|
|
||||||
(('1m', (0, '1k')), '', 'conflicts with'), # two times index 0
|
|
||||||
(['1mV', '10mA'], 'V', 'not the form'), # wrong unit
|
|
||||||
(['.mV'], 'V', 'not the form'), # bad number
|
|
||||||
(['mV'], 'V', 'not the form'), # missing number
|
|
||||||
(['1+mV'], 'V', 'not the form'), # bad number
|
|
||||||
])
|
|
||||||
def test_bad_float_enum(labels, unit, error):
|
|
||||||
with pytest.raises(ProgrammingError, match=error):
|
|
||||||
class Mod(Module): # pylint:disable=unused-variable
|
|
||||||
param = FloatEnumParam('', labels, unit)
|
|
@ -23,8 +23,6 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import importlib
|
|
||||||
from glob import glob
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from frappy.datatypes import BoolType, FloatRange, StringType, IntRange, ScaledInteger
|
from frappy.datatypes import BoolType, FloatRange, StringType, IntRange, ScaledInteger
|
||||||
@ -442,12 +440,12 @@ def test_override():
|
|||||||
assert Mod.value.value == 5
|
assert Mod.value.value == 5
|
||||||
assert Mod.stop.description == "no decorator needed"
|
assert Mod.stop.description == "no decorator needed"
|
||||||
|
|
||||||
class Mod2(Mod):
|
class Mod2(Drivable):
|
||||||
|
@Command()
|
||||||
def stop(self):
|
def stop(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# inherit doc string
|
assert Mod2.stop.description == Drivable.stop.description
|
||||||
assert Mod2.stop.description == Mod.stop.description
|
|
||||||
|
|
||||||
|
|
||||||
def test_command_config():
|
def test_command_config():
|
||||||
@ -922,24 +920,3 @@ def test_interface_classes(bases, iface_classes):
|
|||||||
pass
|
pass
|
||||||
m = Mod('mod', LoggerStub(), {'description': 'test'}, srv)
|
m = Mod('mod', LoggerStub(), {'description': 'test'}, srv)
|
||||||
assert m.interface_classes == iface_classes
|
assert m.interface_classes == iface_classes
|
||||||
|
|
||||||
|
|
||||||
all_drivables = set()
|
|
||||||
for pyfile in glob('frappy_*/*.py'):
|
|
||||||
module = pyfile[:-3].replace('/', '.')
|
|
||||||
try:
|
|
||||||
importlib.import_module(module)
|
|
||||||
except Exception as e:
|
|
||||||
print(module, e)
|
|
||||||
continue
|
|
||||||
for obj_ in sys.modules[module].__dict__.values():
|
|
||||||
if isinstance(obj_, type) and issubclass(obj_, Drivable):
|
|
||||||
all_drivables.add(obj_)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('modcls', all_drivables)
|
|
||||||
def test_stop_doc(modcls):
|
|
||||||
# make sure that implemented stop methods have a doc string
|
|
||||||
if (modcls.stop.description == Drivable.stop.description
|
|
||||||
and modcls.stop.func != Drivable.stop.func):
|
|
||||||
assert modcls.stop.func.__doc__ # stop method needs a doc string
|
|
||||||
|
Reference in New Issue
Block a user