author | lbessard |
Tue, 10 Jul 2007 09:52:53 +0200 | |
changeset 28 | fc23e1f415d8 |
parent 6 | c8cf918ee7ea |
child 29 | 3b7e23a323a6 |
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 |
# |
|
7 |
#Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
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 |
|
19 |
#Lesser General Public License for more details. |
|
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":{}} |
|
0 | 65 |
self.Program = "" |
66 |
||
1 | 67 |
def IsAlreadyDefined(self, name): |
68 |
for list_type, retain, constant, vars in self.Interface: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
69 |
for var_type, var_name, var_initial in vars: |
1 | 70 |
if name == var_name: |
71 |
return True |
|
72 |
return False |
|
73 |
||
0 | 74 |
def GenerateInterface(self, interface): |
75 |
if self.Type == "FUNCTION": |
|
1 | 76 |
self.ReturnType = interface.getReturnType().getValue() |
0 | 77 |
for varlist in interface.getContent(): |
1 | 78 |
variables = [] |
0 | 79 |
for var in varlist["value"].getVariable(): |
80 |
type = var.getType().getValue() |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
81 |
initial = var.getInitialValue() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
82 |
if initial: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
83 |
initial_value = initial.getValue() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
84 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
85 |
initial_value = None |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
86 |
variables.append((type, var.getName(), initial_value)) |
1 | 87 |
self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), |
88 |
varlist["value"].getConstant(), variables)) |
|
0 | 89 |
|
90 |
def GenerateProgram(self, pou): |
|
91 |
body = pou.getBody() |
|
92 |
body_content = body.getContent() |
|
93 |
body_type = body_content["name"] |
|
94 |
if body_type in ["IL","ST"]: |
|
1 | 95 |
self.Program = ReIndentText(body_content["value"].getText(), 2) |
0 | 96 |
elif body_type == "FBD": |
97 |
for instance in body.getContentInstances(): |
|
98 |
if isinstance(instance, plcopen.outVariable): |
|
99 |
var = instance.getExpression() |
|
100 |
connections = instance.connectionPointIn.getConnections() |
|
101 |
if connections and len(connections) == 1: |
|
102 |
expression = self.ComputeFBDExpression(body, connections[0]) |
|
103 |
self.Program += " %s := %s;\n"%(var, expression) |
|
104 |
elif body_type == "LD": |
|
105 |
for instance in body.getContentInstances(): |
|
106 |
if isinstance(instance, plcopen.coil): |
|
107 |
paths = self.GenerateLDPaths(instance, body) |
|
108 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
|
109 |
expression = self.ComputeLDExpression(paths, True) |
|
110 |
self.Program += " %s := %s;\n"%(variable, expression) |
|
111 |
elif body_type == "SFC": |
|
112 |
for instance in body.getContentInstances(): |
|
113 |
if isinstance(instance, plcopen.step): |
|
1 | 114 |
self.GenerateSFCStep(instance, pou) |
0 | 115 |
elif isinstance(instance, plcopen.actionBlock): |
1 | 116 |
self.GenerateSFCStepActions(instance, pou) |
0 | 117 |
elif isinstance(instance, plcopen.transition): |
1 | 118 |
self.GenerateSFCTransition(instance, pou) |
0 | 119 |
elif isinstance(instance, plcopen.jumpStep): |
120 |
self.GenerateSFCJump(instance, pou) |
|
1 | 121 |
for initialstep in self.InitialSteps: |
122 |
self.ComputeSFCStep(initialstep) |
|
0 | 123 |
|
124 |
def ComputeFBDExpression(self, body, link): |
|
125 |
localid = link.getRefLocalId() |
|
126 |
instance = body.getContentInstance(localid) |
|
127 |
if isinstance(instance, plcopen.inVariable): |
|
128 |
return instance.getExpression() |
|
129 |
elif isinstance(instance, plcopen.block): |
|
130 |
name = instance.getInstanceName() |
|
131 |
type = instance.getTypeName() |
|
132 |
block_infos = GetBlockType(type) |
|
133 |
if block_infos["type"] == "function": |
|
134 |
vars = [] |
|
135 |
for variable in instance.inputVariables.getVariable(): |
|
136 |
connections = variable.connectionPointIn.getConnections() |
|
137 |
if connections and len(connections) == 1: |
|
138 |
value = self.ComputeFBDExpression(body, connections[0]) |
|
139 |
vars.append(self.ExtractModifier(variable, value)) |
|
140 |
variable = instance.outputVariables.getVariable()[0] |
|
141 |
return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars))) |
|
142 |
elif block_infos["type"] == "functionBlock": |
|
1 | 143 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2]: |
144 |
self.Interface.append(("VAR", False, False, [])) |
|
145 |
if not self.IsAlreadyDefined(name): |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
146 |
self.Interface[-1][3].append((type, name, None)) |
0 | 147 |
vars = [] |
148 |
for variable in instance.inputVariables.getVariable(): |
|
149 |
connections = variable.connectionPointIn.getConnections() |
|
150 |
if connections and len(connections) == 1: |
|
151 |
parameter = variable.getFormalParameter() |
|
152 |
value = self.ComputeFBDExpression(body, connections[0]) |
|
153 |
vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value))) |
|
154 |
self.Program += " %s(%s);\n"%(name, ", ".join(vars)) |
|
155 |
connectionPoint = link.getPosition()[-1] |
|
156 |
for variable in instance.outputVariables.getVariable(): |
|
157 |
blockPointx, blockPointy = variable.connectionPointOut.getRelPosition() |
|
158 |
if instance.getX() + blockPointx == connectionPoint.getX() and instance.getY() + blockPointy == connectionPoint.getY(): |
|
159 |
return self.ExtractModifier(variable, "%s.%s"%(name, variable.getFormalParameter())) |
|
160 |
raise ValueError, "No output variable found" |
|
161 |
||
162 |
def GenerateLDPaths(self, instance, body): |
|
163 |
paths = [] |
|
164 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
|
165 |
connections = instance.connectionPointIn.getConnections() |
|
166 |
for connection in connections: |
|
167 |
localId = connection.getRefLocalId() |
|
168 |
next = body.getContentInstance(localId) |
|
169 |
if isinstance(next, plcopen.leftPowerRail): |
|
170 |
paths.append(None) |
|
171 |
else: |
|
172 |
paths.append(self.GenerateLDPaths(next, body)) |
|
173 |
if isinstance(instance, plcopen.coil): |
|
174 |
if len(paths) > 1: |
|
175 |
return tuple(paths) |
|
176 |
else: |
|
177 |
return paths |
|
178 |
else: |
|
179 |
if len(paths) > 1: |
|
180 |
return [variable, tuple(paths)] |
|
181 |
elif type(paths[0]) == ListType: |
|
182 |
return [variable] + paths[0] |
|
183 |
elif paths[0]: |
|
184 |
return [variable, paths[0]] |
|
185 |
else: |
|
186 |
return variable |
|
187 |
||
2 | 188 |
def ExtractDivergenceInput(self, divergence, pou): |
189 |
connectionPointIn = divergence.getConnectionPointIn() |
|
190 |
if connectionPointIn: |
|
191 |
connections = connectionPointIn.getConnections() |
|
192 |
if len(connections) == 1: |
|
193 |
instanceLocalId = connections[0].getRefLocalId() |
|
194 |
return pou.body.getContentInstance(instanceLocalId) |
|
195 |
return None |
|
196 |
||
197 |
def ExtractConvergenceInputs(self, convergence, pou): |
|
198 |
instances = [] |
|
199 |
for connectionPointIn in convergence.getConnectionPointIn(): |
|
200 |
connections = connectionPointIn.getConnections() |
|
201 |
if len(connections) == 1: |
|
202 |
instanceLocalId = connections[0].getRefLocalId() |
|
203 |
instances.append(pou.body.getContentInstance(instanceLocalId)) |
|
204 |
return instances |
|
205 |
||
1 | 206 |
def GenerateSFCStep(self, step, pou): |
0 | 207 |
step_name = step.getName() |
1 | 208 |
if step_name not in self.SFCNetworks["Steps"].keys(): |
209 |
if step.getInitialStep(): |
|
210 |
self.InitialSteps.append(step_name) |
|
0 | 211 |
step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []} |
212 |
if step.connectionPointIn: |
|
213 |
instances = [] |
|
214 |
connections = step.connectionPointIn.getConnections() |
|
215 |
if len(connections) == 1: |
|
216 |
instanceLocalId = connections[0].getRefLocalId() |
|
217 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
218 |
if isinstance(instance, plcopen.transition): |
|
219 |
instances.append(instance) |
|
220 |
elif isinstance(instance, plcopen.selectionConvergence): |
|
2 | 221 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
222 |
elif isinstance(instance, plcopen.simultaneousDivergence): |
|
223 |
transition = self.ExtractDivergenceInput(instance, pou) |
|
224 |
if transition: |
|
225 |
if isinstance(transition, plcopen.transition): |
|
1 | 226 |
instances.append(transition) |
2 | 227 |
elif isinstance(transition, plcopen.selectionConvergence): |
228 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
|
0 | 229 |
for instance in instances: |
2 | 230 |
self.GenerateSFCTransition(instance, pou) |
1 | 231 |
if instance in self.SFCNetworks["Transitions"].keys(): |
232 |
self.SFCNetworks["Transitions"][instance]["to"].append(step_name) |
|
233 |
self.SFCNetworks["Steps"][step_name] = step_infos |
|
0 | 234 |
|
235 |
def GenerateSFCJump(self, jump, pou): |
|
236 |
jump_target = jump.getTargetName() |
|
237 |
if jump.connectionPointIn: |
|
238 |
instances = [] |
|
239 |
connections = jump.connectionPointIn.getConnections() |
|
240 |
if len(connections) == 1: |
|
241 |
instanceLocalId = connections[0].getRefLocalId() |
|
242 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
243 |
if isinstance(instance, plcopen.transition): |
|
244 |
instances.append(instance) |
|
245 |
elif isinstance(instance, plcopen.selectionConvergence): |
|
2 | 246 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
247 |
elif isinstance(instance, plcopen.simultaneousDivergence): |
|
248 |
transition = self.ExtractDivergenceInput(instance, pou) |
|
249 |
if transition: |
|
250 |
if isinstance(transition, plcopen.transition): |
|
1 | 251 |
instances.append(transition) |
2 | 252 |
elif isinstance(transition, plcopen.selectionConvergence): |
253 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
|
0 | 254 |
for instance in instances: |
2 | 255 |
self.GenerateSFCTransition(instance, pou) |
1 | 256 |
if instance in self.SFCNetworks["Transitions"].keys(): |
257 |
self.SFCNetworks["Transitions"][instance]["to"].append(jump_target) |
|
258 |
||
259 |
def GenerateSFCStepActions(self, actionBlock, pou): |
|
0 | 260 |
connections = actionBlock.connectionPointIn.getConnections() |
261 |
if len(connections) == 1: |
|
262 |
stepLocalId = connections[0].getRefLocalId() |
|
263 |
step = pou.body.getContentInstance(stepLocalId) |
|
1 | 264 |
self.GenerateSFCStep(step, pou) |
0 | 265 |
step_name = step.getName() |
1 | 266 |
if step_name in self.SFCNetworks["Steps"].keys(): |
0 | 267 |
actions = actionBlock.getActions() |
268 |
for action in actions: |
|
1 | 269 |
action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]} |
270 |
if "duration" in action: |
|
271 |
action_infos["duration"] = action["duration"] |
|
272 |
if "indicator" in action: |
|
273 |
action_infos["indicator"] = action["indicator"] |
|
274 |
if action["type"] == "reference": |
|
275 |
self.GenerateSFCAction(action["value"], pou) |
|
276 |
self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos) |
|
277 |
||
278 |
def GenerateSFCAction(self, action_name, pou): |
|
279 |
if action_name not in self.SFCNetworks["Actions"].keys(): |
|
280 |
actionContent = pou.getAction(action_name) |
|
281 |
if actionContent: |
|
282 |
actionType = actionContent.getBodyType() |
|
283 |
actionBody = actionContent.getBody() |
|
284 |
if actionType in ["ST", "IL"]: |
|
285 |
self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4) |
|
286 |
elif actionType == "FBD": |
|
287 |
for instance in actionBody.getContentInstances(): |
|
288 |
if isinstance(instance, plcopen.outVariable): |
|
289 |
var = instance.getExpression() |
|
290 |
connections = instance.connectionPointIn.getConnections() |
|
291 |
if connections and len(connections) == 1: |
|
292 |
expression = self.ComputeFBDExpression(actionBody, connections[0]) |
|
293 |
self.SFCNetworks["Actions"][action_name] = self.Program + " %s := %s;\n"%(var, expression) |
|
294 |
self.Program = "" |
|
295 |
elif actionType == "LD": |
|
296 |
for instance in actionbody.getContentInstances(): |
|
297 |
if isinstance(instance, plcopen.coil): |
|
298 |
paths = self.GenerateLDPaths(instance, actionBody) |
|
299 |
variable = self.ExtractModifier(instance, instance.getVariable()) |
|
300 |
expression = self.ComputeLDExpression(paths, True) |
|
301 |
self.SFCNetworks["Actions"][action_name] = self.Program + " %s := %s;\n"%(variable, expression) |
|
302 |
self.Program = "" |
|
303 |
||
304 |
def GenerateSFCTransition(self, transition, pou): |
|
305 |
if transition not in self.SFCNetworks["Transitions"].keys(): |
|
306 |
steps = [] |
|
0 | 307 |
connections = transition.connectionPointIn.getConnections() |
308 |
if len(connections) == 1: |
|
309 |
instanceLocalId = connections[0].getRefLocalId() |
|
310 |
instance = pou.body.getContentInstance(instanceLocalId) |
|
311 |
if isinstance(instance, plcopen.step): |
|
2 | 312 |
steps.append(instance) |
0 | 313 |
elif isinstance(instance, plcopen.selectionDivergence): |
2 | 314 |
step = self.ExtractDivergenceInput(instance, pou) |
315 |
if step: |
|
316 |
if isinstance(step, plcopen.step): |
|
317 |
steps.append(step) |
|
318 |
elif isinstance(step, plcopen.simultaneousConvergence): |
|
319 |
steps.extend(self.ExtractConvergenceInputs(step, pou)) |
|
320 |
elif isinstance(instance, plcopen.simultaneousConvergence): |
|
321 |
steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
|
1 | 322 |
transition_infos = {"from": [], "to" : []} |
323 |
transitionValues = transition.getConditionContent() |
|
324 |
if transitionValues["type"] == "inline": |
|
325 |
transition_infos["content"] = "\n := %s;\n"%transitionValues["value"] |
|
326 |
else: |
|
327 |
transitionContent = pou.getTransition(transitionValues["value"]) |
|
328 |
transitionType = transitionContent.getBodyType() |
|
329 |
transitionBody = transitionContent.getBody() |
|
330 |
if transitionType == "IL": |
|
2 | 331 |
transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4) |
1 | 332 |
elif transitionType == "ST": |
2 | 333 |
transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.getText(), 4) |
1 | 334 |
elif conditionType == "FBD": |
335 |
for instance in transitionBody.getContentInstances(): |
|
336 |
if isinstance(instance, plcopen.outVariable): |
|
337 |
connections = instance.connectionPointIn.getConnections() |
|
338 |
if connections and len(connections) == 1: |
|
339 |
expression = self.ComputeFBDExpression(actionBody, connections[0]) |
|
340 |
transition_infos["content"] = "\n := %s;\n"%(var, expression) |
|
341 |
elif actionType == "LD": |
|
342 |
for instance in transitionbody.getContentInstances(): |
|
343 |
if isinstance(instance, plcopen.coil): |
|
344 |
paths = self.GenerateLDPaths(instance, conditionBody) |
|
345 |
expression = self.ComputeLDExpression(paths, True) |
|
346 |
transition_infos["content"] = "\n := %s;\n"%expression |
|
2 | 347 |
for step in steps: |
348 |
self.GenerateSFCStep(step, pou) |
|
349 |
step_name = step.getName() |
|
1 | 350 |
if step_name in self.SFCNetworks["Steps"].keys(): |
351 |
transition_infos["from"].append(step_name) |
|
352 |
self.SFCNetworks["Steps"][step_name]["transitions"].append(transition) |
|
353 |
self.SFCNetworks["Transitions"][transition] = transition_infos |
|
354 |
||
355 |
def ComputeSFCStep(self, step_name): |
|
356 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
357 |
step_infos = self.SFCNetworks["Steps"].pop(step_name) |
|
358 |
if step_infos["initial"]: |
|
359 |
self.Program += " INITIAL_STEP %s:\n"%step_name |
|
360 |
else: |
|
361 |
self.Program += " STEP %s:\n"%step_name |
|
362 |
actions = [] |
|
363 |
for action_infos in step_infos["actions"]: |
|
364 |
actions.append(action_infos["content"]) |
|
365 |
self.Program += " %(content)s(%(qualifier)s"%action_infos |
|
366 |
if "duration" in action_infos: |
|
367 |
self.Program += ", %(duration)s"%action_infos |
|
368 |
if "indicator" in action_infos: |
|
369 |
self.Program += ", %(indicator)s"%action_infos |
|
370 |
self.Program += ");\n" |
|
371 |
self.Program += " END_STEP\n\n" |
|
372 |
for action in actions: |
|
373 |
self.ComputeSFCAction(action) |
|
374 |
for transition in step_infos["transitions"]: |
|
375 |
self.ComputeSFCTransition(transition) |
|
376 |
||
377 |
def ComputeSFCAction(self, action_name): |
|
378 |
if action_name in self.SFCNetworks["Actions"].keys(): |
|
379 |
action_content = self.SFCNetworks["Actions"].pop(action_name) |
|
380 |
self.Program += " ACTION %s:\n%s END_ACTION\n\n"%(action_name, action_content) |
|
381 |
||
382 |
def ComputeSFCTransition(self, transition): |
|
383 |
if transition in self.SFCNetworks["Transitions"].keys(): |
|
384 |
transition_infos = self.SFCNetworks["Transitions"].pop(transition) |
|
385 |
self.Program += " TRANSITION FROM " |
|
386 |
if len(transition_infos["from"]) > 1: |
|
387 |
self.Program += "(%s)"%", ".join(transition_infos["from"]) |
|
388 |
else: |
|
389 |
self.Program += "%s"%transition_infos["from"][0] |
|
390 |
self.Program += " TO " |
|
391 |
if len(transition_infos["to"]) > 1: |
|
392 |
self.Program += "(%s)"%", ".join(transition_infos["to"]) |
|
393 |
else: |
|
394 |
self.Program += "%s"%transition_infos["to"][0] |
|
395 |
self.Program += transition_infos["content"] |
|
396 |
self.Program += " END_TRANSITION\n\n" |
|
397 |
for step_name in transition_infos["to"]: |
|
398 |
self.ComputeSFCStep(step_name) |
|
399 |
||
0 | 400 |
def ComputeLDExpression(self, paths, first = False): |
401 |
if type(paths) == TupleType: |
|
402 |
if None in paths: |
|
403 |
return "TRUE" |
|
404 |
else: |
|
405 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
406 |
if first: |
|
407 |
return " OR ".join(var) |
|
408 |
else: |
|
409 |
return "(%s)"%" OR ".join(var) |
|
410 |
elif type(paths) == ListType: |
|
411 |
var = [self.ComputeLDExpression(path) for path in paths] |
|
412 |
return " AND ".join(var) |
|
413 |
else: |
|
414 |
return paths |
|
415 |
||
416 |
def ExtractModifier(self, variable, text): |
|
417 |
if variable.getNegated(): |
|
418 |
return "NOT(%s)"%text |
|
419 |
else: |
|
420 |
edge = variable.getEdge() |
|
421 |
if edge: |
|
422 |
if edge.getValue() == "rising": |
|
423 |
return self.AddTrigger("R_TRIG", text) |
|
424 |
elif edge.getValue() == "falling": |
|
425 |
return self.AddTrigger("F_TRIG", text) |
|
426 |
return text |
|
427 |
||
428 |
def AddTrigger(self, edge, text): |
|
1 | 429 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2]: |
430 |
self.Interface.append(("VAR", False, False, [])) |
|
0 | 431 |
i = 1 |
432 |
name = "%s%d"%(edge, i) |
|
1 | 433 |
while self.IsAlreadyDefined(name): |
0 | 434 |
i += 1 |
435 |
name = "%s%d"%(edge, i) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
436 |
self.Interface[-1][3].append((edge, name, None)) |
0 | 437 |
self.Program += " %s(CLK := %s);\n"%(name, text) |
438 |
return "%s.Q"%name |
|
439 |
||
440 |
def GenerateSTProgram(self): |
|
441 |
program = "" |
|
1 | 442 |
if self.ReturnType: |
443 |
program += "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType) |
|
0 | 444 |
else: |
445 |
program += "%s %s\n"%(self.Type, self.Name) |
|
1 | 446 |
for list_type, retain, constant, variables in self.Interface: |
447 |
program += " %s"%list_type |
|
448 |
if retain: |
|
449 |
program += " RETAIN" |
|
450 |
if constant: |
|
451 |
program += " CONSTANT" |
|
452 |
program += "\n" |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
453 |
for var_type, var_name, var_initial in variables: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
454 |
if var_initial != None: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
455 |
program += " %s : %s := %s;\n"%(var_name, var_type, {"TRUE":"0","FALSE":"1"}.get(str(var_initial).upper(),str(var_initial))) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
456 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
6
diff
changeset
|
457 |
program += " %s : %s;\n"%(var_name, var_type) |
4
2de7fd952fdd
Adding File Dialog for choosing path to generated program
lbessard
parents:
2
diff
changeset
|
458 |
program += " END_VAR\n" |
0 | 459 |
program += "\n" |
460 |
program += self.Program |
|
461 |
program += "END_%s\n\n"%self.Type |
|
462 |
return program |
|
463 |
||
464 |
def GenerateCurrentProgram(project): |
|
465 |
program = "" |
|
466 |
for pou in project.getPous(): |
|
467 |
pou_type = pou.getPouType().getValue() |
|
6 | 468 |
if pou_type in pouTypeNames: |
469 |
pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type]) |
|
0 | 470 |
else: |
471 |
raise ValueError, "Undefined pou type" |
|
472 |
pou_program.GenerateInterface(pou.getInterface()) |
|
473 |
pou_program.GenerateProgram(pou) |
|
474 |
program += pou_program.GenerateSTProgram() |
|
475 |
return program |
|
476 |