etherlab/etherlab.py
changeset 2101 da450992572e
parent 2099 ea5384ab152c
child 2102 90031b8db62d
equal deleted inserted replaced
2100:bb43a81356eb 2101:da450992572e
   696         slave = self.GetSlave(command_infos["Position"])
   696         slave = self.GetSlave(command_infos["Position"])
   697         if slave is not None:
   697         if slave is not None:
   698             slave.setStartupCommand(command_infos)
   698             slave.setStartupCommand(command_infos)
   699             self.BufferModel()
   699             self.BufferModel()
   700     
   700     
   701     def RemoveStartupCommand(self, slave_pos, command_idx):
   701     def RemoveStartupCommand(self, slave_pos, command_idx, buffer=True):
   702         slave = self.GetSlave(slave_pos)
   702         slave = self.GetSlave(slave_pos)
   703         if slave is not None:
   703         if slave is not None:
   704             slave.removeStartupCommand(command_idx)
   704             slave.removeStartupCommand(command_idx)
   705             self.BufferModel()
   705             if buffer:
       
   706                 self.BufferModel()
   706     
   707     
   707     def SetProcessVariables(self, variables):
   708     def SetProcessVariables(self, variables):
   708         vars = []
   709         vars = []
   709         for var in variables:
   710         for var in variables:
   710             variable = ProcessVariablesClasses["ProcessVariables_variable"]()
   711             variable = ProcessVariablesClasses["ProcessVariables_variable"]()
  1018         process_xmlfile.close()
  1019         process_xmlfile.close()
  1019         
  1020         
  1020         self.Buffer.CurrentSaved()
  1021         self.Buffer.CurrentSaved()
  1021         return True
  1022         return True
  1022 
  1023 
       
  1024     def GetProcessVariableName(self, location, var_type):
       
  1025         return "__M%s_%s" % (self.GetSizeOfType(var_type), "_".join(map(str, location)))
       
  1026 
  1023     def _Generate_C(self, buildpath, locations):
  1027     def _Generate_C(self, buildpath, locations):
  1024         current_location = self.GetCurrentLocation()
  1028         current_location = self.GetCurrentLocation()
  1025         # define a unique name for the generated C file
  1029         # define a unique name for the generated C file
  1026         location_str = "_".join(map(lambda x:str(x), current_location))
  1030         location_str = "_".join(map(lambda x:str(x), current_location))
  1027         
  1031         
  1028         Gen_Ethercatfile_path = os.path.join(buildpath, "ethercat_%s.c"%location_str)
  1032         Gen_Ethercatfile_path = os.path.join(buildpath, "ethercat_%s.c"%location_str)
  1029         
  1033         
  1030         self.FileGenerator = _EthercatCFileGenerator(self)
  1034         self.FileGenerator = _EthercatCFileGenerator(self)
  1031         
  1035         
  1032         LocationCFilesAndCFLAGS, LDFLAGS, extra_files = ConfigTreeNode._Generate_C(self, buildpath, locations)
  1036         LocationCFilesAndCFLAGS, LDFLAGS, extra_files = ConfigTreeNode._Generate_C(self, buildpath, locations)
       
  1037         
       
  1038         for variable in self.ProcessVariables.getvariable():
       
  1039             name = None
       
  1040             var_type = None
       
  1041             read_from = variable.getReadFrom()
       
  1042             write_to = variable.getWriteTo()
       
  1043             if read_from is not None:
       
  1044                 pos = read_from.getPosition()
       
  1045                 index = read_from.getIndex()
       
  1046                 subindex = read_from.getSubIndex()
       
  1047                 location = current_location + (pos, index, subindex)
       
  1048                 var_type = self.GetSlaveVariableDataType(pos, index, subindex)
       
  1049                 name = self.FileGenerator.DeclareVariable(
       
  1050                             pos, index, subindex, var_type, "I",
       
  1051                             self.GetProcessVariableName(location, var_type))
       
  1052             if write_to is not None:
       
  1053                 pos = write_to.getPosition()
       
  1054                 index = write_to.getIndex()
       
  1055                 subindex = write_to.getSubIndex()
       
  1056                 if name is None:
       
  1057                     location = current_location + (pos, index, subindex)
       
  1058                     var_type = self.GetSlaveVariableDataType(pos, index, subindex)
       
  1059                     name = self.GetProcessVariableName(location, var_type)
       
  1060                 self.FileGenerator.DeclareVariable(
       
  1061                             pos, index, subindex, var_type, "Q",
       
  1062                             name)
  1033         
  1063         
  1034         self.FileGenerator.GenerateCFile(Gen_Ethercatfile_path, location_str, self.BaseParams.getIEC_Channel())
  1064         self.FileGenerator.GenerateCFile(Gen_Ethercatfile_path, location_str, self.BaseParams.getIEC_Channel())
  1035         
  1065         
  1036         LocationCFilesAndCFLAGS.append(
  1066         LocationCFilesAndCFLAGS.append(
  1037             (current_location, 
  1067             (current_location, 
  1070                 self.FileGenerator.DeclareSlave(slave_pos, slave)
  1100                 self.FileGenerator.DeclareSlave(slave_pos, slave)
  1071         
  1101         
  1072         for location in locations:
  1102         for location in locations:
  1073             loc = location["LOC"][len(current_location):]
  1103             loc = location["LOC"][len(current_location):]
  1074             slave_pos = loc[0]
  1104             slave_pos = loc[0]
  1075             if slave_pos in slaves and len(loc) == 3:
  1105             if slave_pos in slaves and len(loc) == 3 and location["DIR"] != "M":
  1076                 self.FileGenerator.DeclareVariable(
  1106                 self.FileGenerator.DeclareVariable(
  1077                     slave_pos, loc[1], loc[2], location["IEC_TYPE"], location["DIR"], location["NAME"])
  1107                     slave_pos, loc[1], loc[2], location["IEC_TYPE"], location["DIR"], location["NAME"])
  1078         
  1108         
  1079         return [],"",False
  1109         return [],"",False
  1080         
  1110         
  1178     else:
  1208     else:
  1179         entry_infos["real_var"] = "beremiz" + entry_infos["var_name"]
  1209         entry_infos["real_var"] = "beremiz" + entry_infos["var_name"]
  1180         str_completion["located_variables_declaration"].extend(
  1210         str_completion["located_variables_declaration"].extend(
  1181             ["IEC_%(var_type)s %(real_var)s;" % entry_infos,
  1211             ["IEC_%(var_type)s %(real_var)s;" % entry_infos,
  1182              "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos])
  1212              "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos])
       
  1213     for declaration in entry_infos.get("extra_declarations", []):
       
  1214         entry_infos["extra_decl"] = declaration
       
  1215         str_completion["located_variables_declaration"].append(
       
  1216              "IEC_%(var_type)s *%(extra_decl)s = &%(real_var)s;" % entry_infos)
  1183     
  1217     
  1184     str_completion["used_pdo_entry_offset_variables_declaration"].append(
  1218     str_completion["used_pdo_entry_offset_variables_declaration"].append(
  1185         "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos)
  1219         "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos)
  1186     
  1220     
  1187     if entry_infos["data_type"] == "BIT":
  1221     if entry_infos["data_type"] == "BIT":
  1243         slave_variables = self.UsedVariables.setdefault(slave_index, {})
  1277         slave_variables = self.UsedVariables.setdefault(slave_index, {})
  1244         
  1278         
  1245         entry_infos = slave_variables.get((index, subindex), None)
  1279         entry_infos = slave_variables.get((index, subindex), None)
  1246         if entry_infos is None:
  1280         if entry_infos is None:
  1247             slave_variables[(index, subindex)] = {
  1281             slave_variables[(index, subindex)] = {
  1248                 "infos": (iec_type, dir, name),
  1282                 "infos": (iec_type, dir, name, []),
  1249                 "mapped": False}
  1283                 "mapped": False}
  1250         elif entry_infos["infos"] != (iec_type, dir, name):
  1284             return name
       
  1285         elif entry_infos["infos"][:2] == (iec_type, dir):
       
  1286             if name != entry_infos["infos"][2]:
       
  1287                 if dir == "I":
       
  1288                     entry_infos["infos"][3].append(name)
       
  1289                     return entry_infos["infos"][2]
       
  1290                 else:
       
  1291                     raise ValueError, _("Output variables can't be defined with different locations (%s and %s)") % (entry_infos["infos"][2], name)
       
  1292         else:
       
  1293             print entry_infos["infos"][:2], (iec_type, dir)
  1251             raise ValueError, _("Definition conflict for location \"%s\"") % name 
  1294             raise ValueError, _("Definition conflict for location \"%s\"") % name 
  1252         
  1295         
  1253     def GenerateCFile(self, filepath, location_str, master_number):
  1296     def GenerateCFile(self, filepath, location_str, master_number):
  1254         
  1297         
  1255         # Extract etherlab master code template
  1298         # Extract etherlab master code template
  1455                             
  1498                             
  1456                             entry_declaration = slave_variables.get((index, subindex), None)
  1499                             entry_declaration = slave_variables.get((index, subindex), None)
  1457                             if entry_declaration is not None and not entry_declaration["mapped"]:
  1500                             if entry_declaration is not None and not entry_declaration["mapped"]:
  1458                                 pdo_needed = True
  1501                                 pdo_needed = True
  1459                                 
  1502                                 
  1460                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name"], entry_declaration["infos"])))
  1503                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "extra_declarations"], entry_declaration["infos"])))
  1461                                 entry_declaration["mapped"] = True
  1504                                 entry_declaration["mapped"] = True
  1462                                 
  1505                                 
  1463                                 entry_type = entry.getDataType().getcontent()
  1506                                 entry_type = entry.getDataType().getcontent()
  1464                                 if entry_infos["var_type"] != entry_type:
  1507                                 if entry_infos["var_type"] != entry_type:
  1465                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1508                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1541                                     "name": entry["Name"],
  1584                                     "name": entry["Name"],
  1542                                     "bitlen": entry["BitSize"],
  1585                                     "bitlen": entry["BitSize"],
  1543                                 }
  1586                                 }
  1544                                 entry_infos.update(type_infos)
  1587                                 entry_infos.update(type_infos)
  1545                                 
  1588                                 
  1546                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "real_var"], entry_declaration["infos"])))
  1589                                 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "extra_declarations"], entry_declaration["infos"])))
  1547                                 entry_declaration["mapped"] = True
  1590                                 entry_declaration["mapped"] = True
  1548                                 
  1591                                 
  1549                                 if entry_infos["var_type"] != entry["Type"]:
  1592                                 if entry_infos["var_type"] != entry["Type"]:
  1550                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1593                                     message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
  1551                                     if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != 
  1594                                     if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != 
  1587                                     dynamic_pdos[pdo_type]["sync_manager"]["pdos"].append(pdo)
  1630                                     dynamic_pdos[pdo_type]["sync_manager"]["pdos"].append(pdo)
  1588                                     dynamic_pdos[pdo_type]["pdos"].append(pdo)
  1631                                     dynamic_pdos[pdo_type]["pdos"].append(pdo)
  1589                                 
  1632                                 
  1590                                 pdo["entries"].append("    {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos)
  1633                                 pdo["entries"].append("    {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos)
  1591                                 if entry_infos["bitlen"] < alignment:
  1634                                 if entry_infos["bitlen"] < alignment:
       
  1635                                     print (alignment, entry_infos["bitlen"])
  1592                                     pdo["entries"].append("    {0x0000, 0x00, %d}, /* None */" % (alignment - entry_infos["bitlen"]))
  1636                                     pdo["entries"].append("    {0x0000, 0x00, %d}, /* None */" % (alignment - entry_infos["bitlen"]))
  1593                                 pdo["entries_number"] += 1
  1637                                 pdo["entries_number"] += 1
  1594                                 
  1638                                 
  1595                                 if pdo["entries_number"] == 255:
  1639                                 if pdo["entries_number"] == 255:
  1596                                     dynamic_pdos[pdo_type]["pdos"].pop(0)
  1640                                     dynamic_pdos[pdo_type]["pdos"].pop(0)
  2011             for row in reader:
  2055             for row in reader:
  2012                 if has_header:
  2056                 if has_header:
  2013                     has_header = False
  2057                     has_header = False
  2014                 else:
  2058                 else:
  2015                     try:
  2059                     try:
  2016                         self.Alignments[tuple(map(int, row[:3]))] = row[3]
  2060                         self.Alignments[tuple(map(int, row[:3]))] = int(row[3])
  2017                     except:
  2061                     except:
  2018                         pass
  2062                         pass
  2019             csvfile.close()
  2063             csvfile.close()
  2020         
  2064         
  2021     def SaveAlignments(self):
  2065     def SaveAlignments(self):