author | alexander@60.125.16.172.in-addr.arpa |
Tue, 23 Aug 2016 10:24:47 +0500 | |
changeset 1518 | a656ccb868d4 |
parent 1278 | 74afc7e86d00 |
child 1571 | 486f94a8032c |
permissions | -rw-r--r-- |
58 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
from types import * |
|
26 |
||
27 |
# Translation between IEC types and Can Open types |
|
28 |
IECToCOType = {"BOOL":0x01, "SINT":0x02, "INT":0x03,"DINT":0x04,"LINT":0x10, |
|
29 |
"USINT":0x05,"UINT":0x06,"UDINT":0x07,"ULINT":0x1B,"REAL":0x08, |
|
30 |
"LREAL":0x11,"STRING":0x09,"BYTE":0x05,"WORD":0x06,"DWORD":0x07, |
|
31 |
"LWORD":0x1B,"WSTRING":0x0B} |
|
32 |
||
33 |
# Constants for PDO types |
|
34 |
RPDO = 1 |
|
35 |
TPDO = 2 |
|
36 |
||
37 |
SlavePDOType = {"I" : TPDO, "Q" : RPDO} |
|
38 |
InvertPDOType = {RPDO : TPDO, TPDO : RPDO} |
|
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
39 |
PDOTypeBaseIndex = {RPDO : 0x1400, TPDO : 0x1800} |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
40 |
PDOTypeBaseCobId = {RPDO : 0x200, TPDO : 0x180} |
58 | 41 |
|
42 |
VariableIncrement = 0x100 |
|
43 |
VariableStartIndex = {TPDO : 0x2000, RPDO : 0x4000} |
|
44 |
VariableDirText = {TPDO : "__I", RPDO : "__Q"} |
|
45 |
VariableTypeOffset = dict(zip(["","X","B","W","D","L"], range(6))) |
|
46 |
||
47 |
TrashVariables = [(1, 0x01), (8, 0x05), (16, 0x06), (32, 0x07), (64, 0x1B)] |
|
48 |
||
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
49 |
#------------------------------------------------------------------------------- |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
50 |
# Specific exception for PDO mapping errors |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
51 |
#------------------------------------------------------------------------------- |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
52 |
|
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
53 |
class PDOmappingException(Exception): |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
54 |
pass |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
55 |
|
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
56 |
|
58 | 57 |
def LE_to_BE(value, size): |
58 |
""" |
|
59 |
Convert Little Endian to Big Endian |
|
60 |
@param value: value expressed in integer |
|
61 |
@param size: number of bytes generated |
|
62 |
@return: a string containing the value converted |
|
63 |
""" |
|
64 |
||
65 |
data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value |
|
66 |
list_car = [data[i:i+2] for i in xrange(0, len(data), 2)] |
|
67 |
list_car.reverse() |
|
68 |
return "".join([chr(int(car, 16)) for car in list_car]) |
|
69 |
||
70 |
||
71 |
def GetNodePDOIndexes(node, type, parameters = False): |
|
72 |
""" |
|
73 |
Find the PDO indexes of a node |
|
74 |
@param node: node |
|
75 |
@param type: type of PDO searched (RPDO or TPDO or both) |
|
76 |
@param parameters: indicate which indexes are expected (PDO paramaters : True or PDO mappings : False) |
|
77 |
@return: a list of indexes found |
|
78 |
""" |
|
79 |
||
80 |
indexes = [] |
|
81 |
if type & RPDO: |
|
82 |
indexes.extend([idx for idx in node.GetIndexes() if 0x1400 <= idx <= 0x15FF]) |
|
83 |
if type & TPDO: |
|
84 |
indexes.extend([idx for idx in node.GetIndexes() if 0x1800 <= idx <= 0x19FF]) |
|
85 |
if not parameters: |
|
86 |
return [idx + 0x200 for idx in indexes] |
|
87 |
else: |
|
88 |
return indexes |
|
89 |
||
90 |
||
91 |
def SearchNodePDOMapping(loc_infos, node): |
|
92 |
""" |
|
93 |
Find the PDO indexes of a node |
|
94 |
@param node: node |
|
95 |
@param type: type of PDO searched (RPDO or TPDO or both) |
|
96 |
@param parameters: indicate which indexes are expected (PDO paramaters : True or PDO mappings : False) |
|
97 |
@return: a list of indexes found |
|
98 |
""" |
|
99 |
||
270 | 100 |
model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) |
58 | 101 |
|
102 |
for PDOidx in GetNodePDOIndexes(node, loc_infos["pdotype"]): |
|
103 |
values = node.GetEntry(PDOidx) |
|
104 |
if values != None: |
|
105 |
for subindex, mapping in enumerate(values): |
|
270 | 106 |
if subindex != 0 and mapping & 0xFFFFFF00 == model: |
58 | 107 |
return PDOidx, subindex |
108 |
return None |
|
109 |
||
110 |
||
111 |
def GeneratePDOMappingDCF(idx, cobid, transmittype, pdomapping): |
|
112 |
""" |
|
113 |
Build concise DCF value for configuring a PDO |
|
114 |
@param idx: index of PDO parameters |
|
115 |
@param cobid: PDO generated COB ID |
|
116 |
@param transmittype : PDO transmit type |
|
117 |
@param pdomapping: list of PDO mappings |
|
118 |
@return: a tuple of value and number of parameters to add to DCF |
|
119 |
""" |
|
120 |
||
1278
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
121 |
dcfdata=[] |
58 | 122 |
# Create entry for RPDO or TPDO parameters and Disable PDO |
1278
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
123 |
# ---- INDEX ----- --- SUBINDEX ---- ----- SIZE ------ ------ DATA ------ |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
124 |
dcfdata += [LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x80000000 + cobid, 4)] |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
125 |
# Set Transmit type |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
126 |
dcfdata += [LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(transmittype, 1)] |
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
127 |
if len(pdomapping) > 0: |
1278
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
128 |
# Disable Mapping |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
129 |
dcfdata += [LE_to_BE(idx + 0x200, 2) + LE_to_BE(0x00, 1) + LE_to_BE(0x01, 4) + LE_to_BE(0x00, 1)] |
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
130 |
# Map Variables |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
131 |
for subindex, (name, loc_infos) in enumerate(pdomapping): |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
132 |
value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"] |
1278
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
133 |
dcfdata += [LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4)] |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
134 |
# Re-enable Mapping |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
135 |
dcfdata += [LE_to_BE(idx + 0x200, 2) + LE_to_BE(0x00, 1) + LE_to_BE(0x01, 4) + LE_to_BE(len(pdomapping), 1)] |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
136 |
# Re-Enable PDO |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
137 |
dcfdata += [LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(cobid, 4)] |
74afc7e86d00
CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents:
721
diff
changeset
|
138 |
return "".join(dcfdata), len(dcfdata) |
58 | 139 |
|
140 |
class ConciseDCFGenerator: |
|
141 |
||
142 |
def __init__(self, nodelist, nodename): |
|
143 |
# Dictionary of location informations classed by name |
|
144 |
self.IECLocations = {} |
|
145 |
# Dictionary of location that have not been mapped yet |
|
146 |
self.LocationsNotMapped = {} |
|
147 |
# Dictionary of location informations classed by name |
|
148 |
self.MasterMapping = {} |
|
149 |
# List of COB IDs available |
|
150 |
self.ListCobIDAvailable = range(0x180, 0x580) |
|
151 |
# Dictionary of mapping value where unexpected variables are stored |
|
152 |
self.TrashVariables = {} |
|
163
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
153 |
# Dictionary of pointed variables |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
154 |
self.PointedVariables = {} |
58 | 155 |
|
156 |
self.NodeList = nodelist |
|
157 |
self.Manager = self.NodeList.Manager |
|
158 |
self.MasterNode = self.Manager.GetCurrentNodeCopy() |
|
159 |
self.MasterNode.SetNodeName(nodename) |
|
160 |
self.PrepareMasterNode() |
|
161 |
||
163
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
162 |
def GetPointedVariables(self): |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
163 |
return self.PointedVariables |
58 | 164 |
|
165 |
def RemoveUsedNodeCobId(self, node): |
|
166 |
""" |
|
167 |
Remove all PDO COB ID used by the given node from the list of available COB ID |
|
168 |
@param node: node |
|
169 |
@return: a tuple of number of RPDO and TPDO for the node |
|
170 |
""" |
|
171 |
||
172 |
# Get list of all node TPDO and RPDO indexes |
|
173 |
nodeRpdoIndexes = GetNodePDOIndexes(node, RPDO, True) |
|
174 |
nodeTpdoIndexes = GetNodePDOIndexes(node, TPDO, True) |
|
175 |
||
176 |
# Mark all the COB ID of the node already mapped PDO as not available |
|
177 |
for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes: |
|
178 |
pdo_cobid = node.GetEntry(PdoIdx, 0x01) |
|
179 |
# Extract COB ID, if PDO isn't active |
|
180 |
if pdo_cobid > 0x600 : |
|
181 |
pdo_cobid -= 0x80000000 |
|
182 |
# Remove COB ID from the list of available COB ID |
|
183 |
if pdo_cobid in self.ListCobIDAvailable: |
|
184 |
self.ListCobIDAvailable.remove(pdo_cobid) |
|
185 |
||
186 |
return len(nodeRpdoIndexes), len(nodeTpdoIndexes) |
|
187 |
||
188 |
||
189 |
def PrepareMasterNode(self): |
|
190 |
""" |
|
191 |
Add mandatory entries for DCF generation into MasterNode. |
|
192 |
""" |
|
193 |
||
194 |
# Adding DCF entry into Master node |
|
195 |
if not self.MasterNode.IsEntry(0x1F22): |
|
196 |
self.MasterNode.AddEntry(0x1F22, 1, "") |
|
197 |
self.Manager.AddSubentriesToCurrent(0x1F22, 127, self.MasterNode) |
|
198 |
||
199 |
# Adding trash mappable variables for unused mapped datas |
|
200 |
idxTrashVariables = 0x2000 + self.MasterNode.GetNodeID() |
|
201 |
# Add an entry for storing unexpected all variable |
|
202 |
self.Manager.AddMapVariableToCurrent(idxTrashVariables, self.MasterNode.GetNodeName()+"_trashvariables", 3, len(TrashVariables), self.MasterNode) |
|
203 |
for subidx, (size, typeidx) in enumerate(TrashVariables): |
|
204 |
# Add a subentry for storing unexpected variable of this size |
|
205 |
self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None, self.MasterNode) |
|
206 |
self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, self.MasterNode) |
|
207 |
# Store the mapping value for this entry |
|
208 |
self.TrashVariables[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size |
|
209 |
||
210 |
RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode) |
|
211 |
||
212 |
# Store the indexes of the first RPDO and TPDO available for MasterNode |
|
213 |
self.CurrentPDOParamsIdx = {RPDO : 0x1400 + RPDOnumber, TPDO : 0x1800 + TPDOnumber} |
|
214 |
||
215 |
# Prepare MasterNode with all nodelist slaves |
|
216 |
for idx, (nodeid, nodeinfos) in enumerate(self.NodeList.SlaveNodes.items()): |
|
217 |
node = nodeinfos["Node"] |
|
218 |
node.SetNodeID(nodeid) |
|
219 |
||
220 |
RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(node) |
|
221 |
||
222 |
# Get Slave's default SDO server parameters |
|
223 |
RSDO_cobid = node.GetEntry(0x1200,0x01) |
|
224 |
if not RSDO_cobid: |
|
225 |
RSDO_cobid = 0x600 + nodeid |
|
226 |
TSDO_cobid = node.GetEntry(0x1200,0x02) |
|
227 |
if not TSDO_cobid: |
|
228 |
TSDO_cobid = 0x580 + nodeid |
|
229 |
||
230 |
# Configure Master's SDO parameters entries |
|
231 |
self.Manager.ManageEntriesOfCurrent([0x1280 + idx], [], self.MasterNode) |
|
232 |
self.MasterNode.SetEntry(0x1280 + idx, 0x01, RSDO_cobid) |
|
233 |
self.MasterNode.SetEntry(0x1280 + idx, 0x02, TSDO_cobid) |
|
234 |
self.MasterNode.SetEntry(0x1280 + idx, 0x03, nodeid) |
|
235 |
||
236 |
||
237 |
def GetMasterNode(self): |
|
238 |
""" |
|
239 |
Return MasterNode. |
|
240 |
""" |
|
241 |
return self.MasterNode |
|
242 |
||
243 |
def AddParamsToDCF(self, nodeid, data, nbparams): |
|
244 |
""" |
|
155 | 245 |
Add entry to DCF, for the requested nodeID |
58 | 246 |
@param nodeid: id of the slave (int) |
247 |
@param data: data to add to slave DCF (string) |
|
248 |
@param nbparams: number of params added to slave DCF (int) |
|
249 |
""" |
|
250 |
# Get current DCF for slave |
|
251 |
nodeDCF = self.MasterNode.GetEntry(0x1F22, nodeid) |
|
252 |
||
253 |
# Extract data and number of params in current DCF |
|
254 |
if nodeDCF != None and nodeDCF != '': |
|
255 |
tmpnbparams = [i for i in nodeDCF[:4]] |
|
256 |
tmpnbparams.reverse() |
|
257 |
nbparams += int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16) |
|
258 |
data = nodeDCF[4:] + data |
|
259 |
||
260 |
# Build new DCF |
|
261 |
dcf = LE_to_BE(nbparams, 0x04) + data |
|
262 |
# Set new DCF for slave |
|
263 |
self.MasterNode.SetEntry(0x1F22, nodeid, dcf) |
|
264 |
||
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
265 |
def GetEmptyPDO(self, nodeid, pdotype, start_index=None): |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
266 |
""" |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
267 |
Search a not configured PDO for a slave |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
268 |
@param node: the slave node object |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
269 |
@param pdotype: type of PDO to generated (RPDO or TPDO) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
270 |
@param start_index: Index where search must start (default: None) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
271 |
@return tuple of PDO index, COB ID and number of subindex defined |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
272 |
""" |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
273 |
# If no start_index defined, start with PDOtype base index |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
274 |
if start_index is None: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
275 |
index = PDOTypeBaseIndex[pdotype] |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
276 |
else: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
277 |
index = start_index |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
278 |
|
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
279 |
# Search for all PDO possible index until find a configurable PDO |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
280 |
# starting from start_index |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
281 |
while index < PDOTypeBaseIndex[pdotype] + 0x200: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
282 |
values = self.NodeList.GetSlaveNodeEntry(nodeid, index + 0x200) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
283 |
if values != None and values[0] > 0: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
284 |
# Check that all subindex upper than 0 equal 0 => configurable PDO |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
285 |
if reduce(lambda x, y: x and y, map(lambda x: x == 0, values[1:]), True): |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
286 |
cobid = self.NodeList.GetSlaveNodeEntry(nodeid, index, 1) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
287 |
# If no COB ID defined in PDO, generate a new one (not used) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
288 |
if cobid == 0: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
289 |
if len(self.ListCobIDAvailable) == 0: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
290 |
return None |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
291 |
# Calculate COB ID from standard values |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
292 |
if index < PDOTypeBaseIndex[pdotype] + 4: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
293 |
cobid = PDOTypeBaseCobId[pdotype] + 0x100 * (index - PDOTypeBaseIndex[pdotype]) + nodeid |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
294 |
if cobid not in self.ListCobIDAvailable: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
295 |
cobid = self.ListCobIDAvailable.pop(0) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
296 |
return index, cobid, values[0] |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
297 |
index += 1 |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
298 |
return None |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
299 |
|
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
300 |
def AddPDOMapping(self, nodeid, pdotype, pdoindex, pdocobid, pdomapping, sync_TPDOs): |
58 | 301 |
""" |
155 | 302 |
Record a new mapping request for a slave, and add related slave config to the DCF |
58 | 303 |
@param nodeid: id of the slave (int) |
304 |
@param pdotype: type of PDO to generated (RPDO or TPDO) |
|
305 |
@param pdomapping: list od variables to map with PDO |
|
306 |
""" |
|
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
307 |
# Add an entry to MasterMapping |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
308 |
self.MasterMapping[pdocobid] = {"type" : InvertPDOType[pdotype], |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
309 |
"mapping" : [None] + [(loc_infos["type"], name) for name, loc_infos in pdomapping]} |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
310 |
|
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
311 |
# Return the data to add to DCF |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
312 |
if sync_TPDOs: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
313 |
return GeneratePDOMappingDCF(pdoindex, pdocobid, 0x01, pdomapping) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
314 |
else: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
315 |
return GeneratePDOMappingDCF(pdoindex, pdocobid, 0xFF, pdomapping) |
58 | 316 |
return 0, "" |
317 |
||
318 |
def GenerateDCF(self, locations, current_location, sync_TPDOs): |
|
319 |
""" |
|
320 |
Generate Concise DCF of MasterNode for the locations list given |
|
321 |
@param locations: list of locations to be mapped |
|
322 |
@param current_location: tuple of the located prefixes not to be considered |
|
323 |
@param sync_TPDOs: indicate if TPDO must be synchronous |
|
324 |
""" |
|
325 |
||
326 |
#------------------------------------------------------------------------------- |
|
327 |
# Verify that locations correspond to real slave variables |
|
328 |
#------------------------------------------------------------------------------- |
|
329 |
||
330 |
# Get list of locations check if exists and mappables -> put them in IECLocations |
|
331 |
for location in locations: |
|
332 |
COlocationtype = IECToCOType[location["IEC_TYPE"]] |
|
333 |
name = location["NAME"] |
|
334 |
if name in self.IECLocations: |
|
335 |
if self.IECLocations[name]["type"] != COlocationtype: |
|
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
361
diff
changeset
|
336 |
raise PDOmappingException, _("Type conflict for location \"%s\"") % name |
58 | 337 |
else: |
338 |
# Get only the part of the location that concern this node |
|
339 |
loc = location["LOC"][len(current_location):] |
|
340 |
# loc correspond to (ID, INDEX, SUBINDEX [,BIT]) |
|
166 | 341 |
if len(loc) not in (2, 3, 4): |
361 | 342 |
raise PDOmappingException, _("Bad location size : %s") % str(loc) |
166 | 343 |
elif len(loc) == 2: |
344 |
continue |
|
58 | 345 |
|
346 |
direction = location["DIR"] |
|
347 |
||
348 |
sizelocation = location["SIZE"] |
|
349 |
||
350 |
# Extract and check nodeid |
|
351 |
nodeid, index, subindex = loc[:3] |
|
352 |
||
353 |
# Check Id is in slave node list |
|
354 |
if nodeid not in self.NodeList.SlaveNodes.keys(): |
|
361 | 355 |
raise PDOmappingException, _("Non existing node ID : %d (variable %s)") % (nodeid,name) |
58 | 356 |
|
357 |
# Get the model for this node (made from EDS) |
|
358 |
node = self.NodeList.SlaveNodes[nodeid]["Node"] |
|
359 |
||
360 |
# Extract and check index and subindex |
|
361 |
if not node.IsEntry(index, subindex): |
|
361 | 362 |
raise PDOmappingException, _("No such index/subindex (%x,%x) in ID : %d (variable %s)") % (index,subindex,nodeid,name) |
58 | 363 |
|
364 |
# Get the entry info |
|
365 |
subentry_infos = node.GetSubentryInfos(index, subindex) |
|
366 |
||
367 |
# If a PDO mappable |
|
368 |
if subentry_infos and subentry_infos["pdo"]: |
|
369 |
if sizelocation == "X" and len(loc) > 3: |
|
61 | 370 |
numbit = loc[3] |
58 | 371 |
elif sizelocation != "X" and len(loc) > 3: |
361 | 372 |
raise PDOmappingException, _("Cannot set bit offset for non bool '%s' variable (ID:%d,Idx:%x,sIdx:%x))") % (name,nodeid,index,subindex) |
58 | 373 |
else: |
374 |
numbit = None |
|
375 |
||
166 | 376 |
if location["IEC_TYPE"] != "BOOL" and subentry_infos["type"] != COlocationtype: |
361 | 377 |
raise PDOmappingException, _("Invalid type \"%s\"-> %d != %d for location\"%s\"") % (location["IEC_TYPE"], COlocationtype, subentry_infos["type"] , name) |
58 | 378 |
|
379 |
typeinfos = node.GetEntryInfos(COlocationtype) |
|
380 |
self.IECLocations[name] = {"type":COlocationtype, "pdotype":SlavePDOType[direction], |
|
381 |
"nodeid": nodeid, "index": index,"subindex": subindex, |
|
382 |
"bit": numbit, "size": typeinfos["size"], "sizelocation": sizelocation} |
|
383 |
else: |
|
361 | 384 |
raise PDOmappingException, _("Not PDO mappable variable : '%s' (ID:%d,Idx:%x,sIdx:%x))") % (name,nodeid,index,subindex) |
58 | 385 |
|
386 |
#------------------------------------------------------------------------------- |
|
387 |
# Search for locations already mapped |
|
388 |
#------------------------------------------------------------------------------- |
|
389 |
||
390 |
for name, locationinfos in self.IECLocations.items(): |
|
391 |
node = self.NodeList.SlaveNodes[locationinfos["nodeid"]]["Node"] |
|
392 |
||
393 |
# Search if slave has a PDO mapping this locations |
|
394 |
result = SearchNodePDOMapping(locationinfos, node) |
|
395 |
if result != None: |
|
396 |
index, subindex = result |
|
397 |
# Get COB ID of the PDO |
|
398 |
cobid = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1) |
|
399 |
||
400 |
# Add PDO to MasterMapping |
|
401 |
if cobid not in self.MasterMapping.keys(): |
|
80 | 402 |
# Verify that PDO transmit type is conform to sync_TPDOs |
403 |
transmittype = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 2) |
|
404 |
if sync_TPDOs and transmittype != 0x01 or transmittype != 0xFF: |
|
405 |
if sync_TPDOs: |
|
406 |
# Change TransmitType to SYNCHRONE |
|
407 |
data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0x01, []) |
|
408 |
else: |
|
409 |
# Change TransmitType to ASYCHRONE |
|
410 |
data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0xFF, []) |
|
411 |
||
412 |
# Add entry to slave dcf to change transmit type of |
|
413 |
self.AddParamsToDCF(locationinfos["nodeid"], data, nbparams) |
|
414 |
||
58 | 415 |
mapping = [None] |
416 |
values = node.GetEntry(index) |
|
417 |
# Store the size of each entry mapped in PDO |
|
418 |
for value in values[1:]: |
|
78 | 419 |
if value != 0: |
420 |
mapping.append(value % 0x100) |
|
58 | 421 |
self.MasterMapping[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping} |
422 |
||
423 |
# Indicate that this PDO entry must be saved |
|
270 | 424 |
if locationinfos["bit"] is not None: |
425 |
if not isinstance(self.MasterMapping[cobid]["mapping"][subindex], ListType): |
|
426 |
self.MasterMapping[cobid]["mapping"][subindex] = [1] * self.MasterMapping[cobid]["mapping"][subindex] |
|
427 |
if locationinfos["bit"] < len(self.MasterMapping[cobid]["mapping"][subindex]): |
|
428 |
self.MasterMapping[cobid]["mapping"][subindex][locationinfos["bit"]] = (locationinfos["type"], name) |
|
429 |
else: |
|
430 |
self.MasterMapping[cobid]["mapping"][subindex] = (locationinfos["type"], name) |
|
58 | 431 |
|
432 |
else: |
|
433 |
# Add location to those that haven't been mapped yet |
|
434 |
if locationinfos["nodeid"] not in self.LocationsNotMapped.keys(): |
|
435 |
self.LocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []} |
|
436 |
self.LocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos)) |
|
437 |
||
438 |
#------------------------------------------------------------------------------- |
|
439 |
# Build concise DCF for the others locations |
|
440 |
#------------------------------------------------------------------------------- |
|
441 |
||
442 |
for nodeid, locations in self.LocationsNotMapped.items(): |
|
61 | 443 |
node = self.NodeList.SlaveNodes[nodeid]["Node"] |
58 | 444 |
|
445 |
# Initialize number of params and data to add to node DCF |
|
446 |
nbparams = 0 |
|
447 |
dataparams = "" |
|
448 |
||
449 |
# Generate the best PDO mapping for each type of PDO |
|
450 |
for pdotype in (TPDO, RPDO): |
|
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
451 |
if len(locations[pdotype]) > 0: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
452 |
pdosize = 0 |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
453 |
pdomapping = [] |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
454 |
result = self.GetEmptyPDO(nodeid, pdotype) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
455 |
if result is None: |
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
361
diff
changeset
|
456 |
raise PDOmappingException, _("Unable to define PDO mapping for node %02x") % nodeid |
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
457 |
pdoindex, pdocobid, pdonbparams = result |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
458 |
for name, loc_infos in locations[pdotype]: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
459 |
pdosize += loc_infos["size"] |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
460 |
# If pdo's size > 64 bits |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
461 |
if pdosize > 64 or len(pdomapping) >= pdonbparams: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
462 |
# Generate a new PDO Mapping |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
463 |
data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdoindex, pdocobid, pdomapping, sync_TPDOs) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
464 |
dataparams += data |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
465 |
nbparams += nbaddedparams |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
466 |
pdosize = loc_infos["size"] |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
467 |
pdomapping = [(name, loc_infos)] |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
468 |
result = self.GetEmptyPDO(nodeid, pdotype, pdoindex + 1) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
469 |
if result is None: |
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
361
diff
changeset
|
470 |
raise PDOmappingException, _("Unable to define PDO mapping for node %02x") % nodeid |
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
471 |
pdoindex, pdocobid, pdonbparams = result |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
472 |
else: |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
473 |
pdomapping.append((name, loc_infos)) |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
474 |
# If there isn't locations yet but there is still a PDO to generate |
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
475 |
if len(pdomapping) > 0: |
58 | 476 |
# Generate a new PDO Mapping |
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
477 |
data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdoindex, pdocobid, pdomapping, sync_TPDOs) |
58 | 478 |
dataparams += data |
479 |
nbparams += nbaddedparams |
|
340
651b8fb572e7
Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents:
307
diff
changeset
|
480 |
|
58 | 481 |
# Add number of params and data to node DCF |
482 |
self.AddParamsToDCF(nodeid, dataparams, nbparams) |
|
483 |
||
484 |
#------------------------------------------------------------------------------- |
|
485 |
# Master Node Configuration |
|
486 |
#------------------------------------------------------------------------------- |
|
487 |
||
488 |
# Generate Master's Configuration from informations stored in MasterMapping |
|
489 |
for cobid, pdo_infos in self.MasterMapping.items(): |
|
490 |
# Get next PDO index in MasterNode for this PDO type |
|
491 |
current_idx = self.CurrentPDOParamsIdx[pdo_infos["type"]] |
|
492 |
||
493 |
# Search if there is already a PDO in MasterNode with this cob id |
|
494 |
for idx in GetNodePDOIndexes(self.MasterNode, pdo_infos["type"], True): |
|
495 |
if self.MasterNode.GetEntry(idx, 1) == cobid: |
|
496 |
current_idx = idx |
|
497 |
||
498 |
# Add a PDO to MasterNode if not PDO have been found |
|
499 |
if current_idx == self.CurrentPDOParamsIdx[pdo_infos["type"]]: |
|
500 |
addinglist = [current_idx, current_idx + 0x200] |
|
501 |
self.Manager.ManageEntriesOfCurrent(addinglist, [], self.MasterNode) |
|
502 |
self.MasterNode.SetEntry(current_idx, 0x01, cobid) |
|
503 |
||
504 |
# Increment the number of PDO for this PDO type |
|
505 |
self.CurrentPDOParamsIdx[pdo_infos["type"]] += 1 |
|
506 |
||
507 |
# Change the transmit type of the PDO |
|
508 |
if sync_TPDOs: |
|
509 |
self.MasterNode.SetEntry(current_idx, 0x02, 0x01) |
|
510 |
else: |
|
511 |
self.MasterNode.SetEntry(current_idx, 0x02, 0xFF) |
|
512 |
||
270 | 513 |
mapping = [] |
514 |
for item in pdo_infos["mapping"]: |
|
515 |
if isinstance(item, ListType): |
|
516 |
mapping.extend(item) |
|
517 |
else: |
|
518 |
mapping.append(item) |
|
519 |
||
58 | 520 |
# Add some subentries to PDO mapping if there is not enough |
270 | 521 |
if len(mapping) > 1: |
522 |
self.Manager.AddSubentriesToCurrent(current_idx + 0x200, len(mapping) - 1, self.MasterNode) |
|
58 | 523 |
|
524 |
# Generate MasterNode's PDO mapping |
|
270 | 525 |
for subindex, variable in enumerate(mapping): |
58 | 526 |
if subindex == 0: |
527 |
continue |
|
528 |
new_index = False |
|
529 |
||
225 | 530 |
if isinstance(variable, (IntType, LongType)): |
58 | 531 |
# If variable is an integer then variable is unexpected |
532 |
self.MasterNode.SetEntry(current_idx + 0x200, subindex, self.TrashVariables[variable]) |
|
533 |
else: |
|
534 |
typeidx, varname = variable |
|
535 |
variable_infos = self.IECLocations[varname] |
|
536 |
||
537 |
# Calculate base index for storing variable |
|
538 |
mapvariableidx = VariableStartIndex[variable_infos["pdotype"]] + \ |
|
539 |
VariableTypeOffset[variable_infos["sizelocation"]] * VariableIncrement + \ |
|
540 |
variable_infos["nodeid"] |
|
541 |
||
163
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
542 |
# Generate entry name |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
543 |
indexname = "%s%s%s_%d"%(VariableDirText[variable_infos["pdotype"]], |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
544 |
variable_infos["sizelocation"], |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
545 |
'_'.join(map(str,current_location)), |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
546 |
variable_infos["nodeid"]) |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
547 |
|
58 | 548 |
# Search for an entry that has an empty subindex |
549 |
while mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000: |
|
550 |
# Entry doesn't exist |
|
551 |
if not self.MasterNode.IsEntry(mapvariableidx): |
|
552 |
# Add entry to MasterNode |
|
163
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
553 |
self.Manager.AddMapVariableToCurrent(mapvariableidx, "beremiz"+indexname, 3, 1, self.MasterNode) |
58 | 554 |
new_index = True |
555 |
nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00) |
|
556 |
else: |
|
557 |
# Get Number of subentries already defined |
|
558 |
nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00) |
|
559 |
# if entry is full, go to next entry possible or stop now |
|
560 |
if nbsubentries == 0xFF: |
|
561 |
mapvariableidx += 8 * VariableIncrement |
|
562 |
else: |
|
563 |
break |
|
564 |
||
565 |
# Verify that a not full entry has been found |
|
566 |
if mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000: |
|
567 |
# Generate subentry name |
|
568 |
if variable_infos["bit"] != None: |
|
569 |
subindexname = "%(index)d_%(subindex)d_%(bit)d"%variable_infos |
|
570 |
else: |
|
571 |
subindexname = "%(index)d_%(subindex)d"%variable_infos |
|
572 |
# If entry have just been created, no subentry have to be added |
|
573 |
if not new_index: |
|
574 |
self.Manager.AddSubentriesToCurrent(mapvariableidx, 1, self.MasterNode) |
|
575 |
nbsubentries += 1 |
|
576 |
# Add informations to the new subentry created |
|
577 |
self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname}) |
|
578 |
self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx}) |
|
579 |
||
580 |
# Set value of the PDO mapping |
|
581 |
typeinfos = self.Manager.GetEntryInfos(typeidx) |
|
582 |
if typeinfos != None: |
|
583 |
value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"] |
|
584 |
self.MasterNode.SetEntry(current_idx + 0x200, subindex, value) |
|
163
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
585 |
|
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
586 |
# Add variable to pointed variables |
482ca562d414
Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents:
155
diff
changeset
|
587 |
self.PointedVariables[(mapvariableidx, nbsubentries)] = "%s_%s"%(indexname, subindexname) |
58 | 588 |
|
589 |
def GenerateConciseDCF(locations, current_location, nodelist, sync_TPDOs, nodename): |
|
590 |
""" |
|
591 |
Fills a CanFestival network editor model, with DCF with requested PDO mappings. |
|
592 |
@param locations: List of complete variables locations \ |
|
593 |
[{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) |
|
594 |
"NAME" : name of the variable (generally "__IW0_1_2" style) |
|
595 |
"DIR" : direction "Q","I" or "M" |
|
596 |
"SIZE" : size "X", "B", "W", "D", "L" |
|
597 |
"LOC" : tuple of interger for IEC location (0,1,2,...) |
|
598 |
}, ...] |
|
599 |
@param nodelist: CanFestival network editor model |
|
600 |
@return: a modified copy of the given CanFestival network editor model |
|
601 |
""" |
|
602 |
||
603 |
dcfgenerator = ConciseDCFGenerator(nodelist, nodename) |
|
604 |
dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs) |
|
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
605 |
masternode,pointers = dcfgenerator.GetMasterNode(), dcfgenerator.GetPointedVariables() |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
606 |
# allow access to local OD from Master PLC |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
607 |
pointers.update(LocalODPointers(locations, current_location, masternode)) |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
608 |
return masternode,pointers |
58 | 609 |
|
166 | 610 |
def LocalODPointers(locations, current_location, slave): |
611 |
IECLocations = {} |
|
612 |
pointers = {} |
|
613 |
for location in locations: |
|
614 |
COlocationtype = IECToCOType[location["IEC_TYPE"]] |
|
615 |
name = location["NAME"] |
|
616 |
if name in IECLocations: |
|
617 |
if IECLocations[name] != COlocationtype: |
|
415
339fa2542481
improved english spelling and grammar and internationalization updated
laurent
parents:
361
diff
changeset
|
618 |
raise PDOmappingException, _("Type conflict for location \"%s\"") % name |
166 | 619 |
else: |
620 |
# Get only the part of the location that concern this node |
|
621 |
loc = location["LOC"][len(current_location):] |
|
622 |
# loc correspond to (ID, INDEX, SUBINDEX [,BIT]) |
|
623 |
if len(loc) not in (2, 3, 4): |
|
361 | 624 |
raise PDOmappingException, _("Bad location size : %s") % str(loc) |
166 | 625 |
elif len(loc) != 2: |
626 |
continue |
|
627 |
||
628 |
# Extract and check nodeid |
|
629 |
index, subindex = loc[:2] |
|
630 |
||
631 |
# Extract and check index and subindex |
|
632 |
if not slave.IsEntry(index, subindex): |
|
361 | 633 |
raise PDOmappingException, _("No such index/subindex (%x,%x) (variable %s)") % (index, subindex, name) |
166 | 634 |
|
635 |
# Get the entry info |
|
636 |
subentry_infos = slave.GetSubentryInfos(index, subindex) |
|
637 |
if subentry_infos["type"] != COlocationtype: |
|
361 | 638 |
raise PDOmappingException, _("Invalid type \"%s\"-> %d != %d for location\"%s\"") % (location["IEC_TYPE"], COlocationtype, subentry_infos["type"] , name) |
166 | 639 |
|
640 |
IECLocations[name] = COlocationtype |
|
641 |
pointers[(index, subindex)] = name |
|
642 |
return pointers |
|
643 |
||
58 | 644 |
if __name__ == "__main__": |
645 |
import os, sys, getopt |
|
646 |
||
647 |
def usage(): |
|
648 |
print """ |
|
649 |
Usage of config_utils.py test : |
|
650 |
||
651 |
%s [options] |
|
652 |
||
653 |
Options: |
|
654 |
--help (-h) |
|
655 |
Displays help informations for config_utils |
|
656 |
||
657 |
--reset (-r) |
|
658 |
Reset the reference result of config_utils test. |
|
659 |
Use with caution. Be sure that config_utils |
|
660 |
is currently working properly. |
|
661 |
"""%sys.argv[0] |
|
662 |
||
663 |
# Boolean that indicate if reference result must be redefined |
|
664 |
reset = False |
|
665 |
||
666 |
# Extract command options |
|
667 |
try: |
|
668 |
opts, args = getopt.getopt(sys.argv[1:], "hr", ["help","reset"]) |
|
669 |
except getopt.GetoptError: |
|
670 |
# print help information and exit: |
|
671 |
usage() |
|
672 |
sys.exit(2) |
|
673 |
||
674 |
# Test each option |
|
675 |
for o, a in opts: |
|
676 |
if o in ("-h", "--help"): |
|
677 |
usage() |
|
678 |
sys.exit() |
|
679 |
elif o in ("-r", "--reset"): |
|
680 |
reset = True |
|
681 |
||
682 |
# Extract workspace base folder |
|
683 |
base_folder = sys.path[0] |
|
684 |
for i in xrange(3): |
|
685 |
base_folder = os.path.split(base_folder)[0] |
|
686 |
# Add CanFestival folder to search pathes |
|
687 |
sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen")) |
|
688 |
||
689 |
from nodemanager import * |
|
690 |
from nodelist import * |
|
691 |
||
692 |
# Open the test nodelist contained into test_config folder |
|
693 |
manager = NodeManager() |
|
694 |
nodelist = NodeList(manager) |
|
695 |
result = nodelist.LoadProject("test_config") |
|
696 |
||
697 |
# List of locations, we try to map for test |
|
698 |
locations = [{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24576_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24576,1)}, |
|
699 |
{"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_2","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,2)}, |
|
700 |
{"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_3","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,3)}, |
|
701 |
{"IEC_TYPE":"INT","NAME":"__QW0_1_64_25617_2","DIR":"Q","SIZE":"W","LOC":(0,1,64,25617,1)}, |
|
702 |
{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24578_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24578,1)}, |
|
703 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_1","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,1)}, |
|
704 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_2","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,2)}, |
|
705 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_3","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,3)}, |
|
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
706 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_4","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,4)}, |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
707 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_4096_0","DIR":"I","SIZE":"D","LOC":(0,1,4096,0)}] |
58 | 708 |
|
709 |
# Generate MasterNode configuration |
|
710 |
try: |
|
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
711 |
masternode, pointedvariables = GenerateConciseDCF(locations, (0, 1), nodelist, True, "TestNode") |
58 | 712 |
except ValueError, message: |
713 |
print "%s\nTest Failed!"%message |
|
714 |
sys.exit() |
|
715 |
||
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
716 |
import pprint |
58 | 717 |
# Get Text corresponding to MasterNode |
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
718 |
result_node = masternode.PrintString() |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
719 |
result_vars = pprint.pformat(pointedvariables) |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
720 |
result = result_node + "\n********POINTERS*********\n" + result_vars + "\n" |
58 | 721 |
|
722 |
# If reset has been choosen |
|
723 |
if reset: |
|
724 |
# Write Text into reference result file |
|
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
725 |
testfile = open("test_config/result.txt", "w") |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
726 |
testfile.write(result) |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
727 |
testfile.close() |
58 | 728 |
|
729 |
print "Reset Successful!" |
|
730 |
else: |
|
150
204d515df3dd
Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents:
80
diff
changeset
|
731 |
import os |
204d515df3dd
Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents:
80
diff
changeset
|
732 |
|
307
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
733 |
testfile = open("test_config/result_tmp.txt", "w") |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
734 |
testfile.write(result) |
b80d3a84b8bf
Updated config_utils so that command line tests work.
etisserant
parents:
270
diff
changeset
|
735 |
testfile.close() |
58 | 736 |
|
150
204d515df3dd
Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents:
80
diff
changeset
|
737 |
os.system("diff test_config/result.txt test_config/result_tmp.txt") |
204d515df3dd
Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents:
80
diff
changeset
|
738 |
os.remove("test_config/result_tmp.txt") |