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