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:
Mon Mar 02 09:33:03 2015 +0000
Revision:
42:83931678c4de
Parent:
41:0a3bb028d4ba
Child:
43:d89fe237a684
after setting initialization parameters of Manual, moving Antenna delays to driver as standard value

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 41:0a3bb028d4ba 11 float rangings[1000];
manumaet 41:0a3bb028d4ba 12 int index = 0;
manumaet 6:d5864a1b9e17 13
manumaet 0:f50e671ffff7 14 int main() {
manumaet 41:0a3bb028d4ba 15 pc.printf("\r\nDecaWave 1.0 up and running!\r\n");
manumaet 30:4ecc69d3cf8d 16 dw.setEUI(0xFAEDCD01FAEDCD01); // basic methods called to check if we have a working SPI connection
manumaet 28:a830131560e8 17 pc.printf("DEVICE_ID register: 0x%X\r\n", dw.getDeviceID());
manumaet 28:a830131560e8 18 pc.printf("EUI register: %016llX\r\n", dw.getEUI());
manumaet 38:8ef3b8d8b908 19 pc.printf("Voltage: %fV\r\n", dw.getVoltage());
manumaet 38:8ef3b8d8b908 20 pc.printf("System Configuration: %llX\r\n", dw.readRegister40(DW1000_SYS_CFG, 0));
manumaet 38:8ef3b8d8b908 21 pc.printf("Size of Rangingframe: %d\r\n", sizeof(r.TX));
manumaet 34:f56962030c5c 22
manumaet 34:f56962030c5c 23 pc.printf("Antenna Delay TX: %d\r\n", dw.readRegister16(DW1000_TX_ANTD, 0));
manumaet 34:f56962030c5c 24 pc.printf("Antenna Delay RX: %d\r\n", dw.readRegister16(DW1000_LDE_CTRL, 0x1804));
manumaet 41:0a3bb028d4ba 25 pc.printf("TX Power: %llX\r\n", dw.readRegister40(DW1000_TX_POWER, 0));
manumaet 39:bb57aa77b015 26 //r.receiver = true;
manumaet 36:883de6f9a73b 27 if (r.receiver)
manumaet 40:5ce51b7e3118 28 r.address = 1;
manumaet 36:883de6f9a73b 29 else
manumaet 36:883de6f9a73b 30 r.address = 0; // sender node has address 0
manumaet 36:883de6f9a73b 31 pc.printf("Address: %d\r\n", r.address);
manumaet 41:0a3bb028d4ba 32 //wait(1);
manumaet 32:041dd02e0e3b 33
manumaet 0:f50e671ffff7 34 while(1) {
manumaet 41:0a3bb028d4ba 35 if (!r.receiver) {
manumaet 41:0a3bb028d4ba 36 #if 1 // normal operation
manumaet 41:0a3bb028d4ba 37 r.requestRangingAll();
manumaet 41:0a3bb028d4ba 38 for (int i = 1; i <= 4; i++) { // Request ranging to all anchors
manumaet 41:0a3bb028d4ba 39 pc.printf("%f, ", r.distances[i]);
manumaet 41:0a3bb028d4ba 40 //pc.printf("%f s\r\n", r.roundtriptimes[i]);
manumaet 41:0a3bb028d4ba 41 }
manumaet 41:0a3bb028d4ba 42 pc.printf("\r\n");
manumaet 41:0a3bb028d4ba 43 #endif
manumaet 41:0a3bb028d4ba 44 #if 0 // calibration of antennadelay
manumaet 41:0a3bb028d4ba 45 r.requestRanging(1);
manumaet 41:0a3bb028d4ba 46 if (r.distances[1] < 100) {
manumaet 41:0a3bb028d4ba 47 rangings[index] = r.distances[1];
manumaet 41:0a3bb028d4ba 48 //pc.printf("%f, ", r.distances[1]);
manumaet 41:0a3bb028d4ba 49 index++;
manumaet 41:0a3bb028d4ba 50 }
manumaet 41:0a3bb028d4ba 51 if (index == 1000) {
manumaet 41:0a3bb028d4ba 52 for(int i = 0; i < 999; i++)
manumaet 41:0a3bb028d4ba 53 rangings[999] += rangings[i];
manumaet 41:0a3bb028d4ba 54 float mean = rangings[999] / 1000;
manumaet 41:0a3bb028d4ba 55 pc.printf("\r\n\r\nMean: %f Delay: %d\r\n\r\n", mean, setdelay);
manumaet 41:0a3bb028d4ba 56 wait(2);
manumaet 41:0a3bb028d4ba 57 if(mean > 5)
manumaet 41:0a3bb028d4ba 58 setdelay += 10;
manumaet 41:0a3bb028d4ba 59 else
manumaet 41:0a3bb028d4ba 60 setdelay--;
manumaet 41:0a3bb028d4ba 61 if(abs(mean-5) < 0.003f) {
manumaet 41:0a3bb028d4ba 62 pc.printf("\r\n\r\nDELAY: %d\r\n\r\n", setdelay);
manumaet 41:0a3bb028d4ba 63 return 0;
manumaet 41:0a3bb028d4ba 64 }
manumaet 41:0a3bb028d4ba 65 dw.writeRegister16(DW1000_TX_ANTD, 0, setdelay / 2);
manumaet 41:0a3bb028d4ba 66 dw.writeRegister16(DW1000_LDE_CTRL, 0x1804, setdelay / 2);
manumaet 41:0a3bb028d4ba 67 index = 0;
manumaet 41:0a3bb028d4ba 68 }
manumaet 41:0a3bb028d4ba 69 #endif
manumaet 39:bb57aa77b015 70 } else {
manumaet 41:0a3bb028d4ba 71 //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 72 }
manumaet 36:883de6f9a73b 73
manumaet 41:0a3bb028d4ba 74 #if 0 // averaging the distance with a ringbuffer
manumaet 41:0a3bb028d4ba 75 int average[50];
manumaet 41:0a3bb028d4ba 76 int average_index = 0;
manumaet 41:0a3bb028d4ba 77 float averaged;
manumaet 41:0a3bb028d4ba 78
manumaet 40:5ce51b7e3118 79 pc.printf("Distance: %f\r\n", (r.tofs[2]*300/MMRANGING_TIMEUNIT_US / 2) - 0.5);
manumaet 40:5ce51b7e3118 80 average[average_index] = r.tofs[2];
manumaet 40:5ce51b7e3118 81 average_index++;
manumaet 40:5ce51b7e3118 82 if(average_index == 50)
manumaet 40:5ce51b7e3118 83 average_index = 0;
manumaet 40:5ce51b7e3118 84 for(int i = 0; i < 50; i++)
manumaet 40:5ce51b7e3118 85 averaged += average[i];
manumaet 40:5ce51b7e3118 86 averaged /= 50;
manumaet 40:5ce51b7e3118 87 pc.printf("Distance: %f\r\n", (averaged*300/MMRANGING_TIMEUNIT_US / 2) - 0.5);
manumaet 40:5ce51b7e3118 88 #endif
manumaet 38:8ef3b8d8b908 89
manumaet 39:bb57aa77b015 90 #if 0 // Output bars on console
manumaet 38:8ef3b8d8b908 91 for(int i = 1; i < 3; i++) {
manumaet 38:8ef3b8d8b908 92 //pc.printf("%f ", r.tofs[j]*MMRANGING_TIMEUNIT_NS);
manumaet 38:8ef3b8d8b908 93 pc.printf("%lld [", r.tofs[i]);
manumaet 38:8ef3b8d8b908 94 int dots = r.tofs[i]*70/1400;
manumaet 38:8ef3b8d8b908 95 if (abs(dots) < 10000)
manumaet 38:8ef3b8d8b908 96 for(int j = 0; j < dots; j++)
manumaet 38:8ef3b8d8b908 97 pc.printf("=");
manumaet 38:8ef3b8d8b908 98 pc.printf("]\r\n");
manumaet 38:8ef3b8d8b908 99 }
manumaet 38:8ef3b8d8b908 100 #endif
manumaet 36:883de6f9a73b 101
manumaet 40:5ce51b7e3118 102 #ifdef EVENTS // Output interrupt callback events for debugging (defined in MMRanging.h)
manumaet 36:883de6f9a73b 103 for(int j = 0; j < 10; j++)
manumaet 36:883de6f9a73b 104 if(r.event[j][0] == '!') {
manumaet 36:883de6f9a73b 105 pc.printf("%s\r\n", r.event[j]);
manumaet 36:883de6f9a73b 106 r.event[j][0] = 'X';
manumaet 36:883de6f9a73b 107 }
manumaet 36:883de6f9a73b 108 r.event_i = 0;
manumaet 36:883de6f9a73b 109 #endif
manumaet 39:bb57aa77b015 110 //pc.printf("Status: %llX\r\n", dw.getStatus());
manumaet 39:bb57aa77b015 111 //pc.printf("TX Control: %llX\r\n", dw.readRegister40(DW1000_TX_FCTRL, 0));
manumaet 38:8ef3b8d8b908 112 //pc.printf("\r\n");
manumaet 42:83931678c4de 113 wait(0.002);
manumaet 41:0a3bb028d4ba 114 //wait(0.2);
manumaet 0:f50e671ffff7 115 }
manumaet 0:f50e671ffff7 116 }