generated from MBI-Div-B/pytango-TemplateDeviceServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIGCSMotor.py
executable file
·341 lines (286 loc) · 9.01 KB
/
PIGCSMotor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
"""
PI Hexapod tango device server
@author: M. Schneider, MBI Berlin, 2021
"""
from pipython import GCS2Device
from tango import AttrWriteType, DevState, DeviceProxy
from tango.server import Device, attribute, command, device_property, run
import sys
import time
class PIGCSController(Device):
""" """
host = device_property(
dtype=str,
default_value="192.168.0.100",
doc="controller IP address",
)
port = device_property(
dtype=int,
default_value=50000,
doc="controller port",
)
velocity = attribute(
label="system velocity",
dtype=float,
access=AttrWriteType.READ_WRITE,
unit="mm/s",
)
pivot_X = attribute(
label="pivot X",
dtype=float,
unit="mm",
access=AttrWriteType.READ,
)
pivot_Y = attribute(
label="pivot Y",
dtype=float,
unit="mm",
access=AttrWriteType.READ,
)
pivot_Z = attribute(
label="pivot Z",
dtype=float,
unit="mm",
access=AttrWriteType.READ,
)
def init_device(self):
"""Establish connection to controller."""
super(PIGCSController, self).init_device()
try:
print(
f"Connecting to GCS controller on {self.host}:{self.port}",
file=self.log_info,
)
self.ctrl = GCS2Device()
self.ctrl.ConnectTCPIP(self.host, self.port)
idn = self.ctrl.qIDN()
print(f"Connection established on {self.host}:\n{idn}", file=self.log_info)
self._positions = None
self._limits = None
self._referenced = None
self._moving = None
self._pivot_point = None
self._velocity = None
self._last_query = 0.0
self._query_timeout = 0.1
self._axis_names = self.ctrl.allaxes
self.set_state(DevState.ON)
except Exception as ex:
print(f"Error on initialization: {ex}", file=self.log_error)
self.set_state(DevState.FAULT)
def always_executed_hook(self):
"""Query all axes positions, limits and move states."""
if self.dev_state() == DevState.ON:
if (time.time() - self._last_query) > self._query_timeout:
self._positions = self.ctrl.qPOS()
self._limits = self.ctrl.qLIM()
self._moving = self.ctrl.IsMoving(self._axis_names)
self._referenced = self.ctrl.qFRF()
self._pivot_point = self.ctrl.qSPI()
self._velocity = self.ctrl.qVLS()
self._last_query = time.time()
def query_position(self, axis):
return self._positions[axis]
def read_pivot_X(self):
return self._pivot_point["R"]
def read_pivot_Y(self):
return self._pivot_point["S"]
def read_pivot_Z(self):
return self._pivot_point["T"]
def read_velocity(self):
return self._velocity
def write_velocity(self, value):
self.ctrl.VLS(value)
@command(
dtype_in=str,
doc_in="formatted string '<axis_name>=<position>'",
dtype_out=int,
doc_out="Result code (0: moving)",
)
def set_position(self, position):
axis, target = position.split("=")
target = float(target)
if self.ctrl.qVMO(axis, target):
self.ctrl.MOV(axis, target)
else:
raise ValueError("Target position cannot be reached")
error = self.ctrl.qERR()
return error
@command(
dtype_in=float,
doc_in="platform velocity in physical units (mm/s)",
)
def set_velocity(self, velocity):
self.ctrl.VLS(velocity)
def query_limit(self, axis):
return self._limits[axis]
@command(
dtype_in=str,
doc_in="axis to query",
dtype_out=(float,),
doc_out="[pos, limit, move]",
)
def query_axis_state(self, axis):
"""Return state for axis.
Values:
position (float)
limit state (bool)
movement state (bool)
reference state (bool)
velocity (float)
"""
state = (
self._positions[axis],
self._limits[axis],
self._moving[axis],
self._referenced[axis],
self._velocity,
)
return state
@command(
dtype_out=(str,),
doc_out="available axis names on controller",
)
def get_axis_names(self):
"""Get list of axis names"""
return self._axis_names
@command(
dtype_in=str,
dtype_out=(float,),
)
def query_axis_limits(self, axis):
"""Return limit positions for axis."""
lower = self.ctrl.qTMN()[axis]
upper = self.ctrl.qTMX()[axis]
return [lower, upper]
@command(
dtype_in=str,
dtype_out=str,
)
def query_axis_unit(self, axis):
"""Return unit string for axis."""
return self.ctrl.qPUN()[axis]
@command(
dtype_in=(float,),
doc_in="X,Y,Z pivot point positions.",
)
def set_pivot_point(self, values):
"""Sets the pivot point of the hexapod."""
self.ctrl.SPI(axes=["X", "Y", "Z"], values=list(values[0:3]))
@command
def find_references(self):
"""Find reference marks for all axes."""
self.ctrl.FRF()
@command
def halt(self):
"""Halt motion smoothly. Doesn't stop reference moves."""
self.ctrl.HLT(noraise=True)
@command
def stop(self):
"""Abruptly stop all motions, including reference moves."""
self.ctrl.STP(noraise=True)
class PIGCSAxis(Device):
""""""
controller = device_property(
dtype=str,
default_value="",
doc="tango fqdn of controller device server",
)
axis = device_property(
dtype=str,
default_value="",
doc="name of axis on controller (e.g., X, Y, Z, U, V, W)",
)
position = attribute(
dtype=float,
access=AttrWriteType.READ_WRITE,
)
velocity = attribute(
dtype=float,
access=AttrWriteType.READ_WRITE,
unit="mm/s",
doc="system velocity of all six axes",
)
limit_switch = attribute(
dtype=int,
access=AttrWriteType.READ,
)
referenced = attribute(
dtype=bool,
access=AttrWriteType.READ,
)
inverted = attribute(
dtype=bool,
access=AttrWriteType.READ_WRITE,
memorized=True,
hw_memorized=True,
)
def init_device(self):
self._inverted = False
super(PIGCSAxis, self).init_device()
self.ctrl = DeviceProxy(self.controller)
ctrl_axes = self.ctrl.get_axis_names()
if self.axis in ctrl_axes:
self.set_state(DevState.ON)
self._position = 0
self._velocity = 0
self._limit = 0
self._referenced = False
self.update_attribute_config()
else:
print(f"Axis {self.axis} not in {ctrl_axes}", file=self.log_error)
self.set_state(DevState.FAULT)
def update_attribute_config(self):
vmin, vmax = self.ctrl.query_axis_limits(self.axis)
self.position.set_min_value(vmin)
self.position.set_max_value(vmax)
unit = self.ctrl.query_axis_unit(self.axis)
attr_props = self.position.get_properties()
attr_props.unit = unit
self.position.set_properties(attr_props)
def always_executed_hook(self):
state = self.ctrl.query_axis_state(self.axis)
print(f"READ STATE: {self.axis} {state}", file=self.log_debug)
self._position = state[0]
self._velocity = state[4]
self._limit = bool(state[1])
self._referenced = bool(state[3])
if state[2]:
self.set_state(DevState.MOVING)
else:
self.set_state(DevState.ON)
if not self._referenced:
self.set_state(DevState.WARN)
def read_position(self):
sign = -1 if self._inverted else 1
return sign * self._position
def write_position(self, position):
sign = -1 if self._inverted else 1
ans = self.ctrl.set_position(f"{self.axis}={sign * position}")
# print(f"SET POS: {self.axis} -> {position} (ans={ans})")
if ans == 0:
self.set_state(DevState.MOVING)
def read_limit_switch(self):
return self._limit
def read_referenced(self):
return self._referenced
def read_inverted(self):
return self._inverted
def write_inverted(self, value):
self._inverted = value
def read_velocity(self):
return self._velocity
def write_velocity(self, value):
self.ctrl.set_velocity(value)
@command
def stop(self):
"""Smoothly stop motion"""
self.ctrl.halt()
@command
def abort(self):
"""Abruptly stop all motion."""
self.ctrl.stop()
if __name__ == "__main__":
# PIGCSController.run_server()
run((PIGCSController, PIGCSAxis))