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 Nov 27 13:28:18 2014 +0000
Revision:
22:576ee999b004
Parent:
21:23bf4399020d
Child:
23:661a79e56208
class a bit rearanged for externel time measurement (only use for this)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 0:f50e671ffff7 1 #include "mbed.h"
manumaet 0:f50e671ffff7 2 #include "PC.h" // Serial Port via USB for debugging with Terminal
manumaet 0:f50e671ffff7 3 #include "DW1000.h"
manumaet 0:f50e671ffff7 4
manumaet 0:f50e671ffff7 5 PC pc(USBTX, USBRX, 921600); // USB UART Terminal
manumaet 22:576ee999b004 6 DW1000 dw(PA_7, PA_6, PA_5, PB_6, PB_9); // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
manumaet 22:576ee999b004 7
manumaet 22:576ee999b004 8 Timer LocalTimer; // debuging timer
manumaet 22:576ee999b004 9 int old_time = 0;
manumaet 0:f50e671ffff7 10
manumaet 15:e1fea7e2aff1 11 const float timeunit = 1/(128*499.2e6);
manumaet 14:8041c9b68406 12 int i=0;
manumaet 17:8afa5f9122da 13 char message[1200] = "";
manumaet 21:23bf4399020d 14 uint64_t TX_timestamp = 0;
manumaet 21:23bf4399020d 15 uint64_t RX_timestamp = 0;
manumaet 14:8041c9b68406 16
manumaet 18:bbc7ca7d3a95 17 void callbackRX(int framelength) {
manumaet 21:23bf4399020d 18 RX_timestamp = dw.readRegister40(DW1000_RX_TIME, 0);
manumaet 21:23bf4399020d 19 if (framelength < 200) {
manumaet 16:96879e1c99f2 20 char* receive = dw.receiveString(); // receive a string
manumaet 16:96879e1c99f2 21 pc.printf("Received: \"%s\" %d ", receive, framelength);
manumaet 22:576ee999b004 22 pc.printf("Status: %010llX ", dw.getStatus());
manumaet 21:23bf4399020d 23 #if 0
manumaet 21:23bf4399020d 24 sprintf(message, "ACK \"%s\"", receive);
manumaet 21:23bf4399020d 25 dw.sendString(message);
manumaet 21:23bf4399020d 26 wait(0.1);
manumaet 21:23bf4399020d 27 #endif
manumaet 16:96879e1c99f2 28 delete[] receive;
manumaet 16:96879e1c99f2 29 } else
manumaet 16:96879e1c99f2 30 pc.printf("Received! %d ", framelength);
manumaet 10:d077bb12d259 31
manumaet 21:23bf4399020d 32 uint64_t difference = RX_timestamp - TX_timestamp;
manumaet 15:e1fea7e2aff1 33 //pc.printf("Timestamp: %lld\r\n", difference);
manumaet 21:23bf4399020d 34 pc.printf("Time since TX: %fs\r\n", difference*timeunit); // TODO: gives some wrong values because of timer overflow
manumaet 17:8afa5f9122da 35 dw.startRX();
manumaet 6:d5864a1b9e17 36 }
manumaet 6:d5864a1b9e17 37
manumaet 18:bbc7ca7d3a95 38 void callbackTX() {
manumaet 22:576ee999b004 39 //TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
manumaet 22:576ee999b004 40 /*char messagecheck[1021];
manumaet 21:23bf4399020d 41 dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, 1021);
manumaet 18:bbc7ca7d3a95 42 if (i < 200)
manumaet 21:23bf4399020d 43 pc.printf("%d Sent: \"%s\" %d ", i, messagecheck, strlen(messagecheck)+1);
manumaet 18:bbc7ca7d3a95 44 else
manumaet 22:576ee999b004 45 pc.printf("%d Sent! %d ", i, strlen(messagecheck)+1);*/
manumaet 22:576ee999b004 46 pc.printf("SENT!! Status: %010llX Time: %d\r\n", dw.getStatus(), LocalTimer.read_us()-old_time);
manumaet 18:bbc7ca7d3a95 47 }
manumaet 18:bbc7ca7d3a95 48
manumaet 0:f50e671ffff7 49 int main() {
manumaet 22:576ee999b004 50 LocalTimer.start();
manumaet 20:257d56530ae1 51 pc.printf("DecaWave 0.1\r\nup and running!\r\n");
manumaet 20:257d56530ae1 52 dw.setEUI(0xFAEDCD01FAEDCD01); // basic methods called to check if we have a working SPI connection
manumaet 4:6240b9c7a033 53 pc.printf("%d DEVICE_ID register: 0x%X\r\n", i, dw.getDeviceID());
manumaet 4:6240b9c7a033 54 pc.printf("%d EUI register: %016llX\r\n", i, dw.getEUI());
manumaet 4:6240b9c7a033 55 pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
manumaet 0:f50e671ffff7 56
manumaet 18:bbc7ca7d3a95 57 dw.callbackRX = &callbackRX; // TODO: must not jump to NULL & setter
manumaet 18:bbc7ca7d3a95 58 dw.callbackTX = &callbackTX;
manumaet 14:8041c9b68406 59
manumaet 14:8041c9b68406 60 // Receiver initialisation
manumaet 22:576ee999b004 61 dw.writeRegister16(DW1000_SYS_MASK, 0, 0x4080); //| 0x0080); // TODO: RX only good frame 0x4000, RX all frames 0x2000, TX done 0x0080
manumaet 17:8afa5f9122da 62 dw.startRX();
manumaet 5:111f11c95d27 63
manumaet 0:f50e671ffff7 64 while(1) {
manumaet 19:e94bc88c1eb0 65 #if 1
manumaet 22:576ee999b004 66 //sprintf(message, "Hi %d", i);
manumaet 22:576ee999b004 67 sprintf(message, "012345678901234567890123456789012345678");
manumaet 22:576ee999b004 68 if ((i % 10) > 5) {
manumaet 22:576ee999b004 69 dw.sendString(message);
manumaet 22:576ee999b004 70 old_time = LocalTimer.read_us();
manumaet 22:576ee999b004 71 dw.writeRegister8(DW1000_SYS_CTRL, 0, 0x02);
manumaet 22:576ee999b004 72 pc.printf("%d Sent: \"%s\" %d \r\n", i, message, strlen(message)+1);
manumaet 22:576ee999b004 73 }
manumaet 17:8afa5f9122da 74 #endif
manumaet 15:e1fea7e2aff1 75 #if 0
manumaet 21:23bf4399020d 76 pc.printf("%d Waiting... %d %d ", i, dw.receiving, dw.sending);
manumaet 15:e1fea7e2aff1 77 wait(5);
manumaet 15:e1fea7e2aff1 78 #endif
manumaet 22:576ee999b004 79 wait(0.2);
manumaet 22:576ee999b004 80 //pc.printf("Status: %010llX\r\n", dw.getStatus());
manumaet 15:e1fea7e2aff1 81 i++;
manumaet 0:f50e671ffff7 82 }
manumaet 0:f50e671ffff7 83 }