-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathophyd_tango.py
216 lines (173 loc) · 5.84 KB
/
ophyd_tango.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import threading
import time
from ophyd import Kind
from ophyd.status import Status
import tango
class TangoAttribute:
"Wrap a tango.AttributeProxy in the bluesky interface."
def __init__(
self,
attribute_proxy: tango.AttributeProxy,
*,
parent=None,
kind=Kind.normal,
name=None,
):
self._attribute_proxy = attribute_proxy
if name is None:
name = self._attribute_proxy.name()
self.name = name
self.parent = parent
@property
def proxy(self):
return self._attribute_proxy
def read(self):
reading = self._attribute_proxy.read()
return {self.name: {"value": reading.value, "timestamp": reading.time.totime()}}
def describe(self):
return {
self.name: {
"shape": extract_shape(self._attribute_proxy.read()),
"dtype": "number", # jsonschema types
"source": self._attribute_proxy.name(),
"unit": self._attribute_proxy.get_config().unit,
}
}
def read_configuration(self):
return {}
def describe_configuration(self):
return {}
class TangoWritableAttribute(TangoAttribute):
def set(self, value):
status = Status()
def write_and_wait():
# This blocks until the write is accepted, which in this case means
# the write is complete. Do not use this approach for a motor or
# something that takes time to execute the write.
try:
self._attribute_proxy.write(value)
except Exception as exc:
status.set_exception(exc)
else:
status.set_finished()
threading.Thread(target=write_and_wait).start()
return status
class TangoMotorPosition(TangoAttribute):
""" Wraps a (Sardana) motor position attribute """
def set(self, value):
status = Status()
def write_and_wait():
"""
Write position and block until write has taken effect,
meaning that the attribute quality has gone back to
"VALID". While the attribute is changing to the new position the
quality is usually "CHANGING".
"""
try:
self._attribute_proxy.write(value)
except tango.DevFailed as exc:
status.set_exception(exc)
while self._attribute_proxy.read().quality != tango.AttrQuality.ATTR_VALID:
time.sleep(0.1)
status.set_finished()
threading.Thread(target=write_and_wait).start()
return status
type_map = {
tango.AttrWriteType.READ_WRITE: TangoWritableAttribute,
tango.AttrWriteType.READ: TangoAttribute,
}
class TangoDevice:
"Wrap a tango.DeviceProxy in the Bluesky interface."
READ_FIELDS = []
CONFIG_FIELDS = []
def __init__(self, device_proxy: tango.DeviceProxy, *, name):
self.parent = None
self.name = name
self.attributes = []
for field in self.READ_FIELDS:
class_ = type_map[device_proxy.get_attribute_config(field).writable]
obj = class_(
tango.AttributeProxy(
f"{device_proxy.name()}/{field}",
),
parent=self,
kind=Kind.normal,
name="_".join([self.name, field]),
)
self.attributes.append(obj)
setattr(self, field, obj)
# TODO CONFIG_FIELDS
def set(self):
# TODO Implement this next time.
...
def read(self):
res = {}
for attr in self.attributes:
try:
res.update(attr.read())
except Exception:
continue
return res
def read_configuration(self):
res = {}
for attr in self.attributes:
try:
res.update(attr.read_configuration())
except Exception:
continue
return res
def describe_configuration(self):
res = {}
for attr in self.attributes:
try:
res.update(attr.describe_configuration())
except Exception:
continue
return res
def describe(self):
res = {}
for attr in self.attributes:
try:
res.update(attr.describe())
except Exception:
continue
return res
class TangoThingie(TangoDevice):
READ_FIELDS = [
"string_scalar",
"uchar_scalar",
"ulong64_scalar",
"ushort_scalar",
"ulong_scalar",
]
def extract_shape(reading):
shape = [] # e.g. [10, 15]
if reading.dim_x:
shape.append(reading.dim_x)
if reading.dim_y:
shape.append(reading.dim_y)
return shape
class TangoMotor(TangoDevice):
READ_FIELDS = [
"position",
"velocity",
]
if __name__ == "__main__":
from bluesky.plans import count
from bluesky.callbacks.core import LiveTable
from bluesky import RunEngine
from bluesky.plans import scan
from bluesky.callbacks.best_effort import BestEffortCallback
device_proxy = tango.DeviceProxy("sys/tg_test/1")
attr_proxy = tango.AttributeProxy("sys/tg_test/1/ampli")
tango_attr = TangoWritableAttribute(attr_proxy)
tango_device = TangoThingie(device_proxy, name="thingie")
motor_proxy = tango.DeviceProxy("motor/motctrl04/1")
# motor = TangoMotor(motor_proxy, name="motor")
short_scalar = TangoAttribute(tango.AttributeProxy("sys/tg_test/1/short_scalar"))
motor1 = TangoMotorPosition(tango.AttributeProxy("motor/motctrl01/1/position"),
name="motor1")
RE = RunEngine()
bec = BestEffortCallback()
RE.subscribe(bec)
RE(scan([short_scalar], motor1, 0, 100, 10))