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