Skip to content

Commit

Permalink
:q
Browse files Browse the repository at this point in the history
  • Loading branch information
nezabelej committed May 29, 2016
2 parents 5430167 + 778aab5 commit 024ab41
Showing 1 changed file with 78 additions and 65 deletions.
143 changes: 78 additions & 65 deletions node/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "dev/i2cmaster.h" // Include IC driver
#include "dev/tmp102.h" // Include sensor driver
#include "dev/cc2420/cc2420.h"
#include "dev/cc2520/cc2520.h"
#include "net/rime/rime.h"
#include "net/rime/mesh.h"
#include "dev/button-sensor.h"
Expand Down Expand Up @@ -34,8 +35,8 @@ AUTOSTART_PROCESSES (&herd_monitor_node);
0xF2 -> 3 dBm
0xF7 -> 5 dBm
*/
static int TXPOWER[9] = {0x03, 0x2C, 0x88, 0x81, 0x32, 0x13, 0xAB, 0xF7};
static int power_index = 5; //Default tx power is 1dBm
static int TXPOWER[9] = {3, 7, 11, 15, 19, 23, 27, 31};
static int power_index = 7; //Default and max tx power is 0dBm

static int neighbour_list[NUMBER_OF_COWS];

Expand All @@ -50,6 +51,9 @@ static int num_of_my_clusters = 0;
static int cluster_head_rssi_data[NUMBER_OF_COWS][NUMBER_OF_COWS];
static int cluster_head_battery_data[NUMBER_OF_COWS];
static int cluster_head_temperature_data[NUMBER_OF_COWS];
//Cluster head counts received packets from each cow.
//This is broadcasted every 5th round (when nodes are listening for rssi) as one global acknowledgment.
static int cluster_head_ack_data[NUMBER_OF_COWS];

// 1 = standing, 2 = moving slowly, 3 = running
static int motion_status = 0;
Expand Down Expand Up @@ -77,24 +81,66 @@ static void reset_neighbour_list()

for (i = 0; i < NUMBER_OF_COWS; i++) {
neighbour_list[i] = 1; //Not a RSSI value (-100 - 0)
cluster_head_battery_data[i] = -1;
cluster_head_temperature_data[i] = -1;
int j;
for (j = 0; j < NUMBER_OF_COWS; j++) {
cluster_head_rssi_data[i][j] = 0;
}
}
}

static void increase_txpower()
{
power_index += 2;
if (power_index > 7) {
power_index = 7;
}
cc2420_set_txpower(TXPOWER[power_index]);
}

static void decrease_txpower()
{
if (--power_index < 0) {
power_index = 0;
}
cc2420_set_txpower(TXPOWER[power_index]);
}

static void init_broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
//This function serves two purposes. It measures rssi from neighbours and
//it accepts acknowledgment from cluster head.
//This happens every 5th round.
static void node_receiving_rssi_and_acknowledgment(struct broadcast_conn *c, const linkaddr_t *from)
{
int cow_id = from->u8[0];
int rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI);
neighbour_list[cow_id - 1] = rssi;

printf("broadcast message received from cow %d with rssi %d \n",
cow_id, rssi);
printf("rssi_round_counter: %d \n", rssi_round_counter);
if (rssi_round_counter == 5) {
int * ack_pointer = (int *)packetbuf_dataptr();
//Number of successfully
int ack_number = *(ack_pointer + node_id - 1);
printf("Ack received: %d \n", ack_number);
printf("Current txpower: %d\n", cc2420_get_txpower());
if (ack_number < 5) { //If some packet was not received successfully, then increase power.
printf("Increasing tx power.\n");
increase_txpower();
} else {
printf("Decreasing tx power.\n");
decrease_txpower();
}
} else {
printf("broadcast message received from cow %d with rssi %d \n",
cow_id, rssi);
}
}

//Cluster head receiving data from nodes
static void data_broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
static void cluster_head_broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
int cow_id = from->u8[0];
//We increment received packet count. This will be broadcasted on every 5th interval.
cluster_head_ack_data[cow_id - 1]++;
printf("Battery, temperat and rssi list received from cow %d \n", cow_id);

int * bat_temp = (int *)packetbuf_dataptr();
Expand Down Expand Up @@ -149,24 +195,9 @@ static void clustering_broadcast_recv(struct broadcast_conn *c, const linkaddr_t
printf("\n");
break;
}

/*for (j = 1; j < NUMBER_OF_COWS; j++) {
int node = *(cluster + j) + 1;
if (node == 0 ) {
break;
}
if (node == node_id) { //We save cluster heads of my clusters.
my_clusters[k] = cluster_head;
printf("cluster %d \n",cluster_head);
k++;
break;
}
}*/

}
broadcast_close(c);
mode_of_operation = 4;

}

