Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Revision:
41:0a3bb028d4ba
Parent:
40:5ce51b7e3118
Child:
42:83931678c4de
diff -r 5ce51b7e3118 -r 0a3bb028d4ba main.cpp
--- a/main.cpp	Fri Feb 27 11:47:51 2015 +0000
+++ b/main.cpp	Sun Mar 01 20:19:38 2015 +0000
@@ -8,16 +8,11 @@
 DW1000      dw(PA_7, PA_6, PA_5, PB_6, PB_9);       // Device driver instanceSPI pins: (MOSI, MISO, SCLK, CS, IRQ)
 MMRanging   r(dw);                                  // Ranging class for getting distances and later positions
 
-Timer LocalTimer;
-float time_before;
-
-int average[50];
-int average_index = 0;
-float averaged;
+float rangings[1000];
+int index = 0;
 
 int main() {
-    LocalTimer.start();
-    pc.printf("DecaWave 0.2\r\nup and running!\r\n");  
+    pc.printf("\r\nDecaWave 1.0   up and running!\r\n");  
     dw.setEUI(0xFAEDCD01FAEDCD01);                                  // basic methods called to check if we have a working SPI connection
     pc.printf("DEVICE_ID register: 0x%X\r\n", dw.getDeviceID());
     pc.printf("EUI register: %016llX\r\n", dw.getEUI());
@@ -25,34 +20,65 @@
     pc.printf("System Configuration: %llX\r\n", dw.readRegister40(DW1000_SYS_CFG, 0));
     pc.printf("Size of Rangingframe: %d\r\n", sizeof(r.TX));
     
-    uint16_t setdelay = 32768; // TODO: = 2^15
+    uint16_t setdelay = 32953;//462; // TODO: = 2^15
     dw.writeRegister16(DW1000_TX_ANTD, 0, setdelay);
+    //dw.writeRegister16(DW1000_LDE_CTRL, 0x1804, setdelay / 2);
     pc.printf("Antenna Delay TX: %d\r\n", dw.readRegister16(DW1000_TX_ANTD, 0));
     pc.printf("Antenna Delay RX: %d\r\n", dw.readRegister16(DW1000_LDE_CTRL, 0x1804));
+    pc.printf("TX Power: %llX\r\n", dw.readRegister40(DW1000_TX_POWER, 0));
     //r.receiver = true;
     if (r.receiver)
         r.address = 1;
     else
         r.address = 0; // sender node has address 0
     pc.printf("Address: %d\r\n", r.address);
-    wait(2);
+    //wait(1);
     
     while(1) {
-        if (!r.receiver) {                  // Request ranging
-            for (int i = 1; i <= 4; i++) {
-                r.acknowledgement[i] = false;
-                r.requestRanging(i);            // TODO: ask all available nodes in MMRanging
-                time_before = LocalTimer.read();
-                while(!r.acknowledgement[i] && LocalTimer.read() < time_before + 0.5);
-                pc.printf("%f, ", (r.tofs[i]*300/MMRANGING_TIMEUNIT_US / 2) - 0.5);
-                //pc.printf("%2.6fs\r\n", LocalTimer.read() - time_before);
-            }
-            pc.printf("\r\n"); // logging output
+        if (!r.receiver) {
+            #if 1 // normal operation
+                r.requestRangingAll();
+                for (int i = 1; i <= 4; i++) {  // Request ranging to all anchors
+                    pc.printf("%f, ", r.distances[i]);
+                    //pc.printf("%f s\r\n", r.roundtriptimes[i]);
+                }
+                pc.printf("\r\n");
+            #endif
+            #if 0 // calibration of antennadelay
+                r.requestRanging(1);
+                if (r.distances[1] < 100) {
+                    rangings[index] = r.distances[1];
+                    //pc.printf("%f, ", r.distances[1]);
+                    index++;
+                }
+                if (index == 1000) {
+                    for(int i = 0; i < 999; i++)
+                        rangings[999] += rangings[i];
+                    float mean = rangings[999] / 1000;
+                    pc.printf("\r\n\r\nMean: %f Delay: %d\r\n\r\n", mean, setdelay);
+                    wait(2);
+                    if(mean > 5)
+                        setdelay += 10;
+                    else
+                        setdelay--;
+                    if(abs(mean-5) < 0.003f) {
+                        pc.printf("\r\n\r\nDELAY: %d\r\n\r\n", setdelay);
+                        return 0;
+                    }
+                    dw.writeRegister16(DW1000_TX_ANTD, 0, setdelay / 2);
+                    dw.writeRegister16(DW1000_LDE_CTRL, 0x1804, setdelay / 2);
+                    index = 0; 
+                }
+            #endif
         } else {
-            //pc.printf("%lld\r\n", r.timeDifference40Bit(r.rangingtimingsReceiver[0][0], r.rangingtimingsReceiver[0][1]));
+            //pc.printf("%lld\r\n", r.timeDifference40Bit(r.rangingtimingsReceiver[0][0], r.rangingtimingsReceiver[0][1]));  // debuging output to find 40Bit overflows on receiver side
         }
         
-        #if 0
+        #if 0 // averaging the distance with a ringbuffer
+            int average[50];
+            int average_index = 0;
+            float averaged;
+        
             pc.printf("Distance: %f\r\n", (r.tofs[2]*300/MMRANGING_TIMEUNIT_US / 2) - 0.5);
             average[average_index] = r.tofs[2];
             average_index++;
@@ -87,7 +113,7 @@
         //pc.printf("Status: %llX\r\n", dw.getStatus());
         //pc.printf("TX Control: %llX\r\n", dw.readRegister40(DW1000_TX_FCTRL, 0));
         //pc.printf("\r\n");
-        wait(0.02);
-        wait(0.2);
+        //wait(0.002);
+        //wait(0.2);
     }
 }
\ No newline at end of file