1 import os, sys |
|
2 base_folder = os.path.split(sys.path[0])[0] |
|
3 CanFestivalPath = os.path.join(base_folder, "CanFestival-3") |
|
4 sys.path.append(os.path.join(CanFestivalPath, "objdictgen")) |
|
5 |
|
6 from nodelist import NodeList |
|
7 from nodemanager import NodeManager |
|
8 import config_utils, gen_cfile, eds_utils |
|
9 from networkedit import networkedit |
|
10 from objdictedit import objdictedit |
|
11 import canfestival_config as local_canfestival_config |
|
12 from ConfigTree import ConfigTreeNode |
|
13 from commondialogs import CreateNodeDialog |
|
14 import wx |
|
15 |
|
16 from SlaveEditor import SlaveEditor |
|
17 from NetworkEditor import NetworkEditor |
|
18 |
|
19 from gnosis.xml.pickle import * |
|
20 from gnosis.xml.pickle.util import setParanoia |
|
21 setParanoia(0) |
|
22 |
|
23 if wx.Platform == '__WXMSW__': |
|
24 DEFAULT_SETTINGS = { |
|
25 "CAN_Driver": "can_tcp_win32", |
|
26 "CAN_Device": "127.0.0.1", |
|
27 "CAN_Baudrate": "125K", |
|
28 "Slave_NodeId": 2, |
|
29 "Master_NodeId": 1, |
|
30 } |
|
31 else: |
|
32 DEFAULT_SETTINGS = { |
|
33 "CAN_Driver": "../CanFestival-3/drivers/can_socket/libcanfestival_can_socket.so", |
|
34 "CAN_Device": "vcan0", |
|
35 "CAN_Baudrate": "125K", |
|
36 "Slave_NodeId": 2, |
|
37 "Master_NodeId": 1, |
|
38 } |
|
39 |
|
40 #-------------------------------------------------- |
|
41 # SLAVE |
|
42 #-------------------------------------------------- |
|
43 |
|
44 class _SlaveCTN(NodeManager): |
|
45 XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
46 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
47 <xsd:element name="CanFestivalSlaveNode"> |
|
48 <xsd:complexType> |
|
49 <xsd:attribute name="CAN_Device" type="xsd:string" use="optional" default="%(CAN_Device)s"/> |
|
50 <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="optional" default="%(CAN_Baudrate)s"/> |
|
51 <xsd:attribute name="NodeId" type="xsd:string" use="optional" default="%(Slave_NodeId)d"/> |
|
52 <xsd:attribute name="Sync_Align" type="xsd:integer" use="optional" default="0"/> |
|
53 <xsd:attribute name="Sync_Align_Ratio" use="optional" default="50"> |
|
54 <xsd:simpleType> |
|
55 <xsd:restriction base="xsd:integer"> |
|
56 <xsd:minInclusive value="1"/> |
|
57 <xsd:maxInclusive value="99"/> |
|
58 </xsd:restriction> |
|
59 </xsd:simpleType> |
|
60 </xsd:attribute> |
|
61 </xsd:complexType> |
|
62 </xsd:element> |
|
63 </xsd:schema> |
|
64 """ % DEFAULT_SETTINGS |
|
65 |
|
66 EditorType = SlaveEditor |
|
67 |
|
68 def __init__(self): |
|
69 # TODO change netname when name change |
|
70 NodeManager.__init__(self) |
|
71 odfilepath = self.GetSlaveODPath() |
|
72 if(os.path.isfile(odfilepath)): |
|
73 self.OpenFileInCurrent(odfilepath) |
|
74 else: |
|
75 self.FilePath = "" |
|
76 dialog = CreateNodeDialog(None, wx.OK) |
|
77 dialog.Type.Enable(False) |
|
78 dialog.GenSYNC.Enable(False) |
|
79 if dialog.ShowModal() == wx.ID_OK: |
|
80 name, id, nodetype, description = dialog.GetValues() |
|
81 profile, filepath = dialog.GetProfile() |
|
82 NMT = dialog.GetNMTManagement() |
|
83 options = dialog.GetOptions() |
|
84 self.CreateNewNode(name, # Name - will be changed at build time |
|
85 id, # NodeID - will be changed at build time |
|
86 "slave", # Type |
|
87 description,# description |
|
88 profile, # profile |
|
89 filepath, # prfile filepath |
|
90 NMT, # NMT |
|
91 options) # options |
|
92 else: |
|
93 self.CreateNewNode("SlaveNode", # Name - will be changed at build time |
|
94 0x00, # NodeID - will be changed at build time |
|
95 "slave", # Type |
|
96 "", # description |
|
97 "None", # profile |
|
98 "", # prfile filepath |
|
99 "heartbeat", # NMT |
|
100 []) # options |
|
101 dialog.Destroy() |
|
102 self.OnCTNSave() |
|
103 |
|
104 def GetSlaveODPath(self): |
|
105 return os.path.join(self.CTNPath(), 'slave.od') |
|
106 |
|
107 def GetCanDevice(self): |
|
108 return self.CanFestivalSlaveNode.getCan_Device() |
|
109 |
|
110 def _OpenView(self): |
|
111 ConfigTreeNode._OpenView(self) |
|
112 if self._View is not None: |
|
113 self._View.SetBusId(self.GetCurrentLocation()) |
|
114 |
|
115 ConfNodeMethods = [ |
|
116 {"bitmap" : os.path.join("images", "NetworkEdit"), |
|
117 "name" : "Edit slave", |
|
118 "tooltip" : "Edit CanOpen slave with ObjdictEdit", |
|
119 "method" : "_OpenView"}, |
|
120 ] |
|
121 |
|
122 def OnCTNClose(self): |
|
123 if self._View: |
|
124 self._View.Close() |
|
125 |
|
126 def CTNTestModified(self): |
|
127 return self.ChangesToSave or self.OneFileHasChanged() |
|
128 |
|
129 def OnCTNSave(self): |
|
130 return self.SaveCurrentInFile(self.GetSlaveODPath()) |
|
131 |
|
132 def SetParamsAttribute(self, path, value): |
|
133 result = ConfigTreeNode.SetParamsAttribute(self, path, value) |
|
134 |
|
135 # Filter IEC_Channel and Name, that have specific behavior |
|
136 if path == "BaseParams.IEC_Channel" and self._View is not None: |
|
137 self._View.SetBusId(self.GetCurrentLocation()) |
|
138 |
|
139 return result |
|
140 |
|
141 def CTNGenerate_C(self, buildpath, locations): |
|
142 """ |
|
143 Generate C code |
|
144 @param current_location: Tupple containing confnode IEC location : %I0.0.4.5 => (0,0,4,5) |
|
145 @param locations: List of complete variables locations \ |
|
146 [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) |
|
147 "NAME" : name of the variable (generally "__IW0_1_2" style) |
|
148 "DIR" : direction "Q","I" or "M" |
|
149 "SIZE" : size "X", "B", "W", "D", "L" |
|
150 "LOC" : tuple of interger for IEC location (0,1,2,...) |
|
151 }, ...] |
|
152 @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
|
153 """ |
|
154 current_location = self.GetCurrentLocation() |
|
155 # define a unique name for the generated C file |
|
156 prefix = "_".join(map(str, current_location)) |
|
157 Gen_OD_path = os.path.join(buildpath, "OD_%s.c"%prefix ) |
|
158 # Create a new copy of the model |
|
159 slave = self.GetCurrentNodeCopy() |
|
160 slave.SetNodeName("OD_%s"%prefix) |
|
161 # allow access to local OD from Slave PLC |
|
162 pointers = config_utils.LocalODPointers(locations, current_location, slave) |
|
163 res = gen_cfile.GenerateFile(Gen_OD_path, slave, pointers) |
|
164 if res : |
|
165 raise Exception, res |
|
166 res = eds_utils.GenerateEDSFile(os.path.join(buildpath, "Slave_%s.eds"%prefix), slave) |
|
167 if res : |
|
168 raise Exception, res |
|
169 return [(Gen_OD_path,local_canfestival_config.getCFLAGS(CanFestivalPath))],"",False |
|
170 |
|
171 def LoadPrevious(self): |
|
172 self.LoadCurrentPrevious() |
|
173 |
|
174 def LoadNext(self): |
|
175 self.LoadCurrentNext() |
|
176 |
|
177 def GetBufferState(self): |
|
178 return self.GetCurrentBufferState() |
|
179 |
|
180 #-------------------------------------------------- |
|
181 # MASTER |
|
182 #-------------------------------------------------- |
|
183 |
|
184 class MiniNodeManager(NodeManager): |
|
185 |
|
186 def __init__(self, parent, filepath, fullname): |
|
187 NodeManager.__init__(self) |
|
188 |
|
189 self.OpenFileInCurrent(filepath) |
|
190 |
|
191 self.Parent = parent |
|
192 self.Fullname = fullname |
|
193 |
|
194 def OnCloseEditor(self, view): |
|
195 self.Parent.OnCloseEditor(view) |
|
196 |
|
197 def CTNFullName(self): |
|
198 return self.Fullname |
|
199 |
|
200 def GetBufferState(self): |
|
201 return self.GetCurrentBufferState() |
|
202 |
|
203 class _NodeListCTN(NodeList): |
|
204 XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
205 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
206 <xsd:element name="CanFestivalNode"> |
|
207 <xsd:complexType> |
|
208 <xsd:attribute name="CAN_Device" type="xsd:string" use="optional" default="%(CAN_Device)s"/> |
|
209 <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="optional" default="%(CAN_Baudrate)s"/> |
|
210 <xsd:attribute name="NodeId" type="xsd:string" use="optional" default="%(Master_NodeId)d"/> |
|
211 <xsd:attribute name="Sync_TPDOs" type="xsd:boolean" use="optional" default="true"/> |
|
212 </xsd:complexType> |
|
213 </xsd:element> |
|
214 </xsd:schema> |
|
215 """ % DEFAULT_SETTINGS |
|
216 |
|
217 EditorType = NetworkEditor |
|
218 |
|
219 def __init__(self): |
|
220 manager = NodeManager() |
|
221 NodeList.__init__(self, manager) |
|
222 self.LoadProject(self.CTNPath()) |
|
223 self.SetNetworkName(self.BaseParams.getName()) |
|
224 |
|
225 def GetCanDevice(self): |
|
226 return self.CanFestivalNode.getCan_Device() |
|
227 |
|
228 def SetParamsAttribute(self, path, value): |
|
229 result = ConfigTreeNode.SetParamsAttribute(self, path, value) |
|
230 |
|
231 # Filter IEC_Channel and Name, that have specific behavior |
|
232 if path == "BaseParams.IEC_Channel" and self._View is not None: |
|
233 self._View.SetBusId(self.GetCurrentLocation()) |
|
234 elif path == "BaseParams.Name": |
|
235 self.SetNetworkName(value) |
|
236 |
|
237 return result |
|
238 |
|
239 def _OpenView(self): |
|
240 ConfigTreeNode._OpenView(self) |
|
241 if self._View is not None: |
|
242 self._View.SetBusId(self.GetCurrentLocation()) |
|
243 |
|
244 _GeneratedView = None |
|
245 def _ShowMasterGenerated(self): |
|
246 if self._GeneratedView is None: |
|
247 buildpath = self._getBuildPath() |
|
248 # Eventually create build dir |
|
249 if not os.path.exists(buildpath): |
|
250 self.GetCTRoot().logger.write_error(_("Error: No PLC built\n")) |
|
251 return |
|
252 |
|
253 masterpath = os.path.join(buildpath, "MasterGenerated.od") |
|
254 if not os.path.exists(masterpath): |
|
255 self.GetCTRoot().logger.write_error(_("Error: No Master generated\n")) |
|
256 return |
|
257 |
|
258 app_frame = self.GetCTRoot().AppFrame |
|
259 |
|
260 manager = MiniNodeManager(self, masterpath, self.CTNFullName() + ".generated_master") |
|
261 self._GeneratedView = SlaveEditor(app_frame.TabsOpened, manager, app_frame, False) |
|
262 |
|
263 app_frame.EditProjectElement(self._GeneratedView, "MasterGenerated") |
|
264 |
|
265 def _CloseGenerateView(self): |
|
266 if self._GeneratedView is not None: |
|
267 app_frame = self.GetCTRoot().AppFrame |
|
268 if app_frame is not None: |
|
269 app_frame.DeletePage(self._GeneratedView) |
|
270 |
|
271 ConfNodeMethods = [ |
|
272 {"bitmap" : os.path.join("images", "NetworkEdit"), |
|
273 "name" : _("Edit network"), |
|
274 "tooltip" : _("Edit CanOpen Network with NetworkEdit"), |
|
275 "method" : "_OpenView"}, |
|
276 {"bitmap" : os.path.join("images", "ShowMaster"), |
|
277 "name" : _("Show Master"), |
|
278 "tooltip" : _("Show Master generated by config_utils"), |
|
279 "method" : "_ShowMasterGenerated"} |
|
280 ] |
|
281 |
|
282 def OnCloseEditor(self, view): |
|
283 ConfigTreeNode.OnCloseEditor(self, view) |
|
284 if self._GeneratedView == view: |
|
285 self._GeneratedView = None |
|
286 |
|
287 def OnCTNClose(self): |
|
288 ConfigTreeNode.OnCTNClose(self) |
|
289 self._CloseGenerateView() |
|
290 return True |
|
291 |
|
292 def CTNTestModified(self): |
|
293 return self.ChangesToSave or self.HasChanged() |
|
294 |
|
295 def OnCTNSave(self): |
|
296 self.SetRoot(self.CTNPath()) |
|
297 return self.SaveProject() is None |
|
298 |
|
299 def CTNGenerate_C(self, buildpath, locations): |
|
300 """ |
|
301 Generate C code |
|
302 @param current_location: Tupple containing confnode IEC location : %I0.0.4.5 => (0,0,4,5) |
|
303 @param locations: List of complete variables locations \ |
|
304 [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) |
|
305 "NAME" : name of the variable (generally "__IW0_1_2" style) |
|
306 "DIR" : direction "Q","I" or "M" |
|
307 "SIZE" : size "X", "B", "W", "D", "L" |
|
308 "LOC" : tuple of interger for IEC location (0,1,2,...) |
|
309 }, ...] |
|
310 @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
|
311 """ |
|
312 self._CloseGenerateView() |
|
313 current_location = self.GetCurrentLocation() |
|
314 # define a unique name for the generated C file |
|
315 prefix = "_".join(map(str, current_location)) |
|
316 Gen_OD_path = os.path.join(buildpath, "OD_%s.c"%prefix ) |
|
317 # Create a new copy of the model with DCF loaded with PDO mappings for desired location |
|
318 try: |
|
319 master, pointers = config_utils.GenerateConciseDCF(locations, current_location, self, self.CanFestivalNode.getSync_TPDOs(),"OD_%s"%prefix) |
|
320 except config_utils.PDOmappingException, e: |
|
321 raise Exception, e.message |
|
322 # Do generate C file. |
|
323 res = gen_cfile.GenerateFile(Gen_OD_path, master, pointers) |
|
324 if res : |
|
325 raise Exception, res |
|
326 |
|
327 file = open(os.path.join(buildpath, "MasterGenerated.od"), "w") |
|
328 dump(master, file) |
|
329 file.close() |
|
330 |
|
331 return [(Gen_OD_path,local_canfestival_config.getCFLAGS(CanFestivalPath))],"",False |
|
332 |
|
333 def LoadPrevious(self): |
|
334 self.Manager.LoadCurrentPrevious() |
|
335 |
|
336 def LoadNext(self): |
|
337 self.Manager.LoadCurrentNext() |
|
338 |
|
339 def GetBufferState(self): |
|
340 return self.Manager.GetCurrentBufferState() |
|
341 |
|
342 class RootClass: |
|
343 XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
344 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
345 <xsd:element name="CanFestivalInstance"> |
|
346 <xsd:complexType> |
|
347 <xsd:attribute name="CAN_Driver" type="xsd:string" use="optional" default="%(CAN_Driver)s"/> |
|
348 <xsd:attribute name="Debug_mode" type="xsd:boolean" use="optional" default="false"/> |
|
349 </xsd:complexType> |
|
350 </xsd:element> |
|
351 </xsd:schema> |
|
352 """ % DEFAULT_SETTINGS |
|
353 |
|
354 CTNChildrenTypes = [("CanOpenNode",_NodeListCTN, "CanOpen Master"), |
|
355 ("CanOpenSlave",_SlaveCTN, "CanOpen Slave")] |
|
356 def GetParamsAttributes(self, path = None): |
|
357 infos = ConfigTreeNode.GetParamsAttributes(self, path = None) |
|
358 for element in infos: |
|
359 if element["name"] == "CanFestivalInstance": |
|
360 for child in element["children"]: |
|
361 if child["name"] == "CAN_Driver": |
|
362 DLL_LIST= getattr(local_canfestival_config,"DLL_LIST",None) |
|
363 if DLL_LIST is not None: |
|
364 child["type"] = DLL_LIST |
|
365 return infos |
|
366 |
|
367 def GetCanDriver(self): |
|
368 can_driver = self.CanFestivalInstance.getCAN_Driver() |
|
369 if sys.platform == 'win32': |
|
370 if self.CanFestivalInstance.getDebug_mode() and os.path.isfile(os.path.join("%s"%(can_driver + '_DEBUG.dll'))): |
|
371 can_driver += '_DEBUG.dll' |
|
372 else: |
|
373 can_driver += '.dll' |
|
374 return can_driver |
|
375 |
|
376 def CTNGenerate_C(self, buildpath, locations): |
|
377 |
|
378 format_dict = {"locstr" : "_".join(map(str,self.GetCurrentLocation())), |
|
379 "candriver" : self.GetCanDriver(), |
|
380 "nodes_includes" : "", |
|
381 "board_decls" : "", |
|
382 "nodes_init" : "", |
|
383 "nodes_open" : "", |
|
384 "nodes_stop" : "", |
|
385 "nodes_close" : "", |
|
386 "nodes_send_sync" : "", |
|
387 "nodes_proceed_sync" : "", |
|
388 "slavebootups" : "", |
|
389 "slavebootup_register" : "", |
|
390 "post_sync" : "", |
|
391 "post_sync_register" : "", |
|
392 } |
|
393 for child in self.IECSortedChildren(): |
|
394 childlocstr = "_".join(map(str,child.GetCurrentLocation())) |
|
395 nodename = "OD_%s" % childlocstr |
|
396 |
|
397 # Try to get Slave Node |
|
398 child_data = getattr(child, "CanFestivalSlaveNode", None) |
|
399 if child_data is None: |
|
400 # Not a slave -> master |
|
401 child_data = getattr(child, "CanFestivalNode") |
|
402 # Apply sync setting |
|
403 format_dict["nodes_init"] += 'NODE_MASTER_INIT(%s, %s)\n '%( |
|
404 nodename, |
|
405 child_data.getNodeId()) |
|
406 if child_data.getSync_TPDOs(): |
|
407 format_dict["nodes_send_sync"] += 'NODE_SEND_SYNC(%s)\n '%(nodename) |
|
408 format_dict["nodes_proceed_sync"] += 'NODE_PROCEED_SYNC(%s)\n '%(nodename) |
|
409 |
|
410 # initialize and declare node boot status variables for post_SlaveBootup lookup |
|
411 SlaveIDs = child.GetSlaveIDs() |
|
412 if len(SlaveIDs) == 0: |
|
413 # define post_SlaveBootup lookup functions |
|
414 format_dict["slavebootups"] += ( |
|
415 "static void %s_post_SlaveBootup(CO_Data* d, UNS8 nodeId){}\n"%(nodename)) |
|
416 else: |
|
417 for id in SlaveIDs: |
|
418 format_dict["slavebootups"] += ( |
|
419 "int %s_slave_%d_booted = 0;\n"%(nodename, id)) |
|
420 # define post_SlaveBootup lookup functions |
|
421 format_dict["slavebootups"] += ( |
|
422 "static void %s_post_SlaveBootup(CO_Data* d, UNS8 nodeId){\n"%(nodename)+ |
|
423 " switch(nodeId){\n") |
|
424 # one case per declared node, mark node as booted |
|
425 for id in SlaveIDs: |
|
426 format_dict["slavebootups"] += ( |
|
427 " case %d:\n"%(id)+ |
|
428 " %s_slave_%d_booted = 1;\n"%(nodename, id)+ |
|
429 " break;\n") |
|
430 format_dict["slavebootups"] += ( |
|
431 " default:\n"+ |
|
432 " break;\n"+ |
|
433 " }\n"+ |
|
434 " if( ") |
|
435 # expression to test if all declared nodes booted |
|
436 format_dict["slavebootups"] += " && ".join(["%s_slave_%d_booted"%(nodename, id) for id in SlaveIDs]) |
|
437 format_dict["slavebootups"] += " )\n" + ( |
|
438 " Master_post_SlaveBootup(d,nodeId);\n"+ |
|
439 "}\n") |
|
440 # register previously declared func as post_SlaveBootup callback for that node |
|
441 format_dict["slavebootup_register"] += ( |
|
442 "%s_Data.post_SlaveBootup = %s_post_SlaveBootup;\n"%(nodename,nodename)) |
|
443 else: |
|
444 # Slave node |
|
445 align = child_data.getSync_Align() |
|
446 align_ratio=child_data.getSync_Align_Ratio() |
|
447 if align > 0: |
|
448 format_dict["post_sync"] += ( |
|
449 "static int %s_CalCount = 0;\n"%(nodename)+ |
|
450 "static void %s_post_sync(CO_Data* d){\n"%(nodename)+ |
|
451 " if(%s_CalCount < %d){\n"%(nodename, align)+ |
|
452 " %s_CalCount++;\n"%(nodename)+ |
|
453 " align_tick(-1);\n"+ |
|
454 " }else{\n"+ |
|
455 " align_tick(%d);\n"%(align_ratio)+ |
|
456 " }\n"+ |
|
457 "}\n") |
|
458 format_dict["post_sync_register"] += ( |
|
459 "%s_Data.post_sync = %s_post_sync;\n"%(nodename,nodename)) |
|
460 format_dict["nodes_init"] += 'NODE_SLAVE_INIT(%s, %s)\n '%( |
|
461 nodename, |
|
462 child_data.getNodeId()) |
|
463 |
|
464 # Include generated OD headers |
|
465 format_dict["nodes_includes"] += '#include "%s.h"\n'%(nodename) |
|
466 # Declare CAN channels according user filled config |
|
467 format_dict["board_decls"] += 'BOARD_DECL(%s, "%s", "%s")\n'%( |
|
468 nodename, |
|
469 child.GetCanDevice(), |
|
470 child_data.getCAN_Baudrate()) |
|
471 format_dict["nodes_open"] += 'NODE_OPEN(%s)\n '%(nodename) |
|
472 format_dict["nodes_close"] += 'NODE_CLOSE(%s)\n '%(nodename) |
|
473 format_dict["nodes_stop"] += 'NODE_STOP(%s)\n '%(nodename) |
|
474 |
|
475 filename = os.path.join(os.path.split(__file__)[0],"cf_runtime.c") |
|
476 cf_main = open(filename).read() % format_dict |
|
477 cf_main_path = os.path.join(buildpath, "CF_%(locstr)s.c"%format_dict) |
|
478 f = open(cf_main_path,'w') |
|
479 f.write(cf_main) |
|
480 f.close() |
|
481 |
|
482 return [(cf_main_path, local_canfestival_config.getCFLAGS(CanFestivalPath))],local_canfestival_config.getLDFLAGS(CanFestivalPath), True |
|
483 |
|
484 |
|