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