etherlab/plc_etherlab.c
changeset 2075 21eb91dc7071
parent 2039 3a218f6bd805
child 2076 22e9993348f6
equal deleted inserted replaced
2074:bf2fa4cb62a9 2075:21eb91dc7071
    44 long long wait_period_ns = 100000LL;
    44 long long wait_period_ns = 100000LL;
    45 
    45 
    46 // EtherCAT
    46 // EtherCAT
    47 static ec_master_t *master = NULL;
    47 static ec_master_t *master = NULL;
    48 static ec_domain_t *domain1 = NULL;
    48 static ec_domain_t *domain1 = NULL;
       
    49 static int first_sent=0;
    49 %(slaves_declaration)s
    50 %(slaves_declaration)s
    50 
    51 
    51 /* Beremiz plugin functions */
    52 /* Beremiz plugin functions */
    52 int __init_%(location)s(int argc,char **argv)
    53 int __init_%(location)s(int argc,char **argv)
    53 {
    54 {
   101         fprintf(stderr, "domain1_pd:  0x%%.6lx\n", (unsigned long)domain1_pd);
   102         fprintf(stderr, "domain1_pd:  0x%%.6lx\n", (unsigned long)domain1_pd);
   102         return -1;
   103         return -1;
   103     }
   104     }
   104 
   105 
   105     fprintf(stdout, "Master %(master_number)d activated...\n");
   106     fprintf(stdout, "Master %(master_number)d activated...\n");
   106      
   107     
       
   108     first_sent = 0;
       
   109 
   107     return 0;
   110     return 0;
   108 }
   111 }
   109 
   112 
   110 void __cleanup_%(location)s(void)
   113 void __cleanup_%(location)s(void)
   111 {
   114 {
   112 	if (rt_fd >= 0) {
   115 	if (rt_fd >= 0) {
   113 		rt_dev_close(rt_fd);
   116 		rt_dev_close(rt_fd);
   114 	}
   117 	}
   115 	//release master
   118 	//release master
   116 	ecrt_release_master(master);
   119 	ecrt_release_master(master);
       
   120     first_sent = 0;
   117 }
   121 }
   118 
   122 
   119 void __retrieve_%(location)s(void)
   123 void __retrieve_%(location)s(void)
   120 {
   124 {
   121     // send process data
   125 //    // send process data
   122     ecrt_rtdm_domain_queque(rt_fd);
   126 //    ecrt_rtdm_domain_queque(rt_fd);
   123     ecrt_rtdm_master_send(rt_fd);
   127 //    ecrt_rtdm_master_send(rt_fd);
   124 
   128 //
   125     rt_task_sleep(rt_timer_ns2tsc(wait_period_ns));
   129 //    rt_task_sleep(rt_timer_ns2tsc(wait_period_ns));
   126 
   130 
   127     // receive ethercat
   131     // receive ethercat
   128     ecrt_rtdm_master_recieve(rt_fd);
   132     if(first_sent){
   129     ecrt_rtdm_domain_process(rt_fd);
   133         ecrt_rtdm_master_recieve(rt_fd);
       
   134         ecrt_rtdm_domain_process(rt_fd);
       
   135 %(retrieve_variables)s
       
   136     }
   130 
   137 
   131 %(retrieve_variables)s
       
   132 }
   138 }
       
   139 
       
   140 static RTIME _last_occur=0;
       
   141 RTIME _current_lag=0;
   133 
   142 
   134 void __publish_%(location)s(void)
   143 void __publish_%(location)s(void)
   135 {
   144 {
   136 %(publish_variables)s
   145 %(publish_variables)s
       
   146     ecrt_rtdm_domain_queque(rt_fd);
       
   147     {
       
   148         RTIME _current_time = rt_timer_read();
       
   149         RTIME deadline = _last_occur ?
       
   150             _last_occur + common_ticktime__:
       
   151             _current_time; 
       
   152         _last_occur = _current_time;
       
   153         _current_lag = deadline - _current_time;
       
   154         while(_current_time < deadline) {
       
   155             _last_occur = _current_time;
       
   156             _current_time = rt_timer_read();
       
   157         }
       
   158     }
       
   159     ecrt_rtdm_master_send(rt_fd);
       
   160     first_sent = 1;
   137 }
   161 }