static void init_send_to_gateway(struct unicast_conn *c)
Expand All @@ -180,7 +211,7 @@ static void init_send_to_gateway(struct unicast_conn *c)
printf("Neighbour list sent to the gateway\n");
}

static void cluster_head_sends_all_data_to_gateway(struct unicast_conn *c)
static void cluster_head_sends_all_data_to_gateway(struct unicast_conn *c, struct unicast_callbacks *callback)
{
static linkaddr_t addr;
addr.u8[0] = 0;
Expand All @@ -201,13 +232,25 @@ static void cluster_head_sends_all_data_to_gateway(struct unicast_conn *c)
//We also have to send head's data.
//toSend[node_id - 1][0] = b;
//toSend[node_id - 1][1] = t;

unicast_open(c, 146, callback);
packetbuf_copyfrom(toSend, sizeof(toSend));
unicast_send(c, &addr);
unicast_close(c);
printf("Cluster head sending temperature, battery and rssi data to the gateway.\n");
}

static void cluster_head_sends_acknowledgments()
{
packetbuf_copyfrom(cluster_head_ack_data, sizeof(cluster_head_ack_data));
broadcast_send(&broadcast);
//We reset the acknowledgments
int i;
for (i = 0; i < NUMBER_OF_COWS; i++)
{
cluster_head_ack_data[i] = 0;
}
}

static void rssi_neighbours_send_to_gateway(struct unicast_conn *c)
{
static linkaddr_t addr;
Expand All @@ -230,36 +273,17 @@ static void data_ack_received()

}

static void increase_txpower()
{
if (power_index < 8) {
power_index += 2;
set_txpower(TXPOWER[power_index]);
}
}

static void decrease_txpower()
{
if (power_index > 0) {
power_index--;
set_txpower(TXPOWER[power_index]);
}
}

static void open_broadcast(struct broadcast_callbacks *cl)
{
if (is_broadcast_open == 0) {
is_broadcast_open = 1;
broadcast_open(&broadcast, 129, cl);
printf("Broadcast is open... \n");
}
}
static void close_broadcast()
{
broadcast_close(&broadcast);
is_broadcast_open = 0;
printf("Broadcast closed... \n");

broadcast_close(&broadcast);
is_broadcast_open = 0;
}

static void normal_mode_rssi_call()
Expand All @@ -268,8 +292,8 @@ static void normal_mode_rssi_call()

}

static const struct broadcast_callbacks broadcast_call = {init_broadcast_recv};
static const struct broadcast_callbacks broadcast_data_call = {data_broadcast_recv};
static const struct broadcast_callbacks broadcast_call = {node_receiving_rssi_and_acknowledgment};
static const struct broadcast_callbacks broadcast_data_call = {cluster_head_broadcast_recv};
static const struct broadcast_callbacks broadcast_clustering_call = {clustering_broadcast_recv};


Expand Down Expand Up @@ -438,40 +462,29 @@ PROCESS_THREAD (herd_monitor_node, ev, data)
}

/**********************************************************
HEAD CLUSTER CHECKS FOR DATA TO SEND TO GATEWAY
HEAD CLUSTER SENDS ALL DATA TO GATEWAY
***********************************************************/
if (role == 1 && mode_of_operation == 4) {
if (rssi_round_counter == 5) {
cluster_head_sends_only_rssi = 1;
}
int ctr = 0;
for (i = 0; i < NUMBER_OF_COWS; i++) {
if (cluster_head_temperature_data[i] != -1) {
ctr++;
}
if (cluster_head_battery_data[i] != -1) {
ctr++;
}
printf("Broadcast - Cluster head sending acknowledgments.\n");
cluster_head_sends_acknowledgments();
}
close_broadcast();
printf("Unicast - Sending data to the gateway.\n");

unicast_open(&uc, 146, &unicast_callbacks_data);

cluster_head_sends_all_data_to_gateway(&uc);
cluster_head_sends_all_data_to_gateway(&uc, &unicast_callbacks_data);

//We close the connection to the gateway and start listening again for
//broadcasts of nodes.
unicast_close(&uc);
open_broadcast(&broadcast_data_call);
}


PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&round_timer));
if (mode_of_operation == 1) {
if (mode_of_operation == 1) { //Init mode
close_broadcast();
mode_of_operation++;
} else if (mode_of_operation == 4) {
} else if (mode_of_operation == 4) { // Normal mode. Increment counter for RSSI listening round
if (rssi_round_counter == 5) {
rssi_round_counter = 0;
} else {
Expand Down

0 comments on commit 024ab41

Please sign in to comment.