This is the DW1000 driver and our self developed distance measurement application based on it. We do this as a semester thesis at ETH Zürich under the Automatic Control Laboratory in the Department of electrical engineering.

Dependencies:   mbed

Committer:
manumaet
Date:
Thu Mar 05 10:26:51 2015 +0000
Revision:
43:d89fe237a684
Parent:
42:83931678c4de
Child:
44:2e0045042a59
before fusion with two way ranging

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 24:6f25ba679490 1 // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
manumaet 0:f50e671ffff7 2 #include "mbed.h"
manumaet 26:a65c6f26c458 3 #include "PC.h" // Serial Port via USB for debugging with Terminal
manumaet 27:71178fdb78e1 4 #include "DW1000.h" // our DW1000 device driver
manumaet 27:71178fdb78e1 5 #include "MMRanging.h" // our self developed raning application
manumaet 0:f50e671ffff7 6
manumaet 26:a65c6f26c458 7 PC pc(USBTX, USBRX, 921600); // USB UART Terminal
manumaet 30:4ecc69d3cf8d 8 DW1000 dw(PA_7, PA_6, PA_5, PB_6, PB_9); // Device driver instanceSPI pins: (MOSI, MISO, SCLK, CS, IRQ)
manumaet 28:a830131560e8 9 MMRanging r(dw); // Ranging class for getting distances and later positions
manumaet 14:8041c9b68406 10
manumaet 43:d89fe237a684 11 int average_index = 0;
manumaet 6:d5864a1b9e17 12
manumaet 0:f50e671ffff7 13 int main() {
manumaet 41:0a3bb028d4ba 14 pc.printf("\r\nDecaWave 1.0 up and running!\r\n");
manumaet 30:4ecc69d3cf8d 15 dw.setEUI(0xFAEDCD01FAEDCD01); // basic methods called to check if we have a working SPI connection
manumaet 28:a830131560e8 16 pc.printf("DEVICE_ID register: 0x%X\r\n", dw.getDeviceID());
manumaet 28:a830131560e8 17 pc.printf("EUI register: %016llX\r\n", dw.getEUI());
manumaet 38:8ef3b8d8b908 18 pc.printf("Voltage: %fV\r\n", dw.getVoltage());
manumaet 38:8ef3b8d8b908 19 pc.printf("System Configuration: %llX\r\n", dw.readRegister40(DW1000_SYS_CFG, 0));
manumaet 38:8ef3b8d8b908 20 pc.printf("Size of Rangingframe: %d\r\n", sizeof(r.TX));
manumaet 34:f56962030c5c 21
manumaet 34:f56962030c5c 22 pc.printf("Antenna Delay TX: %d\r\n", dw.readRegister16(DW1000_TX_ANTD, 0));
manumaet 34:f56962030c5c 23 pc.printf("Antenna Delay RX: %d\r\n", dw.readRegister16(DW1000_LDE_CTRL, 0x1804));
manumaet 41:0a3bb028d4ba 24 pc.printf("TX Power: %llX\r\n", dw.readRegister40(DW1000_TX_POWER, 0));
manumaet 39:bb57aa77b015 25 //r.receiver = true;
manumaet 36:883de6f9a73b 26 if (r.receiver)
manumaet 40:5ce51b7e3118 27 r.address = 1;
manumaet 36:883de6f9a73b 28 else
manumaet 36:883de6f9a73b 29 r.address = 0; // sender node has address 0
manumaet 36:883de6f9a73b 30 pc.printf("Address: %d\r\n", r.address);
manumaet 41:0a3bb028d4ba 31 //wait(1);
manumaet 32:041dd02e0e3b 32
manumaet 0:f50e671ffff7 33 while(1) {
manumaet 41:0a3bb028d4ba 34 if (!r.receiver) {
manumaet 43:d89fe237a684 35 #if 0 // normal operation
manumaet 41:0a3bb028d4ba 36 r.requestRangingAll();
manumaet 41:0a3bb028d4ba 37 for (int i = 1; i <= 4; i++) { // Request ranging to all anchors
manumaet 41:0a3bb028d4ba 38 pc.printf("%f, ", r.distances[i]);
manumaet 41:0a3bb028d4ba 39 //pc.printf("%f s\r\n", r.roundtriptimes[i]);
manumaet 41:0a3bb028d4ba 40 }
manumaet 41:0a3bb028d4ba 41 pc.printf("\r\n");
manumaet 41:0a3bb028d4ba 42 #endif
manumaet 41:0a3bb028d4ba 43 #if 0 // calibration of antennadelay
manumaet 43:d89fe237a684 44 float rangings[1000];
manumaet 43:d89fe237a684 45 int index = 0;
manumaet 43:d89fe237a684 46
manumaet 41:0a3bb028d4ba 47 r.requestRanging(1);
manumaet 41:0a3bb028d4ba 48 if (r.distances[1] < 100) {
manumaet 41:0a3bb028d4ba 49 rangings[index] = r.distances[1];
manumaet 41:0a3bb028d4ba 50 //pc.printf("%f, ", r.distances[1]);
manumaet 41:0a3bb028d4ba 51 index++;
manumaet 41:0a3bb028d4ba 52 }
manumaet 41:0a3bb028d4ba 53 if (index == 1000) {
manumaet 41:0a3bb028d4ba 54 for(int i = 0; i < 999; i++)
manumaet 41:0a3bb028d4ba 55 rangings[999] += rangings[i];
manumaet 41:0a3bb028d4ba 56 float mean = rangings[999] / 1000;
manumaet 41:0a3bb028d4ba 57 pc.printf("\r\n\r\nMean: %f Delay: %d\r\n\r\n", mean, setdelay);
manumaet 41:0a3bb028d4ba 58 wait(2);
manumaet 41:0a3bb028d4ba 59 if(mean > 5)
manumaet 41:0a3bb028d4ba 60 setdelay += 10;
manumaet 41:0a3bb028d4ba 61 else
manumaet 41:0a3bb028d4ba 62 setdelay--;
manumaet 41:0a3bb028d4ba 63 if(abs(mean-5) < 0.003f) {
manumaet 41:0a3bb028d4ba 64 pc.printf("\r\n\r\nDELAY: %d\r\n\r\n", setdelay);
manumaet 41:0a3bb028d4ba 65 return 0;
manumaet 41:0a3bb028d4ba 66 }
manumaet 41:0a3bb028d4ba 67 dw.writeRegister16(DW1000_TX_ANTD, 0, setdelay / 2);
manumaet 41:0a3bb028d4ba 68 dw.writeRegister16(DW1000_LDE_CTRL, 0x1804, setdelay / 2);
manumaet 41:0a3bb028d4ba 69 index = 0;
manumaet 41:0a3bb028d4ba 70 }
manumaet 41:0a3bb028d4ba 71 #endif
manumaet 39:bb57aa77b015 72 } else {
manumaet 41:0a3bb028d4ba 73 //pc.printf("%lld\r\n", r.timeDifference40Bit(r.rangingtimingsReceiver[0][0], r.rangingtimingsReceiver[0][1])); // debuging output to find 40Bit overflows on receiver side
manumaet 36:883de6f9a73b 74 }
manumaet 36:883de6f9a73b 75
manumaet 43:d89fe237a684 76 #if 1 // averaging the distance with a ringbuffer
manumaet 43:d89fe237a684 77 float averaged = 0;
manumaet 43:d89fe237a684 78 float average[1000];
manumaet 43:d89fe237a684 79
manumaet 43:d89fe237a684 80 int i = 1;
manumaet 43:d89fe237a684 81 r.requestRanging(i);
manumaet 43:d89fe237a684 82 average[average_index] = r.distances[i];
manumaet 40:5ce51b7e3118 83 average_index++;
manumaet 43:d89fe237a684 84 if(average_index == 1000) {
manumaet 40:5ce51b7e3118 85 average_index = 0;
manumaet 43:d89fe237a684 86 for(int j = 0; j < 1000; j++)
manumaet 43:d89fe237a684 87 averaged += average[j];
manumaet 43:d89fe237a684 88 averaged /= 1000;
manumaet 43:d89fe237a684 89 pc.printf("Distance: %f\r\n", averaged);
manumaet 43:d89fe237a684 90 pc.printf("Calibrated: %f\r\n", -0.123 *averaged*averaged + 2.564 * averaged - 5.332 );
manumaet 43:d89fe237a684 91 }
manumaet 40:5ce51b7e3118 92 #endif
manumaet 38:8ef3b8d8b908 93
manumaet 39:bb57aa77b015 94 #if 0 // Output bars on console
manumaet 38:8ef3b8d8b908 95 for(int i = 1; i < 3; i++) {
manumaet 38:8ef3b8d8b908 96 pc.printf("%lld [", r.tofs[i]);
manumaet 38:8ef3b8d8b908 97 int dots = r.tofs[i]*70/1400;
manumaet 38:8ef3b8d8b908 98 if (abs(dots) < 10000)
manumaet 38:8ef3b8d8b908 99 for(int j = 0; j < dots; j++)
manumaet 38:8ef3b8d8b908 100 pc.printf("=");
manumaet 38:8ef3b8d8b908 101 pc.printf("]\r\n");
manumaet 38:8ef3b8d8b908 102 }
manumaet 38:8ef3b8d8b908 103 #endif
manumaet 36:883de6f9a73b 104
manumaet 40:5ce51b7e3118 105 #ifdef EVENTS // Output interrupt callback events for debugging (defined in MMRanging.h)
manumaet 36:883de6f9a73b 106 for(int j = 0; j < 10; j++)
manumaet 36:883de6f9a73b 107 if(r.event[j][0] == '!') {
manumaet 36:883de6f9a73b 108 pc.printf("%s\r\n", r.event[j]);
manumaet 36:883de6f9a73b 109 r.event[j][0] = 'X';
manumaet 36:883de6f9a73b 110 }
manumaet 36:883de6f9a73b 111 r.event_i = 0;
manumaet 36:883de6f9a73b 112 #endif
manumaet 39:bb57aa77b015 113 //pc.printf("Status: %llX\r\n", dw.getStatus());
manumaet 39:bb57aa77b015 114 //pc.printf("TX Control: %llX\r\n", dw.readRegister40(DW1000_TX_FCTRL, 0));
manumaet 38:8ef3b8d8b908 115 //pc.printf("\r\n");
manumaet 42:83931678c4de 116 wait(0.002);
manumaet 41:0a3bb028d4ba 117 //wait(0.2);
manumaet 0:f50e671ffff7 118 }
manumaet 0:f50e671ffff7 119 }