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-28
Revision:
27:71178fdb78e1
Parent:
26:a65c6f26c458
Child:
28:a830131560e8

File content as of revision 27:71178fdb78e1:

// by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
#include "mbed.h"
#include "PC.h"                                     // Serial Port via USB for debugging with Terminal
#include "DW1000.h"                                 // our DW1000 device driver
#include "MMRanging.h"                              // our self developed raning application

//#define RECEIVER

PC          pc(USBTX, USBRX, 921600);               // USB UART Terminal
DW1000      dw(PA_7, PA_6, PA_5, PB_6, PB_9);       // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
MMRanging   ranging(&dw);                            // Ranging class for getting distances and later positions

const float timeunit = 1/(128*499.2);               // conversion between LSB of TX and RX timestamps and microseconds
int i=0;
char message[1021] = "";
char messageRX[1021] = "";
uint64_t TX_timestamp;
uint64_t RX_timestamp;
int event_i = 0;
char event[10][20];
uint64_t eventtimes[10];

void callbackRX(int framelength) {
    RX_timestamp = dw.getRXTimestamp();
    dw.receiveString(messageRX);
#ifdef RECEIVER
    message[0] = 'A';                               // acknowledge messages
    for(int i = 0; i < 10; i++)
        message[i+1] = messageRX[i];
    dw.sendString(message);
#endif
    eventtimes[event_i] = RX_timestamp - TX_timestamp;                      // TODO: can give some wrong values because of timer reset after 17 seconds
    event[event_i][0] = '!';
    event[event_i][1] = 'R';
    event[event_i][2] = ' ';
    for(int i = 0; i < 10; i++)
        event[event_i][i+3] = messageRX[i];
    if (event_i == 8)
        event_i = 0;
    else
        event_i++;
    dw.startRX();
}

void callbackTX() {
    TX_timestamp = dw.getTXTimestamp();
    eventtimes[event_i] = 0;
    event[event_i][0] = '!';
    event[event_i][1] = 'S';
    event[event_i][2] = ' ';
    for(int i = 0; i < 10; i++)
        event[event_i][i+3] = message[i];
    if (event_i == 8)
        event_i = 0;
    else
        event_i++;
}

int main() {
    pc.printf("DecaWave 0.2\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.setCallbacks(&callbackRX, &callbackTX);
    dw.setInterrupt(true, true);
    dw.startRX();
    
    while(1) {
        for(int j = 0; j < 10; j++)
            if(event[j][0] == '!') {
                pc.printf("%s Time: %fus\r\n", event[j], eventtimes[j]*timeunit);
                event[j][0] = 'X';
            }    
#ifndef RECEIVER
        sprintf(message, "%d", i);                  // send numbers to acknowledge
        dw.sendString(message);
        wait(1);
#endif
        i++;
    }
}