Files at this revision

API Documentation at this revision

Comitter:
sleighton
Date:
Fri Mar 04 18:46:05 2016 +0000
Parent:
11:6b699617fc7f
Commit message:
Before Tidy up

Changed in this revision

XBee_Robot.cpp Show annotated file Show diff for this revision Revisions of this file
XBee_Robot.h Show annotated file Show diff for this revision Revisions of this file
diff -r 6b699617fc7f -r a7ec1238373e XBee_Robot.cpp
--- a/XBee_Robot.cpp	Tue Feb 02 11:02:48 2016 +0000
+++ b/XBee_Robot.cpp	Fri Mar 04 18:46:05 2016 +0000
@@ -57,13 +57,10 @@
 
 //XBEE ROBOT CLASS METHODS
 XBee_Robot::XBee_Robot(PinName _txIn, PinName _rxIn): dataLink(_txIn,_rxIn){
+    dataLink.attach(this,&XBee_Robot::Rx_interrupt, Serial::RxIrq); //set interrupt function on receive pin
     ATQuery(0x4D,0x59); //create AT query with AT command 'MY' to query own 16 bit network address
     commandFlag = 0; //set command flag to 0 on initialisation
-}
-
-void XBee_Robot::setRxInterrupt()
-{
-    dataLink.attach(this,&XBee_Robot::Rx_interrupt, Serial::RxIrq);
+    finishedFlag = 0; //set finished flag to 0 on intialisation
 }
 
 void XBee_Robot::Rx_interrupt()
@@ -73,10 +70,12 @@
         Rx_buffer.push_back(dataLink.getc());//add each incoming byte to buffer
         wait(0.00107); //wait for long enough so the next digit is recognised in the same stream (updated from 0.0011 to accomodate for 2 bytes of data)
     }
-
+    
     //Check valid packet delimeter and checksum
     if((Rx_buffer[0] == 0x7E) && (Rx_buffer[Rx_buffer.size()] == calculateChecksum(Rx_buffer)))
         RxPacketControl(Rx_buffer); //call packet control function
+    else
+        printf("Packet failed delimeter and checksum check");
 }
 
 void XBee_Robot::transmitRequest(uint8_t *BitAddress64, uint8_t *BitAddress16, uint8_t broadcastRadius, uint8_t options, uint8_t *data,size_t dataLength)
@@ -231,8 +230,8 @@
             break;
         }
         case 0x01:{ //Receive location command
-            node_list[currentIndex].setCoordinates((int)packet[1],(int)packet[2],(int)packet[3]); //update coordinates for corresponding node
-            printf("X = %d, Y = %d, Heading = %d\n",node_list[currentIndex].getX(),node_list[currentIndex].getY(),node_list[currentIndex].getHeading()); //display data from packet
+            node_list[currentIndex].setCoordinates(convSigned((int)packet[1]),convSigned((int)packet[2]),convSigned((int)packet[3])); //update coordinates for corresponding node
+            printf("Receive Location Command, X = %d, Y = %d, Heading = %d\n",node_list[currentIndex].getX(),node_list[currentIndex].getY(),node_list[currentIndex].getHeading()); //display data from packet
             break;
         }
         case 0x02: { //Receive destination coordinates from coordinator command
@@ -243,6 +242,19 @@
             commandFlag = 1; //set command flag to indicate received coordinates
             break;
         }
+        case 0xFA: { //Receive router robot finished command
+            printf("Finished flag set\n");
+            finishedFlag = 1; //set command flag to indicate received coordinates
+            break;
+        }
+        case 0xFC: { //Receive obstacle coordinate command
+            printf("Obstacle coordinate received at %d, %d\n",convSigned((int)packet[1]),convSigned((int)packet[2]));
+            std::vector <int> coordinate;
+            coordinate.push_back(convSigned((int)packet[1]));
+            coordinate.push_back(convSigned((int)packet[2]));
+            obstacles.push_back(coordinate);
+            break;
+        }
         default:
             printf("Received data command not recognised: %c",command);
     }
@@ -260,6 +272,30 @@
     return commandFlag; //return value of command flag
 }
 
+int XBee_Robot::getFinishedFlag(){
+    return finishedFlag; //return value of finished flag
+}
+
+void XBee_Robot::resetFinishedFlag(){
+    finishedFlag = 0; //reset finished flag
+}
+
 void XBee_Robot::resetCommandFlag(){
     commandFlag = 0; //reset command flag
+}
+
+int XBee_Robot::checkObstacle(int x_in, int y_in){
+  for (int i = 0; i<obstacles.size();i++){ //search each entry in node_list for matching node
+        if((obstacles[i][0] == x_in)&&(obstacles[i][1] == y_in)){
+            return 1; //return 1 if obstacle exists at given coordinates
+            printf("Obstacle found at %d,%d\n",obstacles[i][0],obstacles[i][1]); //print node number
+        }
+    }
+    return 0; //return 0 if no obstacle exists at given coordinates
+}
+
+int XBee_Robot::convSigned(int int_in){
+    if(int_in >= 128)
+        return int_in-256; //if value is greater or equal to 128 convert to negative number
+    return int_in;
 }
\ No newline at end of file
diff -r 6b699617fc7f -r a7ec1238373e XBee_Robot.h
--- a/XBee_Robot.h	Tue Feb 02 11:02:48 2016 +0000
+++ b/XBee_Robot.h	Fri Mar 04 18:46:05 2016 +0000
@@ -8,9 +8,9 @@
     std::vector<uint8_t> addr; //16 bit network address vector
     std::vector<uint8_t> addr64; //64 bit network address vector
     int nodeNum; //index
-    int x;
-    int y;
-    int heading;
+    int x; //x coordinate
+    int y; //y coordinate
+    int heading; //heading
 public:
     NetworkNode(std::vector<uint8_t> & addrIn, int indexIn);
     //constructor
@@ -50,6 +50,8 @@
 class XBee_Robot {
 private:
 
+    std::vector< vector <int> > obstacles; // 2D vector to hold positions of obstacles 
+    
     int currentIndex; //current index of NetworkNode for received packet, allows coordinates to be updated
 
     std::vector< NetworkNode > node_list; //list of network nodes
@@ -62,14 +64,13 @@
     
     int commandFlag; // flag that indicates if coordinates from the coordinator have been received
     
+    int finishedFlag; // flag that indictates if the router robots have finished their journey (coordinator use only)
+    
 public:
     
     XBee_Robot(PinName _txIn, PinName _rxIn);
     //constructor for XBee
     
-    void setRxInterrupt();
-    //enables interrupt for receive pin
-    
     void Rx_interrupt();
     //ISR for receive pin
     
@@ -103,6 +104,18 @@
     int getCommandFlag();
     //used by main program to poll command flag
     
+    int getFinishedFlag();
+    //used by main program to poll finished flag (coordinator use only)
+    
+    void resetFinishedFlag();
+    //used by main program to reset finished flag (coordinator use only)
+    
     void resetCommandFlag();
     //used by main program to reset command flag
+    
+    int checkObstacle(int x_in, int y_in);
+    //used by main program to check if an obstacle exists at given coordinates (coordinator use only)
+
+    int convSigned(int int_in);
+    //used to convert unsigned 8 bit integers to signed integers
 };