This questions is related to a Skymote broadcasting simulation in cooja on contiki2.7.
I want to use "energest" module in cooja to estimate the energy consumption of a particular mote in different states (rx,tx,cpu and low power). I am using contiki broadcasting example for a "
" simulation. The code is shown below :
#include "contiki.h"
#include "net/rime.h"
#include "random.h"
#include "sys/energest.h"
#include "powertrace.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
PROCESS(example_broadcast_process, "BROADCAST example");
AUTOSTART_PROCESSES(&example_broadcast_process);
/*---------------------------------------------------------------------------*/
static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from){
printf("broadcast message received from %d.%d: '%s'\n",from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
}
static const struct broadcast_callbacks broadcast_call = {broadcast_recv};
static struct broadcast_conn broadcast;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_broadcast_process, ev, data){
static struct etimer et;
static unsigned long rx_start_time,lpm_start_time,cpu_start_time,tx_start_time = 0;
static unsigned long rx_new_time,lpm_new_time,cpu_new_time,tx_new_time = 0;
PROCESS_EXITHANDLER(broadcast_close(&broadcast);)
PROCESS_BEGIN();
broadcast_open(&broadcast, 129, &broadcast_call);
etimer_set(&et, 3*CLOCK_SECOND);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
energest_flush();
packetbuf_copyfrom("Hello", 6);
broadcast_send(&broadcast);
printf("broadcast message sent\n");
rx_new_time = energest_type_time(ENERGEST_TYPE_LISTEN);
lpm_new_time = energest_type_time(ENERGEST_TYPE_LPM);
cpu_new_time = energest_type_time(ENERGEST_TYPE_CPU);
tx_new_time = energest_type_time(ENERGEST_TYPE_TRANSMIT);
printf("Time spent (micro sec) rx: %lu tx: %lu cpu: %lu lpm: %lu\n",
(unsigned long)(1e6*(rx_new_time - rx_start_time) / RTIMER_SECOND),
(unsigned long)(1e6*(tx_new_time - tx_start_time) / RTIMER_SECOND),
(unsigned long)(1e6*(cpu_new_time - cpu_start_time) / RTIMER_SECOND),
(unsigned long)(1e6*(lpm_new_time - lpm_start_time) / RTIMER_SECOND));
printf("Total Energy: %lu uJ\n",
(unsigned long)( (21800*3*(rx_new_time - rx_start_time) / RTIMER_SECOND) +
(19500*3*(tx_new_time - tx_start_time) / RTIMER_SECOND) +
(1800*3 *(cpu_new_time - cpu_start_time) / RTIMER_SECOND) +
(2.6*3 *(lpm_new_time - lpm_start_time) / RTIMER_SECOND) ) );
rx_start_time = energest_type_time(ENERGEST_TYPE_LISTEN);
lpm_start_time = energest_type_time(ENERGEST_TYPE_LPM);
cpu_start_time = energest_type_time(ENERGEST_TYPE_CPU);
tx_start_time = energest_type_time(ENERGEST_TYPE_TRANSMIT);
etimer_reset(&et);
}
PROCESS_END();
}
21800, 19500, 1800 and 2.6 are current values (in uA) at each state and the highlighted expression calculates the total energy of the node . Channel check rate is set to 8 Hz. RTIMER_SECOND is the real time timer ticks per second (?).