Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Sun Nov 23 15:37:45 2014 +0000
Revision:
14:8041c9b68406
Parent:
13:b4d27bf7062a
Child:
15:e1fea7e2aff1
Ping Pong implemented

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