Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Mon Nov 24 12:48:40 2014 +0000
Revision:
16:96879e1c99f2
Parent:
15:e1fea7e2aff1
Child:
17:8afa5f9122da
string of length 1021 works now (was timing problem), now adapt it to API so it always works

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 7:e634eeafc4d2 6 DW1000 dw(D11, D12, D13, D10, D14); // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
manumaet 0:f50e671ffff7 7
manumaet 15:e1fea7e2aff1 8 const float timeunit = 1/(128*499.2e6);
manumaet 14:8041c9b68406 9 int i=0;
manumaet 14:8041c9b68406 10 bool starter = 1;
manumaet 14:8041c9b68406 11 bool ping = 0;
manumaet 14:8041c9b68406 12 char message[1024] = "";
manumaet 11:c87d37db2c6f 13 uint64_t timestamp_old = 0;
manumaet 14:8041c9b68406 14
manumaet 15:e1fea7e2aff1 15 //#define PINGPONG
manumaet 5:111f11c95d27 16
manumaet 6:d5864a1b9e17 17 void Interrupthandler() {
manumaet 10:d077bb12d259 18 /*uint8_t frameready = 0;
manumaet 7:e634eeafc4d2 19 dw.readRegister(DW1000_SYS_STATUS, 1, &frameready, 1);
manumaet 10:d077bb12d259 20 pc.printf("Interrupt status: %X\r\n", frameready);*/
manumaet 10:d077bb12d259 21
manumaet 12:985aa9843c3c 22 uint16_t framelength = 0; // get framelength TODO: just for debugging of string
manumaet 10:d077bb12d259 23 dw.readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2);
manumaet 10:d077bb12d259 24 framelength &= 0x03FF;
manumaet 10:d077bb12d259 25 framelength -= 2;
manumaet 10:d077bb12d259 26
manumaet 16:96879e1c99f2 27 if(framelength<200) {
manumaet 16:96879e1c99f2 28 char* receive = dw.receiveString(); // receive a string
manumaet 16:96879e1c99f2 29 pc.printf("Received: \"%s\" %d ", receive, framelength);
manumaet 16:96879e1c99f2 30 delete[] receive;
manumaet 16:96879e1c99f2 31 } else
manumaet 16:96879e1c99f2 32 pc.printf("Received! %d ", framelength);
manumaet 10:d077bb12d259 33
manumaet 15:e1fea7e2aff1 34 # ifdef PINGPONG
manumaet 11:c87d37db2c6f 35 uint64_t status;
manumaet 11:c87d37db2c6f 36 dw.readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
manumaet 11:c87d37db2c6f 37 status &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 11:c87d37db2c6f 38 pc.printf("Status: %010llX ", status);
manumaet 11:c87d37db2c6f 39
manumaet 11:c87d37db2c6f 40 uint64_t timestamp;
manumaet 11:c87d37db2c6f 41 dw.readRegister(DW1000_RX_TIME, 0, (uint8_t*)&timestamp, 5);
manumaet 11:c87d37db2c6f 42 timestamp &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 11:c87d37db2c6f 43 uint64_t difference = timestamp - timestamp_old;
manumaet 11:c87d37db2c6f 44 timestamp_old = timestamp;
manumaet 15:e1fea7e2aff1 45 //pc.printf("Timestamp: %lld\r\n", difference);
manumaet 15:e1fea7e2aff1 46 pc.printf("Timestamp: %fs", difference*timeunit); // TODO: gives some wrong values because of timer overflow
manumaet 11:c87d37db2c6f 47
manumaet 15:e1fea7e2aff1 48 starter = 0;
manumaet 14:8041c9b68406 49 wait(1); // TODO: ugly, never wait in interrupthandler!
manumaet 14:8041c9b68406 50 if (ping) {
manumaet 14:8041c9b68406 51 sprintf((char*)message, "PING! %d", i);
manumaet 14:8041c9b68406 52 dw.sendString(message);
manumaet 14:8041c9b68406 53 } else {
manumaet 14:8041c9b68406 54 sprintf((char*)message, "PONG! %d", i);
manumaet 14:8041c9b68406 55 dw.sendString(message);
manumaet 14:8041c9b68406 56 }
manumaet 15:e1fea7e2aff1 57 #endif
manumaet 15:e1fea7e2aff1 58 pc.printf("\r\n");
manumaet 8:7a9c61242e2f 59 dw.receiveFrame();
manumaet 6:d5864a1b9e17 60 }
manumaet 6:d5864a1b9e17 61
manumaet 0:f50e671ffff7 62 int main() {
manumaet 0:f50e671ffff7 63 pc.printf("DecaWave 0.1\r\nup and running!\r\n");
manumaet 0:f50e671ffff7 64
manumaet 14:8041c9b68406 65 dw.setEUI(0xFAEDCD01FAEDCD01); // basic methods called to check if we have a slave
manumaet 4:6240b9c7a033 66 pc.printf("%d DEVICE_ID register: 0x%X\r\n", i, dw.getDeviceID());
manumaet 4:6240b9c7a033 67 pc.printf("%d EUI register: %016llX\r\n", i, dw.getEUI());
manumaet 4:6240b9c7a033 68 pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
manumaet 0:f50e671ffff7 69
manumaet 12:985aa9843c3c 70 uint32_t conf = 0; // read System Configuration
manumaet 4:6240b9c7a033 71 dw.readRegister(DW1000_SYS_CFG, 0, (uint8_t*)&conf, 4);
manumaet 4:6240b9c7a033 72 pc.printf("%d System Configuration: %X\r\n", i, conf);
manumaet 4:6240b9c7a033 73
manumaet 8:7a9c61242e2f 74 dw.callbackRX = &Interrupthandler;
manumaet 14:8041c9b68406 75
manumaet 14:8041c9b68406 76 // Receiver initialisation
manumaet 10:d077bb12d259 77 uint8_t dataframereadyinterrupt = 0x40; // only good frame 0x40 all frames 0x20
manumaet 5:111f11c95d27 78 dw.writeRegister(DW1000_SYS_MASK, 1, &dataframereadyinterrupt, 1);
manumaet 7:e634eeafc4d2 79
manumaet 8:7a9c61242e2f 80 dw.receiveFrame();
manumaet 15:e1fea7e2aff1 81 # ifdef PINGPONG
manumaet 14:8041c9b68406 82 wait(3);
manumaet 15:e1fea7e2aff1 83 # endif
manumaet 5:111f11c95d27 84
manumaet 0:f50e671ffff7 85 while(1) {
manumaet 15:e1fea7e2aff1 86 # ifndef PINGPONG
manumaet 15:e1fea7e2aff1 87 message[i] = 48+ (i%10);
manumaet 15:e1fea7e2aff1 88 message[i+1] = '\0';
manumaet 15:e1fea7e2aff1 89
manumaet 15:e1fea7e2aff1 90 pc.printf("%d Message: \"%s\" %d\r\n", i, message, strlen(message)+1);
manumaet 16:96879e1c99f2 91
manumaet 16:96879e1c99f2 92 //wait(0.1);
manumaet 15:e1fea7e2aff1 93 char messagecheck[1024];
manumaet 10:d077bb12d259 94 dw.sendString(message);
manumaet 15:e1fea7e2aff1 95 wait(0.1);
manumaet 10:d077bb12d259 96 dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, strlen(message)+1);
manumaet 15:e1fea7e2aff1 97 pc.printf("%d nBuffer: \"%s\" %d\r\n\r\n", i, messagecheck, strlen(messagecheck)+1);
manumaet 16:96879e1c99f2 98 //wait(0.1);
manumaet 15:e1fea7e2aff1 99
manumaet 15:e1fea7e2aff1 100 //return 0;
manumaet 9:c8839de428ac 101 /*for(int i=0; i<10; i++) { // to control Voltage
manumaet 9:c8839de428ac 102 pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
manumaet 9:c8839de428ac 103 wait(0.2);
manumaet 9:c8839de428ac 104 }*/
manumaet 15:e1fea7e2aff1 105 #else
manumaet 14:8041c9b68406 106 if (starter) {
manumaet 14:8041c9b68406 107 ping = 1;
manumaet 14:8041c9b68406 108 while (starter) {
manumaet 14:8041c9b68406 109 sprintf((char*)message, "PING! %d", i);
manumaet 14:8041c9b68406 110 dw.sendString(message);
manumaet 14:8041c9b68406 111 wait(1);
manumaet 14:8041c9b68406 112 }
manumaet 14:8041c9b68406 113 }
manumaet 14:8041c9b68406 114
manumaet 15:e1fea7e2aff1 115 wait(1);
manumaet 15:e1fea7e2aff1 116 # endif
manumaet 15:e1fea7e2aff1 117 #if 0
manumaet 15:e1fea7e2aff1 118 pc.printf("%d Waiting... ", i);
manumaet 11:c87d37db2c6f 119 uint64_t status;
manumaet 11:c87d37db2c6f 120 dw.readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
manumaet 11:c87d37db2c6f 121 status &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 11:c87d37db2c6f 122 pc.printf("Status: %010llX\r\n", status);
manumaet 15:e1fea7e2aff1 123 wait(5);
manumaet 15:e1fea7e2aff1 124 #endif
manumaet 15:e1fea7e2aff1 125 i++;
manumaet 0:f50e671ffff7 126 }
manumaet 0:f50e671ffff7 127 }