etherlab/etherlab.py
changeset 2103 5a82d40c4b04
parent 2102 90031b8db62d
child 2105 a62c0de6986f
equal deleted inserted replaced
2102:90031b8db62d 2103:5a82d40c4b04
  1056         
  1056         
  1057         self.FileGenerator = _EthercatCFileGenerator(self)
  1057         self.FileGenerator = _EthercatCFileGenerator(self)
  1058         
  1058         
  1059         LocationCFilesAndCFLAGS, LDFLAGS, extra_files = ConfigTreeNode._Generate_C(self, buildpath, locations)
  1059         LocationCFilesAndCFLAGS, LDFLAGS, extra_files = ConfigTreeNode._Generate_C(self, buildpath, locations)
  1060         
  1060         
  1061         for variable in self.ProcessVariables.getvariable():
  1061         for idx, variable in enumerate(self.ProcessVariables.getvariable()):
  1062             name = None
  1062             name = None
  1063             var_type = None
  1063             var_type = None
  1064             read_from = variable.getReadFrom()
  1064             read_from = variable.getReadFrom()
  1065             write_to = variable.getWriteTo()
  1065             write_to = variable.getWriteTo()
  1066             if read_from is not None:
  1066             if read_from is not None:
  1067                 pos = read_from.getPosition()
  1067                 pos = read_from.getPosition()
  1068                 index = read_from.getIndex()
  1068                 index = read_from.getIndex()
  1069                 subindex = read_from.getSubIndex()
  1069                 subindex = read_from.getSubIndex()
  1070                 location = current_location + (pos, index, subindex)
  1070                 location = current_location + (idx, )
  1071                 var_type = self.GetSlaveVariableDataType(pos, index, subindex)
  1071                 var_type = self.GetSlaveVariableDataType(pos, index, subindex)
  1072                 name = self.FileGenerator.DeclareVariable(
  1072                 name = self.FileGenerator.DeclareVariable(
  1073                             pos, index, subindex, var_type, "I",
  1073                             pos, index, subindex, var_type, "I",
  1074                             self.GetProcessVariableName(location, var_type))
  1074                             self.GetProcessVariableName(location, var_type))
  1075             if write_to is not None:
  1075             if write_to is not None:
  1076                 pos = write_to.getPosition()
  1076                 pos = write_to.getPosition()
  1077                 index = write_to.getIndex()
  1077                 index = write_to.getIndex()
  1078                 subindex = write_to.getSubIndex()
  1078                 subindex = write_to.getSubIndex()
  1079                 if name is None:
  1079                 if name is None:
  1080                     location = current_location + (pos, index, subindex)
  1080                     location = current_location + (idx, )
  1081                     var_type = self.GetSlaveVariableDataType(pos, index, subindex)
  1081                     var_type = self.GetSlaveVariableDataType(pos, index, subindex)
  1082                     name = self.GetProcessVariableName(location, var_type)
  1082                     name = self.GetProcessVariableName(location, var_type)
  1083                 self.FileGenerator.DeclareVariable(
  1083                 self.FileGenerator.DeclareVariable(
  1084                             pos, index, subindex, var_type, "Q",
  1084                             pos, index, subindex, var_type, "Q", name, True)
  1085                             name)
       
  1086         
  1085         
  1087         self.FileGenerator.GenerateCFile(Gen_Ethercatfile_path, location_str, self.BaseParams.getIEC_Channel())
  1086         self.FileGenerator.GenerateCFile(Gen_Ethercatfile_path, location_str, self.BaseParams.getIEC_Channel())
  1088         
  1087         
  1089         LocationCFilesAndCFLAGS.append(
  1088         LocationCFilesAndCFLAGS.append(
  1090             (current_location, 
  1089             (current_location, 
  1223 def ConfigureVariable(entry_infos, str_completion):
  1222 def ConfigureVariable(entry_infos, str_completion):
  1224     entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None)
  1223     entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None)
  1225     if entry_infos["data_type"] is None:
  1224     if entry_infos["data_type"] is None:
  1226         raise ValueError, _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"]
  1225         raise ValueError, _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"]
  1227     
  1226     
  1228     if entry_infos.has_key("real_var"):
  1227     if not entry_infos.get("no_decl", False):
  1229         str_completion["located_variables_declaration"].append(
  1228         if entry_infos.has_key("real_var"):
  1230             "IEC_%(var_type)s %(real_var)s;" % entry_infos)
  1229             str_completion["located_variables_declaration"].append(
  1231     else:
  1230                 "IEC_%(var_type)s %(real_var)s;" % entry_infos)
       
  1231         else:
       
  1232             entry_infos["real_var"] = "beremiz" + entry_infos["var_name"]
       
  1233             str_completion["located_variables_declaration"].extend(
       
  1234                 ["IEC_%(var_type)s %(real_var)s;" % entry_infos,
       
  1235                  "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos])
       
  1236         for declaration in entry_infos.get("extra_declarations", []):
       
  1237             entry_infos["extra_decl"] = declaration
       
  1238             str_completion["located_variables_declaration"].append(
       
  1239                  "IEC_%(var_type)s *%(extra_decl)s = &%(real_var)s;" % entry_infos)
       
  1240     elif not entry_infos.has_key("real_var"):
  1232         entry_infos["real_var"] = "beremiz" + entry_infos["var_name"]
  1241         entry_infos["real_var"] = "beremiz" + entry_infos["var_name"]
  1233         str_completion["located_variables_declaration"].extend(
       
  1234             ["IEC_%(var_type)s %(real_var)s;" % entry_infos,
       
  1235              "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos])
       
  1236     for declaration in entry_infos.get("extra_declarations", []):
       
  1237         entry_infos["extra_decl"] = declaration
       
  1238         str_completion["located_variables_declaration"].append(
       
  1239              "IEC_%(var_type)s *%(extra_decl)s = &%(real_var)s;" % entry_infos)
       
  1240     
  1242     
  1241     str_completion["used_pdo_entry_offset_variables_declaration"].append(
  1243     str_completion["used_pdo_entry_offset_variables_declaration"].append(
  1242         "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos)
  1244         "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos)
  1243     
  1245     
  1244     if entry_infos["data_type"] == "BIT":
  1246     if entry_infos["data_type"] == "BIT":
  1294         self.Controler = None            
  1296         self.Controler = None            
  1295     
  1297     
  1296     def DeclareSlave(self, slave_index, slave):
  1298     def DeclareSlave(self, slave_index, slave):
  1297         self.Slaves.append((slave_index, slave.getInfo().getAutoIncAddr(), slave))
  1299         self.Slaves.append((slave_index, slave.getInfo().getAutoIncAddr(), slave))
  1298 
  1300 
  1299     def DeclareVariable(self, slave_index, index, subindex, iec_type, dir, name):
  1301     def DeclareVariable(self, slave_index, index, subindex, iec_type, dir, name, no_decl=False):
  1300         slave_variables = self.UsedVariables.setdefault(slave_index, {})
  1302         slave_variables = self.UsedVariables.setdefault(slave_index, {})
  1301         
  1303         
  1302         entry_infos = slave_variables.get((index, subindex), None)
  1304         entry_infos = slave_variables.get((index, subindex), None)
  1303         if entry_infos is None:
  1305         if entry_infos is None:
  1304             slave_variables[(index, subindex)] = {
  1306             slave_variables[(index, subindex)] = {
  1305                 "infos": (iec_type, dir, name, []),
  1307                 "infos": (iec_type, dir, name, no_decl, []),
  1306                 "mapped": False}
  1308                 "mapped": False}
  1307             return name
  1309             return name
  1308         elif entry_infos["infos"][:2] == (iec_type, dir):
  1310         elif entry_infos["infos"][:2] == (iec_type, dir):
  1309             if name != entry_infos["infos"][2]:
  1311             if name != entry_infos["infos"][2]:
  1310                 if dir == "I":
  1312                 if dir == "I":
  1311                     entry_infos["infos"][3].append(name)
  1313                     entry_infos["infos"][3].append(name)
  1312                     return entry_infos["infos"][2]
  1314                     return entry_infos["infos"][2]
  1313                 else:
  1315                 else:
  1314                     raise ValueError, _("Output variables can't be defined with different locations (%s and %s)") % (entry_infos["infos"][2], name)
  1316                     raise ValueError, _("Output variables can't be defined with different locations (%s and %s)") % (entry_infos["infos"][2], name)
  1315         else:
  1317         else:
  1316             print entry_infos["infos"][:2], (iec_type, dir)
       
  1317             raise ValueError, _("Definition conflict for location \"%s\"") % name 
  1318             raise ValueError, _("Definition conflict for location \"%s\"") % name 
  1318         
  1319         
  1319     def GenerateCFile(self, filepath, location_str, master_number):
  1320     def GenerateCFile(self, filepath, location_str, master_number):
  1320         
  1321         
  1321         # Extract etherlab master code template
  1322         # Extract etherlab master code template
  1521                             
  1522                             
  1522                             entry_declaration = slave_variables.get((index, subindex), None)
  1523                             entry_declaration = slave_variables.get((index, subindex), None)
  1523                             if entry_declaration is not None and not entry_declaration["mapped"]:
  1524                             if entry_declaration is not None and not entry_declaration["mapped"]:
  1524                                 pdo_needed = True
  1525                                 pdo_needed = True
  1525                                 
  1526                                 
  1526                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "extra_declarations"], entry_declaration["infos"])))
  1527                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "no_decl", "extra_declarations"], 
       
  1528                                                             entry_declaration["infos"])))
  1527                                 entry_declaration["mapped"] = True
  1529                                 entry_declaration["mapped"] = True
  1528                                 
  1530                                 
  1529                                 entry_type = entry.getDataType().getcontent()
  1531                                 entry_type = entry.getDataType().getcontent()
  1530                                 if entry_infos["var_type"] != entry_type:
  1532                                 if entry_infos["var_type"] != entry_type:
  1531                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1533                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1607                                     "name": entry["Name"],
  1609                                     "name": entry["Name"],
  1608                                     "bitlen": entry["BitSize"],
  1610                                     "bitlen": entry["BitSize"],
  1609                                 }
  1611                                 }
  1610                                 entry_infos.update(type_infos)
  1612                                 entry_infos.update(type_infos)
  1611                                 
  1613                                 
  1612                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "extra_declarations"], entry_declaration["infos"])))
  1614                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "no_decl", "extra_declarations"], 
       
  1615                                                             entry_declaration["infos"])))
  1613                                 entry_declaration["mapped"] = True
  1616                                 entry_declaration["mapped"] = True
  1614                                 
  1617                                 
  1615                                 if entry_infos["var_type"] != entry["Type"]:
  1618                                 if entry_infos["var_type"] != entry["Type"]:
  1616                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1619                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1617                                     if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != 
  1620                                     if (self.Controler.GetSizeOfType(entry_infos["var_type"]) !=