author | lbessard |
Mon, 10 Sep 2007 09:00:50 +0200 | |
changeset 88 | bac6435df86f |
parent 80 | c798a68c5560 |
child 93 | c3c24b979a4d |
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 = "" |
|
37 |
pouComputed = {} |
|
38 |
||
1 | 39 |
def ReIndentText(text, nb_spaces): |
40 |
compute = "" |
|
41 |
lines = text.splitlines() |
|
42 |
if len(lines) > 0: |
|
43 |
spaces = 0 |
|
44 |
while lines[0][spaces] == " ": |
|
45 |
spaces += 1 |
|
46 |
indent = "" |
|
47 |
for i in xrange(spaces, nb_spaces): |
|
48 |
indent += " " |
|
49 |
for line in lines: |
|
2 | 50 |
if line != "": |
51 |
compute += "%s%s\n"%(indent, line) |
|
52 |
else: |
|
53 |
compute += "\n" |
|
1 | 54 |
return compute |
55 |
||
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
56 |
def GeneratePouProgram(pou_name): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
57 |
if not pouComputed.get(pou_name, True): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
58 |
pouComputed[pou_name] = True |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
59 |
global currentProject, currentProgram |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
60 |
pou = currentProject.getPou(pou_name) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
61 |
pou_type = pou.getPouType().getValue() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
62 |
if pou_type in pouTypeNames: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
63 |
pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type]) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
64 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
65 |
raise ValueError, "Undefined pou type" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
66 |
pou_program.GenerateInterface(pou.getInterface()) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
67 |
pou_program.GenerateProgram(pou) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
68 |
currentProgram += pou_program.GenerateSTProgram() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
69 |
|
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
70 |
def GenerateConfiguration(configuration): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
71 |
config = "\nCONFIGURATION %s\n"%configuration.getName() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
72 |
for varlist in configuration.getGlobalVars(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
73 |
config += " VAR_GLOBAL" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
74 |
if varlist.getRetain(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
75 |
config += " RETAIN" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
76 |
if varlist.getConstant(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
77 |
config += " CONSTANT" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
78 |
config += "\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
79 |
for var in varlist.getVariable(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
80 |
var_type = var.getType().getValue() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
81 |
config += " %s "%var.getName() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
82 |
address = var.getAddress() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
83 |
if address: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
84 |
config += "AT %s "%address |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
85 |
config += ": %s"%var_type |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
86 |
initial = var.getInitialValue() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
87 |
if initial: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
88 |
value = str(initial.getValue()) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
89 |
value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
90 |
if var_type == "STRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
91 |
config += " := '%s'"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
92 |
elif var_type == "WSTRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
93 |
config += " := \"%s\""%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
94 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
95 |
config += " := %s"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
96 |
config += ";\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
97 |
config += " END_VAR\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
98 |
for resource in configuration.getResource(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
99 |
config += GenerateResource(resource) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
100 |
config += "END_CONFIGURATION\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
101 |
return config |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
102 |
|
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
103 |
def GenerateResource(resource): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
104 |
resrce = "\n RESOURCE %s ON BEREMIZ\n"%resource.getName() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
105 |
for varlist in resource.getGlobalVars(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
106 |
resrce += " VAR_GLOBAL" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
107 |
if varlist.getRetain(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
108 |
resrce += " RETAIN" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
109 |
if varlist.getConstant(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
110 |
resrce += " CONSTANT" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
111 |
resrce += "\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
112 |
for var in varlist.getVariable(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
113 |
var_type = var.getType().getValue() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
114 |
resrce += " %s "%var.getName() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
115 |
address = var.getAddress() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
116 |
if address: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
117 |
resrce += "AT %s "%address |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
118 |
resrce += ": %s"%var.getType().getValue() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
119 |
initial = var.getInitialValue() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
120 |
if initial: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
121 |
value = str(initial.getValue()) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
122 |
value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
123 |
if var_type == "STRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
124 |
resrce += " := '%s'"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
125 |
elif var_type == "WSTRING": |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
126 |
resrce += " := \"%s\""%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
127 |
else: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
128 |
resrce += " := %s"%value |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
129 |
resrce += ";\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
130 |
resrce += " END_VAR\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
131 |
tasks = resource.getTask() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
132 |
for task in tasks: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
133 |
resrce += " TASK %s("%task.getName() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
134 |
args = [] |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
135 |
single = task.getSingle() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
136 |
if single: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
137 |
args.append("SINGLE := %s"%single) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
138 |
interval = task.getInterval() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
139 |
if interval: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
140 |
text = "t#" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
141 |
if interval.hour != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
142 |
text += "%dh"%interval.hour |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
143 |
if interval.minute != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
144 |
text += "%dm"%interval.minute |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
145 |
if interval.second != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
146 |
text += "%ds"%interval.second |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
147 |
if interval.microsecond != 0: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
148 |
text += "%dms"%(interval.microsecond / 1000) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
149 |
args.append("INTERVAL := %s"%text) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
150 |
args.append("PRIORITY := %s"%str(task.priority.getValue())) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
151 |
resrce += ",".join(args) + ");\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
152 |
for task in tasks: |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
153 |
for instance in task.getPouInstance(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
154 |
resrce += " PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType()) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
155 |
for instance in resource.getPouInstance(): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
156 |
resrce += " PROGRAM %s : %s;\n"%(instance.getName(), instance.getType()) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
157 |
resrce += " END_RESOURCE\n" |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
158 |
return resrce |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
159 |
|
0 | 160 |
""" |
161 |
Module implementing methods for generating PLC programs in ST or IL |
|
162 |
""" |
|
163 |
||
164 |
class PouProgram: |
|
165 |
||
166 |
def __init__(self, name, type): |
|
167 |
self.Name = name |
|
168 |
self.Type = type |
|
1 | 169 |
self.ReturnType = None |
170 |
self.Interface = [] |
|
171 |
self.InitialSteps = [] |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
172 |
self.ComputedBlocks = {} |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
173 |
self.ComputedConnectors = {} |
1 | 174 |
self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}} |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
175 |
self.SFCComputedBlocks = "" |
46 | 176 |
self.ActionNumber = 0 |
0 | 177 |
self.Program = "" |
178 |
||
46 | 179 |
def GetActionNumber(self): |
180 |
self.ActionNumber += 1 |
|
181 |
return self.ActionNumber |
|
182 |
||
1 | 183 |
def IsAlreadyDefined(self, name): |
33 | 184 |
for list_type, retain, constant, located, vars in self.Interface: |
30 | 185 |
for var_type, var_name, var_address, var_initial in vars: |
1 | 186 |
if name == var_name: |
187 |
return True |
|
188 |
return False |
|
189 |
||
0 | 190 |
def GenerateInterface(self, interface): |
191 |
if self.Type == "FUNCTION": |
|
1 | 192 |
self.ReturnType = interface.getReturnType().getValue() |
0 | 193 |
for varlist in interface.getContent(): |
1 | 194 |
variables = [] |
33 | 195 |
located = False |
0 | 196 |
for var in varlist["value"].getVariable(): |
197 |
type = var.getType().getValue() |
|
68
66308e07402c
Adding support for allowing declarations of function block into POU interface
lbessard
parents:
66
diff
changeset
|
198 |
if not isinstance(type, (StringType, UnicodeType)): |
66308e07402c
Adding support for allowing declarations of function block into POU interface
lbessard
parents:
66
diff
changeset
|
199 |
type = type.getName() |
71 | 200 |
GeneratePouProgram(type) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
201 |
initial = var.getInitialValue() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
202 |
if initial: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
203 |
initial_value = initial.getValue() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
204 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
205 |
initial_value = None |
30 | 206 |
address = var.getAddress() |
68
66308e07402c
Adding support for allowing declarations of function block into POU interface
lbessard
parents:
66
diff
changeset
|
207 |
if address: |
33 | 208 |
located = True |
30 | 209 |
variables.append((type, var.getName(), address, initial_value)) |
1 | 210 |
self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), |
33 | 211 |
varlist["value"].getConstant(), located, variables)) |
0 | 212 |
|
213 |
def GenerateProgram(self, pou): |
|
214 |
body = pou.getBody() |
|
215 |
body_content = body.getContent() |
|
216 |
body_type = body_content["name"] |
|
217 |
if body_type in ["IL","ST"]: |
|
1 | 218 |
self.Program = ReIndentText(body_content["value"].getText(), 2) |
0 | 219 |
elif body_type == "FBD": |
220 |
for instance in body.getContentInstances(): |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
221 |
if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)): |
0 | 222 |
var = instance.getExpression() |
223 |
connections = instance.connectionPointIn.getConnections() |
|
224 |
if connections and len(connections) == 1: |
|
225 |
expression = self.ComputeFBDExpression(body, connections[0]) |
|
226 |
self.Program += " %s := %s;\n"%(var, expression) |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
227 |
elif isinstance(instance, plcopen.block): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
228 |
type = instance.getTypeName() |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
229 |
block_infos = GetBlockType(type) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
230 |
block_infos["generate"](self, instance, body, None) |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
231 |
elif isinstance(instance, plcopen.connector): |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
232 |
connector = instance.getName() |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
233 |
if self.ComputedConnectors.get(connector, None): |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
234 |
continue |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
235 |
connections = instance.connectionPointIn.getConnections() |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
236 |
if connections and len(connections) == 1: |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
237 |
self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0]) |
0 | 238 |
elif body_type == "LD": |
239 |
for instance in body.getContentInstances(): |
|
240 |
if isinstance(instance, plcopen.coil): |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
241 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), body) |
0 | 242 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
243 |
expression = self.ComputeLDExpression(paths, True) |
|
244 |
self.Program += " %s := %s;\n"%(variable, expression) |
|
245 |
elif body_type == "SFC": |
|
246 |
for instance in body.getContentInstances(): |
|
247 |
if isinstance(instance, plcopen.step): |
|
1 | 248 |
self.GenerateSFCStep(instance, pou) |
0 | 249 |
elif isinstance(instance, plcopen.actionBlock): |
1 | 250 |
self.GenerateSFCStepActions(instance, pou) |
0 | 251 |
elif isinstance(instance, plcopen.transition): |
1 | 252 |
self.GenerateSFCTransition(instance, pou) |
0 | 253 |
elif isinstance(instance, plcopen.jumpStep): |
254 |
self.GenerateSFCJump(instance, pou) |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
255 |
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
|
256 |
action_name = "COMPUTE_FUNCTION_BLOCKS" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
self.Program = "" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
261 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
262 |
raise Exception |
1 | 263 |
for initialstep in self.InitialSteps: |
264 |
self.ComputeSFCStep(initialstep) |
|
0 | 265 |
|
266 |
def ComputeFBDExpression(self, body, link): |
|
267 |
localid = link.getRefLocalId() |
|
268 |
instance = body.getContentInstance(localid) |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
269 |
if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)): |
0 | 270 |
return instance.getExpression() |
271 |
elif isinstance(instance, plcopen.block): |
|
272 |
type = instance.getTypeName() |
|
273 |
block_infos = GetBlockType(type) |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
274 |
return block_infos["generate"](self, instance, body, link) |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
275 |
elif isinstance(instance, plcopen.continuation): |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
276 |
name = instance.getName() |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
277 |
computed_value = self.ComputedConnectors.get(name, None) |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
278 |
if computed_value != None: |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
279 |
return computed_value |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
280 |
for tmp_instance in body.getContentInstances(): |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
281 |
if isinstance(tmp_instance, plcopen.connector): |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
282 |
if tmp_instance.getName() == name: |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
283 |
connections = tmp_instance.connectionPointIn.getConnections() |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
284 |
if connections and len(connections) == 1: |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
285 |
expression = self.ComputeFBDExpression(body, connections[0]) |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
286 |
self.ComputedConnectors[name] = expression |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
287 |
return expression |
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
288 |
raise ValueError, "No connector found" |
0 | 289 |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
290 |
def GenerateLDPaths(self, connections, body): |
0 | 291 |
paths = [] |
292 |
for connection in connections: |
|
293 |
localId = connection.getRefLocalId() |
|
294 |
next = body.getContentInstance(localId) |
|
295 |
if isinstance(next, plcopen.leftPowerRail): |
|
296 |
paths.append(None) |
|
297 |
else: |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
298 |
variable = self.ExtractModifier(next, next.getVariable()) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
299 |
result = self.GenerateLDPaths(next.connectionPointIn.getConnections(), body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
300 |
if len(result) > 1: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
301 |
paths.append([variable, tuple(result)]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
302 |
elif type(result[0]) == ListType: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
303 |
paths.append([variable] + result[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
304 |
elif result[0]: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
305 |
paths.append([variable, result[0]]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
306 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
307 |
paths.append(variable) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
308 |
return paths |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
309 |
|
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
310 |
def GetNetworkType(self, connections, body): |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
311 |
network_type = "FBD" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
312 |
for connection in connections: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
313 |
localId = connection.getRefLocalId() |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
314 |
next = body.getContentInstance(localId) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
315 |
if isinstance(next, plcopen.leftPowerRail) or isinstance(next, plcopen.contact): |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
316 |
return "LD" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
317 |
elif isinstance(next, plcopen.block): |
70 | 318 |
for variable in next.inputVariables.getVariable(): |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
319 |
result = self.GetNetworkType(variable.connectionPointIn.getConnections(), body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
320 |
if result != "FBD": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
321 |
return result |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
322 |
elif isinstance(next, plcopen.inVariable): |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
323 |
return "FBD" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
324 |
elif isinstance(next, plcopen.inOutVariable): |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
325 |
return self.GetNetworkType(next.connectionPointIn.getConnections(), body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
326 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
327 |
return None |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
328 |
return "FBD" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
329 |
|
2 | 330 |
def ExtractDivergenceInput(self, divergence, pou): |
331 |
connectionPointIn = divergence.getConnectionPointIn() |
|
332 |
if connectionPointIn: |
|
333 |
connections = connectionPointIn.getConnections() |
|
334 |
if len(connections) == 1: |
|
335 |
instanceLocalId = connections[0].getRefLocalId() |
|
336 |
return pou.body.getContentInstance(instanceLocalId) |
|
337 |
return None |
|
338 |
||
339 |
def ExtractConvergenceInputs(self, convergence, pou): |
|
340 |
instances = [] |
|
341 |
for connectionPointIn in convergence.getConnectionPointIn(): |
|
342 |
connections = connectionPointIn.getConnections() |
|
343 |
if len(connections) == 1: |
|
344 |
instanceLocalId = connections[0].getRefLocalId() |
|
345 |
instances.append(pou.body.getContentInstance(instanceLocalId)) |
|
346 |
return instances |
|
347 |
||
1 | 348 |
def GenerateSFCStep(self, step, pou): |
0 | 349 |
step_name = step.getName() |
1 | 350 |
if step_name not in self.SFCNetworks["Steps"].keys(): |
351 |
if step.getInitialStep(): |
|
352 |
self.InitialSteps.append(step_name) |
|
0 | 353 |
step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []} |
354 |
if step.connectionPointIn: |
|
355 |
instances = [] |
|
356 |
connections = step.connectionPointIn.getConnections() |
|
357 |
if len(connections) == 1: |
|
358 |
instanceLocalId = connections[0].getRefLocalId() |
|
359 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
360 |
if isinstance(instance, plcopen.transition): |
|
361 |
instances.append(instance) |
|
362 |
elif isinstance(instance, plcopen.selectionConvergence): |
|
2 | 363 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
364 |
elif isinstance(instance, plcopen.simultaneousDivergence): |
|
365 |
transition = self.ExtractDivergenceInput(instance, pou) |
|
366 |
if transition: |
|
367 |
if isinstance(transition, plcopen.transition): |
|
1 | 368 |
instances.append(transition) |
2 | 369 |
elif isinstance(transition, plcopen.selectionConvergence): |
370 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
|
0 | 371 |
for instance in instances: |
2 | 372 |
self.GenerateSFCTransition(instance, pou) |
1 | 373 |
if instance in self.SFCNetworks["Transitions"].keys(): |
374 |
self.SFCNetworks["Transitions"][instance]["to"].append(step_name) |
|
375 |
self.SFCNetworks["Steps"][step_name] = step_infos |
|
0 | 376 |
|
377 |
def GenerateSFCJump(self, jump, pou): |
|
378 |
jump_target = jump.getTargetName() |
|
379 |
if jump.connectionPointIn: |
|
380 |
instances = [] |
|
381 |
connections = jump.connectionPointIn.getConnections() |
|
382 |
if len(connections) == 1: |
|
383 |
instanceLocalId = connections[0].getRefLocalId() |
|
384 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
385 |
if isinstance(instance, plcopen.transition): |
|
386 |
instances.append(instance) |
|
387 |
elif isinstance(instance, plcopen.selectionConvergence): |
|
2 | 388 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
389 |
elif isinstance(instance, plcopen.simultaneousDivergence): |
|
390 |
transition = self.ExtractDivergenceInput(instance, pou) |
|
391 |
if transition: |
|
392 |
if isinstance(transition, plcopen.transition): |
|
1 | 393 |
instances.append(transition) |
2 | 394 |
elif isinstance(transition, plcopen.selectionConvergence): |
395 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
|
0 | 396 |
for instance in instances: |
2 | 397 |
self.GenerateSFCTransition(instance, pou) |
1 | 398 |
if instance in self.SFCNetworks["Transitions"].keys(): |
399 |
self.SFCNetworks["Transitions"][instance]["to"].append(jump_target) |
|
400 |
||
401 |
def GenerateSFCStepActions(self, actionBlock, pou): |
|
0 | 402 |
connections = actionBlock.connectionPointIn.getConnections() |
403 |
if len(connections) == 1: |
|
404 |
stepLocalId = connections[0].getRefLocalId() |
|
405 |
step = pou.body.getContentInstance(stepLocalId) |
|
1 | 406 |
self.GenerateSFCStep(step, pou) |
0 | 407 |
step_name = step.getName() |
1 | 408 |
if step_name in self.SFCNetworks["Steps"].keys(): |
0 | 409 |
actions = actionBlock.getActions() |
410 |
for action in actions: |
|
1 | 411 |
action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]} |
412 |
if "duration" in action: |
|
413 |
action_infos["duration"] = action["duration"] |
|
414 |
if "indicator" in action: |
|
415 |
action_infos["indicator"] = action["indicator"] |
|
416 |
if action["type"] == "reference": |
|
417 |
self.GenerateSFCAction(action["value"], pou) |
|
46 | 418 |
else: |
419 |
action_name = "INLINE%d"%self.GetActionNumber() |
|
420 |
self.SFCNetworks["Actions"][action_name] = " %s\n"%action["value"] |
|
421 |
action_infos["content"] = action_name |
|
1 | 422 |
self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos) |
423 |
||
424 |
def GenerateSFCAction(self, action_name, pou): |
|
425 |
if action_name not in self.SFCNetworks["Actions"].keys(): |
|
426 |
actionContent = pou.getAction(action_name) |
|
427 |
if actionContent: |
|
428 |
actionType = actionContent.getBodyType() |
|
429 |
actionBody = actionContent.getBody() |
|
430 |
if actionType in ["ST", "IL"]: |
|
431 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4) |
|
432 |
elif actionType == "FBD": |
|
433 |
for instance in actionBody.getContentInstances(): |
|
434 |
if isinstance(instance, plcopen.outVariable): |
|
435 |
var = instance.getExpression() |
|
436 |
connections = instance.connectionPointIn.getConnections() |
|
437 |
if connections and len(connections) == 1: |
|
438 |
expression = self.ComputeFBDExpression(actionBody, connections[0]) |
|
70 | 439 |
action_content = self.Program + " %s := %s;\n"%(var, expression) |
1 | 440 |
self.Program = "" |
70 | 441 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
1 | 442 |
elif actionType == "LD": |
443 |
for instance in actionbody.getContentInstances(): |
|
444 |
if isinstance(instance, plcopen.coil): |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
445 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), actionBody) |
1 | 446 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
447 |
expression = self.ComputeLDExpression(paths, True) |
|
70 | 448 |
action_content = self.Program + " %s := %s;\n"%(variable, expression) |
1 | 449 |
self.Program = "" |
70 | 450 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
1 | 451 |
|
452 |
def GenerateSFCTransition(self, transition, pou): |
|
453 |
if transition not in self.SFCNetworks["Transitions"].keys(): |
|
454 |
steps = [] |
|
0 | 455 |
connections = transition.connectionPointIn.getConnections() |
456 |
if len(connections) == 1: |
|
457 |
instanceLocalId = connections[0].getRefLocalId() |
|
458 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
459 |
if isinstance(instance, plcopen.step): |
|
2 | 460 |
steps.append(instance) |
0 | 461 |
elif isinstance(instance, plcopen.selectionDivergence): |
2 | 462 |
step = self.ExtractDivergenceInput(instance, pou) |
463 |
if step: |
|
464 |
if isinstance(step, plcopen.step): |
|
465 |
steps.append(step) |
|
466 |
elif isinstance(step, plcopen.simultaneousConvergence): |
|
467 |
steps.extend(self.ExtractConvergenceInputs(step, pou)) |
|
468 |
elif isinstance(instance, plcopen.simultaneousConvergence): |
|
469 |
steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
|
80 | 470 |
transition_infos = {"priority": transition.getPriority(), "from": [], "to" : []} |
1 | 471 |
transitionValues = transition.getConditionContent() |
472 |
if transitionValues["type"] == "inline": |
|
473 |
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
|
474 |
elif transitionValues["type"] == "reference": |
1 | 475 |
transitionContent = pou.getTransition(transitionValues["value"]) |
476 |
transitionType = transitionContent.getBodyType() |
|
477 |
transitionBody = transitionContent.getBody() |
|
478 |
if transitionType == "IL": |
|
2 | 479 |
transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4) |
1 | 480 |
elif transitionType == "ST": |
2 | 481 |
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
|
482 |
elif transitionType == "FBD": |
1 | 483 |
for instance in transitionBody.getContentInstances(): |
484 |
if isinstance(instance, plcopen.outVariable): |
|
485 |
connections = instance.connectionPointIn.getConnections() |
|
486 |
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
|
487 |
expression = self.ComputeFBDExpression(transitionBody, connections[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
488 |
transition_infos["content"] = "\n := %s;\n"%expression |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
489 |
self.SFCComputedBlocks += self.Program |
70 | 490 |
self.Program = "" |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
491 |
elif transitionType == "LD": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
492 |
for instance in transitionBody.getContentInstances(): |
1 | 493 |
if isinstance(instance, plcopen.coil): |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
494 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody) |
1 | 495 |
expression = self.ComputeLDExpression(paths, True) |
496 |
transition_infos["content"] = "\n := %s;\n"%expression |
|
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
497 |
self.SFCComputedBlocks += self.Program |
70 | 498 |
self.Program = "" |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
499 |
elif transitionValues["type"] == "connection": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
500 |
body = pou.getBody() |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
501 |
connections = transition.getConnections() |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
502 |
network_type = self.GetNetworkType(connections, body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
503 |
if network_type == None: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
504 |
raise Exception |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
505 |
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
|
506 |
paths = self.GenerateLDPaths(connections, body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
507 |
expression = self.ComputeLDExpression(paths, True) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
508 |
transition_infos["content"] = "\n := %s;\n"%expression |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
509 |
self.SFCComputedBlocks += self.Program |
70 | 510 |
self.Program = "" |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
511 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
512 |
expression = self.ComputeFBDExpression(body, connections[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
513 |
transition_infos["content"] = "\n := %s;\n"%expression |
72
73212220ad22
Adding support for generating FBD with connectors and continuations
lbessard
parents:
71
diff
changeset
|
514 |
self.SFCComputedBlocks += self.Program |
70 | 515 |
self.Program = "" |
2 | 516 |
for step in steps: |
517 |
self.GenerateSFCStep(step, pou) |
|
518 |
step_name = step.getName() |
|
1 | 519 |
if step_name in self.SFCNetworks["Steps"].keys(): |
520 |
transition_infos["from"].append(step_name) |
|
521 |
self.SFCNetworks["Steps"][step_name]["transitions"].append(transition) |
|
522 |
self.SFCNetworks["Transitions"][transition] = transition_infos |
|
523 |
||
524 |
def ComputeSFCStep(self, step_name): |
|
525 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
526 |
step_infos = self.SFCNetworks["Steps"].pop(step_name) |
|
527 |
if step_infos["initial"]: |
|
528 |
self.Program += " INITIAL_STEP %s:\n"%step_name |
|
529 |
else: |
|
530 |
self.Program += " STEP %s:\n"%step_name |
|
531 |
actions = [] |
|
532 |
for action_infos in step_infos["actions"]: |
|
533 |
actions.append(action_infos["content"]) |
|
534 |
self.Program += " %(content)s(%(qualifier)s"%action_infos |
|
535 |
if "duration" in action_infos: |
|
536 |
self.Program += ", %(duration)s"%action_infos |
|
537 |
if "indicator" in action_infos: |
|
538 |
self.Program += ", %(indicator)s"%action_infos |
|
539 |
self.Program += ");\n" |
|
540 |
self.Program += " END_STEP\n\n" |
|
541 |
for action in actions: |
|
542 |
self.ComputeSFCAction(action) |
|
543 |
for transition in step_infos["transitions"]: |
|
544 |
self.ComputeSFCTransition(transition) |
|
545 |
||
546 |
def ComputeSFCAction(self, action_name): |
|
547 |
if action_name in self.SFCNetworks["Actions"].keys(): |
|
548 |
action_content = self.SFCNetworks["Actions"].pop(action_name) |
|
549 |
self.Program += " ACTION %s:\n%s END_ACTION\n\n"%(action_name, action_content) |
|
550 |
||
551 |
def ComputeSFCTransition(self, transition): |
|
552 |
if transition in self.SFCNetworks["Transitions"].keys(): |
|
553 |
transition_infos = self.SFCNetworks["Transitions"].pop(transition) |
|
80 | 554 |
self.Program += " TRANSITION" |
555 |
if transition_infos["priority"] != None: |
|
556 |
self.Program += " (PRIORITY := %d)"%transition_infos["priority"] |
|
557 |
self.Program += " FROM " |
|
1 | 558 |
if len(transition_infos["from"]) > 1: |
559 |
self.Program += "(%s)"%", ".join(transition_infos["from"]) |
|
560 |
else: |
|
561 |
self.Program += "%s"%transition_infos["from"][0] |
|
562 |
self.Program += " TO " |
|
563 |
if len(transition_infos["to"]) > 1: |
|
564 |
self.Program += "(%s)"%", ".join(transition_infos["to"]) |
|
565 |
else: |
|
566 |
self.Program += "%s"%transition_infos["to"][0] |
|
567 |
self.Program += transition_infos["content"] |
|
568 |
self.Program += " END_TRANSITION\n\n" |
|
569 |
for step_name in transition_infos["to"]: |
|
570 |
self.ComputeSFCStep(step_name) |
|
571 |
||
0 | 572 |
def ComputeLDExpression(self, paths, first = False): |
573 |
if type(paths) == TupleType: |
|
574 |
if None in paths: |
|
575 |
return "TRUE" |
|
576 |
else: |
|
577 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
578 |
if first: |
|
579 |
return " OR ".join(var) |
|
580 |
else: |
|
581 |
return "(%s)"%" OR ".join(var) |
|
582 |
elif type(paths) == ListType: |
|
583 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
584 |
return " AND ".join(var) |
|
585 |
else: |
|
586 |
return paths |
|
587 |
||
588 |
def ExtractModifier(self, variable, text): |
|
589 |
if variable.getNegated(): |
|
590 |
return "NOT(%s)"%text |
|
591 |
else: |
|
592 |
edge = variable.getEdge() |
|
593 |
if edge: |
|
594 |
if edge.getValue() == "rising": |
|
595 |
return self.AddTrigger("R_TRIG", text) |
|
596 |
elif edge.getValue() == "falling": |
|
597 |
return self.AddTrigger("F_TRIG", text) |
|
598 |
return text |
|
599 |
||
600 |
def AddTrigger(self, edge, text): |
|
33 | 601 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]: |
602 |
self.Interface.append(("VAR", False, False, False, [])) |
|
0 | 603 |
i = 1 |
604 |
name = "%s%d"%(edge, i) |
|
1 | 605 |
while self.IsAlreadyDefined(name): |
0 | 606 |
i += 1 |
607 |
name = "%s%d"%(edge, i) |
|
33 | 608 |
self.Interface[-1][4].append((edge, name, None, None)) |
0 | 609 |
self.Program += " %s(CLK := %s);\n"%(name, text) |
610 |
return "%s.Q"%name |
|
611 |
||
612 |
def GenerateSTProgram(self): |
|
1 | 613 |
if self.ReturnType: |
29 | 614 |
program = "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType) |
0 | 615 |
else: |
29 | 616 |
program = "%s %s\n"%(self.Type, self.Name) |
33 | 617 |
for list_type, retain, constant, located, variables in self.Interface: |
1 | 618 |
program += " %s"%list_type |
619 |
if retain: |
|
620 |
program += " RETAIN" |
|
621 |
if constant: |
|
622 |
program += " CONSTANT" |
|
623 |
program += "\n" |
|
30 | 624 |
for var_type, var_name, var_address, var_initial in variables: |
625 |
program += " %s "%var_name |
|
626 |
if var_address != None: |
|
31 | 627 |
program += "AT %s "%var_address |
30 | 628 |
program += ": %s"%var_type |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
629 |
if var_initial != None: |
33 | 630 |
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
|
631 |
if var_type == "STRING": |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
632 |
program += " := '%s'"%value |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
633 |
elif var_type == "WSTRING": |
33 | 634 |
program += " := \"%s\""%value |
635 |
else: |
|
636 |
program += " := %s"%value |
|
30 | 637 |
program += ";\n" |
4
2de7fd952fdd
Adding File Dialog for choosing path to generated program
lbessard
parents:
2
diff
changeset
|
638 |
program += " END_VAR\n" |
0 | 639 |
program += "\n" |
640 |
program += self.Program |
|
641 |
program += "END_%s\n\n"%self.Type |
|
642 |
return program |
|
71 | 643 |
|
78
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
644 |
def GeneratePouProgram(self, pou_name): |
049f2e7090a2
Adding support for adding block types with particular behaviour
lbessard
parents:
72
diff
changeset
|
645 |
GeneratePouProgram(pou_name) |
71 | 646 |
|
647 |
def GenerateCurrentProgram(project): |
|
648 |
global currentProject, currentProgram |
|
649 |
currentProject = project |
|
650 |
currentProgram = "" |
|
651 |
for pou in project.getPous(): |
|
652 |
pouComputed[pou.getName()] = False |
|
653 |
for pou_name in pouComputed.keys(): |
|
654 |
GeneratePouProgram(pou_name) |
|
29 | 655 |
for config in project.getConfigurations(): |
71 | 656 |
currentProgram += GenerateConfiguration(config) |
657 |
return currentProgram |
|
658 |