PLCGenerator.py
changeset 71 0578bc212c20
parent 70 0e48629c1e6d
child 72 73212220ad22
equal deleted inserted replaced
70:0e48629c1e6d 71:0578bc212c20
    30                 "outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL",
    30                 "outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL",
    31                 "globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"}
    31                 "globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"}
    32 
    32 
    33 pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"}
    33 pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"}
    34 
    34 
       
    35 currentProject = None
       
    36 currentProgram = ""
       
    37 pouComputed = {}
       
    38 
    35 def ReIndentText(text, nb_spaces):
    39 def ReIndentText(text, nb_spaces):
    36     compute = ""
    40     compute = ""
    37     lines = text.splitlines()
    41     lines = text.splitlines()
    38     if len(lines) > 0:
    42     if len(lines) > 0:
    39         spaces = 0
    43         spaces = 0
    86             located = False
    90             located = False
    87             for var in varlist["value"].getVariable():
    91             for var in varlist["value"].getVariable():
    88                 type = var.getType().getValue()
    92                 type = var.getType().getValue()
    89                 if not isinstance(type, (StringType, UnicodeType)):
    93                 if not isinstance(type, (StringType, UnicodeType)):
    90                     type = type.getName()
    94                     type = type.getName()
       
    95                     GeneratePouProgram(type)
    91                 initial = var.getInitialValue()
    96                 initial = var.getInitialValue()
    92                 if initial:
    97                 if initial:
    93                     initial_value = initial.getValue()
    98                     initial_value = initial.getValue()
    94                 else:
    99                 else:
    95                     initial_value = None
   100                     initial_value = None
   150         elif isinstance(instance, plcopen.block):
   155         elif isinstance(instance, plcopen.block):
   151             name = instance.getInstanceName()
   156             name = instance.getInstanceName()
   152             type = instance.getTypeName()
   157             type = instance.getTypeName()
   153             block_infos = GetBlockType(type)
   158             block_infos = GetBlockType(type)
   154             if block_infos["type"] == "function":
   159             if block_infos["type"] == "function":
       
   160                 GeneratePouProgram(type)
   155                 vars = []
   161                 vars = []
   156                 for variable in instance.inputVariables.getVariable():
   162                 for variable in instance.inputVariables.getVariable():
   157                     connections = variable.connectionPointIn.getConnections()
   163                     connections = variable.connectionPointIn.getConnections()
   158                     if connections and len(connections) == 1:
   164                     if connections and len(connections) == 1:
   159                         value = self.ComputeFBDExpression(body, connections[0])
   165                         value = self.ComputeFBDExpression(body, connections[0])
   526             program += "  END_VAR\n"
   532             program += "  END_VAR\n"
   527         program += "\n"
   533         program += "\n"
   528         program += self.Program
   534         program += self.Program
   529         program += "END_%s\n\n"%self.Type
   535         program += "END_%s\n\n"%self.Type
   530         return program
   536         return program
   531     
   537 
   532     def GenerateConfiguration(self, configuration):
   538 def GeneratePouProgram(pou_name):
   533         config = "\nCONFIGURATION %s\n"%configuration.getName()
   539     if not pouComputed.get(pou_name, True):
   534         for varlist in configuration.getGlobalVars():
   540         pouComputed[pou_name] = True
   535             config += "  VAR_GLOBAL"
   541         global currentProject, currentProgram
   536             if varlist.getRetain():
   542         pou = currentProject.getPou(pou_name)
   537                 config += " RETAIN"
       
   538             if varlist.getConstant():
       
   539                 config += " CONSTANT"
       
   540             config += "\n"
       
   541             for var in varlist.getVariable():
       
   542                 var_type = var.getType().getValue()
       
   543                 config += "    %s "%var.getName()
       
   544                 address = var.getAddress()
       
   545                 if address:
       
   546                     config += "AT %s "%address
       
   547                 config += ": %s"%var_type
       
   548                 initial = var.getInitialValue()
       
   549                 if initial:
       
   550                     value = str(initial.getValue())
       
   551                     value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   552                     if var_type == "STRING":
       
   553                         config += " := '%s'"%value
       
   554                     elif var_type == "WSTRING":
       
   555                         config += " := \"%s\""%value
       
   556                     else:
       
   557                         config += " := %s"%value
       
   558                 config += ";\n"
       
   559             config += "  END_VAR\n"
       
   560         for resource in configuration.getResource():
       
   561             config += self.GenerateResource(resource)
       
   562         config += "END_CONFIGURATION\n"
       
   563         return config
       
   564         
       
   565     def GenerateResource(self, resource):
       
   566         resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
       
   567         for varlist in resource.getGlobalVars():
       
   568             resrce += "    VAR_GLOBAL"
       
   569             if varlist.getRetain():
       
   570                 resrce += " RETAIN"
       
   571             if varlist.getConstant():
       
   572                 resrce += " CONSTANT"
       
   573             resrce += "\n"
       
   574             for var in varlist.getVariable():
       
   575                 var_type = var.getType().getValue()
       
   576                 resrce += "      %s "%var.getName()
       
   577                 address = var.getAddress()
       
   578                 if address:
       
   579                     resrce += "AT %s "%address
       
   580                 resrce += ": %s"%var.getType().getValue()
       
   581                 initial = var.getInitialValue()
       
   582                 if initial:
       
   583                     value = str(initial.getValue())
       
   584                     value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   585                     if var_type == "STRING":
       
   586                         resrce += " := '%s'"%value
       
   587                     elif var_type == "WSTRING":
       
   588                         resrce += " := \"%s\""%value
       
   589                     else:
       
   590                         resrce += " := %s"%value
       
   591                 resrce += ";\n"
       
   592             resrce += "    END_VAR\n"
       
   593         tasks = resource.getTask()
       
   594         for task in tasks:
       
   595             resrce += "    TASK %s("%task.getName()
       
   596             args = []
       
   597             single = task.getSingle()
       
   598             if single:
       
   599                 args.append("SINGLE := %s"%single)
       
   600             interval = task.getInterval()
       
   601             if interval:
       
   602                 text = "t#"
       
   603                 if interval.hour != 0:
       
   604                     text += "%dh"%interval.hour
       
   605                 if interval.minute != 0:
       
   606                     text += "%dm"%interval.minute
       
   607                 if interval.second != 0:
       
   608                     text += "%ds"%interval.second
       
   609                 if interval.microsecond != 0:
       
   610                     text += "%dms"%(interval.microsecond / 1000)
       
   611                 args.append("INTERVAL := %s"%text)
       
   612             args.append("PRIORITY := %s"%str(task.priority.getValue()))
       
   613             resrce += ",".join(args) + ");\n"
       
   614         for task in tasks:
       
   615             for instance in task.getPouInstance():
       
   616                 resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
       
   617         for instance in resource.getPouInstance():
       
   618             resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
       
   619         resrce += "  END_RESOURCE\n"
       
   620         return resrce
       
   621         
       
   622             
       
   623 def GenerateCurrentProgram(project):
       
   624     program = ""
       
   625     for pou in project.getPous():
       
   626         pou_type = pou.getPouType().getValue()
   543         pou_type = pou.getPouType().getValue()
   627         if pou_type in pouTypeNames:
   544         if pou_type in pouTypeNames:
   628             pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
   545             pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
   629         else:
   546         else:
   630             raise ValueError, "Undefined pou type"
   547             raise ValueError, "Undefined pou type"
   631         pou_program.GenerateInterface(pou.getInterface())
   548         pou_program.GenerateInterface(pou.getInterface())
   632         pou_program.GenerateProgram(pou)
   549         pou_program.GenerateProgram(pou)
   633         program += pou_program.GenerateSTProgram()
   550         currentProgram += pou_program.GenerateSTProgram()
       
   551 
       
   552 def GenerateConfiguration(configuration):
       
   553     config = "\nCONFIGURATION %s\n"%configuration.getName()
       
   554     for varlist in configuration.getGlobalVars():
       
   555         config += "  VAR_GLOBAL"
       
   556         if varlist.getRetain():
       
   557             config += " RETAIN"
       
   558         if varlist.getConstant():
       
   559             config += " CONSTANT"
       
   560         config += "\n"
       
   561         for var in varlist.getVariable():
       
   562             var_type = var.getType().getValue()
       
   563             config += "    %s "%var.getName()
       
   564             address = var.getAddress()
       
   565             if address:
       
   566                 config += "AT %s "%address
       
   567             config += ": %s"%var_type
       
   568             initial = var.getInitialValue()
       
   569             if initial:
       
   570                 value = str(initial.getValue())
       
   571                 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   572                 if var_type == "STRING":
       
   573                     config += " := '%s'"%value
       
   574                 elif var_type == "WSTRING":
       
   575                     config += " := \"%s\""%value
       
   576                 else:
       
   577                     config += " := %s"%value
       
   578             config += ";\n"
       
   579         config += "  END_VAR\n"
       
   580     for resource in configuration.getResource():
       
   581         config += GenerateResource(resource)
       
   582     config += "END_CONFIGURATION\n"
       
   583     return config
       
   584     
       
   585 def GenerateResource(resource):
       
   586     resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
       
   587     for varlist in resource.getGlobalVars():
       
   588         resrce += "    VAR_GLOBAL"
       
   589         if varlist.getRetain():
       
   590             resrce += " RETAIN"
       
   591         if varlist.getConstant():
       
   592             resrce += " CONSTANT"
       
   593         resrce += "\n"
       
   594         for var in varlist.getVariable():
       
   595             var_type = var.getType().getValue()
       
   596             resrce += "      %s "%var.getName()
       
   597             address = var.getAddress()
       
   598             if address:
       
   599                 resrce += "AT %s "%address
       
   600             resrce += ": %s"%var.getType().getValue()
       
   601             initial = var.getInitialValue()
       
   602             if initial:
       
   603                 value = str(initial.getValue())
       
   604                 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
       
   605                 if var_type == "STRING":
       
   606                     resrce += " := '%s'"%value
       
   607                 elif var_type == "WSTRING":
       
   608                     resrce += " := \"%s\""%value
       
   609                 else:
       
   610                     resrce += " := %s"%value
       
   611             resrce += ";\n"
       
   612         resrce += "    END_VAR\n"
       
   613     tasks = resource.getTask()
       
   614     for task in tasks:
       
   615         resrce += "    TASK %s("%task.getName()
       
   616         args = []
       
   617         single = task.getSingle()
       
   618         if single:
       
   619             args.append("SINGLE := %s"%single)
       
   620         interval = task.getInterval()
       
   621         if interval:
       
   622             text = "t#"
       
   623             if interval.hour != 0:
       
   624                 text += "%dh"%interval.hour
       
   625             if interval.minute != 0:
       
   626                 text += "%dm"%interval.minute
       
   627             if interval.second != 0:
       
   628                 text += "%ds"%interval.second
       
   629             if interval.microsecond != 0:
       
   630                 text += "%dms"%(interval.microsecond / 1000)
       
   631             args.append("INTERVAL := %s"%text)
       
   632         args.append("PRIORITY := %s"%str(task.priority.getValue()))
       
   633         resrce += ",".join(args) + ");\n"
       
   634     for task in tasks:
       
   635         for instance in task.getPouInstance():
       
   636             resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
       
   637     for instance in resource.getPouInstance():
       
   638         resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
       
   639     resrce += "  END_RESOURCE\n"
       
   640     return resrce
       
   641 
       
   642 def GenerateCurrentProgram(project):
       
   643     global currentProject, currentProgram
       
   644     currentProject = project
       
   645     currentProgram = ""
       
   646     for pou in project.getPous():
       
   647         pouComputed[pou.getName()] = False
       
   648     for pou_name in pouComputed.keys():
       
   649         GeneratePouProgram(pou_name)
   634     for config in project.getConfigurations():
   650     for config in project.getConfigurations():
   635         program += pou_program.GenerateConfiguration(config)
   651         currentProgram += GenerateConfiguration(config)
   636     return program
   652     return currentProgram
   637 
   653