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