|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 # This file is part of Beremiz |
|
5 # |
|
6 # Copyright (C) 2011-2014: Laurent BESSARD, Edouard TISSERANT |
|
7 # RTES Lab : CRKim, JBLee, youcu |
|
8 # Higen Motor : Donggu Kang |
|
9 # |
|
10 # See COPYING file for copyrights details. |
|
11 |
|
12 import os |
|
13 |
|
14 from EthercatSlave import ExtractHexDecValue, DATATYPECONVERSION, ExtractName |
|
15 |
|
16 SLAVE_PDOS_CONFIGURATION_DECLARATION = """ |
|
17 /* Slave %(slave)d, "%(device_type)s" |
|
18 * Vendor ID: 0x%(vendor).8x |
|
19 * Product code: 0x%(product_code).8x |
|
20 * Revision number: 0x%(revision_number).8x |
|
21 */ |
|
22 |
|
23 ec_pdo_entry_info_t slave_%(slave)d_pdo_entries[] = { |
|
24 %(pdos_entries_infos)s |
|
25 }; |
|
26 |
|
27 ec_pdo_info_t slave_%(slave)d_pdos[] = { |
|
28 %(pdos_infos)s |
|
29 }; |
|
30 |
|
31 ec_sync_info_t slave_%(slave)d_syncs[] = { |
|
32 %(pdos_sync_infos)s |
|
33 {0xff} |
|
34 }; |
|
35 """ |
|
36 |
|
37 SLAVE_CONFIGURATION_TEMPLATE = """ |
|
38 if (!(slave%(slave)d = ecrt_master_slave_config(master, %(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x))) { |
|
39 SLOGF(LOG_CRITICAL, "EtherCAT failed to get slave %(device_type)s configuration at alias %(alias)d and position %(position)d."); |
|
40 goto ecat_failed; |
|
41 } |
|
42 |
|
43 if (ecrt_slave_config_pdos(slave%(slave)d, EC_END, slave_%(slave)d_syncs)) { |
|
44 SLOGF(LOG_CRITICAL, "EtherCAT failed to configure PDOs for slave %(device_type)s at alias %(alias)d and position %(position)d."); |
|
45 goto ecat_failed; |
|
46 } |
|
47 """ |
|
48 |
|
49 SLAVE_INITIALIZATION_TEMPLATE = """ |
|
50 { |
|
51 uint8_t value[%(data_size)d]; |
|
52 EC_WRITE_%(data_type)s((uint8_t *)value, %(data)s); |
|
53 if (ecrt_master_sdo_download(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &abort_code)) { |
|
54 SLOGF(LOG_CRITICAL, "EtherCAT Failed to initialize slave %(device_type)s at alias %(alias)d and position %(position)d. Error: %%d", abort_code); |
|
55 goto ecat_failed; |
|
56 } |
|
57 } |
|
58 """ |
|
59 |
|
60 SLAVE_OUTPUT_PDO_DEFAULT_VALUE = """ |
|
61 { |
|
62 uint8_t value[%(data_size)d]; |
|
63 if (ecrt_master_sdo_upload(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &result_size, &abort_code)) { |
|
64 SLOGF(LOG_CRITICAL, "EtherCAT failed to get default value for output PDO in slave %(device_type)s at alias %(alias)d and position %(position)d. Error: %%ud", abort_code); |
|
65 goto ecat_failed; |
|
66 } |
|
67 %(real_var)s = EC_READ_%(data_type)s((uint8_t *)value); |
|
68 } |
|
69 """ |
|
70 |
|
71 def ConfigureVariable(entry_infos, str_completion): |
|
72 entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None) |
|
73 if entry_infos["data_type"] is None: |
|
74 raise ValueError, _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"] |
|
75 |
|
76 if not entry_infos.get("no_decl", False): |
|
77 if entry_infos.has_key("real_var"): |
|
78 str_completion["located_variables_declaration"].append( |
|
79 "IEC_%(var_type)s %(real_var)s;" % entry_infos) |
|
80 else: |
|
81 entry_infos["real_var"] = "beremiz" + entry_infos["var_name"] |
|
82 str_completion["located_variables_declaration"].extend( |
|
83 ["IEC_%(var_type)s %(real_var)s;" % entry_infos, |
|
84 "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos]) |
|
85 for declaration in entry_infos.get("extra_declarations", []): |
|
86 entry_infos["extra_decl"] = declaration |
|
87 str_completion["located_variables_declaration"].append( |
|
88 "IEC_%(var_type)s *%(extra_decl)s = &%(real_var)s;" % entry_infos) |
|
89 elif not entry_infos.has_key("real_var"): |
|
90 entry_infos["real_var"] = "beremiz" + entry_infos["var_name"] |
|
91 |
|
92 str_completion["used_pdo_entry_offset_variables_declaration"].append( |
|
93 "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos) |
|
94 |
|
95 if entry_infos["data_type"] == "BIT": |
|
96 str_completion["used_pdo_entry_offset_variables_declaration"].append( |
|
97 "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x_bit;" % entry_infos) |
|
98 |
|
99 str_completion["used_pdo_entry_configuration"].append( |
|
100 (" {%(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x, " + |
|
101 "0x%(index).4x, %(subindex)d, &slave%(slave)d_%(index).4x_%(subindex).2x, " + |
|
102 "&slave%(slave)d_%(index).4x_%(subindex).2x_bit},") % entry_infos) |
|
103 |
|
104 if entry_infos["dir"] == "I": |
|
105 str_completion["retrieve_variables"].append( |
|
106 (" %(real_var)s = EC_READ_BIT(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + |
|
107 "slave%(slave)d_%(index).4x_%(subindex).2x_bit);") % entry_infos) |
|
108 elif entry_infos["dir"] == "Q": |
|
109 str_completion["publish_variables"].append( |
|
110 (" EC_WRITE_BIT(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + |
|
111 "slave%(slave)d_%(index).4x_%(subindex).2x_bit, %(real_var)s);") % entry_infos) |
|
112 |
|
113 else: |
|
114 str_completion["used_pdo_entry_configuration"].append( |
|
115 (" {%(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x, 0x%(index).4x, " + |
|
116 "%(subindex)d, &slave%(slave)d_%(index).4x_%(subindex).2x},") % entry_infos) |
|
117 |
|
118 if entry_infos["dir"] == "I": |
|
119 str_completion["retrieve_variables"].append( |
|
120 (" %(real_var)s = EC_READ_%(data_type)s(domain1_pd + " + |
|
121 "slave%(slave)d_%(index).4x_%(subindex).2x);") % entry_infos) |
|
122 elif entry_infos["dir"] == "Q": |
|
123 str_completion["publish_variables"].append( |
|
124 (" EC_WRITE_%(data_type)s(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + |
|
125 "%(real_var)s);") % entry_infos) |
|
126 |
|
127 def ExclusionSortFunction(x, y): |
|
128 if x["matching"] == y["matching"]: |
|
129 if x["assigned"] and not y["assigned"]: |
|
130 return -1 |
|
131 elif not x["assigned"] and y["assigned"]: |
|
132 return 1 |
|
133 return cmp(x["count"], y["count"]) |
|
134 return -cmp(x["matching"], y["matching"]) |
|
135 |
|
136 class _EthercatCFileGenerator: |
|
137 |
|
138 def __init__(self, controler): |
|
139 self.Controler = controler |
|
140 |
|
141 self.Slaves = [] |
|
142 self.UsedVariables = {} |
|
143 |
|
144 def __del__(self): |
|
145 self.Controler = None |
|
146 |
|
147 def DeclareSlave(self, slave_index, slave): |
|
148 self.Slaves.append((slave_index, slave.getInfo().getAutoIncAddr(), slave)) |
|
149 |
|
150 def DeclareVariable(self, slave_index, index, subindex, iec_type, dir, name, no_decl=False): |
|
151 slave_variables = self.UsedVariables.setdefault(slave_index, {}) |
|
152 |
|
153 entry_infos = slave_variables.get((index, subindex), None) |
|
154 if entry_infos is None: |
|
155 slave_variables[(index, subindex)] = { |
|
156 "infos": (iec_type, dir, name, no_decl, []), |
|
157 "mapped": False} |
|
158 return name |
|
159 elif entry_infos["infos"][:2] == (iec_type, dir): |
|
160 if name != entry_infos["infos"][2]: |
|
161 if dir == "I": |
|
162 entry_infos["infos"][4].append(name) |
|
163 return entry_infos["infos"][2] |
|
164 else: |
|
165 raise ValueError, _("Output variables can't be defined with different locations (%s and %s)") % (entry_infos["infos"][2], name) |
|
166 else: |
|
167 raise ValueError, _("Definition conflict for location \"%s\"") % name |
|
168 |
|
169 def GenerateCFile(self, filepath, location_str, master_number): |
|
170 |
|
171 # Extract etherlab master code template |
|
172 plc_etherlab_filepath = os.path.join(os.path.split(__file__)[0], "plc_etherlab.c") |
|
173 plc_etherlab_file = open(plc_etherlab_filepath, 'r') |
|
174 plc_etherlab_code = plc_etherlab_file.read() |
|
175 plc_etherlab_file.close() |
|
176 |
|
177 # Initialize strings for formatting master code template |
|
178 str_completion = { |
|
179 "location": location_str, |
|
180 "master_number": master_number, |
|
181 "located_variables_declaration": [], |
|
182 "used_pdo_entry_offset_variables_declaration": [], |
|
183 "used_pdo_entry_configuration": [], |
|
184 "pdos_configuration_declaration": "", |
|
185 "slaves_declaration": "", |
|
186 "slaves_configuration": "", |
|
187 "slaves_output_pdos_default_values_extraction": "", |
|
188 "slaves_initialization": "", |
|
189 "retrieve_variables": [], |
|
190 "publish_variables": [], |
|
191 } |
|
192 |
|
193 # Initialize variable storing variable mapping state |
|
194 for slave_entries in self.UsedVariables.itervalues(): |
|
195 for entry_infos in slave_entries.itervalues(): |
|
196 entry_infos["mapped"] = False |
|
197 |
|
198 # Sort slaves by position (IEC_Channel) |
|
199 self.Slaves.sort() |
|
200 # Initialize dictionary storing alias auto-increment position values |
|
201 alias = {} |
|
202 |
|
203 # Generating code for each slave |
|
204 for (slave_idx, slave_alias, slave) in self.Slaves: |
|
205 type_infos = slave.getType() |
|
206 |
|
207 # Defining slave alias and auto-increment position |
|
208 if alias.get(slave_alias) is not None: |
|
209 alias[slave_alias] += 1 |
|
210 else: |
|
211 alias[slave_alias] = 0 |
|
212 slave_pos = (slave_alias, alias[slave_alias]) |
|
213 |
|
214 # Extract slave device informations |
|
215 device, module_extra_params = self.Controler.GetModuleInfos(type_infos) |
|
216 if device is None: |
|
217 raise ValueError, _("No informations found for device %s!") % (type_infos["device_type"]) |
|
218 |
|
219 # Extract slaves variables to be mapped |
|
220 slave_variables = self.UsedVariables.get(slave_idx, {}) |
|
221 |
|
222 # Extract slave device object dictionary entries |
|
223 device_entries = device.GetEntriesList() |
|
224 |
|
225 # Adding code for declaring slave in master code template strings |
|
226 for element in ["vendor", "product_code", "revision_number"]: |
|
227 type_infos[element] = ExtractHexDecValue(type_infos[element]) |
|
228 type_infos.update(dict(zip(["slave", "alias", "position"], (slave_idx,) + slave_pos))) |
|
229 |
|
230 # Extract slave device CoE informations |
|
231 device_coe = device.getCoE() |
|
232 if device_coe is not None: |
|
233 |
|
234 # If device support CanOpen over Ethernet, adding code for calling |
|
235 # init commands when initializing slave in master code template strings |
|
236 initCmds = [] |
|
237 for initCmd in device_coe.getInitCmd(): |
|
238 initCmds.append({ |
|
239 "Index": ExtractHexDecValue(initCmd.getIndex()), |
|
240 "Subindex": ExtractHexDecValue(initCmd.getSubIndex()), |
|
241 "Value": initCmd.getData().getcontent()}) |
|
242 initCmds.extend(slave.getStartupCommands()) |
|
243 for initCmd in initCmds: |
|
244 index = initCmd["Index"] |
|
245 subindex = initCmd["Subindex"] |
|
246 entry = device_entries.get((index, subindex), None) |
|
247 if entry is not None: |
|
248 data_size = entry["BitSize"] / 8 |
|
249 data_str = ("0x%%.%dx" % (data_size * 2)) % initCmd["Value"] |
|
250 init_cmd_infos = { |
|
251 "index": index, |
|
252 "subindex": subindex, |
|
253 "data": data_str, |
|
254 "data_type": DATATYPECONVERSION.get(entry["Type"]), |
|
255 "data_size": data_size |
|
256 } |
|
257 init_cmd_infos.update(type_infos) |
|
258 str_completion["slaves_initialization"] += SLAVE_INITIALIZATION_TEMPLATE % init_cmd_infos |
|
259 |
|
260 # Extract slave device PDO configuration capabilities |
|
261 PdoAssign = device_coe.getPdoAssign() |
|
262 PdoConfig = device_coe.getPdoConfig() |
|
263 else: |
|
264 PdoAssign = PdoConfig = False |
|
265 |
|
266 # Test if slave has a configuration or need one |
|
267 if len(device.getTxPdo() + device.getRxPdo()) > 0 or len(slave_variables) > 0 and PdoConfig and PdoAssign: |
|
268 |
|
269 str_completion["slaves_declaration"] += "static ec_slave_config_t *slave%(slave)d = NULL;\n" % type_infos |
|
270 str_completion["slaves_configuration"] += SLAVE_CONFIGURATION_TEMPLATE % type_infos |
|
271 |
|
272 # Initializing |
|
273 pdos_infos = { |
|
274 "pdos_entries_infos": [], |
|
275 "pdos_infos": [], |
|
276 "pdos_sync_infos": [], |
|
277 } |
|
278 pdos_infos.update(type_infos) |
|
279 |
|
280 sync_managers = [] |
|
281 for sync_manager_idx, sync_manager in enumerate(device.getSm()): |
|
282 sync_manager_infos = { |
|
283 "index": sync_manager_idx, |
|
284 "name": sync_manager.getcontent(), |
|
285 "slave": slave_idx, |
|
286 "pdos": [], |
|
287 "pdos_number": 0, |
|
288 } |
|
289 |
|
290 sync_manager_control_byte = ExtractHexDecValue(sync_manager.getControlByte()) |
|
291 sync_manager_direction = sync_manager_control_byte & 0x0c |
|
292 sync_manager_watchdog = sync_manager_control_byte & 0x40 |
|
293 if sync_manager_direction: |
|
294 sync_manager_infos["sync_manager_type"] = "EC_DIR_OUTPUT" |
|
295 else: |
|
296 sync_manager_infos["sync_manager_type"] = "EC_DIR_INPUT" |
|
297 if sync_manager_watchdog: |
|
298 sync_manager_infos["watchdog"] = "EC_WD_ENABLE" |
|
299 else: |
|
300 sync_manager_infos["watchdog"] = "EC_WD_DISABLE" |
|
301 |
|
302 sync_managers.append(sync_manager_infos) |
|
303 |
|
304 pdos_index = [] |
|
305 exclusive_pdos = {} |
|
306 selected_pdos = [] |
|
307 for pdo, pdo_type in ([(pdo, "Inputs") for pdo in device.getTxPdo()] + |
|
308 [(pdo, "Outputs") for pdo in device.getRxPdo()]): |
|
309 |
|
310 pdo_index = ExtractHexDecValue(pdo.getIndex().getcontent()) |
|
311 pdos_index.append(pdo_index) |
|
312 |
|
313 excluded_list = pdo.getExclude() |
|
314 if len(excluded_list) > 0: |
|
315 exclusion_list = [pdo_index] |
|
316 for excluded in excluded_list: |
|
317 exclusion_list.append(ExtractHexDecValue(excluded.getcontent())) |
|
318 exclusion_list.sort() |
|
319 |
|
320 exclusion_scope = exclusive_pdos.setdefault(tuple(exclusion_list), []) |
|
321 |
|
322 entries = pdo.getEntry() |
|
323 pdo_mapping_match = { |
|
324 "index": pdo_index, |
|
325 "matching": 0, |
|
326 "count": len(entries), |
|
327 "assigned": pdo.getSm() is not None |
|
328 } |
|
329 exclusion_scope.append(pdo_mapping_match) |
|
330 |
|
331 for entry in entries: |
|
332 index = ExtractHexDecValue(entry.getIndex().getcontent()) |
|
333 subindex = ExtractHexDecValue(entry.getSubIndex()) |
|
334 if slave_variables.get((index, subindex), None) is not None: |
|
335 pdo_mapping_match["matching"] += 1 |
|
336 |
|
337 if pdo.getFixed() != True: |
|
338 pdo_mapping_match["matching"] += \ |
|
339 module_extra_params["max_pdo_size"] - \ |
|
340 pdo_mapping_match["count"] |
|
341 |
|
342 elif pdo.getMandatory(): |
|
343 selected_pdos.append(pdo_index) |
|
344 |
|
345 excluded_pdos = [] |
|
346 for exclusion_scope in exclusive_pdos.itervalues(): |
|
347 exclusion_scope.sort(ExclusionSortFunction) |
|
348 start_excluding_index = 0 |
|
349 if exclusion_scope[0]["matching"] > 0: |
|
350 selected_pdos.append(exclusion_scope[0]["index"]) |
|
351 start_excluding_index = 1 |
|
352 excluded_pdos.extend([pdo["index"] |
|
353 for pdo in exclusion_scope[start_excluding_index:] |
|
354 if PdoAssign or not pdo["assigned"]]) |
|
355 |
|
356 for pdo, pdo_type in ([(pdo, "Inputs") for pdo in device.getTxPdo()] + |
|
357 [(pdo, "Outputs") for pdo in device.getRxPdo()]): |
|
358 entries = pdo.getEntry() |
|
359 |
|
360 pdo_index = ExtractHexDecValue(pdo.getIndex().getcontent()) |
|
361 if pdo_index in excluded_pdos: |
|
362 continue |
|
363 |
|
364 pdo_needed = pdo_index in selected_pdos |
|
365 |
|
366 entries_infos = [] |
|
367 |
|
368 for entry in entries: |
|
369 index = ExtractHexDecValue(entry.getIndex().getcontent()) |
|
370 subindex = ExtractHexDecValue(entry.getSubIndex()) |
|
371 entry_infos = { |
|
372 "index": index, |
|
373 "subindex": subindex, |
|
374 "name": ExtractName(entry.getName()), |
|
375 "bitlen": entry.getBitLen(), |
|
376 } |
|
377 entry_infos.update(type_infos) |
|
378 entries_infos.append(" {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos) |
|
379 |
|
380 entry_declaration = slave_variables.get((index, subindex), None) |
|
381 if entry_declaration is not None and not entry_declaration["mapped"]: |
|
382 pdo_needed = True |
|
383 |
|
384 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "no_decl", "extra_declarations"], |
|
385 entry_declaration["infos"]))) |
|
386 entry_declaration["mapped"] = True |
|
387 |
|
388 entry_type = entry.getDataType().getcontent() |
|
389 if entry_infos["var_type"] != entry_type: |
|
390 message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"] |
|
391 if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != |
|
392 self.Controler.GetSizeOfType(entry_type)): |
|
393 raise ValueError, message |
|
394 else: |
|
395 self.Controler.GetCTRoot().logger.write_warning(_("Warning: ") + message + "\n") |
|
396 |
|
397 if (entry_infos["dir"] == "I" and pdo_type != "Inputs" or |
|
398 entry_infos["dir"] == "Q" and pdo_type != "Outputs"): |
|
399 raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"] |
|
400 |
|
401 ConfigureVariable(entry_infos, str_completion) |
|
402 |
|
403 elif pdo_type == "Outputs" and entry.getDataType() is not None and device_coe is not None: |
|
404 data_type = entry.getDataType().getcontent() |
|
405 entry_infos["dir"] = "Q" |
|
406 entry_infos["data_size"] = max(1, entry_infos["bitlen"] / 8) |
|
407 entry_infos["data_type"] = DATATYPECONVERSION.get(data_type) |
|
408 entry_infos["var_type"] = data_type |
|
409 entry_infos["real_var"] = "slave%(slave)d_%(index).4x_%(subindex).2x_default" % entry_infos |
|
410 |
|
411 ConfigureVariable(entry_infos, str_completion) |
|
412 |
|
413 str_completion["slaves_output_pdos_default_values_extraction"] += \ |
|
414 SLAVE_OUTPUT_PDO_DEFAULT_VALUE % entry_infos |
|
415 |
|
416 if pdo_needed: |
|
417 for excluded in pdo.getExclude(): |
|
418 excluded_index = ExtractHexDecValue(excluded.getcontent()) |
|
419 if excluded_index not in excluded_pdos: |
|
420 excluded_pdos.append(excluded_index) |
|
421 |
|
422 sm = pdo.getSm() |
|
423 if sm is None: |
|
424 for sm_idx, sync_manager in enumerate(sync_managers): |
|
425 if sync_manager["name"] == pdo_type: |
|
426 sm = sm_idx |
|
427 if sm is None: |
|
428 raise ValueError, _("No sync manager available for %s pdo!") % pdo_type |
|
429 |
|
430 sync_managers[sm]["pdos_number"] += 1 |
|
431 sync_managers[sm]["pdos"].append( |
|
432 {"slave": slave_idx, |
|
433 "index": pdo_index, |
|
434 "name": ExtractName(pdo.getName()), |
|
435 "type": pdo_type, |
|
436 "entries": entries_infos, |
|
437 "entries_number": len(entries_infos), |
|
438 "fixed": pdo.getFixed() == True}) |
|
439 |
|
440 if PdoConfig and PdoAssign: |
|
441 dynamic_pdos = {} |
|
442 dynamic_pdos_number = 0 |
|
443 for category, min_index, max_index in [("Inputs", 0x1600, 0x1800), |
|
444 ("Outputs", 0x1a00, 0x1C00)]: |
|
445 for sync_manager in sync_managers: |
|
446 if sync_manager["name"] == category: |
|
447 category_infos = dynamic_pdos.setdefault(category, {}) |
|
448 category_infos["sync_manager"] = sync_manager |
|
449 category_infos["pdos"] = [pdo for pdo in category_infos["sync_manager"]["pdos"] |
|
450 if not pdo["fixed"] and pdo["type"] == category] |
|
451 category_infos["current_index"] = min_index |
|
452 category_infos["max_index"] = max_index |
|
453 break |
|
454 |
|
455 for (index, subindex), entry_declaration in slave_variables.iteritems(): |
|
456 |
|
457 if not entry_declaration["mapped"]: |
|
458 entry = device_entries.get((index, subindex), None) |
|
459 if entry is None: |
|
460 raise ValueError, _("Unknown entry index 0x%4.4x, subindex 0x%2.2x for device %s") % \ |
|
461 (index, subindex, type_infos["device_type"]) |
|
462 |
|
463 entry_infos = { |
|
464 "index": index, |
|
465 "subindex": subindex, |
|
466 "name": entry["Name"], |
|
467 "bitlen": entry["BitSize"], |
|
468 } |
|
469 entry_infos.update(type_infos) |
|
470 |
|
471 entry_infos.update(dict(zip(["var_type", "dir", "var_name", "no_decl", "extra_declarations"], |
|
472 entry_declaration["infos"]))) |
|
473 entry_declaration["mapped"] = True |
|
474 |
|
475 if entry_infos["var_type"] != entry["Type"]: |
|
476 message = _("Wrong type for location \"%s\"!") % entry_infos["var_name"] |
|
477 if (self.Controler.GetSizeOfType(entry_infos["var_type"]) != |
|
478 self.Controler.GetSizeOfType(entry["Type"])): |
|
479 raise ValueError, message |
|
480 else: |
|
481 self.Controler.GetCTRoot().logger.write_warning(message + "\n") |
|
482 |
|
483 if entry_infos["dir"] == "I" and entry["PDOMapping"] in ["T", "RT"]: |
|
484 pdo_type = "Inputs" |
|
485 elif entry_infos["dir"] == "Q" and entry["PDOMapping"] in ["R", "RT"]: |
|
486 pdo_type = "Outputs" |
|
487 else: |
|
488 raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"] |
|
489 |
|
490 if not dynamic_pdos.has_key(pdo_type): |
|
491 raise ValueError, _("No Sync manager defined for %s!") % pdo_type |
|
492 |
|
493 ConfigureVariable(entry_infos, str_completion) |
|
494 |
|
495 if len(dynamic_pdos[pdo_type]["pdos"]) > 0: |
|
496 pdo = dynamic_pdos[pdo_type]["pdos"][0] |
|
497 elif module_extra_params["add_pdo"]: |
|
498 while dynamic_pdos[pdo_type]["current_index"] in pdos_index: |
|
499 dynamic_pdos[pdo_type]["current_index"] += 1 |
|
500 if dynamic_pdos[pdo_type]["current_index"] >= dynamic_pdos[pdo_type]["max_index"]: |
|
501 raise ValueError, _("No more free PDO index available for %s!") % pdo_type |
|
502 pdos_index.append(dynamic_pdos[pdo_type]["current_index"]) |
|
503 |
|
504 dynamic_pdos_number += 1 |
|
505 pdo = {"slave": slave_idx, |
|
506 "index": dynamic_pdos[pdo_type]["current_index"], |
|
507 "name": "Dynamic PDO %d" % dynamic_pdos_number, |
|
508 "type": pdo_type, |
|
509 "entries": [], |
|
510 "entries_number": 0, |
|
511 "fixed": False} |
|
512 dynamic_pdos[pdo_type]["sync_manager"]["pdos_number"] += 1 |
|
513 dynamic_pdos[pdo_type]["sync_manager"]["pdos"].append(pdo) |
|
514 dynamic_pdos[pdo_type]["pdos"].append(pdo) |
|
515 else: |
|
516 break |
|
517 |
|
518 pdo["entries"].append(" {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos) |
|
519 if entry_infos["bitlen"] < module_extra_params["pdo_alignment"]: |
|
520 pdo["entries"].append(" {0x0000, 0x00, %d}, /* None */" % ( |
|
521 module_extra_params["pdo_alignment"] - entry_infos["bitlen"])) |
|
522 pdo["entries_number"] += 1 |
|
523 |
|
524 if pdo["entries_number"] == module_extra_params["max_pdo_size"]: |
|
525 dynamic_pdos[pdo_type]["pdos"].pop(0) |
|
526 |
|
527 pdo_offset = 0 |
|
528 entry_offset = 0 |
|
529 for sync_manager_infos in sync_managers: |
|
530 |
|
531 for pdo_infos in sync_manager_infos["pdos"]: |
|
532 pdo_infos["offset"] = entry_offset |
|
533 pdo_entries = pdo_infos["entries"] |
|
534 pdos_infos["pdos_infos"].append( |
|
535 (" {0x%(index).4x, %(entries_number)d, " + |
|
536 "slave_%(slave)d_pdo_entries + %(offset)d}, /* %(name)s */") % pdo_infos) |
|
537 entry_offset += len(pdo_entries) |
|
538 pdos_infos["pdos_entries_infos"].extend(pdo_entries) |
|
539 |
|
540 sync_manager_infos["offset"] = pdo_offset |
|
541 pdo_offset_shift = sync_manager_infos["pdos_number"] |
|
542 pdos_infos["pdos_sync_infos"].append( |
|
543 (" {%(index)d, %(sync_manager_type)s, %(pdos_number)d, " + |
|
544 ("slave_%(slave)d_pdos + %(offset)d" if pdo_offset_shift else "NULL") + |
|
545 ", %(watchdog)s},") % sync_manager_infos) |
|
546 pdo_offset += pdo_offset_shift |
|
547 |
|
548 for element in ["pdos_entries_infos", "pdos_infos", "pdos_sync_infos"]: |
|
549 pdos_infos[element] = "\n".join(pdos_infos[element]) |
|
550 |
|
551 str_completion["pdos_configuration_declaration"] += SLAVE_PDOS_CONFIGURATION_DECLARATION % pdos_infos |
|
552 |
|
553 for (index, subindex), entry_declaration in slave_variables.iteritems(): |
|
554 if not entry_declaration["mapped"]: |
|
555 message = _("Entry index 0x%4.4x, subindex 0x%2.2x not mapped for device %s") % \ |
|
556 (index, subindex, type_infos["device_type"]) |
|
557 self.Controler.GetCTRoot().logger.write_warning(_("Warning: ") + message + "\n") |
|
558 |
|
559 for element in ["used_pdo_entry_offset_variables_declaration", |
|
560 "used_pdo_entry_configuration", |
|
561 "located_variables_declaration", |
|
562 "retrieve_variables", |
|
563 "publish_variables"]: |
|
564 str_completion[element] = "\n".join(str_completion[element]) |
|
565 |
|
566 etherlabfile = open(filepath, 'w') |
|
567 etherlabfile.write(plc_etherlab_code % str_completion) |
|
568 etherlabfile.close() |