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