1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
import serial, string, threading
class ReceiveTimeout(Exception):
pass
class ReceiveChecksumError(Exception):
def __init__(self, expected, received):
self.expected = expected
self.received = received
self.message = "Checksum Error: expected %x, got %x" % (expected, received)
class RXBufferOverrunError(EnvironmentError):
pass
class NotNowWarning(UserWarning):
pass
class UnknownCommand(Exception):
pass
class BadValueError(Exception):
pass
class ParameterLimitsError(ValueError):
pass
class Axis:
"""
Abstraction for a IPCOMM Axis
Phytron IPCOMM devices are addressable by a 4 bit ID (i.e. range 0x0 = 0 ... 0xf = 15).
This allows for up to 16 IPCOMM devices being connected to a single communication bus.
Each device is a strict master/slave communication endpoint ultimately driving an
actuator. This is referred to as an Axis.
"""
def __init__(self, ipcomm, ID, name = None):
"""
ipcomm: a instance of Phytron.IPCOMM class, encapsulating a communication bus
name: The human readable name given to the axis (served not purpose in this class at the moment)
"""
self.ipcomm = ipcomm
self.ID = ID
self.name = name
def execute(self, cmd):
"""
Execute a command on the axis.
"""
result = self.ipcomm.execute(self.ID, cmd)
assert result.ID == self.ID
return result
def goToAbs(self, position):
pass
def goToRelative(self, offset):
pass
def runFree(self, direction):
pass
def halt(self):
pass
def stop(self):
pass
def setRunCurrent(self, current):
pass
def getRunCurrent(self):
pass
def setBoostCurrent(self, current):
pass
def getBoostCurrent(self):
pass
def setBoostDuration(self, duration):
pass
def getBoostDuration(self):
pass
def setCurrentPosition(self, position):
pass
def getCurrentPosition(self, position):
pass
def checksum(data):
chksm = 0
for d in data:
chksm = chksm ^ ord(d)
return chksm
class Status:
COLDBOOT = (1<<7)
ANY_ERROR = (1<<6)
RX_ERROR = (1<<5)
SFI_ERROR = (1<<4)
OUTPUTSTAGE_ERROR = (1<<3)
INITIATOR_MINUS = (1<<2)
INITIATOR_PLUS = (1<<1)
RUNNING = (1<<0)
def __init__(self, bitvector):
self.coldboot = not not (bitvector & Status.COLDBOOT)
self.any_error = not not (bitvector & Status.ANY_ERROR)
self.rx_error = not not (bitvector & Status.RX_ERROR)
self.SFI_error = not not (bitvector & Status.SFI_ERROR)
self.outputstage_error = not not (bitvector & Status.OUTPUTSTAGE_ERROR)
self.initiator_minus = not not (bitvector & Status.INITIATOR_MINUS)
self.initiator_plus = not not (bitvector & Status.INITIATOR_PLUS)
self.running = not not (bitvector & Status.RUNNING)
def __str__(self):
status = list()
if self.coldboot:
status += ['Cold Boot']
if self.any_error:
status += ['Any Error']
if self.rx_error:
status += ['RX Error']
if self.SFI_error:
status += ['SFI Error']
if self.outputstage_error:
status += ['Output Stage Error']
if self.initiator_minus:
status += ['Initiator -']
if self.initiator_plus:
status += ['Initiator +']
if self.running:
status += ['Running']
return '{'+ ('|'.join(status)) + '}'
class ExtendedStatus:
CHECKSUM_ERROR = (1<<23)
# (1<<22)
RXBUFFER_OVERRUN = (1<<21)
NOT_NOW = (1<<20)
UNKNOWN_COMMAND = (1<<19)
BAD_VALUE = (1<<18)
PARAMETER_LIMITS = (1<<17)
# (1<<16)
NO_SYSTEM = (1<<15)
NO_RAMPS = (1<<14)
PARAMETER_CHANGED = (1<<13)
BUSY = (1<<12)
PROGRAMING_ERROR = (1<<11)
HIGH_TEMPERATURE = (1<<10)
INITIATOR_ERROR = (1<< 9)
INTERNAL_ERROR = (1<< 8)
DRIVER_ERROR = (1<< 7)
# (1<< 6)
WAIT_FOR_SYNC = (1<< 5)
LINEAR_AXIS = (1<< 4)
FREE_RUNNING = (1<< 3)
INITIALIZED = (1<< 2)
HW_DISABLE = (1<< 1)
INITIALIZING = (1<< 0)
def __init__(self, bitvector):
self.checksum_error = not not (bitvector & ExtendedStatus.CHECKSUM_ERROR)
self.rxbuffer_overrun = not not (bitvector & ExtendedStatus.RXBUFFER_OVERRUN)
self.not_now = not not (bitvector & ExtendedStatus.NOT_NOW)
self.unknown_command = not not (bitvector & ExtendedStatus.UNKNOWN_COMMAND)
self.bad_value = not not (bitvector & ExtendedStatus.BAD_VALUE)
self.parameter_limits = not not (bitvector & ExtendedStatus.PARAMETER_LIMITS)
self.no_system = not not (bitvector & ExtendedStatus.NO_SYSTEM)
self.no_ramps = not not (bitvector & ExtendedStatus.NO_RAMPS)
self.parameter_changed = not not (bitvector & ExtendedStatus.PARAMETER_CHANGED)
self.busy = not not (bitvector & ExtendedStatus.BUSY)
self.programing_error = not not (bitvector & ExtendedStatus.PROGRAMING_ERROR)
self.high_temperature = not not (bitvector & ExtendedStatus.HIGH_TEMPERATURE)
self.initiator_error = not not (bitvector & ExtendedStatus.INITIATOR_ERROR)
self.internal_error = not not (bitvector & ExtendedStatus.INTERNAL_ERROR)
self.driver_error = not not (bitvector & ExtendedStatus.DRIVER_ERROR)
self.wait_for_sync = not not (bitvector & ExtendedStatus.WAIT_FOR_SYNC)
self.linear_axis = not not (bitvector & ExtendedStatus.LINEAR_AXIS)
self.free_running = not not (bitvector & ExtendedStatus.FREE_RUNNING)
self.initialized = not not (bitvector & ExtendedStatus.INITIALIZED)
self.hw_disable = not not (bitvector & ExtendedStatus.HW_DISABLE)
self.initializing = not not (bitvector & ExtendedStatus.INITIALIZING)
def __str__(self):
status = list()
if self.checksum_error:
status += ["Checksum Error"]
if self.rxbuffer_overrun:
status += ["RX Buffer Overrun"]
if self.not_now:
status += ["Not Now"]
if self.unknown_command:
status += ["Unknown Command"]
if self.bad_value:
status += ["Bad Value"]
if self.parameter_limits:
status += ["Parameter Limits"]
if self.no_system:
status += ["Mo System"]
if self.no_ramps:
status += ["No Ramps"]
if self.parameter_changed:
status += ["Parameter Changed"]
if self.busy:
status += ["Busy"]
if self.programing_error:
status += ["Programming Error"]
if self.high_temperature:
status += ["High Temperature"]
if self.initiator_error:
status += ["Initiator Error"]
if self.driver_error:
status += ["Driver Error"]
if self.wait_for_sync:
status += ["Wait For Sync"]
if self.linear_axis:
status += ["Linear Axis"]
if self.free_running:
status += ["Free Running"]
if self.initialized:
status += ["Initialized"]
if self.hw_disable:
status += ["HW Disable"]
if self.initializing:
status += ["Initialzing"]
return '{'+ ('|'.join(status)) + '}'
class ReceiveData:
def __init__(self, ID, status, data):
self.ID = ID
self.status = status
self.data = data
class IPCOMM:
MAX_RETRY_COUNT = 5
def __init__(self, url, baudrate = 38400, axes=range(0x10), axisnames = None):
self.rlock = threading.RLock()
self.conn = serial.serial_for_url(url)
self.conn.baudrate = baudrate
self.conn.parity = serial.PARITY_NONE
self.conn.rtscts = False
self.conn.dsrdtr = False
self.conn.xonxoff = False
self.conn.timeout = 0.5
self.axisByID = dict()
self.axisByName = dict()
self.enumerate(axes, axisnames)
self.max_retry_count = IPCOMM.MAX_RETRY_COUNT
def axis(self, nameOrID):
if isinstance(nameOrID, str) and nameOrID.isalpha():
return self.axisByName[nameOrID]
return self.axisByID[int(nameOrID)]
def enumerate(self, axes=range(0x10), names=None):
# Use a only short timeout for enumeration.
oldtimeout = self.conn.timeout
self.conn.timeout = 0.05
self.axisByID.clear()
self.axisByName.clear()
for i,ID in enumerate(axes):
try:
if self.execute(ID, 'IS?').ID == ID:
if ((isinstance(names, dict) and names.haskey(ID)) or (isinstance(names, list) and i < len(names))) and names[i].isalpha():
if (isinstance(names, dict):
axisname = names[ID]
else:
axisname = names[i]
axis = self.axisByName[str(axisname)] = Axis(self, ID, axisname)
else:
axis = Axis(self, ID)
self.axisByID[ID] = axis
except ReceiveTimeout:
continue
self.conn.timeout = oldtimeout
def send(self, data):
self.conn.write('\x02' + data + ':' + ('%02X' % checksum(data + ':')) + '\x03')
self.conn.flush()
return self
def recv(self):
buf = ''
c = None
while c != '\x02':
c = self.conn.read(1)
if not c:
raise ReceiveTimeout()
c = None
while c != '\x03':
c = self.conn.read(1)
if not c:
raise ReceiveTimeout()
buf += c
status, data, chksm = buf[:-1].split(':')
expected_chksm = checksum(status + ':' + data + ':')
chksm = string.atoi(chksm, 0x10)
if expected_chksm != chksm:
raise ReceiveChecksumError(expected_chksm, chksm)
return ReceiveData( ID = string.atoi(status[0], 0x10),
status = Status(string.atoi(status[1:], 0x10)),
data = data )
def broadcast(self, cmd):
self.conn.flushInput()
self.send( '@' + cmd )
def execute(self, ID, cmd):
if cmd == 'IS?':
return self.queryextendedstatus(ID)
recv_data = None
with self.rlock:
self.conn.flushInput()
self.send( ('%X' % ID) + cmd )
retry_count = 0
while not recv_data and retry_count < self.max_retry_count:
try:
recv_data = self.recv()
except ReceiveChecksumError:
self.send( ('%X' % ID) + 'R')
retry_count += 1
if recv_data.status.rx_error:
extended_status = self.queryextendedstatus(ID).data
if extended_status.checksum_error:
self.conn.flushInput()
self.send( ('%X' % ID) + cmd )
recv_data = None
continue
if extended_status.rxbuffer_overrun:
raise RXBufferOverrunError()
if extended_status.not_now:
raise NotNowWarning()
if extended_status.unknown_command:
raise UnknownCommand()
if extended_status.bad_value:
raise BadValueError()
if extended_status.parameter_limits:
raise ParameterLimitsError()
return recv_data
def queryextendedstatus(self, ID):
"""
Special function for querying the extended status.
Same basic structure like execute, with the following exceptions:
* does not take a command (always issued a IS?)
* will not resend query if status rx_error is reported
* will not request reply retransmission
* will not raise status related exceptions
Since regular execute will raise exceptions based on IS? status
to query the status for further processing, this method must be
used instead to avoid infinite loops.
If the extended status can not be requested, None is returned.
"""
recv_data = None
with self.rlock:
self.conn.flushInput()
self.send( ('%X' % ID) + 'IS?' )
try:
recv_data = self.recv()
except ReceiveChecksumError:
return None
if recv_data:
recv_data.data = ExtendedStatus(string.atoi(recv_data.data, 0x10))
else:
recv_data = None
return recv_data
|