canfestival/config_utils.py
changeset 1739 ec153828ded2
parent 1736 7e61baa047f0
child 1740 b789b695b5c6
equal deleted inserted replaced
1738:d2e979738700 1739:ec153828ded2
    32 
    32 
    33 # Constants for PDO types
    33 # Constants for PDO types
    34 RPDO = 1
    34 RPDO = 1
    35 TPDO = 2
    35 TPDO = 2
    36 
    36 
    37 SlavePDOType = {"I" : TPDO, "Q" : RPDO}
    37 SlavePDOType = {"I": TPDO, "Q": RPDO}
    38 InvertPDOType = {RPDO : TPDO, TPDO : RPDO}
    38 InvertPDOType = {RPDO: TPDO, TPDO: RPDO}
    39 PDOTypeBaseIndex = {RPDO : 0x1400, TPDO : 0x1800}
    39 PDOTypeBaseIndex = {RPDO: 0x1400, TPDO: 0x1800}
    40 PDOTypeBaseCobId = {RPDO : 0x200, TPDO : 0x180}
    40 PDOTypeBaseCobId = {RPDO: 0x200, TPDO: 0x180}
    41 
    41 
    42 VariableIncrement = 0x100
    42 VariableIncrement = 0x100
    43 VariableStartIndex = {TPDO : 0x2000, RPDO : 0x4000}
    43 VariableStartIndex = {TPDO: 0x2000, RPDO: 0x4000}
    44 VariableDirText = {TPDO : "__I", RPDO : "__Q"}
    44 VariableDirText = {TPDO: "__I", RPDO: "__Q"}
    45 VariableTypeOffset = dict(zip(["","X","B","W","D","L"], range(6)))
    45 VariableTypeOffset = dict(zip(["","X","B","W","D","L"], range(6)))
    46 
    46 
    47 TrashVariables = [(1, 0x01), (8, 0x05), (16, 0x06), (32, 0x07), (64, 0x1B)]
    47 TrashVariables = [(1, 0x01), (8, 0x05), (16, 0x06), (32, 0x07), (64, 0x1B)]
    48 
    48 
    49 #-------------------------------------------------------------------------------
    49 #-------------------------------------------------------------------------------
   177 
   177 
   178         # Mark all the COB ID of the node already mapped PDO as not available
   178         # Mark all the COB ID of the node already mapped PDO as not available
   179         for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes:
   179         for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes:
   180             pdo_cobid = node.GetEntry(PdoIdx, 0x01)
   180             pdo_cobid = node.GetEntry(PdoIdx, 0x01)
   181             # Extract COB ID, if PDO isn't active
   181             # Extract COB ID, if PDO isn't active
   182             if pdo_cobid > 0x600 :
   182             if pdo_cobid > 0x600:
   183                 pdo_cobid -= 0x80000000
   183                 pdo_cobid -= 0x80000000
   184             # Remove COB ID from the list of available COB ID
   184             # Remove COB ID from the list of available COB ID
   185             if pdo_cobid in self.ListCobIDAvailable:
   185             if pdo_cobid in self.ListCobIDAvailable:
   186                 self.ListCobIDAvailable.remove(pdo_cobid)
   186                 self.ListCobIDAvailable.remove(pdo_cobid)
   187 
   187 
   210             self.TrashVariables[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
   210             self.TrashVariables[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
   211 
   211 
   212         RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode)
   212         RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode)
   213 
   213 
   214         # Store the indexes of the first RPDO and TPDO available for MasterNode
   214         # Store the indexes of the first RPDO and TPDO available for MasterNode
   215         self.CurrentPDOParamsIdx = {RPDO : 0x1400 + RPDOnumber, TPDO : 0x1800 + TPDOnumber}
   215         self.CurrentPDOParamsIdx = {RPDO: 0x1400 + RPDOnumber, TPDO: 0x1800 + TPDOnumber}
   216 
   216 
   217         # Prepare MasterNode with all nodelist slaves
   217         # Prepare MasterNode with all nodelist slaves
   218         for idx, (nodeid, nodeinfos) in enumerate(self.NodeList.SlaveNodes.items()):
   218         for idx, (nodeid, nodeinfos) in enumerate(self.NodeList.SlaveNodes.items()):
   219             node = nodeinfos["Node"]
   219             node = nodeinfos["Node"]
   220             node.SetNodeID(nodeid)
   220             node.SetNodeID(nodeid)
   305         @param nodeid: id of the slave (int)
   305         @param nodeid: id of the slave (int)
   306         @param pdotype: type of PDO to generated (RPDO or TPDO)
   306         @param pdotype: type of PDO to generated (RPDO or TPDO)
   307         @param pdomapping: list od variables to map with PDO
   307         @param pdomapping: list od variables to map with PDO
   308         """
   308         """
   309         # Add an entry to MasterMapping
   309         # Add an entry to MasterMapping
   310         self.MasterMapping[pdocobid] = {"type" : InvertPDOType[pdotype],
   310         self.MasterMapping[pdocobid] = {"type": InvertPDOType[pdotype],
   311             "mapping" : [None] + [(loc_infos["type"], name) for name, loc_infos in pdomapping]}
   311             "mapping": [None] + [(loc_infos["type"], name) for name, loc_infos in pdomapping]}
   312 
   312 
   313         # Return the data to add to DCF
   313         # Return the data to add to DCF
   314         if sync_TPDOs:
   314         if sync_TPDOs:
   315             return GeneratePDOMappingDCF(pdoindex, pdocobid, 0x01, pdomapping)
   315             return GeneratePDOMappingDCF(pdoindex, pdocobid, 0x01, pdomapping)
   316         else:
   316         else:
   379                     else:
   379                     else:
   380                         numbit = None
   380                         numbit = None
   381 
   381 
   382                     if location["IEC_TYPE"] != "BOOL" and subentry_infos["type"] != COlocationtype:
   382                     if location["IEC_TYPE"] != "BOOL" and subentry_infos["type"] != COlocationtype:
   383                         raise PDOmappingException, _("Invalid type \"{a1}\"-> {a2} != {a3}  for location \"{a4}\"").\
   383                         raise PDOmappingException, _("Invalid type \"{a1}\"-> {a2} != {a3}  for location \"{a4}\"").\
   384                             format(a1 = location["IEC_TYPE"], a2 = COlocationtype, a3 = subentry_infos["type"] , a4 = name)
   384                             format(a1 = location["IEC_TYPE"], a2 = COlocationtype, a3 = subentry_infos["type"], a4 = name)
   385 
   385 
   386                     typeinfos = node.GetEntryInfos(COlocationtype)
   386                     typeinfos = node.GetEntryInfos(COlocationtype)
   387                     self.IECLocations[name] = {"type":COlocationtype, "pdotype":SlavePDOType[direction],
   387                     self.IECLocations[name] = {"type":COlocationtype, "pdotype":SlavePDOType[direction],
   388                                                 "nodeid": nodeid, "index": index,"subindex": subindex,
   388                                                 "nodeid": nodeid, "index": index,"subindex": subindex,
   389                                                 "bit": numbit, "size": typeinfos["size"], "sizelocation": sizelocation}
   389                                                 "bit": numbit, "size": typeinfos["size"], "sizelocation": sizelocation}
   424                     values = node.GetEntry(index)
   424                     values = node.GetEntry(index)
   425                     # Store the size of each entry mapped in PDO
   425                     # Store the size of each entry mapped in PDO
   426                     for value in values[1:]:
   426                     for value in values[1:]:
   427                         if value != 0:
   427                         if value != 0:
   428                             mapping.append(value % 0x100)
   428                             mapping.append(value % 0x100)
   429                     self.MasterMapping[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping}
   429                     self.MasterMapping[cobid] = {"type": InvertPDOType[locationinfos["pdotype"]], "mapping": mapping}
   430 
   430 
   431                 # Indicate that this PDO entry must be saved
   431                 # Indicate that this PDO entry must be saved
   432                 if locationinfos["bit"] is not None:
   432                 if locationinfos["bit"] is not None:
   433                     if not isinstance(self.MasterMapping[cobid]["mapping"][subindex], ListType):
   433                     if not isinstance(self.MasterMapping[cobid]["mapping"][subindex], ListType):
   434                         self.MasterMapping[cobid]["mapping"][subindex] = [1] * self.MasterMapping[cobid]["mapping"][subindex]
   434                         self.MasterMapping[cobid]["mapping"][subindex] = [1] * self.MasterMapping[cobid]["mapping"][subindex]
   438                     self.MasterMapping[cobid]["mapping"][subindex] = (locationinfos["type"], name)
   438                     self.MasterMapping[cobid]["mapping"][subindex] = (locationinfos["type"], name)
   439 
   439 
   440             else:
   440             else:
   441                 # Add location to those that haven't been mapped yet
   441                 # Add location to those that haven't been mapped yet
   442                 if locationinfos["nodeid"] not in self.LocationsNotMapped.keys():
   442                 if locationinfos["nodeid"] not in self.LocationsNotMapped.keys():
   443                     self.LocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []}
   443                     self.LocationsNotMapped[locationinfos["nodeid"]] = {TPDO: [], RPDO: []}
   444                 self.LocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
   444                 self.LocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
   445 
   445 
   446         #-------------------------------------------------------------------------------
   446         #-------------------------------------------------------------------------------
   447         #                         Build concise DCF for the others locations
   447         #                         Build concise DCF for the others locations
   448         #-------------------------------------------------------------------------------
   448         #-------------------------------------------------------------------------------
   580                         # If entry have just been created, no subentry have to be added
   580                         # If entry have just been created, no subentry have to be added
   581                         if not new_index:
   581                         if not new_index:
   582                             self.Manager.AddSubentriesToCurrent(mapvariableidx, 1, self.MasterNode)
   582                             self.Manager.AddSubentriesToCurrent(mapvariableidx, 1, self.MasterNode)
   583                             nbsubentries += 1
   583                             nbsubentries += 1
   584                         # Add informations to the new subentry created
   584                         # Add informations to the new subentry created
   585                         self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname})
   585                         self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name": subindexname})
   586                         self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx})
   586                         self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type": typeidx})
   587 
   587 
   588                         # Set value of the PDO mapping
   588                         # Set value of the PDO mapping
   589                         typeinfos = self.Manager.GetEntryInfos(typeidx)
   589                         typeinfos = self.Manager.GetEntryInfos(typeidx)
   590                         if typeinfos != None:
   590                         if typeinfos != None:
   591                             value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
   591                             value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
   645 
   645 
   646             # Get the entry info
   646             # Get the entry info
   647             subentry_infos = slave.GetSubentryInfos(index, subindex)
   647             subentry_infos = slave.GetSubentryInfos(index, subindex)
   648             if subentry_infos["type"] != COlocationtype:
   648             if subentry_infos["type"] != COlocationtype:
   649                 raise PDOmappingException, _("Invalid type \"{a1}\"-> {a2} != {a3} for location \"{a4}\"").\
   649                 raise PDOmappingException, _("Invalid type \"{a1}\"-> {a2} != {a3} for location \"{a4}\"").\
   650                     format( a1 = location["IEC_TYPE"], a2 = COlocationtype, a3 = subentry_infos["type"] , a4 = name)
   650                     format( a1 = location["IEC_TYPE"], a2 = COlocationtype, a3 = subentry_infos["type"], a4 = name)
   651 
   651 
   652             IECLocations[name] = COlocationtype
   652             IECLocations[name] = COlocationtype
   653             pointers[(index, subindex)] = name
   653             pointers[(index, subindex)] = name
   654     return pointers
   654     return pointers
   655 
   655