plcopen/structures.py
changeset 14 cd0133ed377b
parent 9 b29105e29081
child 15 fc897b7bfa7b
equal deleted inserted replaced
13:69075340d6a9 14:cd0133ed377b
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 import string, os, sys
     3 
     5 
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
     6 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
     5 #based on the plcopen standard. 
     7 #based on the plcopen standard. 
     6 #
     8 #
     7 #Copyright (C): Edouard TISSERANT and Laurent BESSARD
     9 #Copyright (C): Edouard TISSERANT and Laurent BESSARD
    42     - The name
    44     - The name
    43     - The data type
    45     - The data type
    44     - The default modifier which can be "none", "negated", "rising" or "falling"
    46     - The default modifier which can be "none", "negated", "rising" or "falling"
    45 """
    47 """
    46 
    48 
    47 BlockTypes = [{"name" : "Numerical functions", "list":
    49 BlockTypes = [{"name" : "Standard function blocks", "list":
    48                [{"name" : "ADD", "type" : "function", "extensible" : True, 
       
    49                     "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
       
    50                     "outputs" : [("OUT","ANY_NUM","none")],
       
    51                     "comment" : "Addition,\nresult := I1 + I2 + ..."},
       
    52                 {"name" : "MUL", "type" : "function", "extensible" : True, 
       
    53                     "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
       
    54                     "outputs" : [("OUT","ANY_NUM","none")],
       
    55                     "comment" : "Multiplication,\nresult := I1 * I2 * ..."},
       
    56                 {"name" : "SUB", "type" : "function", "extensible" : False, 
       
    57                     "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
       
    58                     "outputs" : [("OUT","ANY_NUM","none")],
       
    59                     "comment" : "Substration,\nresult := I1 - I2"},
       
    60                 {"name" : "DIV", "type" : "function", "extensible" : False, 
       
    61                     "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")], 
       
    62                     "outputs" : [("OUT","ANY_NUM","none")],
       
    63                     "comment" : "Division,\nresult := I1 / I2\n(with integer division, any fractional remainder is truncated)"},
       
    64                 {"name" : "MOD", "type" : "function", "extensible" : False, 
       
    65                     "inputs" : [("IN1","ANY_INT","none"),("IN2","ANY_INT","none")], 
       
    66                     "outputs" : [("OUT","ANY_INT","none")],
       
    67                     "comment" : "Modulus,\nresult := I1 MOD I2\n(only valid with integer values)"},
       
    68                 {"name" : "EXPT", "type" : "function", "extensible" : False, 
       
    69                     "inputs" : [("IN1","ANY_REAL","none"),("IN2","ANY_INT","none")], 
       
    70                     "outputs" : [("OUT","ANY_REAL","none")],
       
    71                     "comment" : "Exponential,\nresult := I1 ^ I2\n(can only take integer exponents)"},
       
    72                 {"name" : "ABS", "type" : "function", "extensible" : False, 
       
    73                     "inputs" : [("IN","ANY_NUM","none")], 
       
    74                     "outputs" : [("OUT","ANY_NUM","none")],
       
    75                     "comment" : "Absolute value (negative values become positive)"},
       
    76                 {"name" : "SQRT", "type" : "function", "extensible" : False, 
       
    77                     "inputs" : [("IN","ANY_REAL","none")], 
       
    78                     "outputs" : [("OUT","ANY_REAL","none")],
       
    79                     "comment" : "Square root"},
       
    80                 {"name" : "LOG", "type" : "function", "extensible" : False, 
       
    81                     "inputs" : [("IN","ANY_REAL","none")], 
       
    82                     "outputs" : [("OUT","ANY_REAL","none")],
       
    83                     "comment" : "Logarithm"},
       
    84                 {"name" : "LN", "type" : "function", "extensible" : False, 
       
    85                     "inputs" : [("IN","ANY_REAL","none")], 
       
    86                     "outputs" : [("OUT","ANY_REAL","none")],
       
    87                     "comment" : "Natural logarithm"},
       
    88                 {"name" : "EXP", "type" : "function", "extensible" : False, 
       
    89                     "inputs" : [("IN","ANY_REAL","none")], 
       
    90                     "outputs" : [("OUT","ANY_REAL","none")],
       
    91                     "comment" : "Natural exponential"},
       
    92                 {"name" : "SIN", "type" : "function", "extensible" : False, 
       
    93                     "inputs" : [("IN","ANY_REAL","none")], 
       
    94                     "outputs" : [("OUT","ANY_REAL","none")],
       
    95                     "comment" : "Sine of input as radians"},
       
    96                 {"name" : "COS", "type" : "function", "extensible" : False, 
       
    97                     "inputs" : [("IN","ANY_REAL","none")], 
       
    98                     "outputs" : [("OUT","ANY_REAL","none")],
       
    99                     "comment" : "Cosine of input as radians"},
       
   100                 {"name" : "TAN", "type" : "function", "extensible" : False, 
       
   101                     "inputs" : [("IN","ANY_REAL","none")], 
       
   102                     "outputs" : [("OUT","ANY_REAL","none")],
       
   103                     "comment" : "Tangent of input as radians"},
       
   104                 {"name" : "ASIN", "type" : "function", "extensible" : False, 
       
   105                     "inputs" : [("IN","ANY_REAL","none")], 
       
   106                     "outputs" : [("OUT","ANY_REAL","none")],
       
   107                     "comment" : "Principal arc-sine, result in radians"},
       
   108                 {"name" : "ACOS", "type" : "function", "extensible" : False, 
       
   109                     "inputs" : [("IN","ANY_REAL","none")], 
       
   110                     "outputs" : [("OUT","ANY_REAL","none")],
       
   111                     "comment" : "Principal arc-cosine, result in radians"},
       
   112                 {"name" : "ATAN", "type" : "function", "extensible" : False, 
       
   113                     "inputs" : [("IN","ANY_REAL","none")], 
       
   114                     "outputs" : [("OUT","ANY_REAL","none")],
       
   115                     "comment" : "Principal arc-tangent, result in radians"}
       
   116                ]},
       
   117               {"name" : "Boolean and bit functions", "list":
       
   118                [{"name" : "AND", "type" : "function", "extensible" : True, 
       
   119                     "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")], 
       
   120                     "outputs" : [("OUT","ANY_BIT","none")],
       
   121                     "comment" : "Result := I1 & I2 & ..."},
       
   122                 {"name" : "OR", "type" : "function", "extensible" : True, 
       
   123                     "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")], 
       
   124                     "outputs" : [("OUT","ANY_BIT","none")],
       
   125                     "comment" : "Result := I1 OR I2 OR ..."},
       
   126                 {"name" : "XOR", "type" : "function", "extensible" : True, 
       
   127                     "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")], 
       
   128                     "outputs" : [("OUT","ANY_BIT","none")],
       
   129                     "comment" : "Result := I1 XOR I2 XOR ..."},
       
   130                 {"name" : "NOT", "type" : "function", "extensible" : False, 
       
   131                     "inputs" : [("IN","BIT","none")], 
       
   132                     "outputs" : [("OUT","BIT","none")],
       
   133                     "comment" : "Result := NOT I1"},
       
   134                 {"name" : "SHL", "type" : "function", "extensible" : False, 
       
   135                     "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
       
   136                     "outputs" : [("OUT","ANY_BIT","none")],
       
   137                     "comment" : "Shift bit-string n bit positions left, zero fill on the right."},
       
   138                 {"name" : "SHR", "type" : "function", "extensible" : False, 
       
   139                     "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
       
   140                     "outputs" : [("OUT","ANY_BIT")],
       
   141                     "comment" : "Shift bit-string right n bit positions, zero fill on the left."},
       
   142                 {"name" : "ROR", "type" : "function", "extensible" : False, 
       
   143                     "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
       
   144                     "outputs" : [("OUT","ANY_BIT","none")],
       
   145                     "comment" : "Shift bit-string right, rotate by n bit positions."},
       
   146                 {"name" : "ROL", "type" : "function", "extensible" : False, 
       
   147                     "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")], 
       
   148                     "outputs" : [("OUT","ANY_BIT","none")],
       
   149                     "comment" : "Shift bit-string left, rotate by n bit positions."}
       
   150                ]},
       
   151               {"name" : "Selection functions", "list":
       
   152                [{"name" : "SEL", "type" : "function", "extensible" : False, 
       
   153                     "inputs" : [("G","BOOL","none"),("IN0","ANY","none"),("IN1","ANY","none")], 
       
   154                     "outputs" : [("OUT","ANY","none")],
       
   155                     "comment" : "Selection\nIf G = TRUE then\nResult := IN1 else\nResult := IN2"},
       
   156                 {"name" : "MAX", "type" : "function", "extensible" : True, 
       
   157                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   158                     "outputs" : [("OUT","ANY","none")],
       
   159                     "comment" : "Maximum\nResult := maximum value of all inputs"},
       
   160                 {"name" : "MIN", "type" : "function", "extensible" : True, 
       
   161                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   162                     "outputs" : [("","ANY","none")],
       
   163                     "comment" : "Minimum\nResult := minimum value of all inputs"},
       
   164                 {"name" : "LIMIT", "type" : "function", "extensible" : False, 
       
   165                     "inputs" : [("MN","ANY","none"),("IN","ANY","none"),("MX","ANY","none")], 
       
   166                     "outputs" : [("OUT","ANY","none")],
       
   167                     "comment" : "Limit\nResult is the value of IN limited between a minimum value of MN and a maximum value of MX."},
       
   168                 {"name" : "MUX", "type" : "function", "extensible" : True, 
       
   169                     "inputs" : [("K","ANY_INT","none"),("IN1","ANY","none"),("IN2","ANY","none")], 
       
   170                     "outputs" : [("OUT","ANY","none")],
       
   171                     "comment" : "Multiplexer\nResult is the value of the input selected by the value of K."}
       
   172                ]},
       
   173               {"name" : "Comparison functions", "list":
       
   174                [{"name" : "GT", "type" : "function", "extensible" : True, 
       
   175                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   176                     "outputs" : [("OUT","ANY","none")],
       
   177                     "comment" : "Greater than\nResult := IN1 > IN2"},
       
   178                 {"name" : "GE", "type" : "function", "extensible" : True, 
       
   179                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   180                     "outputs" : [("OUT","ANY","none")],
       
   181                     "comment" : "Greater than or equal\nResult := IN1 >= IN2"},
       
   182                 {"name" : "EQ", "type" : "function", "extensible" : True, 
       
   183                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   184                     "outputs" : [("OUT","ANY","none")],
       
   185                     "comment" : "Equality\nResult := IN1 = IN2"},
       
   186                 {"name" : "LE", "type" : "function", "extensible" : True, 
       
   187                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   188                     "outputs" : [("OUT","ANY","none")],
       
   189                     "comment" : "Lesser than or equal\nResult := IN1 <= IN2"},
       
   190                 {"name" : "LT", "type" : "function", "extensible" : True, 
       
   191                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   192                     "outputs" : [("OUT","ANY","none")],
       
   193                     "comment" : "Lesser than\nResult := IN1 < IN2"},
       
   194                 {"name" : "NE", "type" : "function", "extensible" : False, 
       
   195                     "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")], 
       
   196                     "outputs" : [("OUT","ANY","none")],
       
   197                     "comment" : "Not equal\nResult := IN1 <> IN2"}
       
   198                ]},
       
   199               {"name" : "Character string functions", "list":
       
   200                [{"name" : "LEFT", "type" : "function", "extensible" : False, 
       
   201                     "inputs" : [("IN","STRING","none"),("L","ANY_INT","none")], 
       
   202                     "outputs" : [("OUT","STRING","none")],
       
   203                     "comment" : "Extract left string\nResult is the string formed from L characters from the leftmost character of string IN."},
       
   204                 {"name" : "RIGHT", "type" : "function", "extensible" : False, 
       
   205                     "inputs" : [("IN","STRING","none"),("L","ANY_INT","none")], 
       
   206                     "outputs" : [("OUT","STRING","none")],
       
   207                     "comment" : "Extract right string\nResult is the string formed from L characters from the rightmost part of string IN."},
       
   208                 {"name" : "MID", "type" : "function", "extensible" : False, 
       
   209                     "inputs" : [("IN","ANY","none"),("L","ANY_INT","none"),("P","ANY_INT","none")], 
       
   210                     "outputs" : [("OUT","STRING","none")],
       
   211                     "comment" : "Extract mid string\nResult is a string extracted from the input string IN starting at character position P, and L characters long."},
       
   212                 {"name" : "CONCAT", "type" : "function", "extensible" : True, 
       
   213                     "inputs" : [("IN1","STRING","none"),("IN2","STRING","none")], 
       
   214                     "outputs" : [("OUT","STRING","none")],
       
   215                     "comment" : "Concatenate strings\nResult is a string formed by joining the input strings together. This is an extensible function that can take two or more input strings."},
       
   216                 {"name" : "INSERT", "type" : "function", "extensible" : False, 
       
   217                     "inputs" : [("IN1","STRING","none"),("IN2","STRING","none"),("P","ANY_INT","none")], 
       
   218                     "outputs" : [("OUT","STRING","none")],
       
   219                     "comment" : "Insert string\nThe result is formed by the string IN2 being inserted into string IN1, P character positions from the start of IN1."},
       
   220                 {"name" : "DELETE", "type" : "function", "extensible" : False, 
       
   221                     "inputs" : [("IN","STRING","none"),("L","ANY_INT","none"),("P","ANY_INT","none")], 
       
   222                     "outputs" : [("OUT","STRING","none")],
       
   223                     "comment" : "Delete string\nThe result is formed by a string of characters L in length, being deleted from the input string IN, starting from character position P."},
       
   224                 {"name" : "REPLACE", "type" : "function", "extensible" : False, 
       
   225                     "inputs" : [("IN1","STRING","none"),("IN2","STRING","none"),("L","ANY_INT","none"),("P","ANY_INT","none")], 
       
   226                     "outputs" : [("OUT","STRING","none")],
       
   227                     "comment" : "Replace string\nThe result is formed by replacing L characters in string IN1, starting at position P, with character string in IN2."},
       
   228                 {"name" : "LEN", "type" : "function", "extensible" : False, 
       
   229                     "inputs" : [("IN","STRING","none")], 
       
   230                     "outputs" : [("OUT","INT","none")],
       
   231                     "comment" : "Length\nResult is length of the input string."},
       
   232                 {"name" : "FIND", "type" : "function", "extensible" : False, 
       
   233                     "inputs" : [("IN1","STRING","none"),("IN2","STRING","none")], 
       
   234                     "outputs" : [("OUT","INT","none")],
       
   235                     "comment" : "Find string\nResult is the position where string IN2 is first found in string IN1.\nIf string IN2 is not found in IN1, the result is 0."},
       
   236                ]},
       
   237               {"name" : "Standard function blocks", "list":
       
   238                [{"name" : "SR", "type" : "functionBlock", "extensible" : False, 
    50                [{"name" : "SR", "type" : "functionBlock", "extensible" : False, 
   239                     "inputs" : [("S1","BOOL","none"),("R","BOOL","none")], 
    51                     "inputs" : [("S1","BOOL","none"),("R","BOOL","none")], 
   240                     "outputs" : [("Q1","BOOL","none")],
    52                     "outputs" : [("Q1","BOOL","none")],
   241                     "comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates."},
    53                     "comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates."},
   242                 {"name" : "RS", "type" : "functionBlock", "extensible" : False, 
    54                 {"name" : "RS", "type" : "functionBlock", "extensible" : False, 
   375         if test == reference:
   187         if test == reference:
   376             return True
   188             return True
   377         test = TypeHierarchy[test]
   189         test = TypeHierarchy[test]
   378     return False
   190     return False
   379 
   191 
   380 
   192 def GetSubTypes(reference):
       
   193     return [ typename for typename in TypeHierarchy.iterkeys() if typename[:3] != "ANY" and IsOfType(typename, reference)]
       
   194 
       
   195 
       
   196 
       
   197 
       
   198             
       
   199 def csv_file_to_table(file):
       
   200 	return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
       
   201 
       
   202 def find_section(section_name, table):
       
   203 	fields = [None]
       
   204 	while(fields[0] != section_name):
       
   205 		fields = table.pop(0)
       
   206 	return fields[1:]
       
   207 
       
   208 
       
   209 def get_standard_funtions_input_variables(table):
       
   210 	variables = find_section("Standard_functions_variables_types", table)
       
   211 	standard_funtions_input_variables = {}
       
   212 	fields = [True,True]
       
   213 	while(fields[1]):
       
   214 		fields = table.pop(0)
       
   215 		variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:])])
       
   216 		standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
       
   217 	return standard_funtions_input_variables
       
   218 	
       
   219 #"(ANY_NUM, ANY_NUM)" ---> [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] 
       
   220 def csv_input_translate(str_decl, variables, base):
       
   221 	decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
       
   222 	param_types = []
       
   223 	param_names = []
       
   224 	modifiers = []
       
   225 	if len(decl)>1 : suffix = str(base)
       
   226 	else: suffix = ''
       
   227 	
       
   228 	for param_type in decl:
       
   229 		predifined_variable_param_type = variables.get(param_type,None)
       
   230 		if predifined_variable_param_type :
       
   231 			param_types.append(predifined_variable_param_type)
       
   232 			param_names.append(param_type)
       
   233 		else:
       
   234 			param_types.append(param_type)
       
   235 			param_names.append("IN"+suffix)
       
   236 			base+=1
       
   237 			suffix = str(base)
       
   238 
       
   239 	modifiers = ["none"]*len(param_types)
       
   240 	return zip(param_names,param_types,modifiers)
       
   241 
       
   242 def decl_function(dico_from_table, variables):
       
   243 	Function_decl = { "type" : "function" }
       
   244 	for field, val in dico_from_table:
       
   245 		translate = {
       
   246 			"baseinputnumber" : lambda x:int(x),
       
   247 			"extensible" : lambda x: {"yes":True, "no":False}[x],
       
   248 			"inputs" : lambda x:csv_input_translate(x,variables,Function_decl.get("baseinputnumber",1)),
       
   249 			"outputs":lambda x:[("OUT",x,"none")]}
       
   250 		Function_decl[field] = translate.get(field,lambda x:x)(val)
       
   251 	#Function_decl.pop("baseinputnumber")
       
   252 	Function_decl.pop("overloaded")
       
   253 	return Function_decl
       
   254 
       
   255 
       
   256 def get_standard_funtions(table):
       
   257 	
       
   258 	variables = get_standard_funtions_input_variables(table)
       
   259 	
       
   260 	fonctions = find_section("Standard_functions_type",table)
       
   261 
       
   262 	Standard_Functions_Decl = []
       
   263 	Current_section = None
       
   264 	for fields in table:
       
   265 		if fields[1]:
       
   266 			# If function section name given
       
   267 			if fields[0] :
       
   268 				if Current_section: 
       
   269 					Standard_Functions_Decl.append(Current_section)
       
   270 				Current_section = { "name" : fields[0], "list" : [] }
       
   271 				Function_decl_list = []
       
   272 		
       
   273 			dico_from_table = zip(fonctions, fields[1:])
       
   274 			Function_decl = decl_function(dico_from_table,variables)
       
   275 			
       
   276 			if Function_decl["name"].startswith("*"):
       
   277 				input_types = GetSubTypes(Function_decl["inputs"][0][1])
       
   278 			else:
       
   279 				input_types = [None]
       
   280 			if Function_decl["name"].endswith("**"):
       
   281 				output_types = GetSubTypes(Function_decl["outputs"][0][1])
       
   282 			else:
       
   283 				output_types = [None]
       
   284 				
       
   285 			funcdecl = Function_decl["name"]
       
   286 			for intype in input_types:
       
   287 				if intype != None:
       
   288 					decl_tpl = Function_decl["inputs"][0]
       
   289 					Function_decl["inputs"] = [decl_tpl[:1] + (intype,) + decl_tpl[2:]] + Function_decl["inputs"][1:]
       
   290 					funcdeclin = funcdecl.replace("*_", intype + '_')
       
   291 					Function_decl["name"] = funcdeclin
       
   292 
       
   293 				for outype in output_types:
       
   294 					if outype != None:
       
   295 						decl_tpl = Function_decl["outputs"][0]
       
   296 						Function_decl["outputs"] = [decl_tpl[:1] + (outype,) + decl_tpl[2:]] + Function_decl["outputs"][1:]
       
   297 						funcdeclout = funcdeclin.replace("_**", '_' + outype)
       
   298 						Function_decl["name"] = funcdeclout
       
   299 					Current_section["list"].append(Function_decl.copy())
       
   300 	
       
   301 	return Standard_Functions_Decl
       
   302 
       
   303 
       
   304 to_append=get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv"))))
       
   305 import pprint
       
   306 pp = pprint.PrettyPrinter(indent=4)
       
   307 pp.pprint(to_append)
       
   308 BlockTypes.extend(to_append)
       
   309 pp.pprint(BlockTypes)
   381 #-------------------------------------------------------------------------------
   310 #-------------------------------------------------------------------------------
   382 #                             Test identifier
   311 #                             Test identifier
   383 #-------------------------------------------------------------------------------
   312 #-------------------------------------------------------------------------------
   384 
   313 
   385 
   314