author | Laurent Bessard |
Wed, 08 May 2013 10:40:06 +0200 | |
changeset 2146 | 0c65c96a8379 |
parent 2141 | 6963460bfe0f |
child 2147 | a8b095de63e8 |
permissions | -rw-r--r-- |
2111 | 1 |
import os |
2 |
import cPickle |
|
3 |
from xml.dom import minidom |
|
4 |
||
5 |
import wx |
|
6 |
||
7 |
from xmlclass import * |
|
8 |
||
9 |
from PLCControler import UndoBuffer, LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY |
|
10 |
from ConfigTreeNode import ConfigTreeNode |
|
11 |
from dialogs import BrowseValuesLibraryDialog |
|
12 |
from IDEFrame import TITLE, FILEMENU, PROJECTTREE |
|
13 |
||
14 |
from EthercatSlave import _EthercatSlaveCTN, ExtractHexDecValue, GenerateHexDecValue, TYPECONVERSION, VARCLASSCONVERSION |
|
15 |
from EthercatCFileGenerator import _EthercatCFileGenerator |
|
16 |
from ConfigEditor import MasterEditor |
|
17 |
from POULibrary import POULibrary |
|
18 |
||
19 |
try: |
|
20 |
from EthercatCIA402Slave import _EthercatCIA402SlaveCTN |
|
21 |
HAS_MCL = True |
|
22 |
except: |
|
23 |
HAS_MCL = False |
|
24 |
||
25 |
#-------------------------------------------------- |
|
26 |
# Remote Exec Etherlab Commands |
|
27 |
#-------------------------------------------------- |
|
28 |
||
29 |
SCAN_COMMAND = """ |
|
30 |
import commands |
|
31 |
result = commands.getoutput("ethercat slaves") |
|
32 |
slaves = [] |
|
33 |
for slave_line in result.splitlines(): |
|
34 |
chunks = slave_line.split() |
|
35 |
idx, pos, state, flag = chunks[:4] |
|
36 |
name = " ".join(chunks[4:]) |
|
37 |
alias, position = pos.split(":") |
|
38 |
slave = {"idx": int(idx), |
|
39 |
"alias": int(alias), |
|
40 |
"position": int(position), |
|
41 |
"name": name} |
|
42 |
details = commands.getoutput("ethercat slaves -p %d -v" % slave["idx"]) |
|
43 |
for details_line in details.splitlines(): |
|
44 |
details_line = details_line.strip() |
|
45 |
for header, param in [("Vendor Id:", "vendor_id"), |
|
46 |
("Product code:", "product_code"), |
|
47 |
("Revision number:", "revision_number")]: |
|
48 |
if details_line.startswith(header): |
|
49 |
slave[param] = details_line.split()[-1] |
|
50 |
break |
|
51 |
slaves.append(slave) |
|
52 |
returnVal = slaves |
|
53 |
""" |
|
54 |
||
55 |
#-------------------------------------------------- |
|
56 |
# Etherlab Specific Blocks Library |
|
57 |
#-------------------------------------------------- |
|
58 |
||
59 |
def GetLocalPath(filename): |
|
60 |
return os.path.join(os.path.split(__file__)[0], filename) |
|
61 |
||
62 |
class EtherlabLibrary(POULibrary): |
|
63 |
def GetLibraryPath(self): |
|
64 |
return GetLocalPath("pous.xml") |
|
65 |
||
66 |
def Generate_C(self, buildpath, varlist, IECCFLAGS): |
|
67 |
etherlab_ext_file = open(GetLocalPath("etherlab_ext.c"), 'r') |
|
68 |
etherlab_ext_code = etherlab_ext_file.read() |
|
69 |
etherlab_ext_file.close() |
|
70 |
||
71 |
Gen_etherlabfile_path = os.path.join(buildpath, "etherlab_ext.c") |
|
72 |
ethelabfile = open(Gen_etherlabfile_path,'w') |
|
73 |
ethelabfile.write(etherlab_ext_code) |
|
74 |
ethelabfile.close() |
|
75 |
||
76 |
return ((["etherlab_ext"], [(Gen_etherlabfile_path, IECCFLAGS)], True), "", |
|
77 |
("runtime_etherlab.py", file(GetLocalPath("runtime_etherlab.py")))) |
|
78 |
||
79 |
#-------------------------------------------------- |
|
80 |
# Ethercat MASTER |
|
81 |
#-------------------------------------------------- |
|
82 |
||
83 |
EtherCATConfigClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "EtherCATConfig.xsd")) |
|
84 |
||
85 |
def sort_commands(x, y): |
|
86 |
if x["Index"] == y["Index"]: |
|
87 |
return cmp(x["Subindex"], y["Subindex"]) |
|
88 |
return cmp(x["Index"], y["Index"]) |
|
89 |
||
90 |
cls = EtherCATConfigClasses.get("Config_Slave", None) |
|
91 |
if cls: |
|
92 |
||
93 |
def getType(self): |
|
94 |
slave_info = self.getInfo() |
|
95 |
return {"device_type": slave_info.getName(), |
|
96 |
"vendor": GenerateHexDecValue(slave_info.getVendorId()), |
|
97 |
"product_code": GenerateHexDecValue(slave_info.getProductCode(), 16), |
|
98 |
"revision_number": GenerateHexDecValue(slave_info.getRevisionNo(), 16)} |
|
99 |
setattr(cls, "getType", getType) |
|
100 |
||
101 |
def setType(self, type_infos): |
|
102 |
slave_info = self.getInfo() |
|
103 |
slave_info.setName(type_infos["device_type"]) |
|
104 |
slave_info.setVendorId(ExtractHexDecValue(type_infos["vendor"])) |
|
105 |
slave_info.setProductCode(ExtractHexDecValue(type_infos["product_code"])) |
|
106 |
slave_info.setRevisionNo(ExtractHexDecValue(type_infos["revision_number"])) |
|
107 |
setattr(cls, "setType", setType) |
|
108 |
||
109 |
def getInitCmds(self, create_default=False): |
|
110 |
Mailbox = self.getMailbox() |
|
111 |
if Mailbox is None: |
|
112 |
if create_default: |
|
113 |
self.addMailbox() |
|
114 |
Mailbox = self.getMailbox() |
|
115 |
else: |
|
116 |
return None |
|
117 |
CoE = Mailbox.getCoE() |
|
118 |
if CoE is None: |
|
119 |
if create_default: |
|
120 |
Mailbox.addCoE() |
|
121 |
CoE = Mailbox.getCoE() |
|
122 |
else: |
|
123 |
return None |
|
124 |
InitCmds = CoE.getInitCmds() |
|
125 |
if InitCmds is None and create_default: |
|
126 |
CoE.addInitCmds() |
|
127 |
InitCmds = CoE.getInitCmds() |
|
128 |
return InitCmds |
|
129 |
setattr(cls, "getInitCmds", getInitCmds) |
|
130 |
||
131 |
def getStartupCommands(self): |
|
132 |
pos = self.getInfo().getPhysAddr() |
|
133 |
InitCmds = self.getInitCmds() |
|
134 |
if InitCmds is None: |
|
135 |
return [] |
|
136 |
commands = [] |
|
137 |
for idx, InitCmd in enumerate(InitCmds.getInitCmd()): |
|
138 |
comment = InitCmd.getComment() |
|
139 |
if comment is None: |
|
140 |
comment = "" |
|
141 |
commands.append({ |
|
142 |
"command_idx": idx, |
|
143 |
"Position": pos, |
|
144 |
"Index": InitCmd.getIndex(), |
|
145 |
"Subindex": InitCmd.getSubIndex(), |
|
146 |
"Value": InitCmd.getData(), |
|
147 |
"Description": comment}) |
|
148 |
commands.sort(sort_commands) |
|
149 |
return commands |
|
150 |
setattr(cls, "getStartupCommands", getStartupCommands) |
|
151 |
||
152 |
def appendStartupCommand(self, command_infos): |
|
153 |
InitCmds = self.getInitCmds(True) |
|
154 |
command = EtherCATConfigClasses["InitCmds_InitCmd"]() |
|
155 |
command.setIndex(command_infos["Index"]) |
|
156 |
command.setSubIndex(command_infos["Subindex"]) |
|
157 |
command.setData(command_infos["Value"]) |
|
158 |
command.setComment(command_infos["Description"]) |
|
159 |
InitCmds.appendInitCmd(command) |
|
160 |
return len(InitCmds.getInitCmd()) - 1 |
|
161 |
setattr(cls, "appendStartupCommand", appendStartupCommand) |
|
162 |
||
163 |
def setStartupCommand(self, command_infos): |
|
164 |
InitCmds = self.getInitCmds() |
|
165 |
if InitCmds is not None: |
|
166 |
commands = InitCmds.getInitCmd() |
|
167 |
if command_infos["command_idx"] < len(commands): |
|
168 |
command = commands[command_infos["command_idx"]] |
|
169 |
command.setIndex(command_infos["Index"]) |
|
170 |
command.setSubIndex(command_infos["Subindex"]) |
|
171 |
command.setData(command_infos["Value"]) |
|
172 |
command.setComment(command_infos["Description"]) |
|
173 |
setattr(cls, "setStartupCommand", setStartupCommand) |
|
174 |
||
175 |
def removeStartupCommand(self, command_idx): |
|
176 |
InitCmds = self.getInitCmds() |
|
177 |
if InitCmds is not None: |
|
178 |
if command_idx < len(InitCmds.getInitCmd()): |
|
179 |
InitCmds.removeInitCmd(command_idx) |
|
180 |
setattr(cls, "removeStartupCommand", removeStartupCommand) |
|
181 |
||
182 |
ProcessVariablesXSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
183 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
184 |
<xsd:element name="ProcessVariables"> |
|
185 |
<xsd:complexType> |
|
186 |
<xsd:sequence> |
|
187 |
<xsd:element name="variable" minOccurs="0" maxOccurs="unbounded"> |
|
188 |
<xsd:complexType> |
|
189 |
<xsd:sequence> |
|
190 |
<xsd:element name="ReadFrom" type="LocationDesc" minOccurs="0"/> |
|
191 |
<xsd:element name="WriteTo" type="LocationDesc" minOccurs="0"/> |
|
192 |
</xsd:sequence> |
|
193 |
<xsd:attribute name="Name" type="xsd:string" use="required"/> |
|
194 |
<xsd:attribute name="Comment" type="xsd:string" use="required"/> |
|
195 |
</xsd:complexType> |
|
196 |
</xsd:element> |
|
197 |
</xsd:sequence> |
|
198 |
</xsd:complexType> |
|
199 |
</xsd:element> |
|
200 |
<xsd:complexType name="LocationDesc"> |
|
201 |
<xsd:attribute name="Position" type="xsd:integer" use="required"/> |
|
202 |
<xsd:attribute name="Index" type="xsd:integer" use="required"/> |
|
203 |
<xsd:attribute name="SubIndex" type="xsd:integer" use="required"/> |
|
204 |
</xsd:complexType> |
|
205 |
</xsd:schema> |
|
206 |
""" |
|
207 |
||
208 |
ProcessVariablesClasses = GenerateClassesFromXSDstring(ProcessVariablesXSD) |
|
209 |
||
210 |
class _EthercatCTN: |
|
211 |
||
212 |
CTNChildrenTypes = [("EthercatSlave", _EthercatSlaveCTN, "Ethercat Slave")] |
|
213 |
if HAS_MCL: |
|
214 |
CTNChildrenTypes.append(("EthercatCIA402Slave", _EthercatCIA402SlaveCTN, "Ethercat CIA402 Slave")) |
|
215 |
EditorType = MasterEditor |
|
216 |
||
217 |
def __init__(self): |
|
218 |
config_filepath = self.ConfigFileName() |
|
219 |
config_is_saved = False |
|
220 |
self.Config = EtherCATConfigClasses["EtherCATConfig"]() |
|
221 |
if os.path.isfile(config_filepath): |
|
222 |
config_xmlfile = open(config_filepath, 'r') |
|
223 |
config_tree = minidom.parse(config_xmlfile) |
|
224 |
config_xmlfile.close() |
|
225 |
||
226 |
for child in config_tree.childNodes: |
|
227 |
if child.nodeType == config_tree.ELEMENT_NODE and child.nodeName == "EtherCATConfig": |
|
228 |
self.Config.loadXMLTree(child) |
|
229 |
config_is_saved = True |
|
230 |
||
231 |
process_filepath = self.ProcessVariablesFileName() |
|
232 |
process_is_saved = False |
|
233 |
self.ProcessVariables = ProcessVariablesClasses["ProcessVariables"]() |
|
234 |
if os.path.isfile(process_filepath): |
|
235 |
process_xmlfile = open(process_filepath, 'r') |
|
236 |
process_tree = minidom.parse(process_xmlfile) |
|
237 |
process_xmlfile.close() |
|
238 |
||
239 |
for child in process_tree.childNodes: |
|
240 |
if child.nodeType == process_tree.ELEMENT_NODE and child.nodeName == "ProcessVariables": |
|
241 |
self.ProcessVariables.loadXMLTree(child) |
|
242 |
process_is_saved = True |
|
243 |
||
244 |
if config_is_saved and process_is_saved: |
|
245 |
self.CreateBuffer(True) |
|
246 |
else: |
|
247 |
self.CreateBuffer(False) |
|
248 |
self.OnCTNSave() |
|
249 |
||
250 |
def GetContextualMenuItems(self): |
|
251 |
return [("Add Ethercat Slave", "Add Ethercat Slave to Master", self.OnAddEthercatSlave)] |
|
252 |
||
253 |
def OnAddEthercatSlave(self, event): |
|
254 |
app_frame = self.GetCTRoot().AppFrame |
|
255 |
dialog = BrowseValuesLibraryDialog(app_frame, |
|
256 |
"Ethercat Slave Type", self.GetSlaveTypesLibrary()) |
|
257 |
if dialog.ShowModal() == wx.ID_OK: |
|
258 |
type_infos = dialog.GetValueInfos() |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
259 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 260 |
if device is not None: |
261 |
if HAS_MCL and _EthercatCIA402SlaveCTN.NODE_PROFILE in device.GetProfileNumbers(): |
|
262 |
ConfNodeType = "EthercatCIA402Slave" |
|
263 |
else: |
|
264 |
ConfNodeType = "EthercatSlave" |
|
265 |
new_child = self.CTNAddChild("%s_0" % ConfNodeType, ConfNodeType) |
|
266 |
new_child.SetParamsAttribute("SlaveParams.Type", type_infos) |
|
267 |
self.CTNRequestSave() |
|
268 |
new_child._OpenView() |
|
269 |
app_frame._Refresh(TITLE, FILEMENU, PROJECTTREE) |
|
270 |
dialog.Destroy() |
|
271 |
||
272 |
def ExtractHexDecValue(self, value): |
|
273 |
return ExtractHexDecValue(value) |
|
274 |
||
275 |
def GetSizeOfType(self, type): |
|
276 |
return TYPECONVERSION.get(self.GetCTRoot().GetBaseType(type), None) |
|
277 |
||
278 |
def ConfigFileName(self): |
|
279 |
return os.path.join(self.CTNPath(), "config.xml") |
|
280 |
||
281 |
def ProcessVariablesFileName(self): |
|
282 |
return os.path.join(self.CTNPath(), "process_variables.xml") |
|
283 |
||
284 |
def FilterSlave(self, slave, vendor=None, slave_pos=None, slave_profile=None): |
|
285 |
if slave_pos is not None and slave.getInfo().getPhysAddr() != slave_pos: |
|
286 |
return False |
|
287 |
type_infos = slave.getType() |
|
288 |
if vendor is not None and ExtractHexDecValue(type_infos["vendor"]) != vendor: |
|
289 |
return False |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
290 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 291 |
if slave_profile is not None and slave_profile not in device.GetProfileNumbers(): |
292 |
return False |
|
293 |
return True |
|
294 |
||
2124
1f2c3fdd70d0
Fixed bugs in drag'n drop from variables panel in master and slaves editor panel
Laurent Bessard
parents:
2113
diff
changeset
|
295 |
def GetSlaveName(self, slave_pos): |
1f2c3fdd70d0
Fixed bugs in drag'n drop from variables panel in master and slaves editor panel
Laurent Bessard
parents:
2113
diff
changeset
|
296 |
CTNChild = self.GetChildByIECLocation((slave_pos,)) |
1f2c3fdd70d0
Fixed bugs in drag'n drop from variables panel in master and slaves editor panel
Laurent Bessard
parents:
2113
diff
changeset
|
297 |
if CTNChild is not None: |
1f2c3fdd70d0
Fixed bugs in drag'n drop from variables panel in master and slaves editor panel
Laurent Bessard
parents:
2113
diff
changeset
|
298 |
return CTNChild.CTNName() |
1f2c3fdd70d0
Fixed bugs in drag'n drop from variables panel in master and slaves editor panel
Laurent Bessard
parents:
2113
diff
changeset
|
299 |
return self.CTNName() |
1f2c3fdd70d0
Fixed bugs in drag'n drop from variables panel in master and slaves editor panel
Laurent Bessard
parents:
2113
diff
changeset
|
300 |
|
2111 | 301 |
def GetSlaves(self, vendor=None, slave_pos=None, slave_profile=None): |
302 |
slaves = [] |
|
303 |
for slave in self.Config.getConfig().getSlave(): |
|
304 |
if self.FilterSlave(slave, vendor, slave_pos, slave_profile): |
|
305 |
slaves.append(slave.getInfo().getPhysAddr()) |
|
306 |
slaves.sort() |
|
307 |
return slaves |
|
308 |
||
309 |
def GetSlave(self, slave_pos): |
|
310 |
for slave in self.Config.getConfig().getSlave(): |
|
311 |
slave_info = slave.getInfo() |
|
312 |
if slave_info.getPhysAddr() == slave_pos: |
|
313 |
return slave |
|
314 |
return None |
|
315 |
||
316 |
def GetStartupCommands(self, vendor=None, slave_pos=None, slave_profile=None): |
|
317 |
commands = [] |
|
318 |
for slave in self.Config.getConfig().getSlave(): |
|
319 |
if self.FilterSlave(slave, vendor, slave_pos, slave_profile): |
|
320 |
commands.append((slave.getInfo().getPhysAddr(), slave.getStartupCommands())) |
|
321 |
commands.sort() |
|
322 |
return reduce(lambda x, y: x + y[1], commands, []) |
|
323 |
||
324 |
def AppendStartupCommand(self, command_infos): |
|
325 |
slave = self.GetSlave(command_infos["Position"]) |
|
326 |
if slave is not None: |
|
327 |
command_idx = slave.appendStartupCommand(command_infos) |
|
328 |
self.BufferModel() |
|
329 |
return command_idx |
|
330 |
return None |
|
331 |
||
332 |
def SetStartupCommandInfos(self, command_infos): |
|
333 |
slave = self.GetSlave(command_infos["Position"]) |
|
334 |
if slave is not None: |
|
335 |
slave.setStartupCommand(command_infos) |
|
336 |
self.BufferModel() |
|
337 |
||
338 |
def RemoveStartupCommand(self, slave_pos, command_idx, buffer=True): |
|
339 |
slave = self.GetSlave(slave_pos) |
|
340 |
if slave is not None: |
|
341 |
slave.removeStartupCommand(command_idx) |
|
342 |
if buffer: |
|
343 |
self.BufferModel() |
|
344 |
||
345 |
def SetProcessVariables(self, variables): |
|
346 |
vars = [] |
|
347 |
for var in variables: |
|
348 |
variable = ProcessVariablesClasses["ProcessVariables_variable"]() |
|
349 |
variable.setName(var["Name"]) |
|
350 |
variable.setComment(var["Description"]) |
|
351 |
if var["ReadFrom"] != "": |
|
352 |
position, index, subindex = var["ReadFrom"] |
|
353 |
if variable.getReadFrom() is None: |
|
354 |
variable.addReadFrom() |
|
355 |
read_from = variable.getReadFrom() |
|
356 |
read_from.setPosition(position) |
|
357 |
read_from.setIndex(index) |
|
358 |
read_from.setSubIndex(subindex) |
|
359 |
elif variable.getReadFrom() is not None: |
|
360 |
variable.deleteReadFrom() |
|
361 |
if var["WriteTo"] != "": |
|
362 |
position, index, subindex = var["WriteTo"] |
|
363 |
if variable.getWriteTo() is None: |
|
364 |
variable.addWriteTo() |
|
365 |
write_to = variable.getWriteTo() |
|
366 |
write_to.setPosition(position) |
|
367 |
write_to.setIndex(index) |
|
368 |
write_to.setSubIndex(subindex) |
|
369 |
elif variable.getWriteTo() is not None: |
|
370 |
variable.deleteWriteTo() |
|
371 |
vars.append(variable) |
|
372 |
self.ProcessVariables.setvariable(vars) |
|
373 |
self.BufferModel() |
|
374 |
||
375 |
def GetProcessVariables(self): |
|
376 |
variables = [] |
|
377 |
idx = 0 |
|
378 |
for variable in self.ProcessVariables.getvariable(): |
|
379 |
var = {"Name": variable.getName(), |
|
380 |
"Number": idx, |
|
381 |
"Description": variable.getComment()} |
|
382 |
read_from = variable.getReadFrom() |
|
383 |
if read_from is not None: |
|
384 |
var["ReadFrom"] = (read_from.getPosition(), |
|
385 |
read_from.getIndex(), |
|
386 |
read_from.getSubIndex()) |
|
387 |
else: |
|
388 |
var["ReadFrom"] = "" |
|
389 |
write_to = variable.getWriteTo() |
|
390 |
if write_to is not None: |
|
391 |
var["WriteTo"] = (write_to.getPosition(), |
|
392 |
write_to.getIndex(), |
|
393 |
write_to.getSubIndex()) |
|
394 |
else: |
|
395 |
var["WriteTo"] = "" |
|
396 |
variables.append(var) |
|
397 |
idx += 1 |
|
398 |
return variables |
|
399 |
||
400 |
def _ScanNetwork(self): |
|
401 |
app_frame = self.GetCTRoot().AppFrame |
|
402 |
||
403 |
execute = True |
|
404 |
if len(self.Children) > 0: |
|
405 |
dialog = wx.MessageDialog(app_frame, |
|
406 |
_("The current network configuration will be deleted.\nDo you want to continue?"), |
|
407 |
_("Scan Network"), |
|
408 |
wx.YES_NO|wx.ICON_QUESTION) |
|
409 |
execute = dialog.ShowModal() == wx.ID_YES |
|
410 |
dialog.Destroy() |
|
411 |
||
412 |
if execute: |
|
413 |
error, returnVal = self.RemoteExec(SCAN_COMMAND, returnVal = None) |
|
414 |
if error != 0: |
|
415 |
dialog = wx.MessageDialog(app_frame, returnVal, "Error", wx.OK|wx.ICON_ERROR) |
|
416 |
dialog.ShowModal() |
|
417 |
dialog.Destroy() |
|
418 |
elif returnVal is not None: |
|
419 |
for child in self.IECSortedChildren(): |
|
420 |
self._doRemoveChild(child) |
|
421 |
||
422 |
for slave in returnVal: |
|
423 |
type_infos = { |
|
424 |
"vendor": slave["vendor_id"], |
|
425 |
"product_code": slave["product_code"], |
|
426 |
"revision_number":slave["revision_number"], |
|
427 |
} |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
428 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 429 |
if device is not None: |
430 |
if HAS_MCL and _EthercatCIA402SlaveCTN.NODE_PROFILE in device.GetProfileNumbers(): |
|
431 |
CTNType = "EthercatCIA402Slave" |
|
432 |
else: |
|
433 |
CTNType = "EthercatSlave" |
|
434 |
self.CTNAddChild("slave%s" % slave["idx"], CTNType, slave["idx"]) |
|
435 |
self.SetSlaveAlias(slave["idx"], slave["alias"]) |
|
436 |
type_infos["device_type"] = device.getType().getcontent() |
|
437 |
self.SetSlaveType(slave["idx"], type_infos) |
|
2127
32255ca50fb0
Fix scan network functionality, project tree not refreshed after adding slaves
Laurent Bessard
parents:
2124
diff
changeset
|
438 |
|
32255ca50fb0
Fix scan network functionality, project tree not refreshed after adding slaves
Laurent Bessard
parents:
2124
diff
changeset
|
439 |
if app_frame: |
32255ca50fb0
Fix scan network functionality, project tree not refreshed after adding slaves
Laurent Bessard
parents:
2124
diff
changeset
|
440 |
app_frame.RefreshProjectTree() |
32255ca50fb0
Fix scan network functionality, project tree not refreshed after adding slaves
Laurent Bessard
parents:
2124
diff
changeset
|
441 |
|
2111 | 442 |
def CTNAddChild(self, CTNName, CTNType, IEC_Channel=0): |
443 |
""" |
|
444 |
Create the confnodes that may be added as child to this node self |
|
445 |
@param CTNType: string desining the confnode class name (get name from CTNChildrenTypes) |
|
446 |
@param CTNName: string for the name of the confnode instance |
|
447 |
""" |
|
448 |
newConfNodeOpj = ConfigTreeNode.CTNAddChild(self, CTNName, CTNType, IEC_Channel) |
|
449 |
||
450 |
slave = self.GetSlave(newConfNodeOpj.BaseParams.getIEC_Channel()) |
|
451 |
if slave is None: |
|
452 |
slave = EtherCATConfigClasses["Config_Slave"]() |
|
453 |
slave_infos = slave.getInfo() |
|
454 |
slave_infos.setName("undefined") |
|
455 |
slave_infos.setPhysAddr(newConfNodeOpj.BaseParams.getIEC_Channel()) |
|
456 |
slave_infos.setAutoIncAddr(0) |
|
457 |
self.Config.getConfig().appendSlave(slave) |
|
458 |
self.BufferModel() |
|
459 |
self.OnCTNSave() |
|
460 |
||
461 |
return newConfNodeOpj |
|
462 |
||
463 |
def _doRemoveChild(self, CTNInstance): |
|
464 |
slave_pos = CTNInstance.GetSlavePos() |
|
465 |
config = self.Config.getConfig() |
|
466 |
for idx, slave in enumerate(config.getSlave()): |
|
467 |
slave_infos = slave.getInfo() |
|
468 |
if slave_infos.getPhysAddr() == slave_pos: |
|
469 |
config.removeSlave(idx) |
|
470 |
self.BufferModel() |
|
471 |
self.OnCTNSave() |
|
472 |
ConfigTreeNode._doRemoveChild(self, CTNInstance) |
|
473 |
||
474 |
def SetSlavePosition(self, slave_pos, new_pos): |
|
475 |
slave = self.GetSlave(slave_pos) |
|
476 |
if slave is not None: |
|
477 |
slave_info = slave.getInfo() |
|
478 |
slave_info.setPhysAddr(new_pos) |
|
479 |
for variable in self.ProcessVariables.getvariable(): |
|
480 |
read_from = variable.getReadFrom() |
|
481 |
if read_from is not None and read_from.getPosition() == slave_pos: |
|
482 |
read_from.setPosition(new_pos) |
|
483 |
write_to = variable.getWriteTo() |
|
484 |
if write_to is not None and write_to.getPosition() == slave_pos: |
|
485 |
write_to.setPosition(new_pos) |
|
486 |
self.CreateBuffer(True) |
|
487 |
self.OnCTNSave() |
|
488 |
if self._View is not None: |
|
489 |
self._View.RefreshView() |
|
490 |
self._View.RefreshBuffer() |
|
491 |
||
492 |
def GetSlaveAlias(self, slave_pos): |
|
493 |
slave = self.GetSlave(slave_pos) |
|
494 |
if slave is not None: |
|
495 |
slave_info = slave.getInfo() |
|
496 |
return slave_info.getAutoIncAddr() |
|
497 |
return None |
|
498 |
||
499 |
def SetSlaveAlias(self, slave_pos, alias): |
|
500 |
slave = self.GetSlave(slave_pos) |
|
501 |
if slave is not None: |
|
502 |
slave_info = slave.getInfo() |
|
503 |
slave_info.setAutoIncAddr(alias) |
|
504 |
self.BufferModel() |
|
505 |
||
506 |
def GetSlaveType(self, slave_pos): |
|
507 |
slave = self.GetSlave(slave_pos) |
|
508 |
if slave is not None: |
|
509 |
return slave.getType() |
|
510 |
return None |
|
511 |
||
512 |
def SetSlaveType(self, slave_pos, type_infos): |
|
513 |
slave = self.GetSlave(slave_pos) |
|
514 |
if slave is not None: |
|
515 |
slave.setType(type_infos) |
|
516 |
self.BufferModel() |
|
517 |
||
518 |
def GetSlaveInfos(self, slave_pos): |
|
519 |
slave = self.GetSlave(slave_pos) |
|
520 |
if slave is not None: |
|
521 |
type_infos = slave.getType() |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
522 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 523 |
if device is not None: |
524 |
infos = type_infos.copy() |
|
525 |
infos.update({"physics": device.getPhysics(), |
|
526 |
"sync_managers": device.GetSyncManagers(), |
|
527 |
"entries": self.GetSlaveVariables(device)}) |
|
528 |
return infos |
|
529 |
return None |
|
530 |
||
531 |
def GetSlaveVariables(self, slave_pos=None, limits=None, device=None): |
|
532 |
if device is None and slave_pos is not None: |
|
533 |
slave = self.GetSlave(slave_pos) |
|
534 |
if slave is not None: |
|
535 |
type_infos = slave.getType() |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
536 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 537 |
if device is not None: |
538 |
entries = device.GetEntriesList(limits) |
|
539 |
entries_list = entries.items() |
|
540 |
entries_list.sort() |
|
541 |
entries = [] |
|
542 |
current_index = None |
|
543 |
current_entry = None |
|
544 |
for (index, subindex), entry in entries_list: |
|
545 |
entry["children"] = [] |
|
546 |
if slave_pos is not None: |
|
547 |
entry["Position"] = str(slave_pos) |
|
548 |
entry |
|
549 |
if index != current_index: |
|
550 |
current_index = index |
|
551 |
current_entry = entry |
|
552 |
entries.append(entry) |
|
553 |
elif current_entry is not None: |
|
554 |
current_entry["children"].append(entry) |
|
555 |
else: |
|
556 |
entries.append(entry) |
|
557 |
return entries |
|
558 |
return [] |
|
559 |
||
560 |
def GetSlaveVariableDataType(self, slave_pos, index, subindex): |
|
561 |
slave = self.GetSlave(slave_pos) |
|
562 |
if slave is not None: |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
563 |
device, module_extra_params = self.GetModuleInfos(slave.getType()) |
2111 | 564 |
if device is not None: |
565 |
entries = device.GetEntriesList() |
|
566 |
entry_infos = entries.get((index, subindex)) |
|
567 |
if entry_infos is not None: |
|
568 |
return entry_infos["Type"] |
|
569 |
return None |
|
570 |
||
571 |
def GetNodesVariables(self, vendor=None, slave_pos=None, slave_profile=None, limits=None): |
|
572 |
entries = [] |
|
573 |
for slave_position in self.GetSlaves(): |
|
574 |
if slave_pos is not None and slave_position != slave_pos: |
|
575 |
continue |
|
576 |
slave = self.GetSlave(slave_position) |
|
577 |
type_infos = slave.getType() |
|
578 |
if vendor is not None and ExtractHexDecValue(type_infos["vendor"]) != vendor: |
|
579 |
continue |
|
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
580 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 581 |
if slave_profile is not None and slave_profile not in device.GetProfileNumbers(): |
582 |
continue |
|
583 |
entries.extend(self.GetSlaveVariables(slave_position, limits, device)) |
|
584 |
return entries |
|
585 |
||
586 |
def GetModuleInfos(self, type_infos): |
|
587 |
return self.CTNParent.GetModuleInfos(type_infos) |
|
588 |
||
589 |
def GetSlaveTypesLibrary(self, profile_filter=None): |
|
590 |
return self.CTNParent.GetModulesLibrary(profile_filter) |
|
591 |
||
592 |
def GetLibraryVendors(self): |
|
593 |
return self.CTNParent.GetVendors() |
|
594 |
||
595 |
def GetDeviceLocationTree(self, slave_pos, current_location, device_name): |
|
596 |
slave = self.GetSlave(slave_pos) |
|
597 |
vars = [] |
|
598 |
if slave is not None: |
|
599 |
type_infos = slave.getType() |
|
600 |
||
2137
b65abacdbdf9
Added support for multiple module extra params in ModulesLibrary
Laurent Bessard
parents:
2133
diff
changeset
|
601 |
device, module_extra_params = self.GetModuleInfos(type_infos) |
2111 | 602 |
if device is not None: |
603 |
sync_managers = [] |
|
604 |
for sync_manager in device.getSm(): |
|
605 |
sync_manager_control_byte = ExtractHexDecValue(sync_manager.getControlByte()) |
|
606 |
sync_manager_direction = sync_manager_control_byte & 0x0c |
|
607 |
if sync_manager_direction: |
|
608 |
sync_managers.append(LOCATION_VAR_OUTPUT) |
|
609 |
else: |
|
610 |
sync_managers.append(LOCATION_VAR_INPUT) |
|
611 |
||
612 |
entries = device.GetEntriesList().items() |
|
613 |
entries.sort() |
|
614 |
for (index, subindex), entry in entries: |
|
615 |
var_size = self.GetSizeOfType(entry["Type"]) |
|
616 |
if var_size is not None: |
|
617 |
var_class = VARCLASSCONVERSION.get(entry["PDOMapping"], None) |
|
618 |
if var_class is not None: |
|
619 |
if var_class == LOCATION_VAR_INPUT: |
|
620 |
var_dir = "%I" |
|
621 |
else: |
|
622 |
var_dir = "%Q" |
|
623 |
||
624 |
vars.append({"name": "0x%4.4x-0x%2.2x: %s" % (index, subindex, entry["Name"]), |
|
625 |
"type": var_class, |
|
626 |
"size": var_size, |
|
627 |
"IEC_type": entry["Type"], |
|
628 |
"var_name": "%s_%4.4x_%2.2x" % ("_".join(device_name.split()), index, subindex), |
|
629 |
"location": "%s%s%s"%(var_dir, var_size, ".".join(map(str, current_location + |
|
630 |
(index, subindex)))), |
|
631 |
"description": "", |
|
632 |
"children": []}) |
|
633 |
||
634 |
return vars |
|
635 |
||
636 |
def CTNTestModified(self): |
|
637 |
return self.ChangesToSave or not self.ModelIsSaved() |
|
638 |
||
2133 | 639 |
def OnCTNSave(self, from_project_path=None): |
2111 | 640 |
config_filepath = self.ConfigFileName() |
641 |
||
642 |
config_text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" |
|
643 |
config_extras = {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", |
|
644 |
"xsi:noNamespaceSchemaLocation" : "EtherCATInfo.xsd"} |
|
645 |
config_text += self.Config.generateXMLText("EtherCATConfig", 0, config_extras) |
|
646 |
||
647 |
config_xmlfile = open(config_filepath,"w") |
|
648 |
config_xmlfile.write(config_text.encode("utf-8")) |
|
649 |
config_xmlfile.close() |
|
650 |
||
651 |
process_filepath = self.ProcessVariablesFileName() |
|
652 |
||
653 |
process_text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" |
|
654 |
process_extras = {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance"} |
|
655 |
process_text += self.ProcessVariables.generateXMLText("ProcessVariables", 0, process_extras) |
|
656 |
||
657 |
process_xmlfile = open(process_filepath,"w") |
|
658 |
process_xmlfile.write(process_text.encode("utf-8")) |
|
659 |
process_xmlfile.close() |
|
660 |
||
661 |
self.Buffer.CurrentSaved() |
|
662 |
return True |
|
663 |
||
664 |
def GetProcessVariableName(self, location, var_type): |
|
665 |
return "__M%s_%s" % (self.GetSizeOfType(var_type), "_".join(map(str, location))) |
|
666 |
||
667 |
def _Generate_C(self, buildpath, locations): |
|
668 |
current_location = self.GetCurrentLocation() |
|
669 |
# define a unique name for the generated C file |
|
670 |
location_str = "_".join(map(lambda x:str(x), current_location)) |
|
671 |
||
672 |
Gen_Ethercatfile_path = os.path.join(buildpath, "ethercat_%s.c"%location_str) |
|
673 |
||
674 |
self.FileGenerator = _EthercatCFileGenerator(self) |
|
675 |
||
676 |
LocationCFilesAndCFLAGS, LDFLAGS, extra_files = ConfigTreeNode._Generate_C(self, buildpath, locations) |
|
677 |
||
678 |
for idx, variable in enumerate(self.ProcessVariables.getvariable()): |
|
679 |
name = None |
|
680 |
var_type = None |
|
681 |
read_from = variable.getReadFrom() |
|
682 |
write_to = variable.getWriteTo() |
|
683 |
if read_from is not None: |
|
684 |
pos = read_from.getPosition() |
|
685 |
index = read_from.getIndex() |
|
686 |
subindex = read_from.getSubIndex() |
|
687 |
location = current_location + (idx, ) |
|
688 |
var_type = self.GetSlaveVariableDataType(pos, index, subindex) |
|
689 |
name = self.FileGenerator.DeclareVariable( |
|
690 |
pos, index, subindex, var_type, "I", |
|
691 |
self.GetProcessVariableName(location, var_type)) |
|
692 |
if write_to is not None: |
|
693 |
pos = write_to.getPosition() |
|
694 |
index = write_to.getIndex() |
|
695 |
subindex = write_to.getSubIndex() |
|
696 |
if name is None: |
|
697 |
location = current_location + (idx, ) |
|
698 |
var_type = self.GetSlaveVariableDataType(pos, index, subindex) |
|
699 |
name = self.GetProcessVariableName(location, var_type) |
|
700 |
self.FileGenerator.DeclareVariable( |
|
701 |
pos, index, subindex, var_type, "Q", name, True) |
|
702 |
||
703 |
self.FileGenerator.GenerateCFile(Gen_Ethercatfile_path, location_str, self.BaseParams.getIEC_Channel()) |
|
704 |
||
2135
ca1c34ff6c10
Fixed order of LocationCFilesAndCFLAGS, master was called after slaves in PLC init and retrieve functions
Laurent Bessard
parents:
2133
diff
changeset
|
705 |
LocationCFilesAndCFLAGS.insert(0, |
2111 | 706 |
(current_location, |
707 |
[(Gen_Ethercatfile_path, '"-I%s"'%os.path.abspath(self.GetCTRoot().GetIECLibPath()))], |
|
708 |
True)) |
|
2112
e88cd6ff885e
Fixed linking with non RTDM etherlab lib. Now with rtdm...
Edouard Tisserant
parents:
2111
diff
changeset
|
709 |
LDFLAGS.append("-lethercat_rtdm -lrtdm") |
2111 | 710 |
|
711 |
return LocationCFilesAndCFLAGS, LDFLAGS, extra_files |
|
712 |
||
713 |
ConfNodeMethods = [ |
|
714 |
{"bitmap" : "ScanNetwork", |
|
715 |
"name" : _("Scan Network"), |
|
716 |
"tooltip" : _("Scan Network"), |
|
717 |
"method" : "_ScanNetwork"}, |
|
718 |
] |
|
719 |
||
720 |
def CTNGenerate_C(self, buildpath, locations): |
|
721 |
current_location = self.GetCurrentLocation() |
|
722 |
||
723 |
slaves = self.GetSlaves() |
|
724 |
for slave_pos in slaves: |
|
725 |
slave = self.GetSlave(slave_pos) |
|
726 |
if slave is not None: |
|
727 |
self.FileGenerator.DeclareSlave(slave_pos, slave) |
|
728 |
||
729 |
for location in locations: |
|
730 |
loc = location["LOC"][len(current_location):] |
|
731 |
slave_pos = loc[0] |
|
732 |
if slave_pos in slaves and len(loc) == 3 and location["DIR"] != "M": |
|
733 |
self.FileGenerator.DeclareVariable( |
|
734 |
slave_pos, loc[1], loc[2], location["IEC_TYPE"], location["DIR"], location["NAME"]) |
|
735 |
||
736 |
return [],"",False |
|
737 |
||
738 |
#------------------------------------------------------------------------------- |
|
739 |
# Current Buffering Management Functions |
|
740 |
#------------------------------------------------------------------------------- |
|
741 |
||
742 |
""" |
|
743 |
Return a copy of the config |
|
744 |
""" |
|
745 |
def Copy(self, model): |
|
746 |
return cPickle.loads(cPickle.dumps(model)) |
|
747 |
||
748 |
def CreateBuffer(self, saved): |
|
749 |
self.Buffer = UndoBuffer(cPickle.dumps((self.Config, self.ProcessVariables)), saved) |
|
750 |
||
751 |
def BufferModel(self): |
|
752 |
self.Buffer.Buffering(cPickle.dumps((self.Config, self.ProcessVariables))) |
|
753 |
||
754 |
def ModelIsSaved(self): |
|
755 |
if self.Buffer is not None: |
|
756 |
return self.Buffer.IsCurrentSaved() |
|
757 |
else: |
|
758 |
return True |
|
759 |
||
760 |
def LoadPrevious(self): |
|
761 |
self.Config, self.ProcessVariables = cPickle.loads(self.Buffer.Previous()) |
|
762 |
||
763 |
def LoadNext(self): |
|
764 |
self.Config, self.ProcessVariables = cPickle.loads(self.Buffer.Next()) |
|
765 |
||
766 |
def GetBufferState(self): |
|
767 |
first = self.Buffer.IsFirst() |
|
768 |
last = self.Buffer.IsLast() |
|
769 |
return not first, not last |
|
770 |