Merged
authorLaurent Bessard
Thu, 11 Apr 2013 10:32:58 +0200
changeset 2120 8e6e6364087e
parent 2119 5460eba6c5e0 (current diff)
parent 2117 accc4cbca8d3 (diff)
child 2121 11102245e1d4
Merged
--- a/etherlab/EthercatCFileGenerator.py	Fri Apr 05 00:14:00 2013 +0200
+++ b/etherlab/EthercatCFileGenerator.py	Thu Apr 11 10:32:58 2013 +0200
@@ -25,13 +25,13 @@
 
 SLAVE_CONFIGURATION_TEMPLATE = """
     if (!(slave%(slave)d = ecrt_master_slave_config(master, %(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x))) {
-        SLOGF(LOG_CRITICAL, "Failed to get slave %(device_type)s configuration at alias %(alias)d and position %(position)d.\\n");
+        SLOGF(LOG_CRITICAL, "EtherCAT failed to get slave %(device_type)s configuration at alias %(alias)d and position %(position)d.");
         return -1;
     }
 
     if (ecrt_slave_config_pdos(slave%(slave)d, EC_END, slave_%(slave)d_syncs)) {
-        SLOGF(LOG_CRITICAL, "Failed to configure PDOs for slave %(device_type)s at alias %(alias)d and position %(position)d.\\n");
-        return -1;
+        SLOGF(LOG_CRITICAL, "EtherCAT failed to configure PDOs for slave %(device_type)s at alias %(alias)d and position %(position)d.");
+        goto ecat_failed;
     }
 """
 
@@ -40,8 +40,8 @@
         uint8_t value[%(data_size)d];
         EC_WRITE_%(data_type)s((uint8_t *)value, %(data)s);
         if (ecrt_master_sdo_download(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &abort_code)) {
-            SLOGF(LOG_CRITICAL, "Failed to initialize slave %(device_type)s at alias %(alias)d and position %(position)d.\\nError: %%d\\n", abort_code);
-            return -1;
+            SLOGF(LOG_CRITICAL, "EtherCAT Failed to initialize slave %(device_type)s at alias %(alias)d and position %(position)d. Error: %%d", abort_code);
+            goto ecat_failed;
         }
     }
 """
@@ -50,8 +50,8 @@
     {
         uint8_t value[%(data_size)d];
         if (ecrt_master_sdo_upload(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &result_size, &abort_code)) {
-            SLOGF(LOG_CRITICAL, "Failed to get default value for output PDO in slave %(device_type)s at alias %(alias)d and position %(position)d.\\nError: %%ud\\n", abort_code);
-            return -1;
+            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);
+            goto ecat_failed;
         }
         %(real_var)s = EC_READ_%(data_type)s((uint8_t *)value);
     }
--- a/etherlab/plc_etherlab.c	Fri Apr 05 00:14:00 2013 +0200
+++ b/etherlab/plc_etherlab.c	Thu Apr 11 10:32:58 2013 +0200
@@ -47,21 +47,26 @@
     uint32_t abort_code;
     size_t result_size;
     
-	master = ecrt_request_master(%(master_number)d);
-	if (!master) return -1;
+    master = ecrt_request_master(%(master_number)d);
+    if (!master) {
+        SLOGF(LOG_CRITICAL, "EtherCAT master request failed!");
+        return -1;
+    }
 
-	domain1 = ecrt_master_create_domain(master);
-	if (!domain1) return -1;
+    if(!(domain1 = ecrt_master_create_domain(master))){
+        SLOGF(LOG_CRITICAL, "EtherCAT Domain Creation failed!");
+        goto ecat_failed;
+    }
 
     // slaves PDO configuration
 %(slaves_configuration)s
 
     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
-        SLOGF(LOG_CRITICAL, "PDO entry registration failed!\n");
-        return -1;
+        SLOGF(LOG_CRITICAL, "EtherCAT PDO registration failed!");
+        goto ecat_failed;
     }
 
-	ecrt_master_set_send_interval(master, common_ticktime__);
+    ecrt_master_set_send_interval(master, common_ticktime__);
 
     // slaves initialization
 %(slaves_initialization)s
@@ -69,25 +74,32 @@
     // extracting default value for not mapped entry in output PDOs
 %(slaves_output_pdos_default_values_extraction)s
 
-    if (ecrt_master_activate(master))
-        return -1;
+    if (ecrt_master_activate(master)){
+        SLOGF(LOG_CRITICAL, "EtherCAT Master activation failed");
+        goto ecat_failed;
+    }
 
     if (!(domain1_pd = ecrt_domain_data(domain1))) {
-        SLOGF(LOG_CRITICAL, "domain1_pd:  0x%%.6lx\n", (unsigned long)domain1_pd);
-        return -1;
+        SLOGF(LOG_CRITICAL, "Failed to map EtherCAT process data");
+        goto ecat_failed;
     }
 
-    SLOGF(LOG_INFO, "Master %(master_number)d activated...\n");
+    SLOGF(LOG_INFO, "Master %(master_number)d activated.");
     
     first_sent = 0;
 
     return 0;
+
+ecat_failed:
+    ecrt_release_master(master);
+    return -1;
+
 }
 
 void __cleanup_%(location)s(void)
 {
-	//release master
-	ecrt_release_master(master);
+    //release master
+    ecrt_release_master(master);
     first_sent = 0;
 }
 
--- a/etherlab/runtime_etherlab.py	Fri Apr 05 00:14:00 2013 +0200
+++ b/etherlab/runtime_etherlab.py	Thu Apr 11 10:32:58 2013 +0200
@@ -1,6 +1,7 @@
 import subprocess,sys,ctypes
 from threading import Thread
 import ctypes,time,re
+from targets.typemapping import LogLevelsDict
 
 SDOAnswered = PLCBinary.SDOAnswered
 SDOAnswered.restype = None
@@ -12,11 +13,12 @@
 def SDOThreadProc(*params):
     global Result
     if params[0] == "upload":
-        command = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
+        cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
     else:
-        command = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
+        cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
     
-    proc = subprocess.Popen(command % params[1:], stdout=subprocess.PIPE, shell=True)
+    command = cmdfmt % params[1:]
+    proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
     res = proc.wait()
     output = proc.communicate()[0]
     
@@ -35,6 +37,10 @@
         Result = res == 0
     
     SDOAnswered()
+    if res != 0 :
+        PLCObject.LogMessage(
+            LogLevelsDict["WARNING"], 
+            "%s : %s"%(command,output))
     
 def EthercatSDOUpload(pos, index, subindex, var_type):
     global SDOThread
@@ -73,9 +79,14 @@
             log = log.rpartition(last)[2]
         if log : 
             last = log.rpartition('\n')[2]
-            for msg in re.findall(r'<\d>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
-                                  log, re.MULTILINE):
-                PLCObject.LogMessage(msg)
+            for lvl,msg in re.findall(
+                            r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
+                            log, re.MULTILINE):
+                PLCObject.LogMessage(
+                    LogLevelsDict[{
+                        "4":"WARNING",
+                        "3":"CRITICAL"}.get(lvl,"DEBUG")],
+                    msg)
         time.sleep(0.5) 
 
 def _runtime_etherlab_init():
@@ -89,4 +100,3 @@
     StopKMSGThread = True
     KMSGPollThread = None
 
-