Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

main.cpp

Committer:
manumaet
Date:
2014-11-27
Revision:
22:576ee999b004
Parent:
21:23bf4399020d
Child:
23:661a79e56208

File content as of revision 22:576ee999b004:

#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(PA_7, PA_6, PA_5, PB_6, PB_9);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)

Timer       LocalTimer;                 // debuging timer
int         old_time = 0;

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);
        pc.printf("Status: %010llX  ", dw.getStatus());
#if 0
        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("SENT!! Status: %010llX Time: %d\r\n", dw.getStatus(), LocalTimer.read_us()-old_time);
}

int main() {
    LocalTimer.start();
    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, 0x4080); //| 0x0080); // TODO: RX only good frame 0x4000, RX all frames 0x2000, TX done 0x0080
    dw.startRX();
    
    while(1) {
#if 1
        //sprintf(message, "Hi %d", i);
        sprintf(message, "012345678901234567890123456789012345678");
        if ((i % 10) > 5) {
            dw.sendString(message);
            old_time = LocalTimer.read_us();
            dw.writeRegister8(DW1000_SYS_CTRL, 0, 0x02);
            pc.printf("%d Sent: \"%s\" %d \r\n", i, message, strlen(message)+1);
        }
#endif
#if 0
        pc.printf("%d Waiting... %d %d ", i, dw.receiving, dw.sending);
        wait(5);
#endif
        wait(0.2);
        //pc.printf("Status: %010llX\r\n", dw.getStatus());
        i++;
    }
}