Better logging, saffer failure at init (allows restart)
authorEdouard Tisserant
Fri, 05 Apr 2013 16:49:41 +0900
changeset 2116 2b1980a038b1
parent 2115 edb49073227e
child 2117 accc4cbca8d3
Better logging, saffer failure at init (allows restart)
etherlab/EthercatCFileGenerator.py
etherlab/plc_etherlab.c
etherlab/runtime_etherlab.py
--- a/etherlab/EthercatCFileGenerator.py	Fri Apr 05 11:55:09 2013 +0900
+++ b/etherlab/EthercatCFileGenerator.py	Fri Apr 05 16:49:41 2013 +0900
@@ -25,12 +25,12 @@
 
 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");
+        SLOGF(LOG_CRITICAL, "EtherCAT failed to configure PDOs for slave %(device_type)s at alias %(alias)d and position %(position)d.");
         return -1;
     }
 """
@@ -40,7 +40,7 @@
         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);
+            SLOGF(LOG_CRITICAL, "EtherCAT Failed to initialize slave %(device_type)s at alias %(alias)d and position %(position)d. Error: %%d", abort_code);
             return -1;
         }
     }
@@ -50,7 +50,7 @@
     {
         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);
+            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);
             return -1;
         }
         %(real_var)s = EC_READ_%(data_type)s((uint8_t *)value);
--- a/etherlab/plc_etherlab.c	Fri Apr 05 11:55:09 2013 +0900
+++ b/etherlab/plc_etherlab.c	Fri Apr 05 16:49:41 2013 +0900
@@ -48,17 +48,22 @@
     size_t result_size;
     
 	master = ecrt_request_master(%(master_number)d);
-	if (!master) return -1;
+	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__);
@@ -69,19 +74,26 @@
     // 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)
--- a/etherlab/runtime_etherlab.py	Fri Apr 05 11:55:09 2013 +0900
+++ b/etherlab/runtime_etherlab.py	Fri Apr 05 16:49:41 2013 +0900
@@ -79,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():