k

Dependencies:   C12832 mbed

Fork of DecaWave by Matthias Grob & Manuel Stalder

Committer:
Kekehoho
Date:
Fri Aug 19 17:40:09 2016 +0000
Revision:
48:bf6c54dc69b3
Parent:
47:b6120c152ad1
j

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 24:6f25ba679490 1 // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
manumaet 0:f50e671ffff7 2 #include "mbed.h"
manumaet 26:a65c6f26c458 3 #include "PC.h" // Serial Port via USB for debugging with Terminal
manumaet 27:71178fdb78e1 4 #include "DW1000.h" // our DW1000 device driver
manumaet 44:2e0045042a59 5 #include "MM2WayRanging.h" // our self developed ranging application
Kekehoho 48:bf6c54dc69b3 6 //#include "C12832.h"
manumaet 44:2e0045042a59 7
manumaet 44:2e0045042a59 8 #define myprintf pc.printf // to make the code adaptable to other outputs that support printf
manumaet 44:2e0045042a59 9
Kekehoho 48:bf6c54dc69b3 10 Serial pc(USBTX, USBRX); // USB UART Terminal
Kekehoho 48:bf6c54dc69b3 11 //C12832 lcd(p5, p7, p6, p8, p11);
Kekehoho 48:bf6c54dc69b3 12 DW1000 dw(p5, p6, p7, p11, p15); // Device driver instanceSPI pins: (MOSI, MISO, SCLK, CS, IRQ)
manumaet 44:2e0045042a59 13 MM2WayRanging node(dw); // Instance of the two way ranging algorithm
manumaet 0:f50e671ffff7 14
manumaet 44:2e0045042a59 15 void rangeAndDisplayAll(){
manumaet 44:2e0045042a59 16 node.requestRangingAll(); // Request ranging to all anchors
manumaet 45:01a33363bc21 17 for (int i = 1; i <= 5; i++) { // Output Results
manumaet 45:01a33363bc21 18 myprintf("%f, ", node.distances[i]);
manumaet 45:01a33363bc21 19 //myprintf("T:%f", node.roundtriptimes[i]);
manumaet 45:01a33363bc21 20 //myprintf("\r\n");
manumaet 44:2e0045042a59 21 }
manumaet 45:01a33363bc21 22 myprintf("\r\n");
manumaet 44:2e0045042a59 23 }
manumaet 6:d5864a1b9e17 24
manumaet 45:01a33363bc21 25 void calibrationRanging(int destination){
manumaet 45:01a33363bc21 26
manumaet 45:01a33363bc21 27 const int numberOfRangings = 100;
manumaet 45:01a33363bc21 28 float rangings[numberOfRangings];
manumaet 45:01a33363bc21 29 int index = 0;
manumaet 45:01a33363bc21 30 float mean = 0;
manumaet 45:01a33363bc21 31 float start, stop;
manumaet 45:01a33363bc21 32
manumaet 45:01a33363bc21 33 Timer localTimer;
manumaet 45:01a33363bc21 34 localTimer.start();
manumaet 45:01a33363bc21 35
manumaet 45:01a33363bc21 36 start = localTimer.read();
manumaet 45:01a33363bc21 37
manumaet 45:01a33363bc21 38 while (1) {
manumaet 45:01a33363bc21 39
manumaet 45:01a33363bc21 40 node.requestRanging(destination);
manumaet 45:01a33363bc21 41 if(node.overflow){
manumaet 47:b6120c152ad1 42 myprintf("Overflow! Measured: %f\r\n", node.distances[destination]); // This is the output to see if a timer overflow was corrected by the two way ranging class
manumaet 45:01a33363bc21 43 }
manumaet 45:01a33363bc21 44
manumaet 45:01a33363bc21 45 if (node.distances[destination] == -1) {
manumaet 45:01a33363bc21 46 myprintf("Measurement timed out\r\n");
Kekehoho 48:bf6c54dc69b3 47 wait(0.1);
manumaet 45:01a33363bc21 48 continue;
manumaet 45:01a33363bc21 49 }
manumaet 45:01a33363bc21 50
manumaet 45:01a33363bc21 51 if (node.distances[destination] < 100) {
manumaet 45:01a33363bc21 52 rangings[index] = node.distances[destination];
manumaet 45:01a33363bc21 53 //myprintf("%f\r\n", node.distances[destination]);
manumaet 45:01a33363bc21 54 index++;
manumaet 45:01a33363bc21 55
manumaet 45:01a33363bc21 56 if (index == numberOfRangings) {
manumaet 45:01a33363bc21 57 stop = localTimer.read();
manumaet 45:01a33363bc21 58
manumaet 45:01a33363bc21 59 for (int i = 0; i < numberOfRangings - 1; i++)
manumaet 45:01a33363bc21 60 rangings[numberOfRangings - 1] += rangings[i];
manumaet 45:01a33363bc21 61
manumaet 45:01a33363bc21 62 mean = rangings[numberOfRangings - 1] / numberOfRangings;
manumaet 45:01a33363bc21 63 myprintf("\r\n\r\nMean %i: %f\r\n", destination, mean);
manumaet 45:01a33363bc21 64 myprintf("Elapsed Time for %i: %f\r\n", numberOfRangings, stop - start);
manumaet 45:01a33363bc21 65
manumaet 45:01a33363bc21 66 mean = 0;
manumaet 45:01a33363bc21 67 index = 0;
manumaet 45:01a33363bc21 68
manumaet 45:01a33363bc21 69 //wait(2);
manumaet 45:01a33363bc21 70
manumaet 45:01a33363bc21 71 start = localTimer.read();
manumaet 45:01a33363bc21 72 }
manumaet 45:01a33363bc21 73 }
manumaet 45:01a33363bc21 74
manumaet 45:01a33363bc21 75 else myprintf("%f\r\n", node.distances[destination]);
manumaet 45:01a33363bc21 76
manumaet 46:6398237672a0 77 }
manumaet 46:6398237672a0 78
manumaet 46:6398237672a0 79 }
manumaet 45:01a33363bc21 80
manumaet 46:6398237672a0 81 struct __attribute__((packed, aligned(1))) DistancesFrame {
manumaet 46:6398237672a0 82 uint8_t source;
manumaet 46:6398237672a0 83 uint8_t destination;
manumaet 46:6398237672a0 84 uint8_t type;
manumaet 46:6398237672a0 85 float dist[4];
manumaet 46:6398237672a0 86 };
manumaet 46:6398237672a0 87
manumaet 45:01a33363bc21 88
manumaet 46:6398237672a0 89
manumaet 46:6398237672a0 90 void altCallbackRX();
manumaet 45:01a33363bc21 91 // -----------------------------------------------------------------------------------------------
manumaet 45:01a33363bc21 92
manumaet 0:f50e671ffff7 93 int main() {
manumaet 44:2e0045042a59 94 pc.printf("\r\nDecaWave 1.0 up and running!\r\n"); // Splashscreen
manumaet 30:4ecc69d3cf8d 95 dw.setEUI(0xFAEDCD01FAEDCD01); // basic methods called to check if we have a working SPI connection
manumaet 28:a830131560e8 96 pc.printf("DEVICE_ID register: 0x%X\r\n", dw.getDeviceID());
manumaet 28:a830131560e8 97 pc.printf("EUI register: %016llX\r\n", dw.getEUI());
manumaet 38:8ef3b8d8b908 98 pc.printf("Voltage: %fV\r\n", dw.getVoltage());
manumaet 34:f56962030c5c 99
manumaet 47:b6120c152ad1 100 node.isAnchor = true; // declare as anchor or beacon
manumaet 47:b6120c152ad1 101
manumaet 44:2e0045042a59 102 if (node.isAnchor) {
Kekehoho 48:bf6c54dc69b3 103 node.address = 4;
manumaet 44:2e0045042a59 104 myprintf("This node is Anchor node %d \r\n", node.address);
manumaet 44:2e0045042a59 105 } else {
manumaet 44:2e0045042a59 106 node.address = 0;
manumaet 44:2e0045042a59 107 myprintf("This node is a Beacon. ");
manumaet 44:2e0045042a59 108 }
manumaet 32:041dd02e0e3b 109
manumaet 47:b6120c152ad1 110 if (node.address == 5){ // the node with address 5 was used as an observer node putting out the results in our test case
manumaet 47:b6120c152ad1 111 dw.setCallbacks(&altCallbackRX, NULL);
manumaet 47:b6120c152ad1 112 }
manumaet 46:6398237672a0 113
manumaet 0:f50e671ffff7 114 while(1) {
manumaet 45:01a33363bc21 115 if (!node.isAnchor){
manumaet 44:2e0045042a59 116 rangeAndDisplayAll();
manumaet 45:01a33363bc21 117 //calibrationRanging(4);
manumaet 47:b6120c152ad1 118 } else {
manumaet 47:b6120c152ad1 119 //myprintf("."); // to see if the core and output is working
manumaet 46:6398237672a0 120 wait(0.5);
manumaet 47:b6120c152ad1 121 }
manumaet 0:f50e671ffff7 122 }
manumaet 46:6398237672a0 123 }
manumaet 46:6398237672a0 124
manumaet 46:6398237672a0 125
manumaet 47:b6120c152ad1 126 void altCallbackRX() { // this callback was used in our verification test case for the observer node which only receives and outputs the resulting data
manumaet 46:6398237672a0 127
manumaet 46:6398237672a0 128 DistancesFrame distFrame;
manumaet 46:6398237672a0 129 float distances[4];
manumaet 46:6398237672a0 130 dw.readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)&distFrame, dw.getFramelength());
manumaet 46:6398237672a0 131
manumaet 47:b6120c152ad1 132 if (distFrame.destination == 5) {
manumaet 46:6398237672a0 133 for (int i = 0; i<4; i++){
manumaet 47:b6120c152ad1 134 myprintf("%f, ", distFrame.dist[i]);
manumaet 46:6398237672a0 135 }
manumaet 47:b6120c152ad1 136
manumaet 46:6398237672a0 137 myprintf("\r\n");
manumaet 46:6398237672a0 138 }
manumaet 46:6398237672a0 139 dw.startRX();
manumaet 46:6398237672a0 140 }