PLCGenerator.py
changeset 78 049f2e7090a2
parent 72 73212220ad22
child 80 c798a68c5560
equal deleted inserted replaced
77:346a43f179a5 78:049f2e7090a2
    50             if line != "":
    50             if line != "":
    51                 compute += "%s%s\n"%(indent, line)
    51                 compute += "%s%s\n"%(indent, line)
    52             else:
    52             else:
    53                 compute += "\n"
    53                 compute += "\n"
    54     return compute
    54     return compute
       
    55 
       
    56 
       
    57 def GeneratePouProgram(pou_name):
       
    58     if not pouComputed.get(pou_name, True):
       
    59         pouComputed[pou_name] = True
       
    60         global currentProject, currentProgram
       
    61         pou = currentProject.getPou(pou_name)
       
    62         pou_type = pou.getPouType().getValue()
       
    63         if pou_type in pouTypeNames:
       
    64             pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
       
    65         else:
       
    66             raise ValueError, "Undefined pou type"
       
    67         pou_program.GenerateInterface(pou.getInterface())
       
    68         pou_program.GenerateProgram(pou)
       
    69         currentProgram += pou_program.GenerateSTProgram()
       
    70 
       
    71 def GenerateConfiguration(configuration):
       
    72     config = "\nCONFIGURATION %s\n"%configuration.getName()
       
    73     for varlist in configuration.getGlobalVars():
       
    74         config += "  VAR_GLOBAL"
       
    75         if varlist.getRetain():
       
    76             config += " RETAIN"
       
    77         if varlist.getConstant():
       
    78             config += " CONSTANT"
       
    79         config += "\n"
       
    80         for var in varlist.getVariable():
       
    81             var_type = var.getType().getValue()
       
    82             config += "    %s "%var.getName()
       
    83             address = var.getAddress()
       
    84             if address:
       
    85                 config += "AT %s "%address
       
    86             config += ": %s"%var_type
       
    87             initial = var.getInitialValue()
       
    88             if initial:
       
    89                 value = str(initial.getValue())
       
    90                 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
    91                 if var_type == "STRING":
       
    92                     config += " := '%s'"%value
       
    93                 elif var_type == "WSTRING":
       
    94                     config += " := \"%s\""%value
       
    95                 else:
       
    96                     config += " := %s"%value
       
    97             config += ";\n"
       
    98         config += "  END_VAR\n"
       
    99     for resource in configuration.getResource():
       
   100         config += GenerateResource(resource)
       
   101     config += "END_CONFIGURATION\n"
       
   102     return config
       
   103     
       
   104 def GenerateResource(resource):
       
   105     resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
       
   106     for varlist in resource.getGlobalVars():
       
   107         resrce += "    VAR_GLOBAL"
       
   108         if varlist.getRetain():
       
   109             resrce += " RETAIN"
       
   110         if varlist.getConstant():
       
   111             resrce += " CONSTANT"
       
   112         resrce += "\n"
       
   113         for var in varlist.getVariable():
       
   114             var_type = var.getType().getValue()
       
   115             resrce += "      %s "%var.getName()
       
   116             address = var.getAddress()
       
   117             if address:
       
   118                 resrce += "AT %s "%address
       
   119             resrce += ": %s"%var.getType().getValue()
       
   120             initial = var.getInitialValue()
       
   121             if initial:
       
   122                 value = str(initial.getValue())
       
   123                 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   124                 if var_type == "STRING":
       
   125                     resrce += " := '%s'"%value
       
   126                 elif var_type == "WSTRING":
       
   127                     resrce += " := \"%s\""%value
       
   128                 else:
       
   129                     resrce += " := %s"%value
       
   130             resrce += ";\n"
       
   131         resrce += "    END_VAR\n"
       
   132     tasks = resource.getTask()
       
   133     for task in tasks:
       
   134         resrce += "    TASK %s("%task.getName()
       
   135         args = []
       
   136         single = task.getSingle()
       
   137         if single:
       
   138             args.append("SINGLE := %s"%single)
       
   139         interval = task.getInterval()
       
   140         if interval:
       
   141             text = "t#"
       
   142             if interval.hour != 0:
       
   143                 text += "%dh"%interval.hour
       
   144             if interval.minute != 0:
       
   145                 text += "%dm"%interval.minute
       
   146             if interval.second != 0:
       
   147                 text += "%ds"%interval.second
       
   148             if interval.microsecond != 0:
       
   149                 text += "%dms"%(interval.microsecond / 1000)
       
   150             args.append("INTERVAL := %s"%text)
       
   151         args.append("PRIORITY := %s"%str(task.priority.getValue()))
       
   152         resrce += ",".join(args) + ");\n"
       
   153     for task in tasks:
       
   154         for instance in task.getPouInstance():
       
   155             resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
       
   156     for instance in resource.getPouInstance():
       
   157         resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
       
   158     resrce += "  END_RESOURCE\n"
       
   159     return resrce
    55 
   160 
    56 """
   161 """
    57 Module implementing methods for generating PLC programs in ST or IL
   162 Module implementing methods for generating PLC programs in ST or IL
    58 """
   163 """
    59 
   164 
   118                     var = instance.getExpression()
   223                     var = instance.getExpression()
   119                     connections = instance.connectionPointIn.getConnections()
   224                     connections = instance.connectionPointIn.getConnections()
   120                     if connections and len(connections) == 1:
   225                     if connections and len(connections) == 1:
   121                         expression = self.ComputeFBDExpression(body, connections[0])
   226                         expression = self.ComputeFBDExpression(body, connections[0])
   122                         self.Program += "  %s := %s;\n"%(var, expression)
   227                         self.Program += "  %s := %s;\n"%(var, expression)
   123                 if isinstance(instance, plcopen.connector):
   228                 elif isinstance(instance, plcopen.block):
       
   229                     type = instance.getTypeName()
       
   230                     block_infos = GetBlockType(type)
       
   231                     block_infos["generate"](self, instance, body, None)
       
   232                 elif isinstance(instance, plcopen.connector):
   124                     connector = instance.getName()
   233                     connector = instance.getName()
   125                     if self.ComputedConnectors.get(connector, None):
   234                     if self.ComputedConnectors.get(connector, None):
   126                         continue 
   235                         continue 
   127                     connections = instance.connectionPointIn.getConnections()
   236                     connections = instance.connectionPointIn.getConnections()
   128                     if connections and len(connections) == 1:
   237                     if connections and len(connections) == 1:
   159         localid = link.getRefLocalId()
   268         localid = link.getRefLocalId()
   160         instance = body.getContentInstance(localid)
   269         instance = body.getContentInstance(localid)
   161         if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
   270         if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
   162             return instance.getExpression()
   271             return instance.getExpression()
   163         elif isinstance(instance, plcopen.block):
   272         elif isinstance(instance, plcopen.block):
   164             name = instance.getInstanceName()
       
   165             type = instance.getTypeName()
   273             type = instance.getTypeName()
   166             block_infos = GetBlockType(type)
   274             block_infos = GetBlockType(type)
   167             if block_infos["type"] == "function":
   275             return block_infos["generate"](self, instance, body, link)
   168                 GeneratePouProgram(type)
       
   169                 vars = []
       
   170                 for variable in instance.inputVariables.getVariable():
       
   171                     connections = variable.connectionPointIn.getConnections()
       
   172                     if connections and len(connections) == 1:
       
   173                         value = self.ComputeFBDExpression(body, connections[0])
       
   174                         vars.append(self.ExtractModifier(variable, value))
       
   175                 variable = instance.outputVariables.getVariable()[0]
       
   176                 return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars)))
       
   177             elif block_infos["type"] == "functionBlock":
       
   178                 if not self.ComputedBlocks.get(name, False):
       
   179                     vars = []
       
   180                     for variable in instance.inputVariables.getVariable():
       
   181                         connections = variable.connectionPointIn.getConnections()
       
   182                         if connections and len(connections) == 1:
       
   183                             parameter = variable.getFormalParameter()
       
   184                             value = self.ComputeFBDExpression(body, connections[0])
       
   185                             vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value)))
       
   186                     self.Program += "  %s(%s);\n"%(name, ", ".join(vars))
       
   187                     self.ComputedBlocks[name] = True
       
   188                 connectionPoint = link.getPosition()[-1]
       
   189                 for variable in instance.outputVariables.getVariable():
       
   190                     blockPointx, blockPointy = variable.connectionPointOut.getRelPosition()
       
   191                     if instance.getX() + blockPointx == connectionPoint.getX() and instance.getY() + blockPointy == connectionPoint.getY():
       
   192                         return self.ExtractModifier(variable, "%s.%s"%(name, variable.getFormalParameter()))
       
   193                 raise ValueError, "No output variable found"
       
   194         elif isinstance(instance, plcopen.continuation):
   276         elif isinstance(instance, plcopen.continuation):
   195             name = instance.getName()
   277             name = instance.getName()
   196             computed_value = self.ComputedConnectors.get(name, None)
   278             computed_value = self.ComputedConnectors.get(name, None)
   197             if computed_value != None:
   279             if computed_value != None:
   198                 return computed_value
   280                 return computed_value
   555         program += "\n"
   637         program += "\n"
   556         program += self.Program
   638         program += self.Program
   557         program += "END_%s\n\n"%self.Type
   639         program += "END_%s\n\n"%self.Type
   558         return program
   640         return program
   559 
   641 
   560 def GeneratePouProgram(pou_name):
   642     def GeneratePouProgram(self, pou_name):
   561     if not pouComputed.get(pou_name, True):
   643         GeneratePouProgram(pou_name)
   562         pouComputed[pou_name] = True
       
   563         global currentProject, currentProgram
       
   564         pou = currentProject.getPou(pou_name)
       
   565         pou_type = pou.getPouType().getValue()
       
   566         if pou_type in pouTypeNames:
       
   567             pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
       
   568         else:
       
   569             raise ValueError, "Undefined pou type"
       
   570         pou_program.GenerateInterface(pou.getInterface())
       
   571         pou_program.GenerateProgram(pou)
       
   572         currentProgram += pou_program.GenerateSTProgram()
       
   573 
       
   574 def GenerateConfiguration(configuration):
       
   575     config = "\nCONFIGURATION %s\n"%configuration.getName()
       
   576     for varlist in configuration.getGlobalVars():
       
   577         config += "  VAR_GLOBAL"
       
   578         if varlist.getRetain():
       
   579             config += " RETAIN"
       
   580         if varlist.getConstant():
       
   581             config += " CONSTANT"
       
   582         config += "\n"
       
   583         for var in varlist.getVariable():
       
   584             var_type = var.getType().getValue()
       
   585             config += "    %s "%var.getName()
       
   586             address = var.getAddress()
       
   587             if address:
       
   588                 config += "AT %s "%address
       
   589             config += ": %s"%var_type
       
   590             initial = var.getInitialValue()
       
   591             if initial:
       
   592                 value = str(initial.getValue())
       
   593                 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   594                 if var_type == "STRING":
       
   595                     config += " := '%s'"%value
       
   596                 elif var_type == "WSTRING":
       
   597                     config += " := \"%s\""%value
       
   598                 else:
       
   599                     config += " := %s"%value
       
   600             config += ";\n"
       
   601         config += "  END_VAR\n"
       
   602     for resource in configuration.getResource():
       
   603         config += GenerateResource(resource)
       
   604     config += "END_CONFIGURATION\n"
       
   605     return config
       
   606     
       
   607 def GenerateResource(resource):
       
   608     resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
       
   609     for varlist in resource.getGlobalVars():
       
   610         resrce += "    VAR_GLOBAL"
       
   611         if varlist.getRetain():
       
   612             resrce += " RETAIN"
       
   613         if varlist.getConstant():
       
   614             resrce += " CONSTANT"
       
   615         resrce += "\n"
       
   616         for var in varlist.getVariable():
       
   617             var_type = var.getType().getValue()
       
   618             resrce += "      %s "%var.getName()
       
   619             address = var.getAddress()
       
   620             if address:
       
   621                 resrce += "AT %s "%address
       
   622             resrce += ": %s"%var.getType().getValue()
       
   623             initial = var.getInitialValue()
       
   624             if initial:
       
   625                 value = str(initial.getValue())
       
   626                 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   627                 if var_type == "STRING":
       
   628                     resrce += " := '%s'"%value
       
   629                 elif var_type == "WSTRING":
       
   630                     resrce += " := \"%s\""%value
       
   631                 else:
       
   632                     resrce += " := %s"%value
       
   633             resrce += ";\n"
       
   634         resrce += "    END_VAR\n"
       
   635     tasks = resource.getTask()
       
   636     for task in tasks:
       
   637         resrce += "    TASK %s("%task.getName()
       
   638         args = []
       
   639         single = task.getSingle()
       
   640         if single:
       
   641             args.append("SINGLE := %s"%single)
       
   642         interval = task.getInterval()
       
   643         if interval:
       
   644             text = "t#"
       
   645             if interval.hour != 0:
       
   646                 text += "%dh"%interval.hour
       
   647             if interval.minute != 0:
       
   648                 text += "%dm"%interval.minute
       
   649             if interval.second != 0:
       
   650                 text += "%ds"%interval.second
       
   651             if interval.microsecond != 0:
       
   652                 text += "%dms"%(interval.microsecond / 1000)
       
   653             args.append("INTERVAL := %s"%text)
       
   654         args.append("PRIORITY := %s"%str(task.priority.getValue()))
       
   655         resrce += ",".join(args) + ");\n"
       
   656     for task in tasks:
       
   657         for instance in task.getPouInstance():
       
   658             resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
       
   659     for instance in resource.getPouInstance():
       
   660         resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
       
   661     resrce += "  END_RESOURCE\n"
       
   662     return resrce
       
   663 
   644 
   664 def GenerateCurrentProgram(project):
   645 def GenerateCurrentProgram(project):
   665     global currentProject, currentProgram
   646     global currentProject, currentProgram
   666     currentProject = project
   647     currentProject = project
   667     currentProgram = ""
   648     currentProgram = ""