Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Sun Mar 01 20:19:38 2015 +0000
Revision:
41:0a3bb028d4ba
Parent:
40:5ce51b7e3118
Child:
43:d89fe237a684
before following exactly manual initializing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 27:71178fdb78e1 1 #include "MMRanging.h"
manumaet 27:71178fdb78e1 2
manumaet 34:f56962030c5c 3 MMRanging::MMRanging(DW1000& DW) : dw(DW) {
manumaet 28:a830131560e8 4 event_i = 0;
manumaet 28:a830131560e8 5 counter = 0;
manumaet 29:019ff388ed76 6 dw.setCallbacks(this, &MMRanging::callbackRX, &MMRanging::callbackTX);
manumaet 40:5ce51b7e3118 7 for (int i = 0; i < 10; i++)
manumaet 40:5ce51b7e3118 8 acknowledgement[i] = true;
manumaet 41:0a3bb028d4ba 9 LocalTimer.start();
manumaet 28:a830131560e8 10 dw.startRX();
manumaet 27:71178fdb78e1 11 }
manumaet 28:a830131560e8 12
manumaet 28:a830131560e8 13 void MMRanging::callbackRX() {
manumaet 32:041dd02e0e3b 14 rangingframe RX;
manumaet 32:041dd02e0e3b 15 dw.readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)&RX, dw.getFramelength()); // get data from buffer
manumaet 31:6f76f3d518ac 16
manumaet 36:883de6f9a73b 17 if (RX.destination == address) // only if received packet is for me
manumaet 36:883de6f9a73b 18 switch (RX.type) {
manumaet 36:883de6f9a73b 19 case 1:
manumaet 36:883de6f9a73b 20 rangingtimingsReceiver[RX.source][0] = dw.getRXTimestamp();
manumaet 36:883de6f9a73b 21 sendRangingframe(RX.source, RX.sequence_number, 2, 0);
manumaet 36:883de6f9a73b 22 break;
manumaet 36:883de6f9a73b 23 case 2:
manumaet 36:883de6f9a73b 24 rangingtimingsSender[RX.source][1] = dw.getRXTimestamp();
manumaet 36:883de6f9a73b 25 sendRangingframe(RX.source, counter, 3, 0);
manumaet 36:883de6f9a73b 26 counter++;
manumaet 36:883de6f9a73b 27 break;
manumaet 36:883de6f9a73b 28 case 3:
manumaet 39:bb57aa77b015 29 sendRangingframe(RX.source, RX.sequence_number, 4, timeDifference40Bit(rangingtimingsReceiver[RX.source][0], rangingtimingsReceiver[RX.source][1]));
manumaet 36:883de6f9a73b 30 break;
manumaet 36:883de6f9a73b 31 case 4:
manumaet 39:bb57aa77b015 32 tofs[RX.source] = timeDifference40Bit(rangingtimingsSender[RX.source][0], rangingtimingsSender[RX.source][1]) - RX.time_difference_receiver;
manumaet 40:5ce51b7e3118 33 acknowledgement[RX.source] = true;
manumaet 36:883de6f9a73b 34 break;
manumaet 36:883de6f9a73b 35 default : break;
manumaet 36:883de6f9a73b 36 }
manumaet 36:883de6f9a73b 37
manumaet 40:5ce51b7e3118 38 #ifdef EVENTS
manumaet 40:5ce51b7e3118 39 sprintf(event[event_i], "!R %d>%d / %d %d", RX.source, RX.destination, RX.sequence_number, RX.type);
manumaet 40:5ce51b7e3118 40 if (event_i == 8)
manumaet 40:5ce51b7e3118 41 event_i = 0;
manumaet 40:5ce51b7e3118 42 else
manumaet 40:5ce51b7e3118 43 event_i++;
manumaet 40:5ce51b7e3118 44 #endif
manumaet 31:6f76f3d518ac 45
manumaet 28:a830131560e8 46 dw.startRX();
manumaet 28:a830131560e8 47 }
manumaet 28:a830131560e8 48
manumaet 28:a830131560e8 49 void MMRanging::callbackTX() {
manumaet 31:6f76f3d518ac 50 switch (TX.type) {
manumaet 31:6f76f3d518ac 51 case 1:
manumaet 36:883de6f9a73b 52 rangingtimingsSender[TX.destination][0] = dw.getTXTimestamp();
manumaet 31:6f76f3d518ac 53 break;
manumaet 31:6f76f3d518ac 54 case 2:
manumaet 36:883de6f9a73b 55 rangingtimingsReceiver[TX.destination][1] = dw.getTXTimestamp();
manumaet 31:6f76f3d518ac 56 break;
manumaet 31:6f76f3d518ac 57 default: break;
manumaet 31:6f76f3d518ac 58 }
manumaet 31:6f76f3d518ac 59
manumaet 40:5ce51b7e3118 60 #ifdef EVENTS
manumaet 40:5ce51b7e3118 61 sprintf(event[event_i], "!S %d>%d / %d %d", TX.source, TX.destination, TX.sequence_number, TX.type);
manumaet 40:5ce51b7e3118 62 if (event_i == 8)
manumaet 40:5ce51b7e3118 63 event_i = 0;
manumaet 40:5ce51b7e3118 64 else
manumaet 40:5ce51b7e3118 65 event_i++;
manumaet 40:5ce51b7e3118 66 #endif
manumaet 28:a830131560e8 67 }
manumaet 28:a830131560e8 68
manumaet 36:883de6f9a73b 69 void MMRanging::requestRanging(uint8_t destination) {
manumaet 41:0a3bb028d4ba 70 acknowledgement[destination] = false;
manumaet 41:0a3bb028d4ba 71 float time_before = LocalTimer.read();
manumaet 36:883de6f9a73b 72 sendRangingframe(destination, counter, 1, 0);
manumaet 41:0a3bb028d4ba 73 while(!acknowledgement[destination] && (LocalTimer.read() < time_before + 0.5f)); // wait for succeeding ranging or timeout
manumaet 41:0a3bb028d4ba 74 roundtriptimes[destination] = LocalTimer.read() - time_before;
manumaet 41:0a3bb028d4ba 75 distances[destination] = (tofs[destination] * 300 / MMRANGING_TIMEUNIT_US / 2);
manumaet 41:0a3bb028d4ba 76 }
manumaet 41:0a3bb028d4ba 77
manumaet 41:0a3bb028d4ba 78 void MMRanging::requestRangingAll() {
manumaet 41:0a3bb028d4ba 79 for (int i = 1; i <= 4; i++) { // Request ranging to all anchors
manumaet 41:0a3bb028d4ba 80 requestRanging(i);
manumaet 41:0a3bb028d4ba 81 }
manumaet 31:6f76f3d518ac 82 }
manumaet 31:6f76f3d518ac 83
manumaet 36:883de6f9a73b 84 void MMRanging::sendRangingframe(uint8_t destination, uint8_t sequence_number, uint8_t type, uint64_t time_difference_receiver) {
manumaet 36:883de6f9a73b 85 TX.source = address;
manumaet 36:883de6f9a73b 86 TX.destination = destination;
manumaet 36:883de6f9a73b 87 TX.sequence_number = sequence_number;
manumaet 34:f56962030c5c 88 TX.type = type;
manumaet 34:f56962030c5c 89 TX.time_difference_receiver = time_difference_receiver;
manumaet 34:f56962030c5c 90 dw.sendFrame((uint8_t*)&TX, sizeof(TX));
manumaet 39:bb57aa77b015 91 }
manumaet 39:bb57aa77b015 92
manumaet 39:bb57aa77b015 93 uint64_t MMRanging::timeDifference40Bit(uint64_t early, uint64_t late) {
manumaet 39:bb57aa77b015 94 int64_t difference = late - early;
manumaet 39:bb57aa77b015 95 if ((difference < -MMRANGING_2POWER40+10000000000) && (difference > -MMRANGING_2POWER40-10000000000)) // if the timestamps differ a negative word length +- ~1sec that was potentially measured, correct it
manumaet 39:bb57aa77b015 96 return difference + MMRANGING_2POWER40;
manumaet 39:bb57aa77b015 97 if ((difference < 0) || (difference > 10000000000))
manumaet 39:bb57aa77b015 98 return 10000000000;
manumaet 39:bb57aa77b015 99 return (uint64_t)difference;
manumaet 28:a830131560e8 100 }