11
|
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
|
27 |
# Translation between IEC types and Can Open types
|
34
|
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}
|
11
|
32 |
|
|
33 |
# Constants for PDO types
|
|
34 |
RPDO = 1
|
|
35 |
TPDO = 2
|
34
|
36 |
|
11
|
37 |
SlavePDOType = {"I" : TPDO, "Q" : RPDO}
|
|
38 |
InvertPDOType = {RPDO : TPDO, TPDO : RPDO}
|
|
39 |
|
34
|
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)]
|
11
|
46 |
|
27
|
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 |
|
34
|
60 |
|
27
|
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 |
|
11
|
70 |
indexes = []
|
|
71 |
if type & RPDO:
|
27
|
72 |
indexes.extend([idx for idx in node.GetIndexes() if 0x1400 <= idx <= 0x15FF])
|
11
|
73 |
if type & TPDO:
|
27
|
74 |
indexes.extend([idx for idx in node.GetIndexes() if 0x1800 <= idx <= 0x19FF])
|
11
|
75 |
if not parameters:
|
|
76 |
return [idx + 0x200 for idx in indexes]
|
|
77 |
else:
|
|
78 |
return indexes
|
|
79 |
|
34
|
80 |
|
27
|
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 |
|
|
90 |
typeinfos = node.GetEntryInfos(loc_infos["type"])
|
11
|
91 |
model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + typeinfos["size"]
|
27
|
92 |
|
|
93 |
for PDOidx in GetNodePDOIndexes(node, loc_infos["pdotype"]):
|
|
94 |
values = node.GetEntry(PDOidx)
|
11
|
95 |
if values != None:
|
|
96 |
for subindex, mapping in enumerate(values):
|
|
97 |
if subindex != 0 and mapping == model:
|
|
98 |
return PDOidx, subindex
|
|
99 |
return None
|
|
100 |
|
34
|
101 |
|
|
102 |
def GeneratePDOMappingDCF(idx, cobid, transmittype, pdomapping):
|
|
103 |
"""
|
|
104 |
Build concise DCF value for configuring a PDO
|
|
105 |
@param idx: index of PDO parameters
|
|
106 |
@param cobid: PDO generated COB ID
|
|
107 |
@param transmittype : PDO transmit type
|
|
108 |
@param pdomapping: list of PDO mappings
|
|
109 |
@return: a tuple of value and number of parameters to add to DCF
|
|
110 |
"""
|
|
111 |
|
|
112 |
# Create entry for RPDO or TPDO parameters and Disable PDO
|
|
113 |
dcfdata = LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE((0x80000000 + cobid), 4)
|
|
114 |
# Set Transmit type synchrone
|
|
115 |
dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(transmittype, 1)
|
|
116 |
# Re-Enable PDO
|
|
117 |
# ---- INDEX ----- --- SUBINDEX ---- ----- SIZE ------ ------ DATA ------
|
|
118 |
dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x00000000 + cobid, 4)
|
|
119 |
nbparams = 3
|
|
120 |
# Map Variables
|
|
121 |
for subindex, (name, loc_infos) in enumerate(pdomapping):
|
|
122 |
value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"]
|
|
123 |
dcfdata += LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4)
|
|
124 |
nbparams += 1
|
|
125 |
return dcfdata, nbparams
|
|
126 |
|
27
|
127 |
class ConciseDCFGenerator:
|
|
128 |
|
|
129 |
def __init__(self, nodelist):
|
|
130 |
# Dictionary of location informations classed by name
|
34
|
131 |
self.IECLocations = {}
|
|
132 |
# Dictionary of location that have not been mapped yet
|
|
133 |
self.LocationsNotMapped = {}
|
27
|
134 |
# Dictionary of location informations classed by name
|
34
|
135 |
self.MasterMapping = {}
|
27
|
136 |
# List of COB IDs available
|
|
137 |
self.ListCobIDAvailable = range(0x180, 0x580)
|
|
138 |
self.SlavesPdoNumber = {}
|
|
139 |
# Dictionary of mapping value where unexpected variables are stored
|
34
|
140 |
self.TrashVariables = {}
|
27
|
141 |
|
|
142 |
self.NodeList = nodelist
|
|
143 |
self.Manager = self.NodeList.Manager
|
|
144 |
self.MasterNode = self.Manager.GetCurrentNodeCopy()
|
|
145 |
self.PrepareMasterNode()
|
|
146 |
|
34
|
147 |
|
27
|
148 |
def RemoveUsedNodeCobId(self, node):
|
|
149 |
"""
|
|
150 |
Remove all PDO COB ID used by the given node from the list of available COB ID
|
|
151 |
@param node: node
|
|
152 |
@return: a tuple of number of RPDO and TPDO for the node
|
|
153 |
"""
|
|
154 |
|
|
155 |
# Get list of all node TPDO and RPDO indexes
|
|
156 |
nodeRpdoIndexes = GetNodePDOIndexes(node, RPDO, True)
|
|
157 |
nodeTpdoIndexes = GetNodePDOIndexes(node, TPDO, True)
|
|
158 |
|
|
159 |
# Mark all the COB ID of the node already mapped PDO as not available
|
|
160 |
for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes:
|
|
161 |
pdo_cobid = node.GetEntry(PdoIdx, 0x01)
|
|
162 |
# Extract COB ID, if PDO isn't active
|
|
163 |
if pdo_cobid > 0x600 :
|
|
164 |
pdo_cobid -= 0x80000000
|
|
165 |
# Remove COB ID from the list of available COB ID
|
|
166 |
if pdo_cobid in self.ListCobIDAvailable:
|
|
167 |
self.ListCobIDAvailable.remove(pdo_cobid)
|
|
168 |
|
|
169 |
return len(nodeRpdoIndexes), len(nodeTpdoIndexes)
|
|
170 |
|
34
|
171 |
|
27
|
172 |
def PrepareMasterNode(self):
|
|
173 |
"""
|
|
174 |
Add mandatory entries for DCF generation into MasterNode.
|
|
175 |
"""
|
|
176 |
|
|
177 |
# Adding DCF entry into Master node
|
|
178 |
if not self.MasterNode.IsEntry(0x1F22):
|
|
179 |
self.MasterNode.AddEntry(0x1F22, 1, "")
|
34
|
180 |
self.Manager.AddSubentriesToCurrent(0x1F22, 127, self.MasterNode)
|
27
|
181 |
|
|
182 |
# Adding trash mappable variables for unused mapped datas
|
34
|
183 |
idxTrashVariables = 0x2000 + self.MasterNode.GetNodeID()
|
27
|
184 |
# Add an entry for storing unexpected all variable
|
34
|
185 |
self.Manager.AddMapVariableToCurrent(idxTrashVariables, "trashvariables", 3, len(TrashVariables), self.MasterNode)
|
|
186 |
for subidx, (size, typeidx) in enumerate(TrashVariables):
|
27
|
187 |
# Add a subentry for storing unexpected variable of this size
|
|
188 |
self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None, self.MasterNode)
|
|
189 |
self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, self.MasterNode)
|
|
190 |
# Store the mapping value for this entry
|
34
|
191 |
self.TrashVariables[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
|
27
|
192 |
|
|
193 |
RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode)
|
|
194 |
|
|
195 |
# Store the indexes of the first RPDO and TPDO available for MasterNode
|
|
196 |
self.CurrentPDOParamsIdx = {RPDO : 0x1400 + RPDOnumber, TPDO : 0x1800 + TPDOnumber}
|
|
197 |
|
|
198 |
# Prepare MasterNode with all nodelist slaves
|
34
|
199 |
for idx, (nodeid, nodeinfos) in enumerate(self.NodeList.SlaveNodes.items()):
|
27
|
200 |
node = nodeinfos["Node"]
|
|
201 |
node.SetNodeID(nodeid)
|
|
202 |
|
|
203 |
RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(node)
|
|
204 |
|
|
205 |
# Store the number of TPDO and RPDO for this node
|
|
206 |
self.SlavesPdoNumber[nodeid] = {RPDO : RPDOnumber, TPDO : TPDOnumber}
|
|
207 |
|
|
208 |
# Get Slave's default SDO server parameters
|
|
209 |
RSDO_cobid = node.GetEntry(0x1200,0x01)
|
|
210 |
if not RSDO_cobid:
|
|
211 |
RSDO_cobid = 0x600 + nodeid
|
|
212 |
TSDO_cobid = node.GetEntry(0x1200,0x02)
|
|
213 |
if not TSDO_cobid:
|
|
214 |
TSDO_cobid = 0x580 + nodeid
|
|
215 |
|
|
216 |
# Configure Master's SDO parameters entries
|
34
|
217 |
self.Manager.ManageEntriesOfCurrent([0x1280 + idx], [], self.MasterNode)
|
|
218 |
self.MasterNode.SetEntry(0x1280 + idx, 0x01, RSDO_cobid)
|
|
219 |
self.MasterNode.SetEntry(0x1280 + idx, 0x02, TSDO_cobid)
|
|
220 |
self.MasterNode.SetEntry(0x1280 + idx, 0x03, nodeid)
|
|
221 |
|
|
222 |
|
27
|
223 |
def GetMasterNode(self):
|
|
224 |
"""
|
|
225 |
Return MasterNode.
|
|
226 |
"""
|
|
227 |
return self.MasterNode
|
|
228 |
|
34
|
229 |
|
27
|
230 |
def GetNewCobID(self, nodeid, type):
|
|
231 |
"""
|
|
232 |
Select a COB ID from the list of those available
|
34
|
233 |
@param nodeid: id of the slave (int)
|
27
|
234 |
@param type: type of PDO (RPDO or TPDO)
|
|
235 |
@return: a tuple of the COD ID and PDO index or None
|
|
236 |
"""
|
|
237 |
# Verify that there is still some cobid available
|
|
238 |
if len(self.ListCobIDAvailable) == 0:
|
|
239 |
return None
|
|
240 |
|
|
241 |
# Get the number of PDO of the type given for the node
|
|
242 |
nbSlavePDO = self.SlavesPdoNumber[nodeid][type]
|
|
243 |
if type == RPDO:
|
|
244 |
if nbSlavePDO < 4:
|
|
245 |
# For the four first RPDO -> cobid = 0x200 + ( numPdo parameters * 0x100) + nodeid
|
|
246 |
newcobid = (0x200 + nbSlavePDO * 0x100 + nodeid)
|
|
247 |
# Return calculated cobid if it's still available
|
|
248 |
if newcobid in self.ListCobIDAvailable:
|
|
249 |
self.ListCobIDAvailable.remove(newcobid)
|
|
250 |
return newcobid, 0x1400 + nbSlavePDO
|
|
251 |
# Return the first cobid available if no cobid found
|
|
252 |
return self.ListCobIDAvailable.pop(0), 0x1400 + nbSlavePDO
|
|
253 |
|
|
254 |
elif type == TPDO:
|
|
255 |
if nbSlavePDO < 4:
|
|
256 |
# For the four first TPDO -> cobid = 0x180 + ( numPdo parameters * 0x100) + nodeid
|
|
257 |
newcobid = (0x180 + nbSlavePDO * 0x100 + nodeid)
|
|
258 |
# Return calculated cobid if it's still available
|
|
259 |
if newcobid in self.ListCobIDAvailable:
|
|
260 |
self.ListCobIDAvailable.remove(newcobid)
|
|
261 |
return newcobid, 0x1800 + nbSlavePDO
|
|
262 |
# Return the first cobid available if no cobid found
|
|
263 |
return self.ListCobIDAvailable.pop(0), 0x1800 + nbSlavePDO
|
|
264 |
|
11
|
265 |
return None
|
34
|
266 |
|
|
267 |
|
|
268 |
def AddParamsToDCF(self, nodeid, data, nbparams):
|
|
269 |
"""
|
|
270 |
Select a COB ID from the list of those available
|
|
271 |
@param nodeid: id of the slave (int)
|
|
272 |
@param data: data to add to slave DCF (string)
|
|
273 |
@param nbparams: number of params added to slave DCF (int)
|
|
274 |
"""
|
|
275 |
# Get current DCF for slave
|
|
276 |
nodeDCF = self.MasterNode.GetEntry(0x1F22, nodeid)
|
|
277 |
|
|
278 |
# Extract data and number of params in current DCF
|
|
279 |
if nodeDCF != None and nodeDCF != '':
|
|
280 |
tmpnbparams = [i for i in nodeDCF[:4]]
|
|
281 |
tmpnbparams.reverse()
|
|
282 |
nbparams += int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16)
|
|
283 |
data = nodeDCF[4:] + data
|
|
284 |
|
|
285 |
# Build new DCF
|
|
286 |
dcf = LE_to_BE(nbparams, 0x04) + data
|
|
287 |
# Set new DCF for slave
|
|
288 |
self.MasterNode.SetEntry(0x1F22, nodeid, dcf)
|
|
289 |
|
|
290 |
def AddPDOMapping(self, nodeid, pdotype, pdomapping, sync_TPDOs):
|
|
291 |
"""
|
|
292 |
Select a COB ID from the list of those available
|
|
293 |
@param nodeid: id of the slave (int)
|
|
294 |
@param pdotype: type of PDO to generated (RPDO or TPDO)
|
|
295 |
@param pdomapping: list od variables to map with PDO
|
|
296 |
"""
|
|
297 |
# Get a new cob id
|
|
298 |
result = self.GetNewCobID(nodeid, pdotype)
|
|
299 |
if result:
|
|
300 |
new_cobid, new_idx = result
|
|
301 |
|
|
302 |
# Increment the number of PDO of this type for node
|
|
303 |
self.SlavesPdoNumber[nodeid][pdotype] += 1
|
|
304 |
|
|
305 |
# Add an entry to MasterMapping
|
|
306 |
self.MasterMapping[new_cobid] = {"type" : InvertPDOType[pdotype],
|
|
307 |
"mapping" : [None] + [(loc_infos["type"], name) for name, loc_infos in pdomapping]}
|
|
308 |
|
|
309 |
# Return the data to add to DCF
|
|
310 |
if sync_TPDOs:
|
|
311 |
return GeneratePDOMappingDCF(new_idx, new_cobid, 0x01, pdomapping)
|
|
312 |
else:
|
|
313 |
return GeneratePDOMappingDCF(new_idx, new_cobid, 0xFF, pdomapping)
|
|
314 |
return 0, ""
|
|
315 |
|
27
|
316 |
def GenerateDCF(self, locations, current_location, sync_TPDOs):
|
|
317 |
"""
|
|
318 |
Generate Concise DCF of MasterNode for the locations list given
|
|
319 |
@param locations: list of locations to be mapped
|
|
320 |
@param current_location: tuple of the located prefixes not to be considered
|
|
321 |
@param sync_TPDOs: indicate if TPDO must be synchronous
|
|
322 |
"""
|
|
323 |
|
34
|
324 |
# Get list of locations check if exists and mappables -> put them in IECLocations
|
27
|
325 |
for location in locations:
|
34
|
326 |
COlocationtype = IECToCOType[location["IEC_TYPE"]]
|
27
|
327 |
name = location["NAME"]
|
34
|
328 |
if name in self.IECLocations:
|
|
329 |
if self.IECLocations[name]["type"] != COlocationtype:
|
27
|
330 |
raise ValueError, "Conflict type for location \"%s\"" % name
|
|
331 |
else:
|
|
332 |
# Get only the part of the location that concern this node
|
|
333 |
loc = location["LOC"][len(current_location):]
|
|
334 |
# loc correspond to (ID, INDEX, SUBINDEX [,BIT])
|
|
335 |
if len(loc) not in (3, 4):
|
|
336 |
raise ValueError, "Bad location size : %s"%str(loc)
|
|
337 |
|
|
338 |
direction = location["DIR"]
|
|
339 |
|
|
340 |
sizelocation = location["SIZE"]
|
|
341 |
|
|
342 |
# Extract and check nodeid
|
|
343 |
nodeid, index, subindex = loc[:3]
|
|
344 |
|
|
345 |
# Check Id is in slave node list
|
34
|
346 |
if nodeid not in self.NodeList.SlaveNodes.keys():
|
27
|
347 |
raise ValueError, "Non existing node ID : %d (variable %s)" % (nodeid,name)
|
|
348 |
|
|
349 |
# Get the model for this node (made from EDS)
|
34
|
350 |
node = self.NodeList.SlaveNodes[nodeid]["Node"]
|
27
|
351 |
|
|
352 |
# Extract and check index and subindex
|
|
353 |
if not node.IsEntry(index, subindex):
|
|
354 |
raise ValueError, "No such index/subindex (%x,%x) in ID : %d (variable %s)" % (index,subindex,nodeid,name)
|
|
355 |
|
|
356 |
#Get the entry info
|
|
357 |
subentry_infos = node.GetSubentryInfos(index, subindex)
|
|
358 |
|
|
359 |
# If a PDO mappable
|
|
360 |
if subentry_infos and subentry_infos["pdo"]:
|
|
361 |
if sizelocation == "X" and len(loc) > 3:
|
|
362 |
numbit = loc[4]
|
|
363 |
elif sizelocation != "X" and len(loc) > 3:
|
|
364 |
raise ValueError, "Cannot set bit offset for non bool '%s' variable (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex)
|
|
365 |
else:
|
|
366 |
numbit = None
|
|
367 |
|
|
368 |
entryinfos = node.GetSubentryInfos(index, subindex)
|
|
369 |
if entryinfos["type"] != COlocationtype:
|
|
370 |
raise ValueError, "Invalid type \"%s\"-> %d != %d for location\"%s\"" % (location["IEC_TYPE"], COlocationtype, entryinfos["type"] , name)
|
|
371 |
|
|
372 |
typeinfos = node.GetEntryInfos(COlocationtype)
|
34
|
373 |
self.IECLocations[name] = {"type":COlocationtype, "pdotype":SlavePDOType[direction],
|
|
374 |
"nodeid": nodeid, "index": index,"subindex": subindex,
|
|
375 |
"bit": numbit, "size": typeinfos["size"], "sizelocation": sizelocation}
|
27
|
376 |
else:
|
|
377 |
raise ValueError, "Not PDO mappable variable : '%s' (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex)
|
34
|
378 |
|
|
379 |
#-------------------------------------------------------------------------------
|
|
380 |
# Search for locations already mapped
|
|
381 |
#-------------------------------------------------------------------------------
|
|
382 |
|
|
383 |
for name, locationinfos in self.IECLocations.items():
|
27
|
384 |
node = self.NodeList.SlaveNodes[locationinfos["nodeid"]]["Node"]
|
34
|
385 |
|
|
386 |
# Search if slave has a PDO mapping this locations
|
27
|
387 |
result = SearchNodePDOMapping(locationinfos, node)
|
|
388 |
if result != None:
|
|
389 |
index, subindex = result
|
34
|
390 |
# Get COB ID of the PDO
|
|
391 |
cobid = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1)
|
|
392 |
|
|
393 |
# Verify that PDO transmit type is conform to sync_TPDOs
|
|
394 |
transmittype = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 2)
|
|
395 |
if sync_TPDOs and transmittype != 0x01 or transmittype != 0xFF:
|
|
396 |
if sync_TPDOs:
|
|
397 |
# Change TransmitType to SYNCHRONE
|
|
398 |
data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0x01, [])
|
|
399 |
else:
|
|
400 |
# Change TransmitType to ASYCHRONE
|
|
401 |
data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0xFF, [])
|
27
|
402 |
|
34
|
403 |
# Add entry to slave dcf to change transmit type of
|
|
404 |
self.AddParamsToDCF(locationinfos["nodeid"], data, nbparams)
|
|
405 |
|
|
406 |
# Add PDO to MasterMapping
|
|
407 |
if cobid not in self.MasterMapping.keys():
|
27
|
408 |
mapping = [None]
|
|
409 |
values = node.GetEntry(index)
|
34
|
410 |
# Store the size of each entry mapped in PDO
|
27
|
411 |
for value in values[1:]:
|
|
412 |
mapping.append(value % 0x100)
|
34
|
413 |
self.MasterMapping[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping}
|
|
414 |
|
|
415 |
# Indicate that this PDO entry must be saved
|
|
416 |
self.MasterMapping[cobid]["mapping"][subindex] = (locationinfos["type"], name)
|
27
|
417 |
|
|
418 |
else:
|
34
|
419 |
# Add location to those that haven't been mapped yet
|
|
420 |
if locationinfos["nodeid"] not in self.LocationsNotMapped.keys():
|
|
421 |
self.LocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []}
|
|
422 |
self.LocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
|
27
|
423 |
|
|
424 |
#-------------------------------------------------------------------------------
|
|
425 |
# Build concise DCF for the others locations
|
|
426 |
#-------------------------------------------------------------------------------
|
|
427 |
|
34
|
428 |
for nodeid, locations in self.LocationsNotMapped.items():
|
27
|
429 |
node = nodelist.SlaveNodes[nodeid]["Node"]
|
34
|
430 |
|
|
431 |
# Initialize number of params and data to add to node DCF
|
|
432 |
nbparams = 0
|
|
433 |
dataparams = ""
|
|
434 |
|
|
435 |
# Generate the best PDO mapping for each type of PDO
|
27
|
436 |
for pdotype in (TPDO, RPDO):
|
|
437 |
pdosize = 0
|
|
438 |
pdomapping = []
|
|
439 |
for name, loc_infos in locations[pdotype]:
|
|
440 |
pdosize += loc_infos["size"]
|
|
441 |
# If pdo's size > 64 bits
|
|
442 |
if pdosize > 64:
|
34
|
443 |
# Generate a new PDO Mapping
|
|
444 |
data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdomapping, sync_TPDOs)
|
|
445 |
dataparams += data
|
|
446 |
nbparams += nbaddedparams
|
27
|
447 |
pdosize = loc_infos["size"]
|
|
448 |
pdomapping = [(name, loc_infos)]
|
|
449 |
else:
|
|
450 |
pdomapping.append((name, loc_infos))
|
34
|
451 |
# If there isn't locations yet but there is still a PDO to generate
|
27
|
452 |
if len(pdomapping) > 0:
|
34
|
453 |
# Generate a new PDO Mapping
|
|
454 |
data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdomapping, sync_TPDOs)
|
|
455 |
dataparams += data
|
|
456 |
nbparams += nbaddedparams
|
|
457 |
|
|
458 |
# Add number of params and data to node DCF
|
|
459 |
self.AddParamsToDCF(nodeid, dataparams, nbparams)
|
|
460 |
|
27
|
461 |
#-------------------------------------------------------------------------------
|
|
462 |
# Master Node Configuration
|
|
463 |
#-------------------------------------------------------------------------------
|
|
464 |
|
34
|
465 |
# Generate Master's Configuration from informations stored in MasterMapping
|
|
466 |
for cobid, pdo_infos in self.MasterMapping.items():
|
|
467 |
# Get next PDO index in MasterNode for this PDO type
|
|
468 |
current_idx = self.CurrentPDOParamsIdx[pdo_infos["type"]]
|
|
469 |
|
|
470 |
# Search if there is already a PDO in MasterNode with this cob id
|
|
471 |
for idx in GetNodePDOIndexes(self.MasterNode, pdo_infos["type"], True):
|
|
472 |
if self.MasterNode.GetEntry(idx, 1) == cobid:
|
|
473 |
current_idx = idx
|
|
474 |
|
|
475 |
# Add a PDO to MasterNode if not PDO have been found
|
|
476 |
if current_idx == self.CurrentPDOParamsIdx[pdo_infos["type"]]:
|
|
477 |
addinglist = [current_idx, current_idx + 0x200]
|
|
478 |
self.Manager.ManageEntriesOfCurrent(addinglist, [], self.MasterNode)
|
|
479 |
self.MasterNode.SetEntry(current_idx, 0x01, cobid)
|
|
480 |
|
|
481 |
# Increment the number of PDO for this PDO type
|
|
482 |
self.CurrentPDOParamsIdx[pdo_infos["type"]] += 1
|
|
483 |
|
|
484 |
# Change the transmit type of the PDO
|
|
485 |
if sync_TPDOs:
|
|
486 |
self.MasterNode.SetEntry(current_idx, 0x02, 0x01)
|
|
487 |
else:
|
|
488 |
self.MasterNode.SetEntry(current_idx, 0x02, 0xFF)
|
|
489 |
|
|
490 |
# Add some subentries to PDO mapping if there is not enough
|
27
|
491 |
if len(pdo_infos["mapping"]) > 2:
|
34
|
492 |
self.Manager.AddSubentriesToCurrent(current_idx + 0x200, len(pdo_infos["mapping"]) - 2, self.MasterNode)
|
|
493 |
|
|
494 |
# Generate MasterNode's PDO mapping
|
27
|
495 |
for subindex, variable in enumerate(pdo_infos["mapping"]):
|
|
496 |
if subindex == 0:
|
|
497 |
continue
|
|
498 |
new_index = False
|
|
499 |
|
34
|
500 |
if type(variable) == IntType:
|
|
501 |
# If variable is an integer then variable is unexpected
|
|
502 |
self.MasterNode.SetEntry(current_idx + 0x200, subindex, self.TrashVariables[variable])
|
|
503 |
else:
|
|
504 |
typeidx, varname = variable
|
|
505 |
variable_infos = self.IECLocations[varname]
|
27
|
506 |
|
34
|
507 |
# Calculate base index for storing variable
|
|
508 |
mapvariableidx = VariableStartIndex[variable_infos["pdotype"]] + \
|
|
509 |
VariableTypeOffset[variable_infos["sizelocation"]] * VariableIncrement + \
|
|
510 |
variable_infos["nodeid"]
|
27
|
511 |
|
34
|
512 |
# Search for an entry that has an empty subindex
|
|
513 |
while mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000:
|
|
514 |
# Entry doesn't exist
|
|
515 |
if not self.MasterNode.IsEntry(mapvariableidx):
|
|
516 |
# Generate entry name
|
|
517 |
indexname = "%s%s%s_%d"%(VariableDirText[variable_infos["pdotype"]],
|
|
518 |
variable_infos["sizelocation"],
|
|
519 |
'_'.join(map(str,current_location)),
|
|
520 |
variable_infos["nodeid"])
|
|
521 |
# Add entry to MasterNode
|
|
522 |
self.Manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, self.MasterNode)
|
|
523 |
new_index = True
|
|
524 |
nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00)
|
27
|
525 |
else:
|
34
|
526 |
# Get Number of subentries already defined
|
|
527 |
nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00)
|
|
528 |
# if entry is full, go to next entry possible or stop now
|
|
529 |
if nbsubentries == 0xFF:
|
|
530 |
mapvariableidx += 8 * VariableIncrement
|
|
531 |
else:
|
|
532 |
break
|
|
533 |
|
|
534 |
# Verify that a not full entry has been found
|
|
535 |
if mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000:
|
|
536 |
# Generate subentry name
|
|
537 |
if variable_infos["bit"] != None:
|
|
538 |
subindexname = "%(index)d_%(subindex)d_%(bit)d"%variable_infos
|
|
539 |
else:
|
|
540 |
subindexname = "%(index)d_%(subindex)d"%variable_infos
|
|
541 |
# If entry have just been created, no subentry have to be added
|
27
|
542 |
if not new_index:
|
34
|
543 |
self.Manager.AddSubentriesToCurrent(mapvariableidx, 1, self.MasterNode)
|
27
|
544 |
nbsubentries += 1
|
34
|
545 |
# Add informations to the new subentry created
|
|
546 |
self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname})
|
|
547 |
self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx})
|
27
|
548 |
|
34
|
549 |
# Set value of the PDO mapping
|
|
550 |
typeinfos = self.Manager.GetEntryInfos(typeidx)
|
27
|
551 |
if typeinfos != None:
|
|
552 |
value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
|
34
|
553 |
self.MasterNode.SetEntry(current_idx + 0x200, subindex, value)
|
27
|
554 |
|
26
|
555 |
def GenerateConciseDCF(locations, current_location, nodelist, sync_TPDOs):
|
22
|
556 |
"""
|
|
557 |
Fills a CanFestival network editor model, with DCF with requested PDO mappings.
|
|
558 |
@param locations: List of complete variables locations \
|
|
559 |
[{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
|
|
560 |
"NAME" : name of the variable (generally "__IW0_1_2" style)
|
|
561 |
"DIR" : direction "Q","I" or "M"
|
|
562 |
"SIZE" : size "X", "B", "W", "D", "L"
|
|
563 |
"LOC" : tuple of interger for IEC location (0,1,2,...)
|
|
564 |
}, ...]
|
|
565 |
@param nodelist: CanFestival network editor model
|
|
566 |
@return: a modified copy of the given CanFestival network editor model
|
|
567 |
"""
|
|
568 |
|
27
|
569 |
dcfgenerator = ConciseDCFGenerator(nodelist)
|
|
570 |
dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs)
|
|
571 |
return dcfgenerator.GetMasterNode()
|
|
572 |
|
34
|
573 |
if __name__ == "__main__":
|
|
574 |
import os, sys
|
|
575 |
|
|
576 |
base_folder = sys.path[0]
|
|
577 |
for i in xrange(3):
|
|
578 |
base_folder = os.path.split(base_folder)[0]
|
|
579 |
sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
|
|
580 |
|
11
|
581 |
from nodemanager import *
|
|
582 |
from nodelist import *
|
34
|
583 |
|
|
584 |
manager = NodeManager()
|
11
|
585 |
nodelist = NodeList(manager)
|
34
|
586 |
result = nodelist.LoadProject("test_config")
|
|
587 |
|
|
588 |
locations = [{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24576_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24576,1)},
|
|
589 |
{"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_2","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,2)},
|
|
590 |
{"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_3","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,3)},
|
|
591 |
{"IEC_TYPE":"INT","NAME":"__QW0_1_64_25617_2","DIR":"Q","SIZE":"W","LOC":(0,1,64,25617,1)},
|
|
592 |
{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24578_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24578,1)},
|
|
593 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_1","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,1)},
|
|
594 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_2","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,2)},
|
|
595 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_3","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,3)},
|
|
596 |
{"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_4","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,4)}]
|
|
597 |
|
|
598 |
masternode = GenerateConciseDCF(locations, (0, 1), nodelist, True)
|
|
599 |
#masternode.Print()
|
|
600 |
result = [line.rstrip() for line in masternode.PrintString().splitlines()]
|
|
601 |
|
|
602 |
file = open("test_config/result.txt", "r")
|
|
603 |
model = [line.rstrip() for line in file.readlines()]
|
11
|
604 |
file.close()
|
34
|
605 |
|
|
606 |
errors = 0
|
|
607 |
for i, line in enumerate(model):
|
|
608 |
if i >= len(result):
|
|
609 |
errors += 1
|
|
610 |
print "Line %d disappear :"%(i + 1)
|
|
611 |
print line
|
|
612 |
elif line != result[i]:
|
|
613 |
errors += 1
|
|
614 |
print "Error on line %d :"%(i + 1)
|
|
615 |
print "\t%s"%result[i]
|
|
616 |
print "Instead of :\n\t%s"%line
|
|
617 |
for i in xrange(len(model), len(result)):
|
|
618 |
errors += 1
|
|
619 |
print "Line %d appear :"%(i + 1)
|
|
620 |
print result[i]
|
|
621 |
|
|
622 |
if errors > 0:
|
|
623 |
print "Test Failed!"
|
|
624 |
else:
|
|
625 |
print "Test Successful!" |