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