author | lbessard |
Fri, 16 Jan 2009 13:22:07 +0100 | |
changeset 304 | 2df3d31d8059 |
parent 284 | 6cf858411d3a |
child 305 | 0ed2b61de43e |
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 |
||
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
36 |
# Helper for emulate join on element list |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
37 |
def JoinList(separator, list): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
38 |
return reduce(lambda x, y: x + separator + y, list) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
39 |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
40 |
def generate_block(generator, block, body, link, order=False): |
151 | 41 |
body_type = body.getcontent()["name"] |
42 |
name = block.getinstanceName() |
|
43 |
type = block.gettypeName() |
|
44 |
executionOrderId = block.getexecutionOrderId() |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
45 |
block_infos = generator.GetBlockType(type) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
46 |
if block_infos["type"] == "function": |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
47 |
output_variables = block.outputVariables.getvariable() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
48 |
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
|
49 |
generator.ComputedBlocks[block] = True |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
50 |
vars = [] |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
51 |
for i, variable in enumerate(block.inputVariables.getvariable()): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
52 |
input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
151 | 53 |
connections = variable.connectionPointIn.getconnections() |
250 | 54 |
if connections is not None: |
55 |
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
|
56 |
if len(output_variables) > 1: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
57 |
parameter = variable.getformalParameter() |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
58 |
vars.append([(parameter, input_info), |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
59 |
(" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
60 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
61 |
vars.append(generator.ExtractModifier(variable, value, input_info)) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
62 |
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
|
63 |
parameter = variable.getformalParameter() |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
64 |
if variable.getformalParameter() == "": |
279
2ad276084038
Bug involving possible name conflict between return value and ouputs fixed
lbessard
parents:
270
diff
changeset
|
65 |
variable_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
|
66 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
67 |
variable_name = "%s%d_%s"%(type, block.getlocalId(), parameter) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
68 |
if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] or generator.Interface[-1][2] or generator.Interface[-1][3]: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
69 |
generator.Interface.append(("VAR", False, False, False, [])) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
70 |
if variable.connectionPointOut in generator.ConnectionTypes: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
71 |
generator.Interface[-1][4].append((generator.ConnectionTypes[variable.connectionPointOut], variable_name, None, None)) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
72 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
73 |
generator.Interface[-1][4].append(("ANY", variable_name, None, None)) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
74 |
if len(output_variables) > 1 and parameter not in ["", "OUT"]: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
75 |
vars.append([(parameter, (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
|
76 |
(" => %s"%variable_name, ())]) |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
77 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
78 |
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
|
79 |
output_name = variable_name |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
80 |
generator.Program += [(generator.CurrentIndent, ()), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
81 |
(output_name, output_info), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
82 |
(" := ", ()), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
83 |
(type, (generator.TagName, "block", block.getlocalId(), "type")), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
84 |
("(", ())] |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
85 |
generator.Program += JoinList([(", ", ())], vars) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
86 |
generator.Program += [(");\n", ())] |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
87 |
if link: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
88 |
connectionPoint = link.getposition()[-1] |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
89 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
90 |
connectionPoint = None |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
91 |
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
|
92 |
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
93 |
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
|
94 |
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
|
95 |
output_name = variable.getformalParameter() |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
96 |
if variable.getformalParameter() == "": |
279
2ad276084038
Bug involving possible name conflict between return value and ouputs fixed
lbessard
parents:
270
diff
changeset
|
97 |
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
|
98 |
else: |
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
99 |
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
|
100 |
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
|
101 |
elif block_infos["type"] == "functionBlock": |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
102 |
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
|
103 |
generator.ComputedBlocks[block] = True |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
104 |
vars = [] |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
105 |
for i, variable in enumerate(block.inputVariables.getvariable()): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
106 |
input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
151 | 107 |
connections = variable.connectionPointIn.getconnections() |
250 | 108 |
if connections is not None: |
151 | 109 |
parameter = variable.getformalParameter() |
250 | 110 |
value = generator.ComputeExpression(body, connections, executionOrderId > 0) |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
111 |
vars.append([(parameter, input_info), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
112 |
(" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
113 |
generator.Program += [(generator.CurrentIndent, ()), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
114 |
(name, (generator.TagName, "block", block.getlocalId(), "name")), |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
115 |
("(", ())] |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
116 |
generator.Program += JoinList([(", ", ())], vars) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
117 |
generator.Program += [(");\n", ())] |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
118 |
if link: |
151 | 119 |
connectionPoint = link.getposition()[-1] |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
120 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
121 |
connectionPoint = None |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
122 |
for i, variable in enumerate(block.outputVariables.getvariable()): |
151 | 123 |
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
124 |
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
|
125 |
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
|
126 |
return generator.ExtractModifier(variable, [("%s.%s"%(name, variable.getformalParameter()), output_info)], output_info) |
270
351d134babd7
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
250
diff
changeset
|
127 |
raise ValueError, "No output variable found" |
21 | 128 |
|
194
1b3f8b4f8e04
Adding support for Beremiz svgui plugin variable declaration
lbessard
parents:
170
diff
changeset
|
129 |
def initialise_block(type, name, block = None): |
93 | 130 |
return [(type, name, None, None)] |
131 |
||
0 | 132 |
#------------------------------------------------------------------------------- |
133 |
# Function Block Types definitions |
|
134 |
#------------------------------------------------------------------------------- |
|
135 |
||
21 | 136 |
|
0 | 137 |
""" |
138 |
Ordored list of common Function Blocks defined in the IEC 61131-3 |
|
139 |
Each block have this attributes: |
|
140 |
- "name" : The block name |
|
141 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
142 |
- "extensible" : Boolean that define if the block is extensible |
|
143 |
- "inputs" : List of the block inputs |
|
144 |
- "outputs" : List of the block outputs |
|
145 |
- "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
|
146 |
- "generate" : Method that generator will call for generating ST block code |
0 | 147 |
Inputs and outputs are a tuple of characteristics that are in order: |
148 |
- The name |
|
149 |
- The data type |
|
150 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
151 |
""" |
|
152 |
||
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
153 |
BlockTypes = [{"name" : "Standard function blocks", "list": |
0 | 154 |
[{"name" : "SR", "type" : "functionBlock", "extensible" : False, |
155 |
"inputs" : [("S1","BOOL","none"),("R","BOOL","none")], |
|
156 |
"outputs" : [("Q1","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
157 |
"comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates.", |
93 | 158 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 159 |
{"name" : "RS", "type" : "functionBlock", "extensible" : False, |
160 |
"inputs" : [("S","BOOL","none"),("R1","BOOL","none")], |
|
161 |
"outputs" : [("Q1","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
162 |
"comment" : "RS bistable\nThe RS bistable is a latch where the Reset dominates.", |
93 | 163 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 164 |
{"name" : "SEMA", "type" : "functionBlock", "extensible" : False, |
165 |
"inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], |
|
166 |
"outputs" : [("BUSY","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
167 |
"comment" : "Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources.", |
93 | 168 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 169 |
{"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, |
170 |
"inputs" : [("CLK","BOOL","none")], |
|
171 |
"outputs" : [("Q","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
172 |
"comment" : "Rising edge detector\nThe output produces a single pulse when a rising edge is detected.", |
93 | 173 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 174 |
{"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, |
175 |
"inputs" : [("CLK","BOOL","none")], |
|
176 |
"outputs" : [("Q","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
177 |
"comment" : "Falling edge detector\nThe output produces a single pulse when a falling edge is detected.", |
93 | 178 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 179 |
{"name" : "CTU", "type" : "functionBlock", "extensible" : False, |
180 |
"inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], |
|
181 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
182 |
"comment" : "Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value.", |
93 | 183 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 184 |
{"name" : "CTD", "type" : "functionBlock", "extensible" : False, |
185 |
"inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], |
|
186 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
187 |
"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 | 188 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 189 |
{"name" : "CTUD", "type" : "functionBlock", "extensible" : False, |
190 |
"inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], |
|
191 |
"outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
192 |
"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 ans down on the other.", |
93 | 193 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 194 |
{"name" : "TP", "type" : "functionBlock", "extensible" : False, |
195 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
196 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
197 |
"comment" : "Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration.", |
93 | 198 |
"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
|
199 |
{"name" : "TON", "type" : "functionBlock", "extensible" : False, |
0 | 200 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
201 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
202 |
"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 | 203 |
"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
|
204 |
{"name" : "TOF", "type" : "functionBlock", "extensible" : False, |
0 | 205 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
206 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
207 |
"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 | 208 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 209 |
{"name" : "RTC", "type" : "functionBlock", "extensible" : False, |
210 |
"inputs" : [("EN","BOOL","none"),("PDT","DATE_AND_TIME","none")], |
|
211 |
"outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
212 |
"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.", |
93 | 213 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 214 |
{"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, |
215 |
"inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], |
|
216 |
"outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
217 |
"comment" : "Integral\nThe integral function block integrates the value of input XIN over time.", |
93 | 218 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 219 |
{"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, |
220 |
"inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], |
|
221 |
"outputs" : [("XOUT","REAL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
222 |
"comment" : "Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN.", |
93 | 223 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 224 |
{"name" : "PID", "type" : "functionBlock", "extensible" : False, |
225 |
"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")], |
|
226 |
"outputs" : [("XOUT","REAL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
227 |
"comment" : "PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control.", |
93 | 228 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 229 |
{"name" : "RAMP", "type" : "functionBlock", "extensible" : False, |
230 |
"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")], |
|
231 |
"outputs" : [("RAMP","BOOL","none"),("XOUT","REAL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
232 |
"comment" : "Ramp\nThe RAMP function block is modelled on example given in the standard but with the addition of a 'Holdback' feature.", |
93 | 233 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 234 |
{"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, |
235 |
"inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], |
|
236 |
"outputs" : [("Q","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
237 |
"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 | 238 |
"generate" : generate_block, "initialise" : initialise_block}, |
0 | 239 |
{"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, |
240 |
"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")], |
|
241 |
"outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")], |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
58
diff
changeset
|
242 |
"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.", |
93 | 243 |
"generate" : generate_block, "initialise" : initialise_block} |
57 | 244 |
]}, |
0 | 245 |
] |
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
246 |
|
0 | 247 |
|
248 |
#------------------------------------------------------------------------------- |
|
249 |
# Data Types definitions |
|
250 |
#------------------------------------------------------------------------------- |
|
251 |
||
252 |
""" |
|
253 |
Ordored list of common data types defined in the IEC 61131-3 |
|
254 |
Each type is associated to his direct parent type. It defines then a hierarchy |
|
255 |
between type that permits to make a comparison of two types |
|
256 |
""" |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
257 |
TypeHierarchy_list = [ |
22 | 258 |
("ANY", None), |
259 |
("ANY_DERIVED", "ANY"), |
|
260 |
("ANY_ELEMENTARY", "ANY"), |
|
261 |
("ANY_MAGNITUDE", "ANY_ELEMENTARY"), |
|
262 |
("ANY_BIT", "ANY_ELEMENTARY"), |
|
25 | 263 |
("ANY_NBIT", "ANY_BIT"), |
22 | 264 |
("ANY_STRING", "ANY_ELEMENTARY"), |
265 |
("ANY_DATE", "ANY_ELEMENTARY"), |
|
266 |
("ANY_NUM", "ANY_MAGNITUDE"), |
|
267 |
("ANY_REAL", "ANY_NUM"), |
|
268 |
("ANY_INT", "ANY_NUM"), |
|
25 | 269 |
("ANY_SINT", "ANY_INT"), |
270 |
("ANY_UINT", "ANY_INT"), |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
271 |
("BOOL", "ANY_BIT"), |
25 | 272 |
("SINT", "ANY_SINT"), |
273 |
("INT", "ANY_SINT"), |
|
274 |
("DINT", "ANY_SINT"), |
|
275 |
("LINT", "ANY_SINT"), |
|
276 |
("USINT", "ANY_UINT"), |
|
277 |
("UINT", "ANY_UINT"), |
|
278 |
("UDINT", "ANY_UINT"), |
|
279 |
("ULINT", "ANY_UINT"), |
|
27 | 280 |
("REAL", "ANY_REAL"), |
281 |
("LREAL", "ANY_REAL"), |
|
22 | 282 |
("TIME", "ANY_MAGNITUDE"), |
27 | 283 |
("DATE", "ANY_DATE"), |
284 |
("TOD", "ANY_DATE"), |
|
285 |
("DT", "ANY_DATE"), |
|
286 |
("STRING", "ANY_STRING"), |
|
25 | 287 |
("BYTE", "ANY_NBIT"), |
288 |
("WORD", "ANY_NBIT"), |
|
289 |
("DWORD", "ANY_NBIT"), |
|
27 | 290 |
("LWORD", "ANY_NBIT") |
291 |
#("WSTRING", "ANY_STRING") # TODO |
|
292 |
] |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
293 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
294 |
TypeHierarchy = dict(TypeHierarchy_list) |
0 | 295 |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
296 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
297 |
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
|
298 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
299 |
def IsOfType(type, reference): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
300 |
if reference is None: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
301 |
return True |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
302 |
elif type == reference: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
303 |
return True |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
304 |
else: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
305 |
parent_type = TypeHierarchy[type] |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
306 |
if parent_type is not None: |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
307 |
return IsOfType(parent_type, reference) |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
308 |
return False |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
309 |
|
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 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
|
312 |
""" |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
313 |
def GetSubTypes(type): |
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
314 |
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
|
315 |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
316 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
317 |
DataTypeRange_list = [ |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
318 |
("SINT", (-2**7, 2**7 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
319 |
("INT", (-2**15, 2**15 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
320 |
("DINT", (-2**31, 2**31 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
321 |
("LINT", (-2**31, 2**31 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
322 |
("USINT", (0, 2**8 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
323 |
("UINT", (0, 2**16 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
324 |
("UDINT", (0, 2**31 - 1)), |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
325 |
("ULINT", (0, 2**31 - 1)) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
326 |
] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
327 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
328 |
DataTypeRange = dict(DataTypeRange_list) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
329 |
|
230
45d70748e45a
Moving Data types and POU types informations into project model
lbessard
parents:
210
diff
changeset
|
330 |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
331 |
|
21 | 332 |
#------------------------------------------------------------------------------- |
333 |
# Test identifier |
|
334 |
#------------------------------------------------------------------------------- |
|
335 |
||
336 |
||
337 |
||
338 |
# Test if identifier is valid |
|
339 |
def TestIdentifier(identifier): |
|
340 |
if identifier[0].isdigit(): |
|
341 |
return False |
|
342 |
words = identifier.split('_') |
|
343 |
for i, word in enumerate(words): |
|
344 |
if len(word) == 0 and i != 0: |
|
345 |
return False |
|
346 |
if len(word) != 0 and not word.isalnum(): |
|
347 |
return False |
|
348 |
return True |
|
349 |
||
350 |
||
351 |
#------------------------------------------------------------------------------- |
|
109
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
352 |
# Standard functions list generation |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
353 |
#------------------------------------------------------------------------------- |
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
354 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
355 |
|
15 | 356 |
""" |
357 |
take a .csv file and translate it it a "csv_table" |
|
358 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
359 |
def csv_file_to_table(file): |
22 | 360 |
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
|
361 |
|
15 | 362 |
""" |
363 |
seek into the csv table to a section ( section_name match 1st field ) |
|
364 |
return the matching row without first field |
|
365 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
366 |
def find_section(section_name, table): |
22 | 367 |
fields = [None] |
368 |
while(fields[0] != section_name): |
|
369 |
fields = table.pop(0) |
|
370 |
return fields[1:] |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
371 |
|
15 | 372 |
""" |
373 |
extract the standard functions standard parameter names and types... |
|
374 |
return a { ParameterName: Type, ...} |
|
375 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
376 |
def get_standard_funtions_input_variables(table): |
22 | 377 |
variables = find_section("Standard_functions_variables_types", table) |
378 |
standard_funtions_input_variables = {} |
|
379 |
fields = [True,True] |
|
380 |
while(fields[1]): |
|
381 |
fields = table.pop(0) |
|
382 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!='']) |
|
383 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
|
384 |
return standard_funtions_input_variables |
|
385 |
||
15 | 386 |
""" |
387 |
translate .csv file input declaration into PLCOpenEditor interessting values |
|
388 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
|
389 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
|
390 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
391 |
def csv_input_translate(str_decl, variables, base): |
22 | 392 |
decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') |
393 |
params = [] |
|
394 |
||
395 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
|
396 |
||
397 |
for param_type in decl: |
|
398 |
if param_type in variables.keys(): |
|
399 |
param_name = param_type |
|
400 |
param_type = variables[param_type] |
|
401 |
elif len_of_not_predifined_variable > 1: |
|
402 |
param_name = "IN%d"%base |
|
403 |
base += 1 |
|
404 |
else: |
|
405 |
param_name = "IN" |
|
406 |
params.append((param_name, param_type, "none")) |
|
407 |
return params |
|
408 |
||
409 |
||
25 | 410 |
ANY_TO_ANY_LIST=[ |
411 |
# simple type conv are let as C cast |
|
284 | 412 |
(("ANY_NUM","ANY_BIT"),("ANY_NUM","ANY_BIT"), ("return_type", None, None)), |
25 | 413 |
# TO_TIME |
284 | 414 |
(("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), ("return_type", "__int_to_time", None)), |
415 |
(("ANY_REAL",),("ANY_DATE","TIME"), ("return_type", "__real_to_time", None)), |
|
416 |
(("ANY_STRING",), ("ANY_DATE","TIME"), ("return_type", "__string_to_time", None)), |
|
25 | 417 |
# FROM_TIME |
284 | 418 |
(("ANY_DATE","TIME"), ("ANY_REAL",), ("return_type", "__time_to_real", None)), |
419 |
(("ANY_DATE","TIME"), ("ANY_INT","ANY_NBIT"), ("return_type", "__time_to_int", None)), |
|
420 |
(("TIME",), ("ANY_STRING",), ("return_type", "__time_to_string", None)), |
|
421 |
(("DATE",), ("ANY_STRING",), ("return_type", "__date_to_string", None)), |
|
422 |
(("TOD",), ("ANY_STRING",), ("return_type", "__tod_to_string", None)), |
|
423 |
(("DT",), ("ANY_STRING",), ("return_type", "__dt_to_string", None)), |
|
25 | 424 |
# TO_STRING |
284 | 425 |
(("BOOL",), ("ANY_STRING",), ("return_type", "__bool_to_string", None)), |
426 |
(("ANY_BIT",), ("ANY_STRING",), ("return_type", "__bit_to_string", None)), |
|
427 |
(("ANY_REAL",), ("ANY_STRING",), ("return_type", "__real_to_string", None)), |
|
428 |
(("ANY_SINT",), ("ANY_STRING",), ("return_type", "__sint_to_string", None)), |
|
429 |
(("ANY_UINT",), ("ANY_STRING",), ("return_type", "__uint_to_string", None)), |
|
25 | 430 |
# FROM_STRING |
284 | 431 |
(("ANY_STRING",), ("BOOL",), ("return_type", "__string_to_bool", None)), |
432 |
(("ANY_STRING",), ("ANY_BIT",), ("return_type", "__string_to_bit", None)), |
|
433 |
(("ANY_STRING",), ("ANY_SINT",), ("return_type", "__string_to_sint", None)), |
|
434 |
(("ANY_STRING",), ("ANY_UINT",), ("return_type", "__string_to_uint", None)), |
|
435 |
(("ANY_STRING",), ("ANY_REAL",), ("return_type", "__string_to_real", None))] |
|
25 | 436 |
|
437 |
||
438 |
BCD_TO_ANY_LIST=[ |
|
284 | 439 |
(("BYTE",),("USINT",), ("return_type", "__bcd_to_uint", None)), |
440 |
(("WORD",),("UINT",), ("return_type", "__bcd_to_uint", None)), |
|
441 |
(("DWORD",),("UDINT",), ("return_type", "__bcd_to_uint", None)), |
|
442 |
(("LWORD",),("ULINT",), ("return_type", "__bcd_to_uint", None))] |
|
25 | 443 |
|
444 |
||
445 |
ANY_TO_BCD_LIST=[ |
|
284 | 446 |
(("USINT",),("BYTE",), ("return_type", "__uint_to_bcd", None)), |
447 |
(("UINT",),("WORD",), ("return_type", "__uint_to_bcd", None)), |
|
448 |
(("UDINT",),("DWORD",), ("return_type", "__uint_to_bcd", None)), |
|
449 |
(("ULINT",),("LWORD",), ("return_type", "__uint_to_bcd", None))] |
|
25 | 450 |
|
451 |
||
452 |
def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl): |
|
453 |
||
454 |
for (InTypes, OutTypes, Format) in any_to_any_list: |
|
22 | 455 |
outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes)) |
456 |
inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes)) |
|
457 |
if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]: |
|
458 |
return Format |
|
459 |
||
460 |
return None |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
461 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
462 |
|
15 | 463 |
""" |
464 |
Returns this kind of declaration for all standard functions |
|
465 |
||
466 |
[{"name" : "Numerical", 'list': [ { |
|
467 |
'baseinputnumber': 1, |
|
468 |
'comment': 'Addition', |
|
469 |
'extensible': True, |
|
470 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
471 |
('IN2', 'ANY_NUM', 'none')], |
|
472 |
'name': 'ADD', |
|
473 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
474 |
'type': 'function'}, ...... ] },.....] |
|
475 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
476 |
def get_standard_funtions(table): |
22 | 477 |
|
478 |
variables = get_standard_funtions_input_variables(table) |
|
479 |
||
480 |
fonctions = find_section("Standard_functions_type",table) |
|
481 |
||
482 |
Standard_Functions_Decl = [] |
|
483 |
Current_section = None |
|
484 |
||
485 |
translate = { |
|
486 |
"extensible" : lambda x: {"yes":True, "no":False}[x], |
|
487 |
"inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber), |
|
488 |
"outputs":lambda x:[("OUT",x,"none")]} |
|
489 |
||
490 |
for fields in table: |
|
491 |
if fields[1]: |
|
492 |
# If function section name given |
|
493 |
if fields[0]: |
|
494 |
Current_section = {"name" : fields[0], "list" : []} |
|
495 |
Standard_Functions_Decl.append(Current_section) |
|
496 |
Function_decl_list = [] |
|
497 |
if Current_section: |
|
498 |
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
|
499 |
Function_decl["generate"] = generate_block |
93 | 500 |
Function_decl["initialise"] = lambda x,y:[] |
22 | 501 |
baseinputnumber = int(Function_decl.get("baseinputnumber",1)) |
502 |
Function_decl["baseinputnumber"] = baseinputnumber |
|
503 |
for param, value in Function_decl.iteritems(): |
|
504 |
if param in translate: |
|
505 |
Function_decl[param] = translate[param](value) |
|
506 |
Function_decl["type"] = "function" |
|
507 |
||
25 | 508 |
if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') : |
22 | 509 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
25 | 510 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
22 | 511 |
else: |
512 |
input_ovrloading_types = [None] |
|
513 |
output_types = [None] |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
109
diff
changeset
|
514 |
|
22 | 515 |
funcdeclname_orig = Function_decl["name"] |
516 |
funcdeclname = Function_decl["name"].strip('*_') |
|
517 |
fdc = Function_decl["inputs"][:] |
|
518 |
for intype in input_ovrloading_types: |
|
519 |
if intype != None: |
|
520 |
Function_decl["inputs"] = [] |
|
521 |
for decl_tpl in fdc: |
|
522 |
if IsOfType(intype, decl_tpl[1]): |
|
523 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
524 |
else: |
|
525 |
Function_decl["inputs"] += [(decl_tpl)] |
|
526 |
||
527 |
if funcdeclname_orig.startswith('*'): |
|
528 |
funcdeclin = intype + '_' + funcdeclname |
|
529 |
else: |
|
530 |
funcdeclin = funcdeclname |
|
531 |
else: |
|
532 |
funcdeclin = funcdeclname |
|
533 |
||
534 |
for outype in output_types: |
|
535 |
if outype != None: |
|
536 |
decl_tpl = Function_decl["outputs"][0] |
|
537 |
Function_decl["outputs"] = [ (decl_tpl[0] , outype, decl_tpl[2])] |
|
538 |
if funcdeclname_orig.endswith('*'): |
|
539 |
funcdeclout = funcdeclin + '_' + outype |
|
540 |
else: |
|
541 |
funcdeclout = funcdeclin |
|
542 |
else: |
|
543 |
funcdeclout = funcdeclin |
|
544 |
Function_decl["name"] = funcdeclout |
|
545 |
||
546 |
||
547 |
fdecl = Function_decl |
|
548 |
res = eval(Function_decl["python_eval_c_code_format"]) |
|
549 |
||
550 |
if res != None : |
|
551 |
# create the copy of decl dict to be appended to section |
|
552 |
Function_decl_copy = Function_decl.copy() |
|
553 |
# Have to generate type description in comment with freshly redefined types |
|
554 |
Function_decl_copy["comment"] += ( |
|
555 |
"\n (" + |
|
556 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') + |
|
557 |
" ) => (" + |
|
558 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') + |
|
559 |
" )") |
|
560 |
Current_section["list"].append(Function_decl_copy) |
|
561 |
#pp.pprint(Function_decl_copy) |
|
562 |
else: |
|
563 |
raise "First function must be in a category" |
|
564 |
||
565 |
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
|
566 |
|
25 | 567 |
std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(os.path.split(__file__)[0],"iec_std.csv"))))#, True) |
568 |
||
569 |
BlockTypes.extend(std_decl) |
|
570 |
||
109
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
571 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
572 |
#------------------------------------------------------------------------------- |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
573 |
# Languages Keywords |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
574 |
#------------------------------------------------------------------------------- |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
575 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
576 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
577 |
# Keywords for Pou Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
578 |
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
|
579 |
"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
|
580 |
for category in BlockTypes: |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
581 |
for block in category["list"]: |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
582 |
if block["name"] not in POU_KEYWORDS: |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
583 |
POU_KEYWORDS.append(block["name"]) |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
584 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
585 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
586 |
# Keywords for Type Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
587 |
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
|
588 |
"D", "TIME_OF_DAY", "DATE_AND_TIME"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
589 |
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
|
590 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
591 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
592 |
# Keywords for Variable Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
593 |
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
|
594 |
"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
|
595 |
|
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 |
# Keywords for Configuration Declaration |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
598 |
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
|
599 |
"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
|
600 |
"VAR_GLOBAL", "END_VAR"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
601 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
602 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
603 |
# Keywords for Structured Function Chart |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
604 |
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
|
605 |
"FROM", "TO", "END_TRANSITION"] |
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 Instruction List |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
609 |
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
|
610 |
"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
|
611 |
"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
|
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 Structured Text |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
615 |
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
|
616 |
"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
|
617 |
"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
|
618 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
619 |
|
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
620 |
# All the keywords of IEC |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
621 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
734e02ab4018
Bug that didn't affect standard function names as keywords fixed
lbessard
parents:
108
diff
changeset
|
622 |
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
|
623 |
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
|
624 |
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
|
625 |
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
|
626 |
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
|
627 |
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
|
628 |
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
|
629 |