Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
408 lines (345 loc) · 13.1 KB
/
Copy path__init__.py
File metadata and controls
408 lines (345 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""
Object Dictionary module
"""
import struct
try:
from collections.abc import MutableMapping, Mapping
except ImportError:
from collections import MutableMapping, Mapping
import logging
from .datatypes import *
logger = logging.getLogger(__name__)
def import_od(source, node_id=None):
"""Parse an EDS, DCF, or EPF file.
:param source:
Path to object dictionary file or a file like object or an EPF XML tree.
:return:
An Object Dictionary instance.
:rtype: canopen.ObjectDictionary
"""
if source is None:
return ObjectDictionary()
if hasattr(source, "read"):
# File like object
filename = source.name
elif hasattr(source, "tag"):
# XML tree, probably from an EPF file
filename = "od.epf"
else:
# Path to file
filename = source
suffix = filename[filename.rfind("."):].lower()
if suffix in (".eds", ".dcf"):
from . import eds
return eds.import_eds(source, node_id)
elif suffix == ".epf":
from . import epf
return epf.import_epf(source)
else:
raise NotImplementedError("No support for this format")
class ObjectDictionary(MutableMapping):
"""Representation of the object dictionary as a Python dictionary."""
def __init__(self):
self.indices = {}
self.names = {}
#: Default bitrate if specified by file
self.bitrate = None
#: Node ID if specified by file
self.node_id = None
def __getitem__(self, index):
"""Get object from object dictionary by name or index."""
item = self.names.get(index) or self.indices.get(index)
if item is None:
name = "0x%X" % index if isinstance(index, int) else index
raise KeyError("%s was not found in Object Dictionary" % name)
return item
def __setitem__(self, index, obj):
assert index == obj.index or index == obj.name
self.add_object(obj)
def __delitem__(self, index):
obj = self[index]
del self.indices[obj.index]
del self.names[obj.name]
def __iter__(self):
return iter(sorted(self.indices))
def __len__(self):
return len(self.indices)
def __contains__(self, index):
return index in self.names or index in self.indices
def add_object(self, obj):
"""Add object to the object dictionary.
:param obj:
Should be either one of
:class:`~canopen.objectdictionary.Variable`,
:class:`~canopen.objectdictionary.Record`, or
:class:`~canopen.objectdictionary.Array`.
"""
obj.parent = self
self.indices[obj.index] = obj
self.names[obj.name] = obj
def get_variable(self, index, subindex=0):
"""Get the variable object at specified index (and subindex if applicable).
:return: Variable if found, else `None`
:rtype: canopen.objectdictionary.Variable
"""
obj = self.get(index)
if isinstance(obj, Variable):
return obj
elif isinstance(obj, (Record, Array)):
return obj.get(subindex)
class Record(MutableMapping):
"""Groups multiple :class:`~canopen.objectdictionary.Variable` objects using
subindices.
"""
#: Description for the whole record
description = ""
def __init__(self, name, index):
#: The :class:`~canopen.ObjectDictionary` owning the record.
self.parent = None
#: 16-bit address of the record
self.index = index
#: Name of record
self.name = name
self.subindices = {}
self.names = {}
def __getitem__(self, subindex):
item = self.names.get(subindex) or self.subindices.get(subindex)
if item is None:
raise KeyError("Subindex %s was not found" % subindex)
return item
def __setitem__(self, subindex, var):
assert subindex == var.subindex
self.add_member(var)
def __delitem__(self, subindex):
var = self[subindex]
del self.subindices[var.subindex]
del self.names[var.name]
def __len__(self):
return len(self.subindices)
def __iter__(self):
return iter(sorted(self.subindices))
def __contains__(self, subindex):
return subindex in self.names or subindex in self.subindices
def __eq__(self, other):
return self.index == other.index
def add_member(self, variable):
"""Adds a :class:`~canopen.objectdictionary.Variable` to the record."""
variable.parent = self
self.subindices[variable.subindex] = variable
self.names[variable.name] = variable
class Array(Mapping):
"""An array of :class:`~canopen.objectdictionary.Variable` objects using
subindices.
Actual length of array must be read from the node using SDO.
"""
#: Description for the whole array
description = ""
def __init__(self, name, index):
#: The :class:`~canopen.ObjectDictionary` owning the record.
self.parent = None
#: 16-bit address of the array
self.index = index
#: Name of array
self.name = name
self.subindices = {}
self.names = {}
def __getitem__(self, subindex):
var = self.names.get(subindex) or self.subindices.get(subindex)
if var is not None:
# This subindex is defined
pass
elif isinstance(subindex, int) and 0 < subindex < 256:
# Create a new variable based on first array item
template = self.subindices[1]
name = "%s_%x" % (template.name, subindex)
var = Variable(name, self.index, subindex)
var.parent = self
for attr in ("data_type", "unit", "factor", "min", "max", "default",
"access_type", "description", "value_descriptions",
"bit_definitions"):
if attr in template.__dict__:
var.__dict__[attr] = template.__dict__[attr]
else:
raise KeyError("Could not find subindex %r" % subindex)
return var
def __len__(self):
return len(self.subindices)
def __iter__(self):
return iter(sorted(self.subindices))
def __eq__(self, other):
return self.index == other.index
def add_member(self, variable):
"""Adds a :class:`~canopen.objectdictionary.Variable` to the record."""
variable.parent = self
self.subindices[variable.subindex] = variable
self.names[variable.name] = variable
class Variable(object):
"""Simple variable."""
STRUCT_TYPES = {
BOOLEAN: struct.Struct("?"),
INTEGER8: struct.Struct("b"),
INTEGER16: struct.Struct("<h"),
INTEGER32: struct.Struct("<l"),
INTEGER64: struct.Struct("<q"),
UNSIGNED8: struct.Struct("B"),
UNSIGNED16: struct.Struct("<H"),
UNSIGNED32: struct.Struct("<L"),
UNSIGNED64: struct.Struct("<Q"),
REAL32: struct.Struct("<f"),
REAL64: struct.Struct("<d")
}
def __init__(self, name, index, subindex=0):
#: The :class:`~canopen.ObjectDictionary`,
#: :class:`~canopen.objectdictionary.Record` or
#: :class:`~canopen.objectdictionary.Array` owning the variable
self.parent = None
#: 16-bit address of the object in the dictionary
self.index = index
#: 8-bit sub-index of the object in the dictionary
self.subindex = subindex
#: String representation of the variable
self.name = name
#: Physical unit
self.unit = ""
#: Factor between physical unit and integer value
self.factor = 1
#: Minimum allowed value
self.min = None
#: Maximum allowed value
self.max = None
#: Default value at start-up
self.default = None
#: The value of this variable stored in the object dictionary
self.value = None
#: Data type according to the standard as an :class:`int`
self.data_type = None
#: Access type, should be "rw", "ro", "wo", or "const"
self.access_type = "rw"
#: Description of variable
self.description = ""
#: Dictionary of value descriptions
self.value_descriptions = {}
#: Dictionary of bitfield definitions
self.bit_definitions = {}
def __eq__(self, other):
return (self.index == other.index and
self.subindex == other.subindex)
def __len__(self):
if self.data_type in self.STRUCT_TYPES:
return self.STRUCT_TYPES[self.data_type].size * 8
else:
return 8
@property
def writable(self):
return "w" in self.access_type
@property
def readable(self):
return "r" in self.access_type or self.access_type == "const"
def add_value_description(self, value, descr):
"""Associate a value with a string description.
:param int value: Value to describe
:param str desc: Description of value
"""
self.value_descriptions[value] = descr
def add_bit_definition(self, name, bits):
"""Associate bit(s) with a string description.
:param str name: Name of bit(s)
:param list bits: List of bits as integers
"""
self.bit_definitions[name] = bits
def decode_raw(self, data):
if self.data_type == VISIBLE_STRING:
return data.rstrip(b"\x00").decode("ascii", errors="ignore")
elif self.data_type == UNICODE_STRING:
# Is this correct?
return data.rstrip(b"\x00").decode("utf_16_le", errors="ignore")
elif self.data_type in self.STRUCT_TYPES:
try:
value, = self.STRUCT_TYPES[self.data_type].unpack(data)
return value
except struct.error:
raise ObjectDictionaryError(
"Mismatch between expected and actual data size")
else:
# Just return the data as is
return data
def encode_raw(self, value):
if isinstance(value, (bytes, bytearray)):
return value
elif self.data_type == VISIBLE_STRING:
return value.encode("ascii")
elif self.data_type == UNICODE_STRING:
# Is this correct?
return value.encode("utf_16_le")
elif self.data_type in self.STRUCT_TYPES:
if self.data_type in INTEGER_TYPES:
value = int(value)
if self.data_type in NUMBER_TYPES:
if self.min is not None and value < self.min:
logger.warning(
"Value %d is less than min value %d", value, self.min)
if self.max is not None and value > self.max:
logger.warning(
"Value %d is greater than max value %d",
value,
self.max)
try:
return self.STRUCT_TYPES[self.data_type].pack(value)
except struct.error:
raise ValueError("Value does not fit in specified type")
elif self.data_type is None:
raise ObjectDictionaryError("Data type has not been specified")
else:
raise TypeError(
"Do not know how to encode %r to data type %Xh" % (
value, self.data_type))
def decode_phys(self, value):
if self.data_type in INTEGER_TYPES:
value *= self.factor
return value
def encode_phys(self, value):
if self.data_type in INTEGER_TYPES:
value /= self.factor
value = int(round(value))
return value
def decode_desc(self, value):
if not self.value_descriptions:
raise ObjectDictionaryError("No value descriptions exist")
elif value not in self.value_descriptions:
raise ObjectDictionaryError(
"No value description exists for %d" % value)
else:
return self.value_descriptions[value]
def encode_desc(self, desc):
if not self.value_descriptions:
raise ObjectDictionaryError("No value descriptions exist")
else:
for value, description in self.value_descriptions.items():
if description == desc:
return value
valid_values = ", ".join(self.value_descriptions.values())
error_text = "No value corresponds to '%s'. Valid values are: %s"
raise ValueError(error_text % (desc, valid_values))
def decode_bits(self, value, bits):
try:
bits = self.bit_definitions[bits]
except (TypeError, KeyError):
pass
mask = 0
for bit in bits:
mask |= 1 << bit
return (value & mask) >> min(bits)
def encode_bits(self, original_value, bits, bit_value):
try:
bits = self.bit_definitions[bits]
except (TypeError, KeyError):
pass
temp = original_value
mask = 0
for bit in bits:
mask |= 1 << bit
temp &= ~mask
temp |= bit_value << min(bits)
return temp
class ObjectDictionaryError(Exception):
"""Unsupported operation with the current Object Dictionary."""