author | lbessard |
Tue, 25 Mar 2008 18:43:48 +0100 | |
changeset 191 | d77f9b783ce8 |
parent 189 | 43cd3ef83a1f |
child 194 | 1b3f8b4f8e04 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 8 |
# |
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
5 | 12 |
#modify it under the terms of the GNU General Public |
0 | 13 |
#License as published by the Free Software Foundation; either |
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 19 |
#General Public License for more details. |
0 | 20 |
# |
5 | 21 |
#You should have received a copy of the GNU General Public |
0 | 22 |
#License along with this library; if not, write to the Free Software |
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
from plcopen import plcopen |
|
26 |
from plcopen.structures import * |
|
27 |
from types import * |
|
28 |
||
29 |
varTypeNames = {"localVars" : "VAR", "tempVars" : "VAR_TEMP", "inputVars" : "VAR_INPUT", |
|
30 |
"outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL", |
|
31 |
"globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"} |
|
1 | 32 |
|
6 | 33 |
pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"} |
34 |
||
71 | 35 |
currentProject = None |
36 |
currentProgram = "" |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
37 |
datatypeComputed = {} |
71 | 38 |
pouComputed = {} |
39 |
||
171 | 40 |
class PLCGenException(Exception): |
41 |
pass |
|
42 |
||
1 | 43 |
def ReIndentText(text, nb_spaces): |
44 |
compute = "" |
|
45 |
lines = text.splitlines() |
|
46 |
if len(lines) > 0: |
|
151 | 47 |
line_num = 0 |
48 |
while line_num < len(lines) and len(lines[line_num].strip()) == 0: |
|
49 |
line_num += 1 |
|
50 |
if line_num < len(lines): |
|
51 |
spaces = 0 |
|
52 |
while lines[line_num][spaces] == " ": |
|
53 |
spaces += 1 |
|
54 |
indent = "" |
|
55 |
for i in xrange(spaces, nb_spaces): |
|
56 |
indent += " " |
|
57 |
for line in lines: |
|
58 |
if line != "": |
|
59 |
compute += "%s%s\n"%(indent, line) |
|
60 |
else: |
|
61 |
compute += "\n" |
|
1 | 62 |
return compute |
63 |
||
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
64 |
def GenerateDataType(datatype_name): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
65 |
if not datatypeComputed.get(datatype_name, True): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
66 |
datatypeComputed[datatype_name] = True |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
67 |
global currentProject, currentProgram |
151 | 68 |
datatype = currentProject.getdataType(datatype_name) |
69 |
datatype_def = " %s :"%datatype.getname() |
|
70 |
basetype_content = datatype.baseType.getcontent() |
|
141 | 71 |
if basetype_content["name"] in ["string", "wstring"]: |
72 |
datatype_def += " %s"%basetype_content["name"].upper() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
73 |
elif basetype_content["name"] == "derived": |
151 | 74 |
basetype_name = basetype_content["value"].getname() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
75 |
GenerateDataType(basetype_name) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
76 |
datatype_def += " %s"%basetype_name |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
77 |
elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: |
151 | 78 |
base_type = basetype_content["value"].baseType.getcontent() |
141 | 79 |
if base_type["name"] == "derived": |
151 | 80 |
basetype_name = base_type["value"].getname() |
141 | 81 |
else: |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
82 |
basetype_name = base_type["name"] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
83 |
GenerateDataType(basetype_name) |
151 | 84 |
min_value = basetype_content["value"].range.getlower() |
85 |
max_value = basetype_content["value"].range.getupper() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
86 |
datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
87 |
elif basetype_content["name"] == "enum": |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
88 |
values = [] |
151 | 89 |
for value in basetype_content["value"].values.getvalue(): |
90 |
values.append(value.getname()) |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
91 |
datatype_def += " (%s)"%", ".join(values) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
92 |
elif basetype_content["name"] == "array": |
151 | 93 |
base_type = basetype_content["value"].baseType.getcontent() |
141 | 94 |
if base_type["name"] == "derived": |
151 | 95 |
basetype_name = base_type["value"].getname() |
141 | 96 |
elif base_type["name"] in ["string", "wstring"]: |
97 |
basetype_name = base_type["name"].upper() |
|
98 |
else: |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
99 |
basetype_name = base_type["name"] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
100 |
GenerateDataType(basetype_name) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
101 |
dimensions = [] |
151 | 102 |
for dimension in basetype_content["value"].getdimension(): |
103 |
dimensions.append("0..%d"%(dimension.getupper() - 1)) |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
104 |
datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name) |
141 | 105 |
else: |
106 |
datatype_def += " %s"%basetype_content["name"] |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
107 |
if datatype.initialValue is not None: |
151 | 108 |
datatype_def += " := %s"%str(datatype.initialValue.getvalue()) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
109 |
currentProgram += "%s;\n"%datatype_def |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
110 |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
111 |
def GeneratePouProgram(pou_name): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
112 |
if not pouComputed.get(pou_name, True): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
113 |
pouComputed[pou_name] = True |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
114 |
global currentProject, currentProgram |
151 | 115 |
pou = currentProject.getpou(pou_name) |
116 |
pou_type = pou.getpouType() |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
117 |
if pou_type in pouTypeNames: |
151 | 118 |
pou_program = PouProgram(pou.getname(), pouTypeNames[pou_type]) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
119 |
else: |
171 | 120 |
raise PLCGenException, "Undefined pou type" |
189
43cd3ef83a1f
Bug with generation of pou without interface fixed
lbessard
parents:
171
diff
changeset
|
121 |
interface = pou.getinterface() |
43cd3ef83a1f
Bug with generation of pou without interface fixed
lbessard
parents:
171
diff
changeset
|
122 |
if interface is not None: |
43cd3ef83a1f
Bug with generation of pou without interface fixed
lbessard
parents:
171
diff
changeset
|
123 |
pou_program.GenerateInterface(interface) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
124 |
pou_program.GenerateConnectionTypes(pou) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
125 |
pou_program.GenerateProgram(pou) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
126 |
currentProgram += pou_program.GenerateSTProgram() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
127 |
|
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
128 |
def GenerateConfiguration(configuration): |
151 | 129 |
config = "\nCONFIGURATION %s\n"%configuration.getname() |
130 |
for varlist in configuration.getglobalVars(): |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
131 |
config += " VAR_GLOBAL" |
151 | 132 |
if varlist.getretain(): |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
133 |
config += " RETAIN" |
151 | 134 |
if varlist.getconstant(): |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
135 |
config += " CONSTANT" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
136 |
config += "\n" |
151 | 137 |
for var in varlist.getvariable(): |
138 |
vartype_content = var.gettype().getcontent() |
|
141 | 139 |
if vartype_content["name"] == "derived": |
151 | 140 |
var_type = vartype_content["value"].getname() |
141 | 141 |
elif vartype_content["name"] in ["string", "wstring"]: |
142 |
var_type = vartype_content["name"].upper() |
|
143 |
else: |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
144 |
var_type = vartype_content["name"] |
151 | 145 |
config += " %s "%var.getname() |
146 |
address = var.getaddress() |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
147 |
if address: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
148 |
config += "AT %s "%address |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
149 |
config += ": %s"%var_type |
151 | 150 |
initial = var.getinitialValue() |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
151 |
if initial: |
151 | 152 |
value = str(initial.getvalue()) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
153 |
value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
154 |
if var_type == "STRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
155 |
config += " := '%s'"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
156 |
elif var_type == "WSTRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
157 |
config += " := \"%s\""%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
158 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
159 |
config += " := %s"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
160 |
config += ";\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
161 |
config += " END_VAR\n" |
151 | 162 |
for resource in configuration.getresource(): |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
163 |
config += GenerateResource(resource) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
164 |
config += "END_CONFIGURATION\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
165 |
return config |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
166 |
|
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
167 |
def GenerateResource(resource): |
151 | 168 |
resrce = "\n RESOURCE %s ON BEREMIZ\n"%resource.getname() |
169 |
for varlist in resource.getglobalVars(): |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
170 |
resrce += " VAR_GLOBAL" |
151 | 171 |
if varlist.getretain(): |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
172 |
resrce += " RETAIN" |
151 | 173 |
if varlist.getconstant(): |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
174 |
resrce += " CONSTANT" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
175 |
resrce += "\n" |
151 | 176 |
for var in varlist.getvariable(): |
177 |
vartype_content = var.gettype().getcontent() |
|
141 | 178 |
if vartype_content["name"] == "derived": |
151 | 179 |
var_type = vartype_content["value"].getname() |
141 | 180 |
elif vartype_content["name"] in ["string", "wstring"]: |
181 |
var_type = vartype_content["name"].upper() |
|
182 |
else: |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
183 |
var_type = vartype_content["name"] |
151 | 184 |
resrce += " %s "%var.getname() |
185 |
address = var.getaddress() |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
186 |
if address: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
187 |
resrce += "AT %s "%address |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
188 |
resrce += ": %s"%var_type |
151 | 189 |
initial = var.getinitialValue() |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
190 |
if initial: |
151 | 191 |
value = str(initial.getvalue()) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
192 |
value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
193 |
if var_type == "STRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
194 |
resrce += " := '%s'"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
195 |
elif var_type == "WSTRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
196 |
resrce += " := \"%s\""%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
197 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
198 |
resrce += " := %s"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
199 |
resrce += ";\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
200 |
resrce += " END_VAR\n" |
151 | 201 |
tasks = resource.gettask() |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
202 |
for task in tasks: |
151 | 203 |
resrce += " TASK %s("%task.getname() |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
204 |
args = [] |
151 | 205 |
single = task.getsingle() |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
206 |
if single: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
207 |
args.append("SINGLE := %s"%single) |
151 | 208 |
interval = task.getinterval() |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
209 |
if interval: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
210 |
text = "t#" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
211 |
if interval.hour != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
212 |
text += "%dh"%interval.hour |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
213 |
if interval.minute != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
214 |
text += "%dm"%interval.minute |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
215 |
if interval.second != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
216 |
text += "%ds"%interval.second |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
217 |
if interval.microsecond != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
218 |
text += "%dms"%(interval.microsecond / 1000) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
219 |
args.append("INTERVAL := %s"%text) |
151 | 220 |
args.append("PRIORITY := %s"%str(task.getpriority())) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
221 |
resrce += ",".join(args) + ");\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
222 |
for task in tasks: |
151 | 223 |
for instance in task.getpouInstance(): |
224 |
resrce += " PROGRAM %s WITH %s : %s;\n"%(instance.getname(), task.getname(), instance.gettype()) |
|
225 |
for instance in resource.getpouInstance(): |
|
226 |
resrce += " PROGRAM %s : %s;\n"%(instance.getname(), instance.gettype()) |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
227 |
resrce += " END_RESOURCE\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
228 |
return resrce |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
229 |
|
0 | 230 |
""" |
231 |
Module implementing methods for generating PLC programs in ST or IL |
|
232 |
""" |
|
233 |
||
234 |
class PouProgram: |
|
235 |
||
236 |
def __init__(self, name, type): |
|
237 |
self.Name = name |
|
238 |
self.Type = type |
|
1 | 239 |
self.ReturnType = None |
240 |
self.Interface = [] |
|
241 |
self.InitialSteps = [] |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
242 |
self.ComputedBlocks = {} |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
243 |
self.ComputedConnectors = {} |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
244 |
self.ConnectionTypes = {} |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
245 |
self.RelatedConnections = [] |
1 | 246 |
self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}} |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
247 |
self.SFCComputedBlocks = "" |
46 | 248 |
self.ActionNumber = 0 |
0 | 249 |
self.Program = "" |
250 |
||
46 | 251 |
def GetActionNumber(self): |
252 |
self.ActionNumber += 1 |
|
253 |
return self.ActionNumber |
|
254 |
||
1 | 255 |
def IsAlreadyDefined(self, name): |
33 | 256 |
for list_type, retain, constant, located, vars in self.Interface: |
30 | 257 |
for var_type, var_name, var_address, var_initial in vars: |
1 | 258 |
if name == var_name: |
259 |
return True |
|
260 |
return False |
|
261 |
||
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
262 |
def GetVariableType(self, name): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
263 |
for list_type, retain, constant, located, vars in self.Interface: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
264 |
for var_type, var_name, var_address, var_initial in vars: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
265 |
if name == var_name: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
266 |
return var_type |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
267 |
return None |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
268 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
269 |
def GetConnectedConnection(self, connection, body): |
151 | 270 |
links = connection.getconnections() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
271 |
if links and len(links) == 1: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
272 |
return self.GetLinkedConnection(links[0], body) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
273 |
return None |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
274 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
275 |
def GetLinkedConnection(self, link, body): |
151 | 276 |
parameter = link.getformalParameter() |
277 |
instance = body.getcontentInstance(link.getrefLocalId()) |
|
278 |
if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable, plcopen.commonObjects_continuation, plcopen.ldObjects_contact, plcopen.ldObjects_coil)): |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
279 |
return instance.connectionPointOut |
151 | 280 |
elif isinstance(instance, plcopen.fbdObjects_block): |
281 |
outputvariables = instance.outputVariables.getvariable() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
282 |
if len(outputvariables) == 1: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
283 |
return outputvariables[0].connectionPointOut |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
284 |
elif parameter: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
285 |
for variable in outputvariables: |
151 | 286 |
if variable.getformalParameter() == parameter: |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
287 |
return variable.connectionPointOut |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
288 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
289 |
point = link.getPosition()[-1] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
290 |
for variable in outputvariables: |
151 | 291 |
relposition = variable.connectionPointOut.getrelPositionXY() |
292 |
blockposition = instance.getposition() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
293 |
if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
294 |
return variable.connectionPointOut |
151 | 295 |
elif isinstance(instance, plcopen.ldObjects_leftPowerRail): |
296 |
outputconnections = instance.getconnectionPointOut() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
297 |
if len(outputconnections) == 1: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
298 |
return outputconnections[0] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
299 |
else: |
151 | 300 |
point = link.getposition()[-1] |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
301 |
for outputconnection in outputconnections: |
151 | 302 |
relposition = outputconnection.getrelPositionXY() |
303 |
powerrailposition = instance.getposition() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
304 |
if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
305 |
return outputconnection |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
306 |
return None |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
307 |
|
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
308 |
def ExtractRelatedConnections(self, connection): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
309 |
for i, related in enumerate(self.RelatedConnections): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
310 |
if connection in related: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
311 |
return self.RelatedConnections.pop(i) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
312 |
return [connection] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
313 |
|
0 | 314 |
def GenerateInterface(self, interface): |
315 |
if self.Type == "FUNCTION": |
|
151 | 316 |
returntype_content = interface.getreturnType().getcontent() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
317 |
if returntype_content["value"] is None: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
318 |
self.ReturnType = returntype_content["name"] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
319 |
else: |
151 | 320 |
self.ReturnType = returntype_content["value"].getname() |
321 |
for varlist in interface.getcontent(): |
|
1 | 322 |
variables = [] |
191
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
323 |
located = [] |
151 | 324 |
for var in varlist["value"].getvariable(): |
325 |
vartype_content = var.gettype().getcontent() |
|
141 | 326 |
if vartype_content["name"] == "derived": |
151 | 327 |
var_type = vartype_content["value"].getname() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
328 |
GeneratePouProgram(var_type) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
329 |
blocktype = GetBlockType(var_type) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
330 |
if blocktype is not None: |
191
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
331 |
for variable in blocktype["initialise"](var_type, var.getname()): |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
332 |
if variable[2] is not None: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
333 |
located.append(variable) |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
334 |
else: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
335 |
variables.append(variable) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
336 |
else: |
151 | 337 |
initial = var.getinitialValue() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
338 |
if initial: |
151 | 339 |
initial_value = initial.getvalue() |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
340 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
341 |
initial_value = None |
151 | 342 |
address = var.getaddress() |
191
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
343 |
if address is not None: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
344 |
located.append((vartype_content["value"].getname(), var.getname(), address, initial_value)) |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
345 |
else: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
346 |
variables.append((vartype_content["value"].getname(), var.getname(), None, initial_value)) |
141 | 347 |
else: |
151 | 348 |
initial = var.getinitialValue() |
141 | 349 |
if initial: |
151 | 350 |
initial_value = initial.getvalue() |
141 | 351 |
else: |
352 |
initial_value = None |
|
151 | 353 |
address = var.getaddress() |
141 | 354 |
if vartype_content["name"] in ["string", "wstring"]: |
191
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
355 |
if address is not None: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
356 |
located.append((vartype_content["name"].upper(), var.getname(), address, initial_value)) |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
357 |
else: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
358 |
variables.append((vartype_content["name"].upper(), var.getname(), None, initial_value)) |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
359 |
elif address is not None: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
360 |
located.append((vartype_content["name"], var.getname(), address, initial_value)) |
141 | 361 |
else: |
191
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
362 |
variables.append((vartype_content["name"], var.getname(), None, initial_value)) |
93 | 363 |
if len(variables) > 0: |
151 | 364 |
self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), |
191
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
365 |
varlist["value"].getconstant(), False, variables)) |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
366 |
if len(located) > 0: |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
367 |
self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
368 |
varlist["value"].getconstant(), True, located)) |
d77f9b783ce8
Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents:
189
diff
changeset
|
369 |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
370 |
def GenerateConnectionTypes(self, pou): |
151 | 371 |
body = pou.getbody() |
372 |
body_content = body.getcontent() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
373 |
body_type = body_content["name"] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
374 |
if body_type in ["FBD", "LD", "SFC"]: |
151 | 375 |
for instance in body.getcontentInstances(): |
376 |
if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)): |
|
377 |
expression = instance.getexpression() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
378 |
var_type = self.GetVariableType(expression) |
151 | 379 |
if expression == pou.getname(): |
380 |
returntype_content = pou.interface.getreturnType().getcontent() |
|
141 | 381 |
if returntype_content["name"] == "derived": |
151 | 382 |
var_type = returntype_content["value"].getname() |
141 | 383 |
elif returntype_content["name"] in ["string", "wstring"]: |
384 |
var_type = returntype_content["name"].upper() |
|
385 |
else: |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
386 |
var_type = returntype_content["name"] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
387 |
elif var_type is None: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
388 |
var_type = expression.split("#")[0] |
151 | 389 |
if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)): |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
390 |
self.ConnectionTypes[instance.connectionPointOut] = var_type |
151 | 391 |
if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)): |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
392 |
self.ConnectionTypes[instance.connectionPointIn] = var_type |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
393 |
connected = self.GetConnectedConnection(instance.connectionPointIn, body) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
394 |
if connected and connected not in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
395 |
for connection in self.ExtractRelatedConnections(connected): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
396 |
self.ConnectionTypes[connection] = var_type |
151 | 397 |
elif isinstance(instance, (plcopen.ldObjects_contact, plcopen.ldObjects_coil)): |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
398 |
self.ConnectionTypes[instance.connectionPointOut] = "BOOL" |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
399 |
self.ConnectionTypes[instance.connectionPointIn] = "BOOL" |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
400 |
connected = self.GetConnectedConnection(instance.connectionPointIn, body) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
401 |
if connected and connected not in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
402 |
for connection in self.ExtractRelatedConnections(connected): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
403 |
self.ConnectionTypes[connection] = "BOOL" |
151 | 404 |
elif isinstance(instance, plcopen.ldObjects_leftPowerRail): |
405 |
for connection in instance.getconnectionPointOut(): |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
406 |
self.ConnectionTypes[connection] = "BOOL" |
151 | 407 |
elif isinstance(instance, plcopen.ldObjects_rightPowerRail): |
408 |
for connection in instance.getconnectionPointIn(): |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
409 |
self.ConnectionTypes[connection] = "BOOL" |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
410 |
connected = self.GetConnectedConnection(connection, body) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
411 |
if connected and connected not in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
412 |
for connection in self.ExtractRelatedConnections(connected): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
413 |
self.ConnectionTypes[connection] = "BOOL" |
151 | 414 |
elif isinstance(instance, plcopen.sfcObjects_transition): |
415 |
content = instance.condition.getcontent() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
416 |
if content["name"] == "connection" and len(content["value"]) == 1: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
417 |
connected = self.GetLinkedConnection(content["value"][0], body) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
418 |
if connected and connected not in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
419 |
for connection in self.ExtractRelatedConnections(connected): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
420 |
self.ConnectionTypes[connection] = "BOOL" |
151 | 421 |
elif isinstance(instance, plcopen.fbdObjects_block): |
422 |
block_infos = GetBlockType(instance.gettypeName()) |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
423 |
undefined = {} |
151 | 424 |
for variable in instance.outputVariables.getvariable(): |
425 |
output_name = variable.getformalParameter() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
426 |
for oname, otype, oqualifier in block_infos["outputs"]: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
427 |
if output_name == oname and variable.connectionPointOut not in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
428 |
if otype.startswith("ANY"): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
429 |
if otype not in undefined: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
430 |
undefined[otype] = [] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
431 |
undefined[otype].append(variable.connectionPointOut) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
432 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
433 |
for connection in self.ExtractRelatedConnections(variable.connectionPointOut): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
434 |
self.ConnectionTypes[connection] = otype |
151 | 435 |
for variable in instance.inputVariables.getvariable(): |
436 |
input_name = variable.getformalParameter() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
437 |
for iname, itype, iqualifier in block_infos["inputs"]: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
438 |
if input_name == iname: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
439 |
connected = self.GetConnectedConnection(variable.connectionPointIn, body) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
440 |
if itype.startswith("ANY"): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
441 |
if itype not in undefined: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
442 |
undefined[itype] = [] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
443 |
undefined[itype].append(variable.connectionPointIn) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
444 |
if connected: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
445 |
undefined[itype].append(connected) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
446 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
447 |
self.ConnectionTypes[variable.connectionPointIn] = itype |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
448 |
if connected and connected not in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
449 |
for connection in self.ExtractRelatedConnections(connected): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
450 |
self.ConnectionTypes[connection] = itype |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
451 |
for var_type, connections in undefined.items(): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
452 |
related = [] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
453 |
for connection in connections: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
454 |
if connection in self.ConnectionTypes: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
455 |
var_type = self.ConnectionTypes[connection] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
456 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
457 |
related.extend(self.ExtractRelatedConnections(connection)) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
458 |
if var_type.startswith("ANY") and len(related) > 0: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
459 |
self.RelatedConnections.append(related) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
460 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
461 |
for connection in related: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
462 |
self.ConnectionTypes[connection] = var_type |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
463 |
|
0 | 464 |
def GenerateProgram(self, pou): |
151 | 465 |
body = pou.getbody() |
466 |
body_content = body.getcontent() |
|
0 | 467 |
body_type = body_content["name"] |
468 |
if body_type in ["IL","ST"]: |
|
151 | 469 |
self.Program = ReIndentText(body_content["value"].gettext(), 2) |
0 | 470 |
elif body_type == "FBD": |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
471 |
orderedInstances = [] |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
472 |
otherInstances = [] |
151 | 473 |
for instance in body.getcontentInstances(): |
474 |
if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable, plcopen.fbdObjects_block)): |
|
475 |
executionOrderId = instance.getexecutionOrderId() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
476 |
if executionOrderId > 0: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
477 |
orderedInstances.append((executionOrderId, instance)) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
478 |
else: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
479 |
otherInstances.append(instance) |
151 | 480 |
elif isinstance(instance, plcopen.commonObjects_connector): |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
481 |
otherInstances.append(instance) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
482 |
orderedInstances.sort() |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
483 |
instances = [instance for (executionOrderId, instance) in orderedInstances] + otherInstances |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
484 |
for instance in instances: |
151 | 485 |
if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)): |
486 |
var = instance.getexpression() |
|
487 |
connections = instance.connectionPointIn.getconnections() |
|
0 | 488 |
if connections and len(connections) == 1: |
489 |
expression = self.ComputeFBDExpression(body, connections[0]) |
|
490 |
self.Program += " %s := %s;\n"%(var, expression) |
|
151 | 491 |
elif isinstance(instance, plcopen.fbdObjects_block): |
492 |
block_type = instance.gettypeName() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
493 |
self.GeneratePouProgram(block_type) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
494 |
block_infos = GetBlockType(block_type) |
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
495 |
block_infos["generate"](self, instance, body, None) |
151 | 496 |
elif isinstance(instance, plcopen.commonObjects_connector): |
497 |
connector = instance.getname() |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
498 |
if self.ComputedConnectors.get(connector, None): |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
499 |
continue |
151 | 500 |
connections = instance.connectionPointIn.getconnections() |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
501 |
if connections and len(connections) == 1: |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
502 |
self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0]) |
0 | 503 |
elif body_type == "LD": |
151 | 504 |
for instance in body.getcontentInstances(): |
505 |
if isinstance(instance, plcopen.ldObjects_coil): |
|
506 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), body) |
|
104 | 507 |
if len(paths) > 0: |
508 |
paths = tuple(paths) |
|
509 |
else: |
|
510 |
paths = paths[0] |
|
151 | 511 |
variable = self.ExtractModifier(instance, instance.getvariable()) |
0 | 512 |
expression = self.ComputeLDExpression(paths, True) |
513 |
self.Program += " %s := %s;\n"%(variable, expression) |
|
514 |
elif body_type == "SFC": |
|
151 | 515 |
for instance in body.getcontentInstances(): |
516 |
if isinstance(instance, plcopen.sfcObjects_step): |
|
1 | 517 |
self.GenerateSFCStep(instance, pou) |
151 | 518 |
elif isinstance(instance, plcopen.commonObjects_actionBlock): |
1 | 519 |
self.GenerateSFCStepActions(instance, pou) |
151 | 520 |
elif isinstance(instance, plcopen.sfcObjects_transition): |
1 | 521 |
self.GenerateSFCTransition(instance, pou) |
151 | 522 |
elif isinstance(instance, plcopen.sfcObjects_jumpStep): |
0 | 523 |
self.GenerateSFCJump(instance, pou) |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
524 |
if len(self.InitialSteps) > 0 and self.SFCComputedBlocks != "": |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
525 |
action_name = "COMPUTE_FUNCTION_BLOCKS" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
526 |
action_infos = {"qualifier" : "S", "content" : action_name} |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
527 |
self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos) |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
528 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(self.SFCComputedBlocks, 4) |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
529 |
self.Program = "" |
1 | 530 |
for initialstep in self.InitialSteps: |
531 |
self.ComputeSFCStep(initialstep) |
|
0 | 532 |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
533 |
def ComputeFBDExpression(self, body, link, order = False): |
151 | 534 |
localid = link.getrefLocalId() |
535 |
instance = body.getcontentInstance(localid) |
|
536 |
if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)): |
|
537 |
return instance.getexpression() |
|
538 |
elif isinstance(instance, plcopen.fbdObjects_block): |
|
539 |
block_type = instance.gettypeName() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
540 |
self.GeneratePouProgram(block_type) |
104 | 541 |
block_infos = GetBlockType(block_type) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
542 |
return block_infos["generate"](self, instance, body, link, order) |
151 | 543 |
elif isinstance(instance, plcopen.commonObjects_continuation): |
544 |
name = instance.getname() |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
545 |
computed_value = self.ComputedConnectors.get(name, None) |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
546 |
if computed_value != None: |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
547 |
return computed_value |
151 | 548 |
for tmp_instance in body.getcontentInstances(): |
549 |
if isinstance(tmp_instance, plcopen.commonObjects_connector): |
|
550 |
if tmp_instance.getname() == name: |
|
551 |
connections = tmp_instance.connectionPointIn.getconnections() |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
552 |
if connections and len(connections) == 1: |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
553 |
expression = self.ComputeFBDExpression(body, connections[0], order) |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
554 |
self.ComputedConnectors[name] = expression |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
555 |
return expression |
171 | 556 |
raise PLCGenException, "No connector found" |
0 | 557 |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
558 |
def GenerateLDPaths(self, connections, body): |
0 | 559 |
paths = [] |
560 |
for connection in connections: |
|
151 | 561 |
localId = connection.getrefLocalId() |
562 |
next = body.getcontentInstance(localId) |
|
563 |
if isinstance(next, plcopen.ldObjects_leftPowerRail): |
|
0 | 564 |
paths.append(None) |
151 | 565 |
elif isinstance(next, plcopen.fbdObjects_block): |
566 |
block_type = next.gettypeName() |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
567 |
self.GeneratePouProgram(block_type) |
104 | 568 |
block_infos = GetBlockType(block_type) |
569 |
paths.append(block_infos["generate"](self, next, body, connection)) |
|
0 | 570 |
else: |
151 | 571 |
variable = self.ExtractModifier(next, next.getvariable()) |
572 |
result = self.GenerateLDPaths(next.connectionPointIn.getconnections(), body) |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
573 |
if len(result) > 1: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
574 |
paths.append([variable, tuple(result)]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
575 |
elif type(result[0]) == ListType: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
576 |
paths.append([variable] + result[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
577 |
elif result[0]: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
578 |
paths.append([variable, result[0]]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
579 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
580 |
paths.append(variable) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
581 |
return paths |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
582 |
|
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
583 |
def GetNetworkType(self, connections, body): |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
584 |
network_type = "FBD" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
585 |
for connection in connections: |
151 | 586 |
localId = connection.getrefLocalId() |
587 |
next = body.getcontentInstance(localId) |
|
588 |
if isinstance(next, plcopen.ldObjects_leftPowerRail) or isinstance(next, plcopen.ldObjects_contact): |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
589 |
return "LD" |
151 | 590 |
elif isinstance(next, plcopen.fbdObjects_block): |
591 |
for variable in next.inputVariables.getvariable(): |
|
592 |
result = self.GetNetworkType(variable.connectionPointIn.getconnections(), body) |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
593 |
if result != "FBD": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
594 |
return result |
151 | 595 |
elif isinstance(next, plcopen.fbdObjects_inVariable): |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
596 |
return "FBD" |
151 | 597 |
elif isinstance(next, plcopen.fbdObjects_inOutVariable): |
598 |
return self.GetNetworkType(next.connectionPointIn.getconnections(), body) |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
599 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
600 |
return None |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
601 |
return "FBD" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
602 |
|
2 | 603 |
def ExtractDivergenceInput(self, divergence, pou): |
151 | 604 |
connectionPointIn = divergence.getconnectionPointIn() |
2 | 605 |
if connectionPointIn: |
151 | 606 |
connections = connectionPointIn.getconnections() |
2 | 607 |
if len(connections) == 1: |
151 | 608 |
instanceLocalId = connections[0].getrefLocalId() |
609 |
return pou.body.getcontentInstance(instanceLocalId) |
|
2 | 610 |
return None |
611 |
||
612 |
def ExtractConvergenceInputs(self, convergence, pou): |
|
613 |
instances = [] |
|
151 | 614 |
for connectionPointIn in convergence.getconnectionPointIn(): |
615 |
connections = connectionPointIn.getconnections() |
|
2 | 616 |
if len(connections) == 1: |
151 | 617 |
instanceLocalId = connections[0].getrefLocalId() |
618 |
instances.append(pou.body.getcontentInstance(instanceLocalId)) |
|
2 | 619 |
return instances |
620 |
||
1 | 621 |
def GenerateSFCStep(self, step, pou): |
151 | 622 |
step_name = step.getname() |
1 | 623 |
if step_name not in self.SFCNetworks["Steps"].keys(): |
151 | 624 |
if step.getinitialStep(): |
1 | 625 |
self.InitialSteps.append(step_name) |
151 | 626 |
step_infos = {"initial" : step.getinitialStep(), "transitions" : [], "actions" : []} |
0 | 627 |
if step.connectionPointIn: |
628 |
instances = [] |
|
151 | 629 |
connections = step.connectionPointIn.getconnections() |
0 | 630 |
if len(connections) == 1: |
151 | 631 |
instanceLocalId = connections[0].getrefLocalId() |
632 |
instance = pou.body.getcontentInstance(instanceLocalId) |
|
633 |
if isinstance(instance, plcopen.sfcObjects_transition): |
|
0 | 634 |
instances.append(instance) |
151 | 635 |
elif isinstance(instance, plcopen.sfcObjects_selectionConvergence): |
2 | 636 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
151 | 637 |
elif isinstance(instance, plcopen.sfcObjects_simultaneousDivergence): |
2 | 638 |
transition = self.ExtractDivergenceInput(instance, pou) |
639 |
if transition: |
|
151 | 640 |
if isinstance(transition, plcopen.sfcObjects_transition): |
1 | 641 |
instances.append(transition) |
151 | 642 |
elif isinstance(transition, plcopen.sfcObjects_selectionConvergence): |
2 | 643 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
0 | 644 |
for instance in instances: |
2 | 645 |
self.GenerateSFCTransition(instance, pou) |
1 | 646 |
if instance in self.SFCNetworks["Transitions"].keys(): |
647 |
self.SFCNetworks["Transitions"][instance]["to"].append(step_name) |
|
648 |
self.SFCNetworks["Steps"][step_name] = step_infos |
|
0 | 649 |
|
650 |
def GenerateSFCJump(self, jump, pou): |
|
151 | 651 |
jump_target = jump.gettargetName() |
0 | 652 |
if jump.connectionPointIn: |
653 |
instances = [] |
|
151 | 654 |
connections = jump.connectionPointIn.getconnections() |
0 | 655 |
if len(connections) == 1: |
151 | 656 |
instanceLocalId = connections[0].getrefLocalId() |
657 |
instance = pou.body.getcontentInstance(instanceLocalId) |
|
658 |
if isinstance(instance, plcopen.sfcObjects_transition): |
|
0 | 659 |
instances.append(instance) |
151 | 660 |
elif isinstance(instance, plcopen.sfcObjects_selectionConvergence): |
2 | 661 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
151 | 662 |
elif isinstance(instance, plcopen.sfcObjects_simultaneousDivergence): |
2 | 663 |
transition = self.ExtractDivergenceInput(instance, pou) |
664 |
if transition: |
|
151 | 665 |
if isinstance(transition, plcopen.sfcObjects_transition): |
1 | 666 |
instances.append(transition) |
151 | 667 |
elif isinstance(transition, plcopen.sfcObjects_selectionConvergence): |
2 | 668 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
0 | 669 |
for instance in instances: |
2 | 670 |
self.GenerateSFCTransition(instance, pou) |
1 | 671 |
if instance in self.SFCNetworks["Transitions"].keys(): |
672 |
self.SFCNetworks["Transitions"][instance]["to"].append(jump_target) |
|
673 |
||
674 |
def GenerateSFCStepActions(self, actionBlock, pou): |
|
151 | 675 |
connections = actionBlock.connectionPointIn.getconnections() |
0 | 676 |
if len(connections) == 1: |
151 | 677 |
stepLocalId = connections[0].getrefLocalId() |
678 |
step = pou.body.getcontentInstance(stepLocalId) |
|
1 | 679 |
self.GenerateSFCStep(step, pou) |
151 | 680 |
step_name = step.getname() |
1 | 681 |
if step_name in self.SFCNetworks["Steps"].keys(): |
151 | 682 |
actions = actionBlock.getactions() |
0 | 683 |
for action in actions: |
1 | 684 |
action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]} |
685 |
if "duration" in action: |
|
686 |
action_infos["duration"] = action["duration"] |
|
687 |
if "indicator" in action: |
|
688 |
action_infos["indicator"] = action["indicator"] |
|
689 |
if action["type"] == "reference": |
|
690 |
self.GenerateSFCAction(action["value"], pou) |
|
46 | 691 |
else: |
168
fb500cc79164
Adding support for structure variable list generation module in matiec
lbessard
parents:
151
diff
changeset
|
692 |
action_name = "%s_INLINE%d"%(step_name.upper(), self.GetActionNumber()) |
46 | 693 |
self.SFCNetworks["Actions"][action_name] = " %s\n"%action["value"] |
694 |
action_infos["content"] = action_name |
|
1 | 695 |
self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos) |
696 |
||
697 |
def GenerateSFCAction(self, action_name, pou): |
|
698 |
if action_name not in self.SFCNetworks["Actions"].keys(): |
|
151 | 699 |
actionContent = pou.getaction(action_name) |
1 | 700 |
if actionContent: |
151 | 701 |
actionType = actionContent.getbodyType() |
702 |
actionBody = actionContent.getbody() |
|
1 | 703 |
if actionType in ["ST", "IL"]: |
151 | 704 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.gettext(), 4) |
1 | 705 |
elif actionType == "FBD": |
151 | 706 |
for instance in actionBody.getcontentInstances(): |
707 |
if isinstance(instance, plcopen.fbdObjects_outVariable): |
|
708 |
var = instance.getexpression() |
|
709 |
connections = instance.connectionPointIn.getconnections() |
|
1 | 710 |
if connections and len(connections) == 1: |
711 |
expression = self.ComputeFBDExpression(actionBody, connections[0]) |
|
70 | 712 |
action_content = self.Program + " %s := %s;\n"%(var, expression) |
1 | 713 |
self.Program = "" |
70 | 714 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
1 | 715 |
elif actionType == "LD": |
151 | 716 |
for instance in actionbody.getcontentInstances(): |
717 |
if isinstance(instance, plcopen.ldObjects_coil): |
|
718 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), actionBody) |
|
719 |
variable = self.ExtractModifier(instance, instance.getvariable()) |
|
1 | 720 |
expression = self.ComputeLDExpression(paths, True) |
70 | 721 |
action_content = self.Program + " %s := %s;\n"%(variable, expression) |
1 | 722 |
self.Program = "" |
70 | 723 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
1 | 724 |
|
725 |
def GenerateSFCTransition(self, transition, pou): |
|
726 |
if transition not in self.SFCNetworks["Transitions"].keys(): |
|
727 |
steps = [] |
|
151 | 728 |
connections = transition.connectionPointIn.getconnections() |
0 | 729 |
if len(connections) == 1: |
151 | 730 |
instanceLocalId = connections[0].getrefLocalId() |
731 |
instance = pou.body.getcontentInstance(instanceLocalId) |
|
732 |
if isinstance(instance, plcopen.sfcObjects_step): |
|
2 | 733 |
steps.append(instance) |
151 | 734 |
elif isinstance(instance, plcopen.sfcObjects_selectionDivergence): |
2 | 735 |
step = self.ExtractDivergenceInput(instance, pou) |
736 |
if step: |
|
151 | 737 |
if isinstance(step, plcopen.sfcObjects_step): |
2 | 738 |
steps.append(step) |
151 | 739 |
elif isinstance(step, plcopen.sfcObjects_simultaneousConvergence): |
2 | 740 |
steps.extend(self.ExtractConvergenceInputs(step, pou)) |
151 | 741 |
elif isinstance(instance, plcopen.sfcObjects_simultaneousConvergence): |
2 | 742 |
steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
151 | 743 |
transition_infos = {"priority": transition.getpriority(), "from": [], "to" : []} |
744 |
transitionValues = transition.getconditionContent() |
|
1 | 745 |
if transitionValues["type"] == "inline": |
746 |
transition_infos["content"] = "\n := %s;\n"%transitionValues["value"] |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
747 |
elif transitionValues["type"] == "reference": |
151 | 748 |
transitionContent = pou.gettransition(transitionValues["value"]) |
749 |
transitionType = transitionContent.getbodyType() |
|
750 |
transitionBody = transitionContent.getbody() |
|
1 | 751 |
if transitionType == "IL": |
151 | 752 |
transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.gettext(), 4) |
1 | 753 |
elif transitionType == "ST": |
151 | 754 |
transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.gettext(), 4) |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
755 |
elif transitionType == "FBD": |
151 | 756 |
for instance in transitionBody.getcontentInstances(): |
757 |
if isinstance(instance, plcopen.fbdObjects_outVariable): |
|
758 |
connections = instance.connectionPointIn.getconnections() |
|
1 | 759 |
if connections and len(connections) == 1: |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
760 |
expression = self.ComputeFBDExpression(transitionBody, connections[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
761 |
transition_infos["content"] = "\n := %s;\n"%expression |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
762 |
self.SFCComputedBlocks += self.Program |
70 | 763 |
self.Program = "" |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
764 |
elif transitionType == "LD": |
151 | 765 |
for instance in transitionBody.getcontentInstances(): |
766 |
if isinstance(instance, plcopen.ldObjects_coil): |
|
767 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), transitionBody) |
|
1 | 768 |
expression = self.ComputeLDExpression(paths, True) |
769 |
transition_infos["content"] = "\n := %s;\n"%expression |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
770 |
self.SFCComputedBlocks += self.Program |
70 | 771 |
self.Program = "" |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
772 |
elif transitionValues["type"] == "connection": |
151 | 773 |
body = pou.getbody() |
774 |
connections = transition.getconnections() |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
775 |
network_type = self.GetNetworkType(connections, body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
776 |
if network_type == None: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
777 |
raise Exception |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
778 |
if len(connections) > 1 or network_type == "LD": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
779 |
paths = self.GenerateLDPaths(connections, body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
780 |
expression = self.ComputeLDExpression(paths, True) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
781 |
transition_infos["content"] = "\n := %s;\n"%expression |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
782 |
self.SFCComputedBlocks += self.Program |
70 | 783 |
self.Program = "" |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
784 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
785 |
expression = self.ComputeFBDExpression(body, connections[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
786 |
transition_infos["content"] = "\n := %s;\n"%expression |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
787 |
self.SFCComputedBlocks += self.Program |
70 | 788 |
self.Program = "" |
2 | 789 |
for step in steps: |
790 |
self.GenerateSFCStep(step, pou) |
|
151 | 791 |
step_name = step.getname() |
1 | 792 |
if step_name in self.SFCNetworks["Steps"].keys(): |
793 |
transition_infos["from"].append(step_name) |
|
794 |
self.SFCNetworks["Steps"][step_name]["transitions"].append(transition) |
|
795 |
self.SFCNetworks["Transitions"][transition] = transition_infos |
|
796 |
||
797 |
def ComputeSFCStep(self, step_name): |
|
798 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
799 |
step_infos = self.SFCNetworks["Steps"].pop(step_name) |
|
800 |
if step_infos["initial"]: |
|
801 |
self.Program += " INITIAL_STEP %s:\n"%step_name |
|
802 |
else: |
|
803 |
self.Program += " STEP %s:\n"%step_name |
|
804 |
actions = [] |
|
805 |
for action_infos in step_infos["actions"]: |
|
806 |
actions.append(action_infos["content"]) |
|
807 |
self.Program += " %(content)s(%(qualifier)s"%action_infos |
|
808 |
if "duration" in action_infos: |
|
809 |
self.Program += ", %(duration)s"%action_infos |
|
810 |
if "indicator" in action_infos: |
|
811 |
self.Program += ", %(indicator)s"%action_infos |
|
812 |
self.Program += ");\n" |
|
813 |
self.Program += " END_STEP\n\n" |
|
814 |
for action in actions: |
|
815 |
self.ComputeSFCAction(action) |
|
816 |
for transition in step_infos["transitions"]: |
|
817 |
self.ComputeSFCTransition(transition) |
|
818 |
||
819 |
def ComputeSFCAction(self, action_name): |
|
820 |
if action_name in self.SFCNetworks["Actions"].keys(): |
|
821 |
action_content = self.SFCNetworks["Actions"].pop(action_name) |
|
822 |
self.Program += " ACTION %s:\n%s END_ACTION\n\n"%(action_name, action_content) |
|
823 |
||
824 |
def ComputeSFCTransition(self, transition): |
|
825 |
if transition in self.SFCNetworks["Transitions"].keys(): |
|
826 |
transition_infos = self.SFCNetworks["Transitions"].pop(transition) |
|
80 | 827 |
self.Program += " TRANSITION" |
828 |
if transition_infos["priority"] != None: |
|
829 |
self.Program += " (PRIORITY := %d)"%transition_infos["priority"] |
|
830 |
self.Program += " FROM " |
|
1 | 831 |
if len(transition_infos["from"]) > 1: |
832 |
self.Program += "(%s)"%", ".join(transition_infos["from"]) |
|
833 |
else: |
|
834 |
self.Program += "%s"%transition_infos["from"][0] |
|
835 |
self.Program += " TO " |
|
836 |
if len(transition_infos["to"]) > 1: |
|
837 |
self.Program += "(%s)"%", ".join(transition_infos["to"]) |
|
838 |
else: |
|
839 |
self.Program += "%s"%transition_infos["to"][0] |
|
840 |
self.Program += transition_infos["content"] |
|
841 |
self.Program += " END_TRANSITION\n\n" |
|
842 |
for step_name in transition_infos["to"]: |
|
843 |
self.ComputeSFCStep(step_name) |
|
844 |
||
0 | 845 |
def ComputeLDExpression(self, paths, first = False): |
846 |
if type(paths) == TupleType: |
|
847 |
if None in paths: |
|
848 |
return "TRUE" |
|
849 |
else: |
|
850 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
851 |
if first: |
|
852 |
return " OR ".join(var) |
|
853 |
else: |
|
854 |
return "(%s)"%" OR ".join(var) |
|
855 |
elif type(paths) == ListType: |
|
856 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
857 |
return " AND ".join(var) |
|
858 |
else: |
|
859 |
return paths |
|
860 |
||
861 |
def ExtractModifier(self, variable, text): |
|
151 | 862 |
if variable.getnegated(): |
0 | 863 |
return "NOT(%s)"%text |
864 |
else: |
|
151 | 865 |
edge = variable.getedge() |
866 |
if edge == "rising": |
|
867 |
return self.AddTrigger("R_TRIG", text) |
|
868 |
elif edge == "falling": |
|
869 |
return self.AddTrigger("F_TRIG", text) |
|
0 | 870 |
return text |
871 |
||
872 |
def AddTrigger(self, edge, text): |
|
33 | 873 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]: |
874 |
self.Interface.append(("VAR", False, False, False, [])) |
|
0 | 875 |
i = 1 |
876 |
name = "%s%d"%(edge, i) |
|
1 | 877 |
while self.IsAlreadyDefined(name): |
0 | 878 |
i += 1 |
879 |
name = "%s%d"%(edge, i) |
|
33 | 880 |
self.Interface[-1][4].append((edge, name, None, None)) |
0 | 881 |
self.Program += " %s(CLK := %s);\n"%(name, text) |
882 |
return "%s.Q"%name |
|
883 |
||
884 |
def GenerateSTProgram(self): |
|
1 | 885 |
if self.ReturnType: |
29 | 886 |
program = "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType) |
0 | 887 |
else: |
29 | 888 |
program = "%s %s\n"%(self.Type, self.Name) |
33 | 889 |
for list_type, retain, constant, located, variables in self.Interface: |
1 | 890 |
program += " %s"%list_type |
891 |
if retain: |
|
892 |
program += " RETAIN" |
|
893 |
if constant: |
|
894 |
program += " CONSTANT" |
|
895 |
program += "\n" |
|
30 | 896 |
for var_type, var_name, var_address, var_initial in variables: |
93 | 897 |
program += " " |
898 |
if var_name: |
|
899 |
program += "%s "%var_name |
|
30 | 900 |
if var_address != None: |
31 | 901 |
program += "AT %s "%var_address |
30 | 902 |
program += ": %s"%var_type |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
903 |
if var_initial != None: |
33 | 904 |
value = {"TRUE":"0","FALSE":"1"}.get(str(var_initial).upper(), str(var_initial)) |
37
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
905 |
if var_type == "STRING": |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
906 |
program += " := '%s'"%value |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
907 |
elif var_type == "WSTRING": |
33 | 908 |
program += " := \"%s\""%value |
909 |
else: |
|
910 |
program += " := %s"%value |
|
30 | 911 |
program += ";\n" |
4
2de7fd952fdd
Adding File Dialog for choosing path to generated program
lbessard
parents:
2
diff
changeset
|
912 |
program += " END_VAR\n" |
0 | 913 |
program += "\n" |
914 |
program += self.Program |
|
915 |
program += "END_%s\n\n"%self.Type |
|
916 |
return program |
|
71 | 917 |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
918 |
def GeneratePouProgram(self, pou_name): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
919 |
GeneratePouProgram(pou_name) |
71 | 920 |
|
921 |
def GenerateCurrentProgram(project): |
|
922 |
global currentProject, currentProgram |
|
923 |
currentProject = project |
|
924 |
currentProgram = "" |
|
151 | 925 |
for datatype in project.getdataTypes(): |
926 |
datatypeComputed[datatype.getname()] = False |
|
927 |
for pou in project.getpous(): |
|
928 |
pouComputed[pou.getname()] = False |
|
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
929 |
if len(datatypeComputed) > 0: |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
930 |
currentProgram += "TYPE\n" |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
931 |
for datatype_name in datatypeComputed.keys(): |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
932 |
GenerateDataType(datatype_name) |
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
108
diff
changeset
|
933 |
currentProgram += "END_TYPE\n\n" |
71 | 934 |
for pou_name in pouComputed.keys(): |
935 |
GeneratePouProgram(pou_name) |
|
151 | 936 |
for config in project.getconfigurations(): |
71 | 937 |
currentProgram += GenerateConfiguration(config) |
938 |
return currentProgram |
|
939 |