Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Revision:
24:6f25ba679490
Parent:
23:661a79e56208
Child:
25:d58b0595b300
--- a/main.cpp	Thu Nov 27 13:34:35 2014 +0000
+++ b/main.cpp	Thu Nov 27 16:26:42 2014 +0000
@@ -1,3 +1,4 @@
+// by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
 #include "mbed.h"
 #include "PC.h"                         // Serial Port via USB for debugging with Terminal
 #include "DW1000.h"
@@ -5,42 +6,55 @@
 PC          pc(USBTX, USBRX, 921600);   // USB UART Terminal
 DW1000      dw(PA_7, PA_6, PA_5, PB_6, PB_9);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
 
-const float timeunit = 1/(128*499.2e6);
+const float timeunit = 1/(128*499.2);
 int i=0;
-char message[1200] = "";
-uint64_t TX_timestamp = 0;
-uint64_t RX_timestamp = 0;
+char message[1021] = "";
+char messageRX[1021] = "";
+
+uint64_t TX_timestamp;
+uint64_t RX_timestamp;
+
+int event_i = 0;
+char event[10][20];
+uint64_t eventtimes[10];
 
 void callbackRX(int framelength) {
+    TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
     RX_timestamp = dw.readRegister40(DW1000_RX_TIME, 0);
-    if (framelength < 200) {
-        char* receive = dw.receiveString();                             // receive a string
-        pc.printf("Received: \"%s\" %d ", receive, framelength);
-        pc.printf("Status: %010llX  ", dw.getStatus());
+    dw.receiveString(messageRX);                             // receive a string
 #if 0
-        sprintf(message, "ACK \"%s\"", receive);
-        dw.sendString(message);
-        wait(0.1);
+    message[0] = 'A';
+    for(int i = 0; i < 10; i++)
+        message[i+1] = messageRX[i];
+    dw.sendString(message);
 #endif
-        delete[] receive;
-    } else
-        pc.printf("Received! %d ", framelength);
-    
-    uint64_t difference = RX_timestamp - TX_timestamp;
-    //pc.printf("Timestamp: %lld\r\n", difference);
-    pc.printf("Time since TX: %fs\r\n", difference*timeunit);       // TODO: gives some wrong values because of timer overflow
+    eventtimes[event_i] = RX_timestamp - TX_timestamp;                      // TODO: gives some wrong values because of timer overflow  
+    event[event_i][0] = '!';
+    event[event_i][1] = 'R';
+    event[event_i][2] = ' ';
+    for(int i = 0; i < 10; i++)
+        event[event_i][i+3] = messageRX[i];
+    if (event_i == 8)
+        event_i = 0;
+    else
+        event_i++;
     dw.startRX();
 }
 
 void callbackTX() {
     //TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
-    /*char messagecheck[1021];
-    dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, 1021);
-    if (i < 200)
-        pc.printf("%d Sent: \"%s\" %d ", i, messagecheck, strlen(messagecheck)+1);
+    //dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, 1021);
+    //pc.printf("! %d Sent: \"%s\" %d\r\n", i, message, strlen(message)+1);
+    eventtimes[event_i] = 0;
+    event[event_i][0] = '!';
+    event[event_i][1] = 'S';
+    event[event_i][2] = ' ';
+    for(int i = 0; i < 10; i++)
+        event[event_i][i+3] = message[i];
+    if (event_i == 8)
+        event_i = 0;
     else
-        pc.printf("%d Sent! %d ", i, strlen(messagecheck)+1);*/
-    pc.printf("SENT!! Status: %010llX\r\n", dw.getStatus());
+        event_i++;
 }
 
 int main() {
@@ -54,23 +68,29 @@
     dw.callbackTX = &callbackTX;
     
     // Receiver initialisation
-    dw.writeRegister16(DW1000_SYS_MASK, 0, 0x4080); //| 0x0080); // TODO: RX only good frame 0x4000, RX all frames 0x2000, TX done 0x0080
+    dw.writeRegister16(DW1000_SYS_MASK, 0, 0x4000); //| 0x0080); // TODO: RX only good frame 0x4000, RX all frames 0x2000, TX done 0x0080
     dw.startRX();
     
     while(1) {
+        for(int i = 0; i < 10; i++)
+            if(event[i][0] == '!') {
+                pc.printf("%s Time: %fus\r\n", event[i], eventtimes[i]*timeunit);
+                event[i][0] = 'X';
+            }    
 #if 1
-        sprintf(message, "Hi %d", i);
-        if ((i % 10) > 5) {
+        sprintf(message, "%d", i);
+        //if ((i % 10) > 5) {
             dw.sendString(message);
             //pc.printf("%d Sent: \"%s\" %d \r\n", i, message, strlen(message)+1);
-        }
+        //}
 #endif
 #if 0
         pc.printf("%d Waiting... %d %d ", i, dw.receiving, dw.sending);
         wait(5);
 #endif
-        wait(0.2);
+        wait(1);
         //pc.printf("Status: %010llX\r\n", dw.getStatus());
+        //pc.printf("State: %010llX\r\n", dw.readRegister40(DW1000_SYS_STATE, 0));
         i++;
     }
 }
\ No newline at end of file