Huseyin Berkay Berabi / GA-Final

Dependencies:   mbed-dev

Fork of GA-Berkay_Alex by Alejandro Ungria Hirte

Revision:
4:120ff05a7c27
Parent:
3:8bee1711d186
--- a/nodes/nodes.cpp	Wed Feb 28 16:10:21 2018 +0000
+++ b/nodes/nodes.cpp	Wed Feb 28 17:06:22 2018 +0000
@@ -29,22 +29,26 @@
 
 
 //------- BeaconNode Class -------------------
+
+//This is the class for the Tag !
+
 BeaconNode::BeaconNode(DecaWave& DW) : Node(DW) {
-    Anchor = false;
+    Anchor = false; // since it is a tag
     for (int i = 0; i < 3; i++)
         acknowledgement[i] = true;
-    LocalTimer.start();
+    LocalTimer.start(); //starts the timer of the tag.
     mode=0;   
 }
 
 #pragma Otime // Compiler optimize Runtime at the cost of image size
 // Two Way Ranging Actions when Frame was received
+//a Frame is basically a message.
 void BeaconNode::callbackRX(uint64_t RxTime) {
    //pc.printf("Got a frame, adress: %d source: %d \r\n", receivedFrame.destination, receivedFrame.source);   
-        if (receivedFrame.destination == address){
+        if (receivedFrame.destination == address){ //if id of the package is the same as the id of the anchor to which the tag was communicating
 
             switch (receivedFrame.type) {
-                case SWITCH_TYPE:
+                case SWITCH_TYPE: //base station can change the type of an antenna, making an anchor tag or making a tag anchor
                     sendBaseAck();
                     if((receivedFrame.signedTime >> 24) == BECOME_BEACON){
                         pc.printf("\r\n \r\n ---This Node is already a beacon ---\r\n \r\n");
@@ -55,13 +59,13 @@
                     }
                     
                     break;
-                case BASE_ORDER:
-                    repetitions = receivedFrame.signedTime&0xffff;
+                case BASE_ORDER: // tag received an order from base station
+                    repetitions = receivedFrame.signedTime&0xffff; 
                     mode = receivedFrame.signedTime >> 24;
                     destination = (receivedFrame.signedTime&0xff0000) >> 16;
-                    sendBaseAck();
+                    sendBaseAck(); //send an acknowledgement to base that message is received
                     break;
-                case ANCHOR_RESPONSE:
+                case ANCHOR_RESPONSE: //tag was measuring distance to an anchor, and respond from anchor came
                 {
                     sendAnswer(receivedFrame.source, BEACON_RESPONSE);  
                     senderTimestamps[receivedFrame.source][1] = RxTime;        //Save the second timestamp on the sending node/beacon (T_rr)
@@ -135,7 +139,12 @@
  */
 void BeaconNode::requestRanging(uint8_t destination) {
     if(noRec[destination] <= MAX_TRIES){
-        pc.printf(" max try %d\n\r",MAX_TRIES);
+       
+       //norec array containg for each id(index) how many times it was tried to contact that anchor.
+       //for example if tag has tried to contact anchor 5 3 times, noRec[5] = 3 !
+       //MAX_TRIES can be changed at nodex.h 
+      
+       // pc.printf(" max try %d\n\r",MAX_TRIES);
         float time_before = LocalTimer.read();
         
         while(!acknowledgement[2] && LocalTimer.read() < time_before + 0.0025f); // Wait until previous StreamFrame is sent 
@@ -143,18 +152,18 @@
         acknowledgement[0] = false;
         acknowledgement[1] = false;
         acknowledgement[2] = false;
-        time_before = LocalTimer.read();
+        time_before = LocalTimer.read(); //start time t0
     
         sendPingFrame(destination);
     
         while(!acknowledgement[1] && (LocalTimer.read() < time_before + 0.0025f + 0.003f*acknowledgement[0])); // One Ranging normaly takes less than 1.5 miliseconds
     
-        if(acknowledgement[1]){
+        if(acknowledgement[1]){ //measurement is done
             distances[destination] = calibratedDistance(destination);
             noRec[destination] = 0;
             // Stream Data to Basestation                
             sendStreamFrame(destination);         
-        } else {
+        } else { //maesurement was not succesfull
             //acknowledgement[1]=1;
             distances[destination] = -10;
             noRec[destination] = 0;
@@ -169,22 +178,27 @@
 #pragma Otime // Compiler optimize Runtime at the cost of image size
 inline float BeaconNode::calibratedDistance(uint8_t destination) {
 
+    //the error of two way ranging also depends on how long the distance is. We have tried to calibrate these errors.
+    //the numbers are found according to our measurements and test. 
+    //PLEASE NOTE THAT THESE CALIBRATIONS MIGHT BE DONE WISELY IN ORDER TO INCREASE ACCURANCY !
+
     float rawDistance = (tofs[destination] * 300 * TIMEUNITS_TO_US / 4);
     //float correctDistance = rawDistance + dwt_getrangebias(7, rawDistance, DWT_PRF_64M);
 
     //if(rawDistance <= 8.458)
     //    rawDistance -= 0.0541*rawDistance; // Correction Term 22-03-2017
     //else
-    //if(rawDistance >= 22.7)
-    //    rawDistance += -0.0004*rawDistance - 0.3971;
-    //else if (rawDistance >= 14.3)
-    //    rawDistance += -0.0015*rawDistance - 0.372;
-    //else if (rawDistance >= 8)
-    //    rawDistance += -0.0029*rawDistance - 0.352;
-    //else if (rawDistance >= 3.93)
-    //    rawDistance += 0.001*rawDistance - 0.370;
-    //else
-    //    rawDistance += -0.0235*rawDistance - 0.273;
+    
+    if(rawDistance >= 22.7)
+        rawDistance += -0.0004*rawDistance - 0.3971;
+    else if (rawDistance >= 14.3)
+        rawDistance += -0.0015*rawDistance - 0.372;
+    else if (rawDistance >= 8)
+        rawDistance += -0.0029*rawDistance - 0.352;
+    else if (rawDistance >= 3.93)
+        rawDistance += 0.001*rawDistance - 0.370;
+    else
+        rawDistance += -0.0235*rawDistance - 0.273;
         
     //else if (rawDistance >= 3)
     //    rawDistance += 0.0004*rawDistance - 0.5556
@@ -217,6 +231,7 @@
 
 #pragma Otime // Compiler optimize Runtime at the cost of image size
 void BeaconNode::requestRangingAll() {
+    //ADRESSES_COUNT can be changed in nodes.h !
     for (int i = 0; i < ADRESSES_COUNT; i++) {  // Request ranging to all anchors
         if(i != address){
             requestRanging(i);  
@@ -226,6 +241,8 @@
     }
 }
 void BeaconNode::requestRangingInt(uint8_t from,uint8_t to) {
+    //measures distances to anhors with id in interval [from,to]
+    //------------- Unfourtunately, it does not work properly, this case needs to be debugged !!! ---------
     for (int i=from; i <= to; i++){
         if (i!= address) {
             requestRanging(i);
@@ -319,7 +336,7 @@
     return destination;
 }
 
-void BeaconNode::clearRec(){
+void BeaconNode::clearRec(){ //the fufnction clears the noRec array ! 
     for(int j = 0; j < ADRESSES_COUNT; j++)
         noRec[j] = 0;
 }
@@ -327,7 +344,7 @@
 
 //------- AnchorNode Class -------------------
 AnchorNode::AnchorNode(DecaWave& DW) : Node(DW) {
-    Anchor = true;
+    Anchor = true; //Since it is an anchor
 }
 
 #pragma Otime // Compiler optimize Runtime at the cost of image size
@@ -337,7 +354,7 @@
    // if(!isBaseStation()){    
         if (receivedFrame.destination == address){
             switch (receivedFrame.type) {
-                case SWITCH_TYPE:
+                case SWITCH_TYPE: //as explained in beaconnode class
                     sendBaseAck();
                     if((receivedFrame.signedTime >> 24) == BECOME_BEACON){
                         node = &beaconNode; 
@@ -347,12 +364,12 @@
                         pc.printf("\r\n \r\n ---This Node is already an anchor ---\r\n \r\n");
                     }
                     break;
-                case PING:
+                case PING: 
                     sendAnswer(receivedFrame.source, ANCHOR_RESPONSE);  
                     receiverTimestamps[receivedFrame.source][0] = RxTime;      //Save the first timestamp on the receiving node/anchor (T_rp)             
                     //dw.turnonrx();
                     break;
-                case BEACON_RESPONSE:
+                case BEACON_RESPONSE:  
                     {
                     receiverTimestamps[receivedFrame.source][2] = RxTime;      //Save the third timestamp on the receiving node/anchor (T_rf)
                     correctReceiverTimestamps(receivedFrame.source);                //Correct the timestamps for the case of a counter overflow
@@ -364,7 +381,7 @@
                     break;
                     }
                 default : 
-                dw.turnonrx(); 
+                dw.turnonrx(); //turn on listening mode
                 break;
             }
         }
@@ -392,12 +409,12 @@
 
 
 #pragma Otime // Compiler optimize Runtime at the cost of image size
-void AnchorNode::sendBaseAck() {
+void AnchorNode::sendBaseAck() { //send base ack that the order is received
     rangingFrame.source = address;
     rangingFrame.destination = BASE_STATION_ADDR;
     rangingFrame.type = BASE_ORDER_ACK;
     //dw.sendFrame((uint8_t*)&rangingFrame, sizeof(rangingFrame));
-    dw.sendFrame((uint8_t*)&rangingFrame, sizeof(rangingFrame), 0, 0);
+    dw.sendFrame((uint8_t*)&rangingFrame, sizeof(rangingFrame), 0, 0); //function for sending messages(frames)
 }
 
 
@@ -450,6 +467,7 @@
 
 
 //------- BaseStationNode Class -------------------
+
 BaseStationNode::BaseStationNode(DecaWave& DW) : Node(DW) {
     Anchor = false;
     LocalTimer.start();    
@@ -469,14 +487,14 @@
     //pc.printf("Got a frame, adress: %d source: %d \r\n", receivedStreamFrame.destination, receivedStreamFrame.source);
     if (receivedStreamFrame.destination == address){
         switch(receivedStreamFrame.type){
-            case STREAM_TO_BASE:
+            case STREAM_TO_BASE: //send the received string from tag to the matlab with pc.printf
                 
                 //pc.printf("#%d to #%d %f(%f) \r\n", receivedStreamFrame.source, receivedStreamFrame.anchor_adress, receivedStreamFrame.distance, receivedStreamFrame.signalStrength);
                 pc.printf("#%03d/#%03d/%+011.6f/%+011.6f/%+011.6f/ \r\n", receivedStreamFrame.source, receivedStreamFrame.anchor_adress, receivedStreamFrame.distance, receivedStreamFrame.signalStrength, receivedStreamFrame.FPLevel/*dw.getFPLevel()*/);
                 break;
             case BASE_ORDER_ACK:
                
-                ack = true;
+                ack = true; // so that base station knows order was received
                 break;
             
             default: 
@@ -505,7 +523,7 @@
 void BaseStationNode::sendOrder(uint8_t beacon_destination, uint8_t anchor_destination, uint8_t action, uint16_t repetitions, uint8_t type) {
     ExtendedRangingFrame orderFrame;
     ack = false;
-
+    //the function is used to send orders one has to create and order frame
     orderFrame.source = address;
     orderFrame.destination = beacon_destination;
     orderFrame.type = type;