author | lbessard |
Thu, 09 Aug 2007 18:06:54 +0200 | |
changeset 66 | fd138fc77510 |
parent 58 | 39cd981ff242 |
child 68 | 66308e07402c |
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 = [] |
|
64 |
self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}} |
|
46 | 65 |
self.ActionNumber = 0 |
0 | 66 |
self.Program = "" |
67 |
||
46 | 68 |
def GetActionNumber(self): |
69 |
self.ActionNumber += 1 |
|
70 |
return self.ActionNumber |
|
71 |
||
1 | 72 |
def IsAlreadyDefined(self, name): |
33 | 73 |
for list_type, retain, constant, located, vars in self.Interface: |
30 | 74 |
for var_type, var_name, var_address, var_initial in vars: |
1 | 75 |
if name == var_name: |
76 |
return True |
|
77 |
return False |
|
78 |
||
0 | 79 |
def GenerateInterface(self, interface): |
80 |
if self.Type == "FUNCTION": |
|
1 | 81 |
self.ReturnType = interface.getReturnType().getValue() |
0 | 82 |
for varlist in interface.getContent(): |
1 | 83 |
variables = [] |
33 | 84 |
located = False |
0 | 85 |
for var in varlist["value"].getVariable(): |
86 |
type = var.getType().getValue() |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
87 |
initial = var.getInitialValue() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
88 |
if initial: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
89 |
initial_value = initial.getValue() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
90 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
91 |
initial_value = None |
30 | 92 |
address = var.getAddress() |
93 |
if address == "": |
|
94 |
address = None |
|
33 | 95 |
else: |
96 |
located = True |
|
30 | 97 |
variables.append((type, var.getName(), address, initial_value)) |
1 | 98 |
self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), |
33 | 99 |
varlist["value"].getConstant(), located, variables)) |
0 | 100 |
|
101 |
def GenerateProgram(self, pou): |
|
102 |
body = pou.getBody() |
|
103 |
body_content = body.getContent() |
|
104 |
body_type = body_content["name"] |
|
105 |
if body_type in ["IL","ST"]: |
|
1 | 106 |
self.Program = ReIndentText(body_content["value"].getText(), 2) |
0 | 107 |
elif body_type == "FBD": |
108 |
for instance in body.getContentInstances(): |
|
109 |
if isinstance(instance, plcopen.outVariable): |
|
110 |
var = instance.getExpression() |
|
111 |
connections = instance.connectionPointIn.getConnections() |
|
112 |
if connections and len(connections) == 1: |
|
113 |
expression = self.ComputeFBDExpression(body, connections[0]) |
|
114 |
self.Program += " %s := %s;\n"%(var, expression) |
|
115 |
elif body_type == "LD": |
|
116 |
for instance in body.getContentInstances(): |
|
117 |
if isinstance(instance, plcopen.coil): |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
118 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), body) |
0 | 119 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
120 |
expression = self.ComputeLDExpression(paths, True) |
|
121 |
self.Program += " %s := %s;\n"%(variable, expression) |
|
122 |
elif body_type == "SFC": |
|
123 |
for instance in body.getContentInstances(): |
|
124 |
if isinstance(instance, plcopen.step): |
|
1 | 125 |
self.GenerateSFCStep(instance, pou) |
0 | 126 |
elif isinstance(instance, plcopen.actionBlock): |
1 | 127 |
self.GenerateSFCStepActions(instance, pou) |
0 | 128 |
elif isinstance(instance, plcopen.transition): |
1 | 129 |
self.GenerateSFCTransition(instance, pou) |
0 | 130 |
elif isinstance(instance, plcopen.jumpStep): |
131 |
self.GenerateSFCJump(instance, pou) |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
132 |
if len(self.InitialSteps) > 0: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
133 |
action_name = "COMPUTE_FUNCTION_BLOCKS" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
134 |
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
|
135 |
self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
136 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(self.Program, 4) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
137 |
self.Program = "" |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
138 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
139 |
raise Exception |
1 | 140 |
for initialstep in self.InitialSteps: |
141 |
self.ComputeSFCStep(initialstep) |
|
0 | 142 |
|
143 |
def ComputeFBDExpression(self, body, link): |
|
144 |
localid = link.getRefLocalId() |
|
145 |
instance = body.getContentInstance(localid) |
|
146 |
if isinstance(instance, plcopen.inVariable): |
|
147 |
return instance.getExpression() |
|
148 |
elif isinstance(instance, plcopen.block): |
|
149 |
name = instance.getInstanceName() |
|
150 |
type = instance.getTypeName() |
|
151 |
block_infos = GetBlockType(type) |
|
152 |
if block_infos["type"] == "function": |
|
153 |
vars = [] |
|
154 |
for variable in instance.inputVariables.getVariable(): |
|
155 |
connections = variable.connectionPointIn.getConnections() |
|
156 |
if connections and len(connections) == 1: |
|
157 |
value = self.ComputeFBDExpression(body, connections[0]) |
|
158 |
vars.append(self.ExtractModifier(variable, value)) |
|
159 |
variable = instance.outputVariables.getVariable()[0] |
|
160 |
return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars))) |
|
161 |
elif block_infos["type"] == "functionBlock": |
|
33 | 162 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]: |
163 |
self.Interface.append(("VAR", False, False, False, [])) |
|
1 | 164 |
if not self.IsAlreadyDefined(name): |
33 | 165 |
self.Interface[-1][4].append((type, name, None, None)) |
0 | 166 |
vars = [] |
167 |
for variable in instance.inputVariables.getVariable(): |
|
168 |
connections = variable.connectionPointIn.getConnections() |
|
169 |
if connections and len(connections) == 1: |
|
170 |
parameter = variable.getFormalParameter() |
|
171 |
value = self.ComputeFBDExpression(body, connections[0]) |
|
172 |
vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value))) |
|
173 |
self.Program += " %s(%s);\n"%(name, ", ".join(vars)) |
|
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): |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
209 |
for variable in instance.inputVariables.getVariable(): |
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]) |
|
330 |
self.SFCNetworks["Actions"][action_name] = self.Program + " %s := %s;\n"%(var, expression) |
|
331 |
self.Program = "" |
|
332 |
elif actionType == "LD": |
|
333 |
for instance in actionbody.getContentInstances(): |
|
334 |
if isinstance(instance, plcopen.coil): |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
335 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), actionBody) |
1 | 336 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
337 |
expression = self.ComputeLDExpression(paths, True) |
|
338 |
self.SFCNetworks["Actions"][action_name] = self.Program + " %s := %s;\n"%(variable, expression) |
|
339 |
self.Program = "" |
|
340 |
||
341 |
def GenerateSFCTransition(self, transition, pou): |
|
342 |
if transition not in self.SFCNetworks["Transitions"].keys(): |
|
343 |
steps = [] |
|
0 | 344 |
connections = transition.connectionPointIn.getConnections() |
345 |
if len(connections) == 1: |
|
346 |
instanceLocalId = connections[0].getRefLocalId() |
|
347 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
348 |
if isinstance(instance, plcopen.step): |
|
2 | 349 |
steps.append(instance) |
0 | 350 |
elif isinstance(instance, plcopen.selectionDivergence): |
2 | 351 |
step = self.ExtractDivergenceInput(instance, pou) |
352 |
if step: |
|
353 |
if isinstance(step, plcopen.step): |
|
354 |
steps.append(step) |
|
355 |
elif isinstance(step, plcopen.simultaneousConvergence): |
|
356 |
steps.extend(self.ExtractConvergenceInputs(step, pou)) |
|
357 |
elif isinstance(instance, plcopen.simultaneousConvergence): |
|
358 |
steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
|
1 | 359 |
transition_infos = {"from": [], "to" : []} |
360 |
transitionValues = transition.getConditionContent() |
|
361 |
if transitionValues["type"] == "inline": |
|
362 |
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
|
363 |
elif transitionValues["type"] == "reference": |
1 | 364 |
transitionContent = pou.getTransition(transitionValues["value"]) |
365 |
transitionType = transitionContent.getBodyType() |
|
366 |
transitionBody = transitionContent.getBody() |
|
367 |
if transitionType == "IL": |
|
2 | 368 |
transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4) |
1 | 369 |
elif transitionType == "ST": |
2 | 370 |
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
|
371 |
elif transitionType == "FBD": |
1 | 372 |
for instance in transitionBody.getContentInstances(): |
373 |
if isinstance(instance, plcopen.outVariable): |
|
374 |
connections = instance.connectionPointIn.getConnections() |
|
375 |
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
|
376 |
expression = self.ComputeFBDExpression(transitionBody, connections[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
377 |
transition_infos["content"] = "\n := %s;\n"%expression |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
378 |
elif transitionType == "LD": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
379 |
for instance in transitionBody.getContentInstances(): |
1 | 380 |
if isinstance(instance, plcopen.coil): |
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
381 |
paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody) |
1 | 382 |
expression = self.ComputeLDExpression(paths, True) |
383 |
transition_infos["content"] = "\n := %s;\n"%expression |
|
66
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
384 |
elif transitionValues["type"] == "connection": |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
385 |
body = pou.getBody() |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
386 |
connections = transition.getConnections() |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
387 |
network_type = self.GetNetworkType(connections, body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
388 |
if network_type == None: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
389 |
raise Exception |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
390 |
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
|
391 |
paths = self.GenerateLDPaths(connections, body) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
392 |
expression = self.ComputeLDExpression(paths, True) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
393 |
transition_infos["content"] = "\n := %s;\n"%expression |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
394 |
else: |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
395 |
expression = self.ComputeFBDExpression(body, connections[0]) |
fd138fc77510
Adding support for generating network and rung connected to transitions into SFC
lbessard
parents:
58
diff
changeset
|
396 |
transition_infos["content"] = "\n := %s;\n"%expression |
2 | 397 |
for step in steps: |
398 |
self.GenerateSFCStep(step, pou) |
|
399 |
step_name = step.getName() |
|
1 | 400 |
if step_name in self.SFCNetworks["Steps"].keys(): |
401 |
transition_infos["from"].append(step_name) |
|
402 |
self.SFCNetworks["Steps"][step_name]["transitions"].append(transition) |
|
403 |
self.SFCNetworks["Transitions"][transition] = transition_infos |
|
404 |
||
405 |
def ComputeSFCStep(self, step_name): |
|
406 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
407 |
step_infos = self.SFCNetworks["Steps"].pop(step_name) |
|
408 |
if step_infos["initial"]: |
|
409 |
self.Program += " INITIAL_STEP %s:\n"%step_name |
|
410 |
else: |
|
411 |
self.Program += " STEP %s:\n"%step_name |
|
412 |
actions = [] |
|
413 |
for action_infos in step_infos["actions"]: |
|
414 |
actions.append(action_infos["content"]) |
|
415 |
self.Program += " %(content)s(%(qualifier)s"%action_infos |
|
416 |
if "duration" in action_infos: |
|
417 |
self.Program += ", %(duration)s"%action_infos |
|
418 |
if "indicator" in action_infos: |
|
419 |
self.Program += ", %(indicator)s"%action_infos |
|
420 |
self.Program += ");\n" |
|
421 |
self.Program += " END_STEP\n\n" |
|
422 |
for action in actions: |
|
423 |
self.ComputeSFCAction(action) |
|
424 |
for transition in step_infos["transitions"]: |
|
425 |
self.ComputeSFCTransition(transition) |
|
426 |
||
427 |
def ComputeSFCAction(self, action_name): |
|
428 |
if action_name in self.SFCNetworks["Actions"].keys(): |
|
429 |
action_content = self.SFCNetworks["Actions"].pop(action_name) |
|
430 |
self.Program += " ACTION %s:\n%s END_ACTION\n\n"%(action_name, action_content) |
|
431 |
||
432 |
def ComputeSFCTransition(self, transition): |
|
433 |
if transition in self.SFCNetworks["Transitions"].keys(): |
|
434 |
transition_infos = self.SFCNetworks["Transitions"].pop(transition) |
|
435 |
self.Program += " TRANSITION FROM " |
|
436 |
if len(transition_infos["from"]) > 1: |
|
437 |
self.Program += "(%s)"%", ".join(transition_infos["from"]) |
|
438 |
else: |
|
439 |
self.Program += "%s"%transition_infos["from"][0] |
|
440 |
self.Program += " TO " |
|
441 |
if len(transition_infos["to"]) > 1: |
|
442 |
self.Program += "(%s)"%", ".join(transition_infos["to"]) |
|
443 |
else: |
|
444 |
self.Program += "%s"%transition_infos["to"][0] |
|
445 |
self.Program += transition_infos["content"] |
|
446 |
self.Program += " END_TRANSITION\n\n" |
|
447 |
for step_name in transition_infos["to"]: |
|
448 |
self.ComputeSFCStep(step_name) |
|
449 |
||
0 | 450 |
def ComputeLDExpression(self, paths, first = False): |
451 |
if type(paths) == TupleType: |
|
452 |
if None in paths: |
|
453 |
return "TRUE" |
|
454 |
else: |
|
455 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
456 |
if first: |
|
457 |
return " OR ".join(var) |
|
458 |
else: |
|
459 |
return "(%s)"%" OR ".join(var) |
|
460 |
elif type(paths) == ListType: |
|
461 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
462 |
return " AND ".join(var) |
|
463 |
else: |
|
464 |
return paths |
|
465 |
||
466 |
def ExtractModifier(self, variable, text): |
|
467 |
if variable.getNegated(): |
|
468 |
return "NOT(%s)"%text |
|
469 |
else: |
|
470 |
edge = variable.getEdge() |
|
471 |
if edge: |
|
472 |
if edge.getValue() == "rising": |
|
473 |
return self.AddTrigger("R_TRIG", text) |
|
474 |
elif edge.getValue() == "falling": |
|
475 |
return self.AddTrigger("F_TRIG", text) |
|
476 |
return text |
|
477 |
||
478 |
def AddTrigger(self, edge, text): |
|
33 | 479 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]: |
480 |
self.Interface.append(("VAR", False, False, False, [])) |
|
0 | 481 |
i = 1 |
482 |
name = "%s%d"%(edge, i) |
|
1 | 483 |
while self.IsAlreadyDefined(name): |
0 | 484 |
i += 1 |
485 |
name = "%s%d"%(edge, i) |
|
33 | 486 |
self.Interface[-1][4].append((edge, name, None, None)) |
0 | 487 |
self.Program += " %s(CLK := %s);\n"%(name, text) |
488 |
return "%s.Q"%name |
|
489 |
||
490 |
def GenerateSTProgram(self): |
|
1 | 491 |
if self.ReturnType: |
29 | 492 |
program = "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType) |
0 | 493 |
else: |
29 | 494 |
program = "%s %s\n"%(self.Type, self.Name) |
33 | 495 |
for list_type, retain, constant, located, variables in self.Interface: |
1 | 496 |
program += " %s"%list_type |
497 |
if retain: |
|
498 |
program += " RETAIN" |
|
499 |
if constant: |
|
500 |
program += " CONSTANT" |
|
501 |
program += "\n" |
|
30 | 502 |
for var_type, var_name, var_address, var_initial in variables: |
503 |
program += " %s "%var_name |
|
504 |
if var_address != None: |
|
31 | 505 |
program += "AT %s "%var_address |
30 | 506 |
program += ": %s"%var_type |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
507 |
if var_initial != None: |
33 | 508 |
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
|
509 |
if var_type == "STRING": |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
510 |
program += " := '%s'"%value |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
511 |
elif var_type == "WSTRING": |
33 | 512 |
program += " := \"%s\""%value |
513 |
else: |
|
514 |
program += " := %s"%value |
|
30 | 515 |
program += ";\n" |
4
2de7fd952fdd
Adding File Dialog for choosing path to generated program
lbessard
parents:
2
diff
changeset
|
516 |
program += " END_VAR\n" |
0 | 517 |
program += "\n" |
518 |
program += self.Program |
|
519 |
program += "END_%s\n\n"%self.Type |
|
520 |
return program |
|
521 |
||
29 | 522 |
def GenerateConfiguration(self, configuration): |
523 |
config = "\nCONFIGURATION %s\n"%configuration.getName() |
|
524 |
for varlist in configuration.getGlobalVars(): |
|
525 |
config += " VAR_GLOBAL" |
|
526 |
if varlist.getRetain(): |
|
527 |
config += " RETAIN" |
|
528 |
if varlist.getConstant(): |
|
529 |
config += " CONSTANT" |
|
530 |
config += "\n" |
|
531 |
for var in varlist.getVariable(): |
|
33 | 532 |
var_type = var.getType().getValue() |
533 |
config += " %s "%var.getName() |
|
30 | 534 |
address = var.getAddress() |
535 |
if address: |
|
536 |
config += "AT %s "%address |
|
33 | 537 |
config += ": %s"%var_type |
29 | 538 |
initial = var.getInitialValue() |
539 |
if initial: |
|
30 | 540 |
value = str(initial.getValue()) |
33 | 541 |
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
|
542 |
if var_type == "STRING": |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
543 |
config += " := '%s'"%value |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
544 |
elif var_type == "WSTRING": |
33 | 545 |
config += " := \"%s\""%value |
546 |
else: |
|
547 |
config += " := %s"%value |
|
30 | 548 |
config += ";\n" |
29 | 549 |
config += " END_VAR\n" |
550 |
for resource in configuration.getResource(): |
|
551 |
config += self.GenerateResource(resource) |
|
552 |
config += "END_CONFIGURATION\n" |
|
553 |
return config |
|
554 |
||
555 |
def GenerateResource(self, resource): |
|
33 | 556 |
resrce = "\n RESOURCE %s ON BEREMIZ\n"%resource.getName() |
29 | 557 |
for varlist in resource.getGlobalVars(): |
558 |
resrce += " VAR_GLOBAL" |
|
559 |
if varlist.getRetain(): |
|
560 |
resrce += " RETAIN" |
|
561 |
if varlist.getConstant(): |
|
562 |
resrce += " CONSTANT" |
|
563 |
resrce += "\n" |
|
564 |
for var in varlist.getVariable(): |
|
33 | 565 |
var_type = var.getType().getValue() |
30 | 566 |
resrce += " %s "%var.getName() |
567 |
address = var.getAddress() |
|
568 |
if address: |
|
569 |
resrce += "AT %s "%address |
|
570 |
resrce += ": %s"%var.getType().getValue() |
|
29 | 571 |
initial = var.getInitialValue() |
572 |
if initial: |
|
30 | 573 |
value = str(initial.getValue()) |
33 | 574 |
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
|
575 |
if var_type == "STRING": |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
576 |
resrce += " := '%s'"%value |
256eedd275d0
Adding support for disable interface variable change if a POU is used
lbessard
parents:
33
diff
changeset
|
577 |
elif var_type == "WSTRING": |
33 | 578 |
resrce += " := \"%s\""%value |
579 |
else: |
|
580 |
resrce += " := %s"%value |
|
30 | 581 |
resrce += ";\n" |
29 | 582 |
resrce += " END_VAR\n" |
30 | 583 |
tasks = resource.getTask() |
584 |
for task in tasks: |
|
585 |
resrce += " TASK %s("%task.getName() |
|
586 |
args = [] |
|
587 |
single = task.getSingle() |
|
588 |
if single: |
|
589 |
args.append("SINGLE := %s"%single) |
|
590 |
interval = task.getInterval() |
|
591 |
if interval: |
|
592 |
text = "t#" |
|
593 |
if interval.hour != 0: |
|
594 |
text += "%dh"%interval.hour |
|
595 |
if interval.minute != 0: |
|
596 |
text += "%dm"%interval.minute |
|
597 |
if interval.second != 0: |
|
598 |
text += "%ds"%interval.second |
|
599 |
if interval.microsecond != 0: |
|
600 |
text += "%dms"%(interval.microsecond / 1000) |
|
601 |
args.append("INTERVAL := %s"%text) |
|
602 |
args.append("PRIORITY := %s"%str(task.priority.getValue())) |
|
603 |
resrce += ",".join(args) + ");\n" |
|
604 |
for task in tasks: |
|
605 |
for instance in task.getPouInstance(): |
|
606 |
resrce += " PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType()) |
|
607 |
for instance in resource.getPouInstance(): |
|
608 |
resrce += " PROGRAM %s : %s;\n"%(instance.getName(), instance.getType()) |
|
609 |
resrce += " END_RESOURCE\n" |
|
29 | 610 |
return resrce |
611 |
||
612 |
||
0 | 613 |
def GenerateCurrentProgram(project): |
614 |
program = "" |
|
615 |
for pou in project.getPous(): |
|
616 |
pou_type = pou.getPouType().getValue() |
|
6 | 617 |
if pou_type in pouTypeNames: |
618 |
pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type]) |
|
0 | 619 |
else: |
620 |
raise ValueError, "Undefined pou type" |
|
621 |
pou_program.GenerateInterface(pou.getInterface()) |
|
622 |
pou_program.GenerateProgram(pou) |
|
623 |
program += pou_program.GenerateSTProgram() |
|
29 | 624 |
for config in project.getConfigurations(): |
625 |
program += pou_program.GenerateConfiguration(config) |
|
0 | 626 |
return program |
627 |