lbessard@11: #!/usr/bin/env python lbessard@11: # -*- coding: utf-8 -*- lbessard@11: lbessard@11: #This file is part of Beremiz, a Integrated Development Environment for lbessard@11: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. lbessard@11: # lbessard@11: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD lbessard@11: # lbessard@11: #See COPYING file for copyrights details. lbessard@11: # lbessard@11: #This library is free software; you can redistribute it and/or lbessard@11: #modify it under the terms of the GNU General Public lbessard@11: #License as published by the Free Software Foundation; either lbessard@11: #version 2.1 of the License, or (at your option) any later version. lbessard@11: # lbessard@11: #This library is distributed in the hope that it will be useful, lbessard@11: #but WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@11: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@11: #General Public License for more details. lbessard@11: # lbessard@11: #You should have received a copy of the GNU General Public lbessard@11: #License along with this library; if not, write to the Free Software lbessard@11: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA lbessard@11: lbessard@11: from types import * lbessard@11: greg@27: # Translation between IEC types and Can Open types lbessard@11: DicoTypes = {"BOOL":0x01, "SINT":0x02, "INT":0x03,"DINT":0x04,"LINT":0x10, lbessard@11: "USINT":0x05,"UINT":0x06,"UDINT":0x07,"ULINT":0x1B,"REAL":0x08, etisserant@24: "LREAL":0x11,"STRING":0x09,"BYTE":0x05,"WORD":0x06,"DWORD":0x07, lbessard@11: "LWORD":0x1B,"WSTRING":0x0B} lbessard@11: greg@27: DictNameVariable = { "" : 1, "X": 2, "B": 3, "W": 4, "D": 5, "L": 6, "increment": 0x100, 1:("__I", 0x2000), 2:("__Q", 0x4000)} lbessard@11: lbessard@11: # Constants for PDO types lbessard@11: RPDO = 1 lbessard@11: TPDO = 2 lbessard@11: SlavePDOType = {"I" : TPDO, "Q" : RPDO} lbessard@11: InvertPDOType = {RPDO : TPDO, TPDO : RPDO} lbessard@11: lbessard@11: GenerateMasterMapping = lambda x:[None] + [(loc_infos["type"], name) for name, loc_infos in x] lbessard@11: lbessard@11: TrashVariableSizes = {1 : 0x01, 8 : 0x05, 16 : 0x06, 32 : 0x07, 64 : 0x1B} lbessard@11: greg@27: def LE_to_BE(value, size): greg@27: """ greg@27: Convert Little Endian to Big Endian greg@27: @param value: value expressed in integer greg@27: @param size: number of bytes generated greg@27: @return: a string containing the value converted greg@27: """ greg@27: greg@27: data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value greg@27: list_car = [data[i:i+2] for i in xrange(0, len(data), 2)] greg@27: list_car.reverse() greg@27: return "".join([chr(int(car, 16)) for car in list_car]) greg@27: greg@27: def GetNodePDOIndexes(node, type, parameters = False): greg@27: """ greg@27: Find the PDO indexes of a node greg@27: @param node: node greg@27: @param type: type of PDO searched (RPDO or TPDO or both) greg@27: @param parameters: indicate which indexes are expected (PDO paramaters : True or PDO mappings : False) greg@27: @return: a list of indexes found greg@27: """ greg@27: lbessard@11: indexes = [] lbessard@11: if type & RPDO: greg@27: indexes.extend([idx for idx in node.GetIndexes() if 0x1400 <= idx <= 0x15FF]) lbessard@11: if type & TPDO: greg@27: indexes.extend([idx for idx in node.GetIndexes() if 0x1800 <= idx <= 0x19FF]) lbessard@11: if not parameters: lbessard@11: return [idx + 0x200 for idx in indexes] lbessard@11: else: lbessard@11: return indexes lbessard@11: greg@27: def SearchNodePDOMapping(loc_infos, node): greg@27: """ greg@27: Find the PDO indexes of a node greg@27: @param node: node greg@27: @param type: type of PDO searched (RPDO or TPDO or both) greg@27: @param parameters: indicate which indexes are expected (PDO paramaters : True or PDO mappings : False) greg@27: @return: a list of indexes found greg@27: """ greg@27: greg@27: typeinfos = node.GetEntryInfos(loc_infos["type"]) lbessard@11: model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + typeinfos["size"] greg@27: greg@27: for PDOidx in GetNodePDOIndexes(node, loc_infos["pdotype"]): greg@27: values = node.GetEntry(PDOidx) lbessard@11: if values != None: lbessard@11: for subindex, mapping in enumerate(values): lbessard@11: if subindex != 0 and mapping == model: lbessard@11: return PDOidx, subindex lbessard@11: return None lbessard@11: greg@27: class ConciseDCFGenerator: greg@27: greg@27: def __init__(self, nodelist): greg@27: # Dictionary of location informations classed by name greg@27: self.DictLocations = {} greg@27: # Dictionary of location informations classed by name greg@27: self.DictCobID = {} greg@27: # Dictionary of location that have not been mapped yet greg@27: self.DictLocationsNotMapped = {} greg@27: # List of COB IDs available greg@27: self.ListCobIDAvailable = range(0x180, 0x580) greg@27: self.SlavesPdoNumber = {} greg@27: # Dictionary of mapping value where unexpected variables are stored greg@27: TrashVariableValue = {} greg@27: greg@27: self.NodeList = nodelist greg@27: self.Manager = self.NodeList.Manager greg@27: self.MasterNode = self.Manager.GetCurrentNodeCopy() greg@27: self.PrepareMasterNode() greg@27: greg@27: def RemoveUsedNodeCobId(self, node): greg@27: """ greg@27: Remove all PDO COB ID used by the given node from the list of available COB ID greg@27: @param node: node greg@27: @return: a tuple of number of RPDO and TPDO for the node greg@27: """ greg@27: greg@27: # Get list of all node TPDO and RPDO indexes greg@27: nodeRpdoIndexes = GetNodePDOIndexes(node, RPDO, True) greg@27: nodeTpdoIndexes = GetNodePDOIndexes(node, TPDO, True) greg@27: greg@27: # Mark all the COB ID of the node already mapped PDO as not available greg@27: for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes: greg@27: pdo_cobid = node.GetEntry(PdoIdx, 0x01) greg@27: # Extract COB ID, if PDO isn't active greg@27: if pdo_cobid > 0x600 : greg@27: pdo_cobid -= 0x80000000 greg@27: # Remove COB ID from the list of available COB ID greg@27: if pdo_cobid in self.ListCobIDAvailable: greg@27: self.ListCobIDAvailable.remove(pdo_cobid) greg@27: greg@27: return len(nodeRpdoIndexes), len(nodeTpdoIndexes) greg@27: greg@27: def PrepareMasterNode(self): greg@27: """ greg@27: Add mandatory entries for DCF generation into MasterNode. greg@27: """ greg@27: greg@27: # Adding DCF entry into Master node greg@27: if not self.MasterNode.IsEntry(0x1F22): greg@27: self.MasterNode.AddEntry(0x1F22, 1, "") greg@27: self.Manager.AddSubentriesToCurrent(0x1F22, 127, masternode) greg@27: greg@27: # Adding trash mappable variables for unused mapped datas greg@27: idxTrashVariables = 0x2000 + masternode.GetNodeID() greg@27: # Add an entry for storing unexpected all variable greg@27: self.Manager.AddMapVariableToCurrent(idxTrashVariables, "trashvariables", 3, len(TrashVariableSizes), self.MasterNode) greg@27: for subidx, (size, typeidx) in enumerate(TrashVariableSizes.items()): greg@27: # Add a subentry for storing unexpected variable of this size greg@27: self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None, self.MasterNode) greg@27: self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, self.MasterNode) greg@27: # Store the mapping value for this entry greg@27: self.TrashVariableValue[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size greg@27: greg@27: RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode) greg@27: greg@27: # Store the indexes of the first RPDO and TPDO available for MasterNode greg@27: self.CurrentPDOParamsIdx = {RPDO : 0x1400 + RPDOnumber, TPDO : 0x1800 + TPDOnumber} greg@27: greg@27: # Prepare MasterNode with all nodelist slaves greg@27: for nodeid, nodeinfos in self.NodeList.SlaveNodes.items(): greg@27: node = nodeinfos["Node"] greg@27: node.SetNodeID(nodeid) greg@27: greg@27: RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(node) greg@27: greg@27: # Store the number of TPDO and RPDO for this node greg@27: self.SlavesPdoNumber[nodeid] = {RPDO : RPDOnumber, TPDO : TPDOnumber} greg@27: greg@27: # Get Slave's default SDO server parameters greg@27: RSDO_cobid = node.GetEntry(0x1200,0x01) greg@27: if not RSDO_cobid: greg@27: RSDO_cobid = 0x600 + nodeid greg@27: TSDO_cobid = node.GetEntry(0x1200,0x02) greg@27: if not TSDO_cobid: greg@27: TSDO_cobid = 0x580 + nodeid greg@27: greg@27: # Configure Master's SDO parameters entries greg@27: self.Manager.ManageEntriesOfCurrent([0x1280 + nodeid], [], self.MasterNode) greg@27: self.MasterNode.SetEntry(0x1280 + nodeid, 0x01, RSDO_cobid) greg@27: self.MasterNode.SetEntry(0x1280 + nodeid, 0x02, TSDO_cobid) greg@27: self.MasterNode.SetEntry(0x1280 + nodeid, 0x03, nodeid) greg@27: greg@27: def GetMasterNode(self): greg@27: """ greg@27: Return MasterNode. greg@27: """ greg@27: return self.MasterNode greg@27: greg@27: # Build concise DCF greg@27: def GenerateMappingDCF(cobid, idx, pdomapping, mapped): greg@27: greg@27: # Create entry for RPDO or TPDO parameters and Disable PDO greg@27: dcfdata = LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE((0x80000000 + cobid), 4) greg@27: # Set Transmit type synchrone greg@27: dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(DefaultTransmitTypeSlave, 1) greg@27: # Re-Enable PDO greg@27: # ---- INDEX ----- --- SUBINDEX ---- ----- SIZE ------ ------ DATA ------ greg@27: dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x00000000 + cobid, 4) greg@27: nbparams = 3 greg@27: if mapped == False and pdomapping != None: greg@27: # Map Variables greg@27: for subindex, (name, loc_infos) in enumerate(pdomapping): greg@27: value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"] greg@27: dcfdata += LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4) greg@27: nbparams += 1 greg@27: return dcfdata, nbparams greg@27: greg@27: # Return a cobid not used greg@27: def GetNewCobID(self, nodeid, type): greg@27: """ greg@27: Select a COB ID from the list of those available greg@27: @param nodeid: id of the slave node greg@27: @param type: type of PDO (RPDO or TPDO) greg@27: @return: a tuple of the COD ID and PDO index or None greg@27: """ greg@27: # Verify that there is still some cobid available greg@27: if len(self.ListCobIDAvailable) == 0: greg@27: return None greg@27: greg@27: # Get the number of PDO of the type given for the node greg@27: nbSlavePDO = self.SlavesPdoNumber[nodeid][type] greg@27: if type == RPDO: greg@27: if nbSlavePDO < 4: greg@27: # For the four first RPDO -> cobid = 0x200 + ( numPdo parameters * 0x100) + nodeid greg@27: newcobid = (0x200 + nbSlavePDO * 0x100 + nodeid) greg@27: # Return calculated cobid if it's still available greg@27: if newcobid in self.ListCobIDAvailable: greg@27: self.ListCobIDAvailable.remove(newcobid) greg@27: return newcobid, 0x1400 + nbSlavePDO greg@27: # Return the first cobid available if no cobid found greg@27: return self.ListCobIDAvailable.pop(0), 0x1400 + nbSlavePDO greg@27: greg@27: elif type == TPDO: greg@27: if nbSlavePDO < 4: greg@27: # For the four first TPDO -> cobid = 0x180 + ( numPdo parameters * 0x100) + nodeid greg@27: newcobid = (0x180 + nbSlavePDO * 0x100 + nodeid) greg@27: # Return calculated cobid if it's still available greg@27: if newcobid in self.ListCobIDAvailable: greg@27: self.ListCobIDAvailable.remove(newcobid) greg@27: return newcobid, 0x1800 + nbSlavePDO greg@27: # Return the first cobid available if no cobid found greg@27: return self.ListCobIDAvailable.pop(0), 0x1800 + nbSlavePDO greg@27: lbessard@11: return None greg@27: greg@27: def GenerateDCF(self, locations, current_location, sync_TPDOs): greg@27: """ greg@27: Generate Concise DCF of MasterNode for the locations list given greg@27: @param locations: list of locations to be mapped greg@27: @param current_location: tuple of the located prefixes not to be considered greg@27: @param sync_TPDOs: indicate if TPDO must be synchronous greg@27: """ greg@27: greg@27: # Get list of locations check if exists and mappables -> put them in DictLocations greg@27: for location in locations: greg@27: COlocationtype = DicoTypes[location["IEC_TYPE"]] greg@27: name = location["NAME"] greg@27: if name in DictLocations: greg@27: if DictLocations[name]["type"] != COlocationtype: greg@27: raise ValueError, "Conflict type for location \"%s\"" % name greg@27: else: greg@27: # Get only the part of the location that concern this node greg@27: loc = location["LOC"][len(current_location):] greg@27: # loc correspond to (ID, INDEX, SUBINDEX [,BIT]) greg@27: if len(loc) not in (3, 4): greg@27: raise ValueError, "Bad location size : %s"%str(loc) greg@27: greg@27: direction = location["DIR"] greg@27: greg@27: sizelocation = location["SIZE"] greg@27: greg@27: # Extract and check nodeid greg@27: nodeid, index, subindex = loc[:3] greg@27: greg@27: # Check Id is in slave node list greg@27: if nodeid not in nodelist.SlaveNodes.keys(): greg@27: raise ValueError, "Non existing node ID : %d (variable %s)" % (nodeid,name) greg@27: greg@27: # Get the model for this node (made from EDS) greg@27: node = nodelist.SlaveNodes[nodeid]["Node"] greg@27: greg@27: # Extract and check index and subindex greg@27: if not node.IsEntry(index, subindex): greg@27: raise ValueError, "No such index/subindex (%x,%x) in ID : %d (variable %s)" % (index,subindex,nodeid,name) greg@27: greg@27: #Get the entry info greg@27: subentry_infos = node.GetSubentryInfos(index, subindex) greg@27: greg@27: # If a PDO mappable greg@27: if subentry_infos and subentry_infos["pdo"]: greg@27: if sizelocation == "X" and len(loc) > 3: greg@27: numbit = loc[4] greg@27: elif sizelocation != "X" and len(loc) > 3: greg@27: raise ValueError, "Cannot set bit offset for non bool '%s' variable (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex) greg@27: else: greg@27: numbit = None greg@27: greg@27: entryinfos = node.GetSubentryInfos(index, subindex) greg@27: if entryinfos["type"] != COlocationtype: greg@27: raise ValueError, "Invalid type \"%s\"-> %d != %d for location\"%s\"" % (location["IEC_TYPE"], COlocationtype, entryinfos["type"] , name) greg@27: greg@27: typeinfos = node.GetEntryInfos(COlocationtype) greg@27: DictLocations[name] = {"type":COlocationtype, "pdotype":SlavePDOType[direction], greg@27: "nodeid": nodeid, "index": index,"subindex": subindex, greg@27: "bit": numbit, "size": typeinfos["size"], "sizelocation": sizelocation} greg@27: else: greg@27: raise ValueError, "Not PDO mappable variable : '%s' (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex) greg@27: greg@27: # Create DictCobID with variables already mapped and add them in DictValidLocations greg@27: for name, locationinfos in DictLocations.items(): greg@27: node = self.NodeList.SlaveNodes[locationinfos["nodeid"]]["Node"] greg@27: result = SearchNodePDOMapping(locationinfos, node) greg@27: if result != None: greg@27: index, subindex = result greg@27: cobid = nodelist.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1) greg@27: greg@27: transmittype = nodelist.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 2) greg@27: greg@27: if transmittype != sync_TPDOs: greg@27: if sync_TPDOs : # Change TransmitType to ASYCHRONE greg@27: node = nodelist.SlaveNodes[nodeid]["Node"] greg@27: nodeDCF = masternode.GetEntry(0x1F22, nodeid) greg@27: greg@27: if nodeDCF != None and nodeDCF != '': greg@27: tmpnbparams = [i for i in nodeDCF[:4]] greg@27: tmpnbparams.reverse() greg@27: nbparams = int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16) greg@27: dataparams = nodeDCF[4:] greg@27: else: greg@27: nbparams = 0 greg@27: dataparams = "" greg@27: greg@27: else: # Change TransmitType to SYNCHRONE greg@27: pass greg@27: greg@27: greg@27: if cobid not in DictCobID.keys(): greg@27: mapping = [None] greg@27: values = node.GetEntry(index) greg@27: for value in values[1:]: greg@27: mapping.append(value % 0x100) greg@27: DictCobID[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping} greg@27: greg@27: DictCobID[cobid]["mapping"][subindex] = (locationinfos["type"], name) greg@27: greg@27: else: greg@27: if locationinfos["nodeid"] not in DictLocationsNotMapped.keys(): greg@27: DictLocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []} greg@27: DictLocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos)) greg@27: greg@27: #------------------------------------------------------------------------------- greg@27: # Build concise DCF for the others locations greg@27: #------------------------------------------------------------------------------- greg@27: greg@27: for nodeid, locations in DictLocationsNotMapped.items(): greg@27: # Get current concise DCF greg@27: node = nodelist.SlaveNodes[nodeid]["Node"] greg@27: nodeDCF = masternode.GetEntry(0x1F22, nodeid) greg@27: greg@27: if nodeDCF != None and nodeDCF != '': greg@27: tmpnbparams = [i for i in nodeDCF[:4]] greg@27: tmpnbparams.reverse() greg@27: nbparams = int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16) greg@27: dataparams = nodeDCF[4:] greg@27: else: greg@27: nbparams = 0 greg@27: dataparams = "" greg@27: greg@27: for pdotype in (TPDO, RPDO): greg@27: pdosize = 0 greg@27: pdomapping = [] greg@27: for name, loc_infos in locations[pdotype]: greg@27: pdosize += loc_infos["size"] greg@27: # If pdo's size > 64 bits greg@27: if pdosize > 64: greg@27: result = GetNewCobID(nodeid, pdotype) greg@27: if result: greg@27: SlavesPdoNumber[nodeid][pdotype] += 1 greg@27: new_cobid, new_idx = result greg@27: data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False) greg@27: dataparams += data greg@27: nbparams += nbaddedparams greg@27: DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)} greg@27: pdosize = loc_infos["size"] greg@27: pdomapping = [(name, loc_infos)] greg@27: else: greg@27: pdomapping.append((name, loc_infos)) greg@27: if len(pdomapping) > 0: greg@27: result = GetNewCobID(nodeid, pdotype) greg@27: if result: greg@27: SlavesPdoNumber[nodeid][pdotype] += 1 greg@27: new_cobid, new_idx = result greg@27: data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False) greg@27: dataparams += data greg@27: nbparams += nbaddedparams greg@27: DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)} greg@27: greg@27: dcf = LE_to_BE(nbparams, 0x04) + dataparams greg@27: masternode.SetEntry(0x1F22, nodeid, dcf) greg@27: greg@27: greg@27: #------------------------------------------------------------------------------- greg@27: # Master Node Configuration greg@27: #------------------------------------------------------------------------------- greg@27: greg@27: greg@27: greg@27: # Configure Master's PDO parameters entries and set cobid, transmit type greg@27: for cobid, pdo_infos in DictCobID.items(): greg@27: current_idx = CurrentPDOParamsIdx[pdo_infos["type"]] greg@27: addinglist = [current_idx, current_idx + 0x200] greg@27: manager.ManageEntriesOfCurrent(addinglist, [], masternode) greg@27: masternode.SetEntry(current_idx, 0x01, cobid) greg@27: masternode.SetEntry(current_idx, 0x02, DefaultTransmitTypeMaster) greg@27: if len(pdo_infos["mapping"]) > 2: greg@27: manager.AddSubentriesToCurrent(current_idx + 0x200, len(pdo_infos["mapping"]) - 2, masternode) greg@27: greg@27: # Create Master's PDO mapping greg@27: for subindex, variable in enumerate(pdo_infos["mapping"]): greg@27: if subindex == 0: greg@27: continue greg@27: new_index = False greg@27: greg@27: if type(variable) != IntType: greg@27: greg@27: typeidx, varname = variable greg@27: indexname = \ greg@27: DictNameVariable[DictLocations[variable[1]]["pdotype"]][0] + \ greg@27: DictLocations[variable[1]]["sizelocation"] + \ greg@27: '_'.join(map(str,current_location)) + \ greg@27: "_" + \ greg@27: str(DictLocations[variable[1]]["nodeid"]) greg@27: mapvariableidx = DictNameVariable[DictLocations[variable[1]]["pdotype"]][1] + DictNameVariable[DictLocations[variable[1]]["sizelocation"]] * DictNameVariable["increment"] greg@27: greg@27: #indexname = DictNameVariable[DictLocations[variable[1]]["pdotype"]][0] + DictLocations[variable[1]]["sizelocation"] + str(DictLocations[variable[1]]["prefix"]) + "_" + str(DictLocations[variable[1]]["nodeid"]) greg@27: #mapvariableidx = DictNameVariable[DictLocations[variable[1]]["pdotype"]][1] + DictNameVariable[DictLocations[variable[1]]["sizelocation"]] * DictNameVariable["increment"] greg@27: greg@27: if not masternode.IsEntry(mapvariableidx): greg@27: manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, masternode) greg@27: new_index = True greg@27: nbsubentries = masternode.GetEntry(mapvariableidx, 0x00) greg@27: else: greg@27: nbsubentries = masternode.GetEntry(mapvariableidx, 0x00) greg@27: mapvariableidxbase = mapvariableidx greg@27: while mapvariableidx < (mapvariableidxbase + 0x1FF) and nbsubentries == 0xFF: greg@27: mapvariableidx += 0x800 greg@27: if not manager.IsCurrentEntry(mapvariableidx): greg@27: manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, masternode) greg@27: new_index = True greg@27: nbsubentries = masternode.GetEntry(mapvariableidx, 0x00) greg@27: greg@27: if mapvariableidx < 0x6000: greg@27: if DictLocations[variable[1]]["bit"] != None: greg@27: subindexname = str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"]) + "_" + str(DictLocations[variable[1]]["bit"]) greg@27: else: greg@27: subindexname = str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"]) greg@27: if not new_index: greg@27: manager.AddSubentriesToCurrent(mapvariableidx, 1, masternode) greg@27: nbsubentries += 1 greg@27: masternode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname}) greg@27: masternode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx}) greg@27: greg@27: # Map Variable greg@27: typeinfos = manager.GetEntryInfos(typeidx) greg@27: if typeinfos != None: greg@27: value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"] greg@27: masternode.SetEntry(current_idx + 0x200, subindex, value) greg@27: else: greg@27: masternode.SetEntry(current_idx + 0x200, subindex, TrashVariableValue[variable]) greg@27: greg@27: CurrentPDOParamsIdx[pdo_infos["type"]] += 1 greg@27: greg@27: etisserant@26: def GenerateConciseDCF(locations, current_location, nodelist, sync_TPDOs): etisserant@22: """ etisserant@22: Fills a CanFestival network editor model, with DCF with requested PDO mappings. etisserant@22: @param locations: List of complete variables locations \ etisserant@22: [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) etisserant@22: "NAME" : name of the variable (generally "__IW0_1_2" style) etisserant@22: "DIR" : direction "Q","I" or "M" etisserant@22: "SIZE" : size "X", "B", "W", "D", "L" etisserant@22: "LOC" : tuple of interger for IEC location (0,1,2,...) etisserant@22: }, ...] etisserant@22: @param nodelist: CanFestival network editor model etisserant@22: @return: a modified copy of the given CanFestival network editor model etisserant@22: """ etisserant@22: greg@27: dcfgenerator = ConciseDCFGenerator(nodelist) greg@27: dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs) greg@27: return dcfgenerator.GetMasterNode() greg@27: greg@27: if __name__ == "__main__": lbessard@11: from nodemanager import * lbessard@11: from nodelist import * lbessard@11: import sys lbessard@11: lbessard@11: manager = NodeManager(sys.path[0]) lbessard@11: nodelist = NodeList(manager) lbessard@11: result = nodelist.LoadProject("/home/deobox/Desktop/TestMapping") lbessard@11: lbessard@11: ## if result != None: lbessard@11: ## print result lbessard@11: ## else: lbessard@11: ## print "MasterNode :" lbessard@11: ## manager.CurrentNode.Print() lbessard@11: ## for nodeid, node in nodelist.SlaveNodes.items(): lbessard@11: ## print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid) lbessard@11: ## node["Node"].Print() lbessard@11: lbessard@11: #filepath = "/home/deobox/beremiz/test_nodelist/listlocations.txt" lbessard@11: filepath = "/home/deobox/Desktop/TestMapping/listlocations.txt" lbessard@11: lbessard@11: file = open(filepath,'r') lbessard@11: locations = [location.split(' ') for location in [line.strip() for line in file.readlines() if len(line) > 0]] lbessard@11: file.close() lbessard@11: GenerateConciseDCF(locations, 32, nodelist) lbessard@11: print "MasterNode :" lbessard@11: manager.CurrentNode.Print() lbessard@11: #masternode.Print()