author | edouard |
Mon, 02 Nov 2009 18:51:27 +0100 | |
changeset 457 | 91b28dd0c9c9 |
parent 431 | c1c92d068ac5 |
child 483 | 779a519f78f2 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
4 |
import string, os, sys |
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
5 |
|
0 | 6 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
7 |
#based on the plcopen standard. |
|
8 |
# |
|
58 | 9 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 10 |
# |
11 |
#See COPYING file for copyrights details. |
|
12 |
# |
|
13 |
#This library is free software; you can redistribute it and/or |
|
5 | 14 |
#modify it under the terms of the GNU General Public |
0 | 15 |
#License as published by the Free Software Foundation; either |
16 |
#version 2.1 of the License, or (at your option) any later version. |
|
17 |
# |
|
18 |
#This library is distributed in the hope that it will be useful, |
|
19 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 21 |
#General Public License for more details. |
0 | 22 |
# |
5 | 23 |
#You should have received a copy of the GNU General Public |
0 | 24 |
#License along with this library; if not, write to the Free Software |
25 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
26 |
||
27 |
||
9 | 28 |
LANGUAGES = ["IL","ST","FBD","LD","SFC"] |
29 |
||
104 | 30 |
LOCATIONDATATYPES = {"X" : ["BOOL"], |
31 |
"B" : ["SINT", "USINT", "BYTE", "STRING"], |
|
32 |
"W" : ["INT", "UINT", "WORD", "WSTRING"], |
|
33 |
"D" : ["DINT", "UDINT", "REAL", "DWORD"], |
|
34 |
"L" : ["LINT", "ULINT", "LREAL", "LWORD"]} |
|
35 |
||
391 | 36 |
_ = lambda x:x |
37 |
||
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
38 |
# Helper for emulate join on element list |
305
0ed2b61de43e
Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents:
284
diff
changeset
|
39 |
def JoinList(separator, mylist): |
0ed2b61de43e
Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents:
284
diff
changeset
|
40 |
if len(mylist) > 0 : |
0ed2b61de43e
Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents:
284
diff
changeset
|
41 |
return reduce(lambda x, y: x + separator + y, mylist) |
0ed2b61de43e
Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents:
284
diff
changeset
|
42 |
else : |
0ed2b61de43e
Fixed bug structures.py crashing with non-connected blocks.
etisserant
parents:
284
diff
changeset
|
43 |
return mylist |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
44 |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
45 |
def generate_block(generator, block, body, link, order=False): |
151 | 46 |
body_type = body.getcontent()["name"] |
47 |
name = block.getinstanceName() |
|
48 |
type = block.gettypeName() |
|
49 |
executionOrderId = block.getexecutionOrderId() |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
50 |
block_infos = generator.GetBlockType(type) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
51 |
if block_infos["type"] == "function": |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
52 |
output_variables = block.outputVariables.getvariable() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
53 |
if not generator.ComputedBlocks.get(block, False) and not order: |
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
194
diff
changeset
|
54 |
generator.ComputedBlocks[block] = True |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
55 |
vars = [] |
307
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
56 |
one_input_connected = False |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
57 |
for i, variable in enumerate(block.inputVariables.getvariable()): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
58 |
input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
151 | 59 |
connections = variable.connectionPointIn.getconnections() |
250 | 60 |
if connections is not None: |
307
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
61 |
parameter = variable.getformalParameter() |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
62 |
if parameter != "EN": |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
63 |
one_input_connected = True |
250 | 64 |
value = generator.ComputeExpression(body, connections, executionOrderId > 0) |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
65 |
if len(output_variables) > 1: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
66 |
vars.append([(parameter, input_info), |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
67 |
(" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
68 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
69 |
vars.append(generator.ExtractModifier(variable, value, input_info)) |
307
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
70 |
if one_input_connected: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
71 |
for i, variable in enumerate(output_variables): |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
72 |
parameter = variable.getformalParameter() |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
73 |
if variable.getformalParameter() == "": |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
74 |
variable_name = "%s%d"%(type, block.getlocalId()) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
75 |
else: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
76 |
variable_name = "%s%d_%s"%(type, block.getlocalId(), parameter) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
77 |
if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] or generator.Interface[-1][2] or generator.Interface[-1][3]: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
78 |
generator.Interface.append(("VAR", False, False, False, [])) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
79 |
if variable.connectionPointOut in generator.ConnectionTypes: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
80 |
generator.Interface[-1][4].append((generator.ConnectionTypes[variable.connectionPointOut], variable_name, None, None)) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
81 |
else: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
82 |
generator.Interface[-1][4].append(("ANY", variable_name, None, None)) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
83 |
if len(output_variables) > 1 and parameter not in ["", "OUT"]: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
84 |
vars.append([(parameter, (generator.TagName, "block", block.getlocalId(), "output", i)), |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
85 |
(" => %s"%variable_name, ())]) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
86 |
else: |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
87 |
output_info = (generator.TagName, "block", block.getlocalId(), "output", i) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
88 |
output_name = variable_name |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
89 |
generator.Program += [(generator.CurrentIndent, ()), |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
90 |
(output_name, output_info), |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
91 |
(" := ", ()), |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
92 |
(type, (generator.TagName, "block", block.getlocalId(), "type")), |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
93 |
("(", ())] |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
94 |
generator.Program += JoinList([(", ", ())], vars) |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
95 |
generator.Program += [(");\n", ())] |
fd1f6ae26d4f
Adding support for cancelling code generation of function with no input connected
lbessard
parents:
305
diff
changeset
|
96 |
else: |
391 | 97 |
generator.Warnings.append(_("\"%s\" function cancelled in \"%s\" POU: No input connected")%(type, generator.TagName.split("::")[-1])) |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
98 |
if link: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
99 |
connectionPoint = link.getposition()[-1] |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
100 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
101 |
connectionPoint = None |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
102 |
for i, variable in enumerate(output_variables): |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
103 |
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
104 |
if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety(): |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
105 |
output_info = (generator.TagName, "block", block.getlocalId(), "output", i) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
106 |
output_name = variable.getformalParameter() |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
107 |
if variable.getformalParameter() == "": |
279
2ad276084038
Bug involving possible name conflict between return value and ouputs fixed
lbessard
parents:
270
diff
changeset
|
108 |
output_name = "%s%d"%(type, block.getlocalId()) |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
109 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
110 |
output_name = "%s%d_%s"%(type, block.getlocalId(), variable.getformalParameter()) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
111 |
return generator.ExtractModifier(variable, [(output_name, output_info)], output_info) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
112 |
elif block_infos["type"] == "functionBlock": |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
113 |
if not generator.ComputedBlocks.get(block, False) and not order: |
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
194
diff
changeset
|
114 |
generator.ComputedBlocks[block] = True |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
115 |
vars = [] |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
116 |
for i, variable in enumerate(block.inputVariables.getvariable()): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
117 |
input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
151 | 118 |
connections = variable.connectionPointIn.getconnections() |
250 | 119 |
if connections is not None: |
151 | 120 |
parameter = variable.getformalParameter() |
250 | 121 |
value = generator.ComputeExpression(body, connections, executionOrderId > 0) |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
122 |
vars.append([(parameter, input_info), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
123 |
(" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
124 |
generator.Program += [(generator.CurrentIndent, ()), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
125 |
(name, (generator.TagName, "block", block.getlocalId(), "name")), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
126 |
("(", ())] |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
127 |
generator.Program += JoinList([(", ", ())], vars) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
128 |
generator.Program += [(");\n", ())] |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
129 |
if link: |
151 | 130 |
connectionPoint = link.getposition()[-1] |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
131 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
132 |
connectionPoint = None |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
133 |
for i, variable in enumerate(block.outputVariables.getvariable()): |
151 | 134 |
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
135 |
if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety(): |
|
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
136 |
output_info = (generator.TagName, "block", block.getlocalId(), "output", i) |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
137 |
return generator.ExtractModifier(variable, [("%s.%s"%(name, variable.getformalParameter()), output_info)], output_info) |
354 | 138 |
if link is not None: |
391 | 139 |
raise ValueError, _("No output variable found") |
21 | 140 |
|
194
1b3f8b4f8e04
Adding support for Beremiz svgui plugin variable declaration
lbessard
parents:
170
diff
changeset
|
141 |
def initialise_block(type, name, block = None): |
93 | 142 |
return [(type, name, None, None)] |
143 |
||
0 | 144 |
#------------------------------------------------------------------------------- |
145 |
# Function Block Types definitions |
|
146 |
#------------------------------------------------------------------------------- |
|
147 |
||
21 | 148 |
|
0 | 149 |
""" |
150 |
Ordored list of common Function Blocks defined in the IEC 61131-3 |
|
151 |
Each block have this attributes: |
|
152 |
- "name" : The block name |
|
153 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
154 |
- "extensible" : Boolean that define if the block is extensible |
|
155 |
- "inputs" : List of the block inputs |
|
156 |
- "outputs" : List of the block outputs |
|
157 |
- "comment" : Comment that will be displayed in the block popup |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
158 |
- "generate" : Method that generator will call for generating ST block code |
0 | 159 |
Inputs and outputs are a tuple of characteristics that are in order: |
160 |
- The name |
|
161 |
- The data type |
|
162 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
163 |
""" |
|
164 |
||
391 | 165 |
BlockTypes = [{"name" : _("Standard function blocks"), "list": |
0 | 166 |
[{"name" : "SR", "type" : "functionBlock", "extensible" : False, |
167 |
"inputs" : [("S1","BOOL","none"),("R","BOOL","none")], |
|
168 |
"outputs" : [("Q1","BOOL","none")], |
|
391 | 169 |
"comment" : _("SR bistable\nThe SR bistable is a latch where the Set dominates."), |
93 | 170 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 171 |
{"name" : "RS", "type" : "functionBlock", "extensible" : False, |
172 |
"inputs" : [("S","BOOL","none"),("R1","BOOL","none")], |
|
173 |
"outputs" : [("Q1","BOOL","none")], |
|
391 | 174 |
"comment" : _("RS bistable\nThe RS bistable is a latch where the Reset dominates."), |
93 | 175 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 176 |
{"name" : "SEMA", "type" : "functionBlock", "extensible" : False, |
177 |
"inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], |
|
178 |
"outputs" : [("BUSY","BOOL","none")], |
|
391 | 179 |
"comment" : _("Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."), |
93 | 180 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 181 |
{"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, |
182 |
"inputs" : [("CLK","BOOL","none")], |
|
183 |
"outputs" : [("Q","BOOL","none")], |
|
391 | 184 |
"comment" : _("Rising edge detector\nThe output produces a single pulse when a rising edge is detected."), |
93 | 185 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 186 |
{"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, |
187 |
"inputs" : [("CLK","BOOL","none")], |
|
188 |
"outputs" : [("Q","BOOL","none")], |
|
391 | 189 |
"comment" : _("Falling edge detector\nThe output produces a single pulse when a falling edge is detected."), |
93 | 190 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 191 |
{"name" : "CTU", "type" : "functionBlock", "extensible" : False, |
192 |
"inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], |
|
193 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
391 | 194 |
"comment" : _("Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."), |
93 | 195 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 196 |
{"name" : "CTD", "type" : "functionBlock", "extensible" : False, |
197 |
"inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], |
|
198 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
391 | 199 |
"comment" : _("Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."), |
93 | 200 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 201 |
{"name" : "CTUD", "type" : "functionBlock", "extensible" : False, |
202 |
"inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], |
|
203 |
"outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")], |
|
391 | 204 |
"comment" : _("Up-down counter\nThe up-down counter has two inputs CU and CD. It can be used to both count up on one input and down on the other."), |
93 | 205 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 206 |
{"name" : "TP", "type" : "functionBlock", "extensible" : False, |
207 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
208 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
391 | 209 |
"comment" : _("Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."), |
93 | 210 |
"generate" : generate_block, "initialise" : initialise_block}, |
247
4a47ccafbef7
Inversion between TON and TOF comments and errors in IL keywords fixed
lbessard
parents:
242
diff
changeset
|
211 |
{"name" : "TON", "type" : "functionBlock", "extensible" : False, |
0 | 212 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
213 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
391 | 214 |
"comment" : _("On-delay timer\nThe on-delay timer can be used to delay setting an output true, for fixed period after an input becomes true."), |
93 | 215 |
"generate" : generate_block, "initialise" : initialise_block}, |
247
4a47ccafbef7
Inversion between TON and TOF comments and errors in IL keywords fixed
lbessard
parents:
242
diff
changeset
|
216 |
{"name" : "TOF", "type" : "functionBlock", "extensible" : False, |
0 | 217 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
218 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
391 | 219 |
"comment" : _("Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."), |
93 | 220 |
"generate" : generate_block, "initialise" : initialise_block}, |
373 | 221 |
]}, |
427 | 222 |
{"name" : _("Additional function blocks"), "list": |
371 | 223 |
## {"name" : "RTC", "type" : "functionBlock", "extensible" : False, |
224 |
## "inputs" : [("EN","BOOL","none"),("PDT","DATE_AND_TIME","none")], |
|
225 |
## "outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")], |
|
391 | 226 |
## "comment" : _("Real time clock\nThe real time clock has many uses including time stamping, setting dates and times of day in batch reports, in alarm messages and so on."), |
371 | 227 |
## "generate" : generate_block, "initialise" : initialise_block}, |
373 | 228 |
[{"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, |
0 | 229 |
"inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], |
230 |
"outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")], |
|
391 | 231 |
"comment" : _("Integral\nThe integral function block integrates the value of input XIN over time."), |
93 | 232 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 233 |
{"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, |
234 |
"inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], |
|
235 |
"outputs" : [("XOUT","REAL","none")], |
|
391 | 236 |
"comment" : _("Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."), |
93 | 237 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 238 |
{"name" : "PID", "type" : "functionBlock", "extensible" : False, |
239 |
"inputs" : [("AUTO","BOOL","none"),("PV","REAL","none"),("SP","REAL","none"),("X0","REAL","none"),("KP","REAL","none"),("TR","REAL","none"),("TD","REAL","none"),("CYCLE","TIME","none")], |
|
240 |
"outputs" : [("XOUT","REAL","none")], |
|
391 | 241 |
"comment" : _("PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."), |
93 | 242 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 243 |
{"name" : "RAMP", "type" : "functionBlock", "extensible" : False, |
244 |
"inputs" : [("RUN","BOOL","none"),("X0","REAL","none"),("X1","REAL","none"),("TR","TIME","none"),("CYCLE","TIME","none"),("HOLDBACK","BOOL","none"),("ERROR","REAL","none"),("PV","REAL","none")], |
|
245 |
"outputs" : [("RAMP","BOOL","none"),("XOUT","REAL","none")], |
|
391 | 246 |
"comment" : _("Ramp\nThe RAMP function block is modelled on example given in the standard but with the addition of a 'Holdback' feature."), |
93 | 247 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 248 |
{"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, |
249 |
"inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], |
|
250 |
"outputs" : [("Q","BOOL","none")], |
|
391 | 251 |
"comment" : _("Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."), |
93 | 252 |
"generate" : generate_block, "initialise" : initialise_block}, |
371 | 253 |
## {"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, |
254 |
## "inputs" : [("PV1","REAL","none"),("PV2","REAL","none"),("RATIO","REAL","none"),("TIMON","TIME","none"),("TIMOFF","TIME","none"),("TOLERANCE","BOOL","none"),("RESET","BOOL","none"),("CYCLE","TIME","none")], |
|
255 |
## "outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")], |
|
391 | 256 |
## "comment" : _("Ratio monitor\nThe ratio_monitor function block checks that one process value PV1 is always a given ratio (defined by input RATIO) of a second process value PV2."), |
371 | 257 |
## "generate" : generate_block, "initialise" : initialise_block} |
57 | 258 |
]}, |
0 | 259 |
] |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
260 |
|
0 | 261 |
|
262 |
#------------------------------------------------------------------------------- |
|
263 |
# Data Types definitions |
|
264 |
#------------------------------------------------------------------------------- |
|
265 |
||
266 |
""" |
|
267 |
Ordored list of common data types defined in the IEC 61131-3 |
|
268 |
Each type is associated to his direct parent type. It defines then a hierarchy |
|
269 |
between type that permits to make a comparison of two types |
|
270 |
""" |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
271 |
TypeHierarchy_list = [ |
22 | 272 |
("ANY", None), |
273 |
("ANY_DERIVED", "ANY"), |
|
274 |
("ANY_ELEMENTARY", "ANY"), |
|
275 |
("ANY_MAGNITUDE", "ANY_ELEMENTARY"), |
|
276 |
("ANY_BIT", "ANY_ELEMENTARY"), |
|
25 | 277 |
("ANY_NBIT", "ANY_BIT"), |
22 | 278 |
("ANY_STRING", "ANY_ELEMENTARY"), |
279 |
("ANY_DATE", "ANY_ELEMENTARY"), |
|
280 |
("ANY_NUM", "ANY_MAGNITUDE"), |
|
281 |
("ANY_REAL", "ANY_NUM"), |
|
282 |
("ANY_INT", "ANY_NUM"), |
|
25 | 283 |
("ANY_SINT", "ANY_INT"), |
284 |
("ANY_UINT", "ANY_INT"), |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
285 |
("BOOL", "ANY_BIT"), |
25 | 286 |
("SINT", "ANY_SINT"), |
287 |
("INT", "ANY_SINT"), |
|
288 |
("DINT", "ANY_SINT"), |
|
289 |
("LINT", "ANY_SINT"), |
|
290 |
("USINT", "ANY_UINT"), |
|
291 |
("UINT", "ANY_UINT"), |
|
292 |
("UDINT", "ANY_UINT"), |
|
293 |
("ULINT", "ANY_UINT"), |
|
27 | 294 |
("REAL", "ANY_REAL"), |
295 |
("LREAL", "ANY_REAL"), |
|
22 | 296 |
("TIME", "ANY_MAGNITUDE"), |
27 | 297 |
("DATE", "ANY_DATE"), |
298 |
("TOD", "ANY_DATE"), |
|
299 |
("DT", "ANY_DATE"), |
|
300 |
("STRING", "ANY_STRING"), |
|
25 | 301 |
("BYTE", "ANY_NBIT"), |
302 |
("WORD", "ANY_NBIT"), |
|
303 |
("DWORD", "ANY_NBIT"), |
|
27 | 304 |
("LWORD", "ANY_NBIT") |
305 |
#("WSTRING", "ANY_STRING") # TODO |
|
306 |
] |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
307 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
308 |
TypeHierarchy = dict(TypeHierarchy_list) |
0 | 309 |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
310 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
311 |
returns true if the given data type is the same that "reference" meta-type or one of its types. |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
312 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
313 |
def IsOfType(type, reference): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
314 |
if reference is None: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
315 |
return True |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
316 |
elif type == reference: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
317 |
return True |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
318 |
else: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
319 |
parent_type = TypeHierarchy[type] |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
320 |
if parent_type is not None: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
321 |
return IsOfType(parent_type, reference) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
322 |
return False |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
323 |
|
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
324 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
325 |
returns list of all types that correspont to the ANY* meta type |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
326 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
327 |
def GetSubTypes(type): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
328 |
return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)] |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
329 |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
330 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
331 |
DataTypeRange_list = [ |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
332 |
("SINT", (-2**7, 2**7 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
333 |
("INT", (-2**15, 2**15 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
334 |
("DINT", (-2**31, 2**31 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
335 |
("LINT", (-2**31, 2**31 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
336 |
("USINT", (0, 2**8 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
337 |
("UINT", (0, 2**16 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
338 |
("UDINT", (0, 2**31 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
339 |
("ULINT", (0, 2**31 - 1)) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
340 |
] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
341 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
342 |
DataTypeRange = dict(DataTypeRange_list) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
343 |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
344 |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
345 |
|
21 | 346 |
#------------------------------------------------------------------------------- |
347 |
# Test identifier |
|
348 |
#------------------------------------------------------------------------------- |
|
349 |
||
350 |
||
351 |
||
352 |
# Test if identifier is valid |
|
353 |
def TestIdentifier(identifier): |
|
354 |
if identifier[0].isdigit(): |
|
355 |
return False |
|
356 |
words = identifier.split('_') |
|
357 |
for i, word in enumerate(words): |
|
358 |
if len(word) == 0 and i != 0: |
|
359 |
return False |
|
360 |
if len(word) != 0 and not word.isalnum(): |
|
361 |
return False |
|
362 |
return True |
|
363 |
||
364 |
||
365 |
#------------------------------------------------------------------------------- |
|
109
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
366 |
# Standard functions list generation |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
367 |
#------------------------------------------------------------------------------- |
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
368 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
369 |
|
15 | 370 |
""" |
371 |
take a .csv file and translate it it a "csv_table" |
|
372 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
373 |
def csv_file_to_table(file): |
22 | 374 |
return [ map(string.strip,line.split(';')) for line in file.xreadlines()] |
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
375 |
|
15 | 376 |
""" |
377 |
seek into the csv table to a section ( section_name match 1st field ) |
|
378 |
return the matching row without first field |
|
379 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
380 |
def find_section(section_name, table): |
22 | 381 |
fields = [None] |
382 |
while(fields[0] != section_name): |
|
383 |
fields = table.pop(0) |
|
384 |
return fields[1:] |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
385 |
|
15 | 386 |
""" |
387 |
extract the standard functions standard parameter names and types... |
|
388 |
return a { ParameterName: Type, ...} |
|
389 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
390 |
def get_standard_funtions_input_variables(table): |
22 | 391 |
variables = find_section("Standard_functions_variables_types", table) |
392 |
standard_funtions_input_variables = {} |
|
393 |
fields = [True,True] |
|
394 |
while(fields[1]): |
|
395 |
fields = table.pop(0) |
|
396 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!='']) |
|
397 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
|
398 |
return standard_funtions_input_variables |
|
399 |
||
15 | 400 |
""" |
401 |
translate .csv file input declaration into PLCOpenEditor interessting values |
|
402 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
|
403 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
|
404 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
405 |
def csv_input_translate(str_decl, variables, base): |
22 | 406 |
decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') |
407 |
params = [] |
|
408 |
||
409 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
|
410 |
||
411 |
for param_type in decl: |
|
412 |
if param_type in variables.keys(): |
|
413 |
param_name = param_type |
|
414 |
param_type = variables[param_type] |
|
415 |
elif len_of_not_predifined_variable > 1: |
|
416 |
param_name = "IN%d"%base |
|
417 |
base += 1 |
|
418 |
else: |
|
419 |
param_name = "IN" |
|
420 |
params.append((param_name, param_type, "none")) |
|
421 |
return params |
|
422 |
||
423 |
||
25 | 424 |
ANY_TO_ANY_LIST=[ |
425 |
# simple type conv are let as C cast |
|
405 | 426 |
(("ANY_NUM","ANY_BIT"),("ANY_NUM","ANY_BIT"), ("return_type", "__move_", "IN_type")), |
25 | 427 |
# TO_TIME |
284 | 428 |
(("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), ("return_type", "__int_to_time", None)), |
429 |
(("ANY_REAL",),("ANY_DATE","TIME"), ("return_type", "__real_to_time", None)), |
|
430 |
(("ANY_STRING",), ("ANY_DATE","TIME"), ("return_type", "__string_to_time", None)), |
|
25 | 431 |
# FROM_TIME |
284 | 432 |
(("ANY_DATE","TIME"), ("ANY_REAL",), ("return_type", "__time_to_real", None)), |
433 |
(("ANY_DATE","TIME"), ("ANY_INT","ANY_NBIT"), ("return_type", "__time_to_int", None)), |
|
434 |
(("TIME",), ("ANY_STRING",), ("return_type", "__time_to_string", None)), |
|
435 |
(("DATE",), ("ANY_STRING",), ("return_type", "__date_to_string", None)), |
|
436 |
(("TOD",), ("ANY_STRING",), ("return_type", "__tod_to_string", None)), |
|
437 |
(("DT",), ("ANY_STRING",), ("return_type", "__dt_to_string", None)), |
|
25 | 438 |
# TO_STRING |
284 | 439 |
(("BOOL",), ("ANY_STRING",), ("return_type", "__bool_to_string", None)), |
440 |
(("ANY_BIT",), ("ANY_STRING",), ("return_type", "__bit_to_string", None)), |
|
441 |
(("ANY_REAL",), ("ANY_STRING",), ("return_type", "__real_to_string", None)), |
|
442 |
(("ANY_SINT",), ("ANY_STRING",), ("return_type", "__sint_to_string", None)), |
|
443 |
(("ANY_UINT",), ("ANY_STRING",), ("return_type", "__uint_to_string", None)), |
|
25 | 444 |
# FROM_STRING |
284 | 445 |
(("ANY_STRING",), ("BOOL",), ("return_type", "__string_to_bool", None)), |
446 |
(("ANY_STRING",), ("ANY_BIT",), ("return_type", "__string_to_bit", None)), |
|
447 |
(("ANY_STRING",), ("ANY_SINT",), ("return_type", "__string_to_sint", None)), |
|
448 |
(("ANY_STRING",), ("ANY_UINT",), ("return_type", "__string_to_uint", None)), |
|
449 |
(("ANY_STRING",), ("ANY_REAL",), ("return_type", "__string_to_real", None))] |
|
25 | 450 |
|
451 |
||
452 |
BCD_TO_ANY_LIST=[ |
|
284 | 453 |
(("BYTE",),("USINT",), ("return_type", "__bcd_to_uint", None)), |
454 |
(("WORD",),("UINT",), ("return_type", "__bcd_to_uint", None)), |
|
455 |
(("DWORD",),("UDINT",), ("return_type", "__bcd_to_uint", None)), |
|
456 |
(("LWORD",),("ULINT",), ("return_type", "__bcd_to_uint", None))] |
|
25 | 457 |
|
458 |
||
459 |
ANY_TO_BCD_LIST=[ |
|
284 | 460 |
(("USINT",),("BYTE",), ("return_type", "__uint_to_bcd", None)), |
461 |
(("UINT",),("WORD",), ("return_type", "__uint_to_bcd", None)), |
|
462 |
(("UDINT",),("DWORD",), ("return_type", "__uint_to_bcd", None)), |
|
463 |
(("ULINT",),("LWORD",), ("return_type", "__uint_to_bcd", None))] |
|
25 | 464 |
|
465 |
||
466 |
def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl): |
|
467 |
||
468 |
for (InTypes, OutTypes, Format) in any_to_any_list: |
|
22 | 469 |
outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes)) |
470 |
inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes)) |
|
471 |
if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]: |
|
472 |
return Format |
|
473 |
||
474 |
return None |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
475 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
476 |
|
15 | 477 |
""" |
478 |
Returns this kind of declaration for all standard functions |
|
479 |
||
480 |
[{"name" : "Numerical", 'list': [ { |
|
481 |
'baseinputnumber': 1, |
|
482 |
'comment': 'Addition', |
|
483 |
'extensible': True, |
|
484 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
485 |
('IN2', 'ANY_NUM', 'none')], |
|
486 |
'name': 'ADD', |
|
487 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
488 |
'type': 'function'}, ...... ] },.....] |
|
489 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
490 |
def get_standard_funtions(table): |
22 | 491 |
|
492 |
variables = get_standard_funtions_input_variables(table) |
|
493 |
||
494 |
fonctions = find_section("Standard_functions_type",table) |
|
495 |
||
496 |
Standard_Functions_Decl = [] |
|
497 |
Current_section = None |
|
498 |
||
499 |
translate = { |
|
500 |
"extensible" : lambda x: {"yes":True, "no":False}[x], |
|
501 |
"inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber), |
|
502 |
"outputs":lambda x:[("OUT",x,"none")]} |
|
503 |
||
504 |
for fields in table: |
|
505 |
if fields[1]: |
|
506 |
# If function section name given |
|
507 |
if fields[0]: |
|
391 | 508 |
words = fields[0].split('"') |
509 |
if len(words) > 1: |
|
510 |
section_name = words[1] |
|
511 |
else: |
|
512 |
section_name = fields[0] |
|
513 |
Current_section = {"name" : section_name, "list" : []} |
|
22 | 514 |
Standard_Functions_Decl.append(Current_section) |
515 |
Function_decl_list = [] |
|
516 |
if Current_section: |
|
517 |
Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ]) |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
518 |
Function_decl["generate"] = generate_block |
93 | 519 |
Function_decl["initialise"] = lambda x,y:[] |
22 | 520 |
baseinputnumber = int(Function_decl.get("baseinputnumber",1)) |
521 |
Function_decl["baseinputnumber"] = baseinputnumber |
|
522 |
for param, value in Function_decl.iteritems(): |
|
523 |
if param in translate: |
|
524 |
Function_decl[param] = translate[param](value) |
|
525 |
Function_decl["type"] = "function" |
|
526 |
||
25 | 527 |
if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') : |
22 | 528 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
25 | 529 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
22 | 530 |
else: |
531 |
input_ovrloading_types = [None] |
|
532 |
output_types = [None] |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
533 |
|
22 | 534 |
funcdeclname_orig = Function_decl["name"] |
535 |
funcdeclname = Function_decl["name"].strip('*_') |
|
536 |
fdc = Function_decl["inputs"][:] |
|
537 |
for intype in input_ovrloading_types: |
|
538 |
if intype != None: |
|
539 |
Function_decl["inputs"] = [] |
|
540 |
for decl_tpl in fdc: |
|
541 |
if IsOfType(intype, decl_tpl[1]): |
|
542 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
543 |
else: |
|
544 |
Function_decl["inputs"] += [(decl_tpl)] |
|
545 |
||
546 |
if funcdeclname_orig.startswith('*'): |
|
547 |
funcdeclin = intype + '_' + funcdeclname |
|
548 |
else: |
|
549 |
funcdeclin = funcdeclname |
|
550 |
else: |
|
551 |
funcdeclin = funcdeclname |
|
552 |
||
553 |
for outype in output_types: |
|
554 |
if outype != None: |
|
555 |
decl_tpl = Function_decl["outputs"][0] |
|
556 |
Function_decl["outputs"] = [ (decl_tpl[0] , outype, decl_tpl[2])] |
|
557 |
if funcdeclname_orig.endswith('*'): |
|
558 |
funcdeclout = funcdeclin + '_' + outype |
|
559 |
else: |
|
560 |
funcdeclout = funcdeclin |
|
561 |
else: |
|
562 |
funcdeclout = funcdeclin |
|
563 |
Function_decl["name"] = funcdeclout |
|
564 |
||
565 |
||
566 |
fdecl = Function_decl |
|
567 |
res = eval(Function_decl["python_eval_c_code_format"]) |
|
568 |
||
569 |
if res != None : |
|
570 |
# create the copy of decl dict to be appended to section |
|
571 |
Function_decl_copy = Function_decl.copy() |
|
572 |
# Have to generate type description in comment with freshly redefined types |
|
391 | 573 |
words = Function_decl_copy["comment"].split('"') |
574 |
if len(words) > 1: |
|
575 |
Function_decl_copy["comment"] = words[1] |
|
576 |
Function_decl_copy["usage"] = ( |
|
22 | 577 |
"\n (" + |
578 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') + |
|
579 |
" ) => (" + |
|
580 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') + |
|
581 |
" )") |
|
582 |
Current_section["list"].append(Function_decl_copy) |
|
583 |
#pp.pprint(Function_decl_copy) |
|
584 |
else: |
|
585 |
raise "First function must be in a category" |
|
586 |
||
587 |
return Standard_Functions_Decl |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
588 |
|
25 | 589 |
std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(os.path.split(__file__)[0],"iec_std.csv"))))#, True) |
590 |
||
591 |
BlockTypes.extend(std_decl) |
|
592 |
||
109
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
593 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
594 |
#------------------------------------------------------------------------------- |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
595 |
# Languages Keywords |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
596 |
#------------------------------------------------------------------------------- |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
597 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
598 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
599 |
# Keywords for Pou Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
600 |
POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
601 |
"PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
602 |
for category in BlockTypes: |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
603 |
for block in category["list"]: |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
604 |
if block["name"] not in POU_KEYWORDS: |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
605 |
POU_KEYWORDS.append(block["name"]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
606 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
607 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
608 |
# Keywords for Type Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
609 |
TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
610 |
"D", "TIME_OF_DAY", "DATE_AND_TIME"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
611 |
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
612 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
613 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
614 |
# Keywords for Variable Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
615 |
VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
616 |
"VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
617 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
618 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
619 |
# Keywords for Configuration Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
620 |
CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
621 |
"PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
622 |
"VAR_GLOBAL", "END_VAR"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
623 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
624 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
625 |
# Keywords for Structured Function Chart |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
626 |
SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
627 |
"FROM", "TO", "END_TRANSITION"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
628 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
629 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
630 |
# Keywords for Instruction List |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
631 |
IL_KEYWORDS = ["TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
632 |
"XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE", |
247
4a47ccafbef7
Inversion between TON and TOF comments and errors in IL keywords fixed
lbessard
parents:
242
diff
changeset
|
633 |
"LE", "LT", "JMP", "JMPC", "JMPCN", "CAL", "CALC", "CALCN", "RET", "RETC", "RETCN"] |
109
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
634 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
635 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
636 |
# Keywords for Structured Text |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
637 |
ST_KEYWORDS = ["TRUE", "FALSE", "IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
638 |
"FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
639 |
"END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
640 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
641 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
642 |
# All the keywords of IEC |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
643 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
644 |
IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
645 |
IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
646 |
IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
647 |
IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
648 |
IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
649 |
IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
650 |
IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
651 |