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