Thursday, June 27, 2013

Using rtimer in Contiki for more accurate timing

In contiki programs, the usage of rtimer is important when we need a real-time functionality. While having different kinds of timers such as ctimer and etimer, the most accurate timing can be achieved by using rtimer. Recently had such a requirement where my program should have very accurate timing. So, I learned about rtimer and used it in my program. For the sake of remembering it, I'm writing down a simple program which use rtimer.

The program shown below is a modified version of the hellow-world program which is in /contiki-2.6/examples/hello-world directory. Therefore I can run it just by issuing the following command in terminal inside that directory.

make TARGET=cooja hello-world

1:  #include "contiki.h"  
2:  #include <stdio.h>  
3:  #include "sys/rtimer.h"  
4:  #define     PERIOD_T     5*RTIMER_SECOND  
5:    
6:  static struct rtimer my_timer;  
7:    
8:  PROCESS(hello_world_process, "Hello world process");  
9:  AUTOSTART_PROCESSES(&hello_world_process);  
10:    
11:  // the function which gets called each time the rtimer triggers  
12:  static char periodic_rtimer(struct rtimer *rt, void* ptr){  
13:    uint8_t ret;  
14:    rtimer_clock_t time_now = RTIMER_NOW();  
15:    
16:    printf("Hello from rtimer!!!\n");  
17:    
18:    ret = rtimer_set(&my_timer, time_now + PERIOD_T, 1,   
19:          (void (*)(struct rtimer *, void *))periodic_rtimer, NULL);  
20:    if(ret){  
21:     printf("Error Timer: %u\n", ret);  
22:    }  
23:    return 1;  
24:  }  
25:    
26:  PROCESS_THREAD(hello_world_process, ev, data)  
27:  {  
28:   PROCESS_BEGIN();  
29:    
30:   printf("Starting the application...\n");  
31:    
32:   periodic_rtimer(&my_timer, NULL);  
33:    
34:   while(1){             
35:    PROCESS_YIELD();  
36:   }  
37:   PROCESS_END();  
38:  }  

Our program will keep printing Hello from rtimer!!! as the output.

1 comment:

  1. I tried to use rtimer with broadcast packet sending and implemented rtimer as you have shown above . but I just receive packets in the first 3 seconds . WHy is it like that`?

    ReplyDelete