Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Revision:
26:a65c6f26c458
Parent:
25:d58b0595b300
Child:
27:71178fdb78e1
diff -r d58b0595b300 -r a65c6f26c458 main.cpp
--- a/main.cpp	Thu Nov 27 17:51:54 2014 +0000
+++ b/main.cpp	Thu Nov 27 19:19:35 2014 +0000
@@ -1,34 +1,33 @@
 // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
 #include "mbed.h"
-#include "PC.h"                         // Serial Port via USB for debugging with Terminal
+#include "PC.h"                                     // Serial Port via USB for debugging with Terminal
 #include "DW1000.h"
 
-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)
+//#define RECEIVER
+
+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.2);
 int i=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);
-    dw.receiveString(messageRX);                             // receive a string
-#if 1
-    message[0] = 'A';
+    RX_timestamp = dw.getRXTimestamp();
+    dw.receiveString(messageRX);
+#ifdef RECEIVER
+    message[0] = 'A';                               // acknowledge messages
     for(int i = 0; i < 10; i++)
         message[i+1] = messageRX[i];
     dw.sendString(message);
 #endif
-    eventtimes[event_i] = RX_timestamp - TX_timestamp;                      // TODO: gives some wrong values because of timer overflow  
+    eventtimes[event_i] = RX_timestamp - TX_timestamp;                      // TODO: can give some wrong values because of timer reset after 17 seconds
     event[event_i][0] = '!';
     event[event_i][1] = 'R';
     event[event_i][2] = ' ';
@@ -42,9 +41,7 @@
 }
 
 void callbackTX() {
-    //TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
-    //dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, 1021);
-    //pc.printf("! %d Sent: \"%s\" %d\r\n", i, message, strlen(message)+1);
+    TX_timestamp = dw.getTXTimestamp();
     eventtimes[event_i] = 0;
     event[event_i][0] = '!';
     event[event_i][1] = 'S';
@@ -64,33 +61,21 @@
     pc.printf("%d EUI register: %016llX\r\n", i, dw.getEUI());
     pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
     
-    dw.callbackRX = &callbackRX;        // TODO: must not jump to NULL & setter
-    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.setCallbacks(&callbackRX, &callbackTX);
+    dw.setInterrupt(true, true);
     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';
+        for(int j = 0; j < 10; j++)
+            if(event[j][0] == '!') {
+                pc.printf("%s Time: %fus\r\n", event[j], eventtimes[j]*timeunit);
+                event[j][0] = 'X';
             }    
-#if 0
-        sprintf(message, "%d", i);
-        //if ((i % 10) > 5) {
-            dw.sendString(message);
-            //pc.printf("%d Sent: \"%s\" %d \r\n", i, message, strlen(message)+1);
-        //}
+#ifndef RECEIVER
+        sprintf(message, "%d", i);                  // send numbers to acknowledge
+        dw.sendString(message);
+        wait(1);
 #endif
-#if 0
-        pc.printf("%d Waiting... %d %d ", i, dw.receiving, dw.sending);
-        wait(5);
-#endif
-        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