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