Working Xenomai Posix example
authorAndreas Stewering-Bone <ab@igh-essen.com>
Mon, 21 Mar 2011 21:07:38 +0100
changeset 2057 23fca60b7ca2
parent 2056 a92e8f119723
child 2058 543c4ce9e86e
Working Xenomai Posix example
examples/xenomai_posix/Makefile.am
examples/xenomai_posix/main.c
--- a/examples/xenomai_posix/Makefile.am	Mon Mar 21 08:06:49 2011 +0100
+++ b/examples/xenomai_posix/Makefile.am	Mon Mar 21 21:07:38 2011 +0100
@@ -36,4 +36,5 @@
 ec_xenomai_posix_example_CFLAGS = -I/opt/etherlab/xenomai/include -D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__ -I/opt/etherlab/xenomai/include/posix -I$(top_srcdir)/include
 ec_xenomai_posix_example_LDFLAGS = -Wl,@/opt/etherlab/xenomai/lib/posix.wrappers -L/opt/etherlab/xenomai/lib -lpthread_rt -lrtdm -L$(top_builddir)/lib/.libs -lethercat -L/opt/etherlab/xenomai/lib -lrtdk -lxenomai -lpthread
 
+
 #------------------------------------------------------------------------------
--- a/examples/xenomai_posix/main.c	Mon Mar 21 08:06:49 2011 +0100
+++ b/examples/xenomai_posix/main.c	Mon Mar 21 21:07:38 2011 +0100
@@ -40,9 +40,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
-#include <getopt.h>
-#include <netinet/in.h>
-#include <net/if.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <time.h>
@@ -481,16 +478,7 @@
 /**********************************************************/
 void cleanup_all(void)
 {
-    printf("delete my_task\n");
-
-    pthread_kill(cyclicthread, SIGHUP);
-    pthread_join(cyclicthread, NULL);
-    
-    if (rt_fd >= 0) {
-        printf("closing rt device %s\n", &rt_dev_file[0]);
-        rt_dev_close(rt_fd);
-        
-    }
+    run = 0;   
 }
 
 void catch_signal(int sig)
@@ -504,30 +492,34 @@
 
 void *my_thread(void *arg)
 {
-    struct sched_param param = { .sched_priority = 1 };
     struct timespec next_period;
+
     int counter = 0;
     int divcounter = 0;
     int divider = 10;
-    pthread_set_name_np(pthread_self(), "ec_xenomai_posix_test");
-    pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
-
-    clock_gettime(CLOCK_MONOTONIC, &next_period);
-
+
+
+    clock_gettime(CLOCK_REALTIME, &next_period);
     while(1) {
         next_period.tv_nsec += cycle * 1000;
         while (next_period.tv_nsec >= NSEC_PER_SEC) {
                 next_period.tv_nsec -= NSEC_PER_SEC;
                 next_period.tv_sec++;
-        }
-
-        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_period, NULL);
+                }
+
+        clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next_period, NULL);
+
 
         counter++;
-        if (counter>600000) {
+        if (counter>60000) {
             run=0;
             return NULL;
         }
+
+        if(run ==  0) {
+            return NULL;
+        }
+
         
         // receive ethercat
         ecrt_rtdm_master_recieve(rt_fd);
@@ -565,8 +557,6 @@
 
 int main(int argc, char *argv[])
 {
-    struct sched_param param = { .sched_priority = 1 };
-    pthread_attr_t thattr;
     ec_slave_config_t *sc;
     int rtstatus;
 
@@ -727,16 +717,21 @@
     run=1;
 
     /* Create cyclic RT-thread */
+    struct sched_param param = { .sched_priority = 82 };
+    pthread_attr_t thattr;
     pthread_attr_init(&thattr);
     pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
-    pthread_attr_setstacksize(&thattr, PTHREAD_STACK_MIN);
+    pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED);
+    pthread_attr_setschedpolicy(&thattr, SCHED_FIFO);
+    pthread_setschedparam(cyclicthread, SCHED_FIFO, &param);
+    pthread_set_name_np(cyclicthread, "ec_xenomai_posix_test");
     ret = pthread_create(&cyclicthread, &thattr, &my_thread, NULL);
     if (ret) {
         fprintf(stderr, "%s: pthread_create cyclic task failed\n",
                 strerror(-ret));
         goto failure;
     }
-    pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
+
 
 
 
@@ -745,12 +740,9 @@
     	sched_yield();
       }
 
-    //rt_task_delete(&my_task);
-
-
-
-    pthread_kill(cyclicthread, SIGHUP);
-    pthread_join(cyclicthread, NULL);
+
+
+
 
 
     if (rt_fd >= 0)