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

main.cpp

Committer:
manumaet
Date:
2014-11-26
Revision:
21:23bf4399020d
Parent:
20:257d56530ae1
Child:
22:576ee999b004

File content as of revision 21:23bf4399020d:

#include "mbed.h"
#include "PC.h"                         // Serial Port via USB for debugging with Terminal
#include "DW1000.h"

PC          pc(USBTX, USBRX, 921600);   // USB UART Terminal
DW1000      dw(D11, D12, D13, D10, D14);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)

const float timeunit = 1/(128*499.2e6);
int i=0;
char message[1200] = "";
uint64_t TX_timestamp = 0;
uint64_t RX_timestamp = 0;

void callbackRX(int framelength) {
    RX_timestamp = dw.readRegister40(DW1000_RX_TIME, 0);
    if (framelength < 200) {
        char* receive = dw.receiveString();                             // receive a string
        pc.printf("Received: \"%s\" %d ", receive, framelength);
#if 0
        pc.printf("Status: %010llX  ", dw.getStatus());
        sprintf(message, "ACK \"%s\"", receive);
        dw.sendString(message);
        wait(0.1);
#endif
        delete[] receive;
    } else
        pc.printf("Received! %d ", framelength);
    
    uint64_t difference = RX_timestamp - TX_timestamp;
    //pc.printf("Timestamp: %lld\r\n", difference);
    pc.printf("Time since TX: %fs\r\n", difference*timeunit);       // TODO: gives some wrong values because of timer overflow
    dw.startRX();
}

void callbackTX() {
    TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
    char messagecheck[1021];
    dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, 1021);
    if (i < 200)
        pc.printf("%d Sent: \"%s\" %d ", i, messagecheck, strlen(messagecheck)+1);
    else
        pc.printf("%d Sent! %d ", i, strlen(messagecheck)+1);
    pc.printf("Status: %010llX\r\n", dw.getStatus());
}

int main() {
    pc.printf("DecaWave 0.1\r\nup and running!\r\n");  
    dw.setEUI(0xFAEDCD01FAEDCD01);                  // basic methods called to check if we have a working SPI connection
    pc.printf("%d DEVICE_ID register: 0x%X\r\n", i, dw.getDeviceID());
    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, 0x4000); //| 0x0080); // TODO: RX only good frame 0x4000, RX all frames 0x2000, TX done 0x0080
    dw.startRX();
    
    while(1) {
#if 1
        wait(10);
        sprintf(message, "Hi %d", i);
        dw.sendString(message);
        wait(0.3);
#endif
#if 0
        pc.printf("%d Waiting... %d %d ", i, dw.receiving, dw.sending);
        pc.printf("Status: %010llX\r\n", dw.getStatus());
        wait(5);
#endif
        i++;
    }
}