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