Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Sun Feb 22 17:40:38 2015 +0000
Revision:
39:bb57aa77b015
Parent:
38:8ef3b8d8b908
Child:
40:5ce51b7e3118
setup to find the 40 bit overflow in the timestamps, seems to be fixed

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 28:a830131560e8 7 dw.startRX();
manumaet 27:71178fdb78e1 8 }
manumaet 28:a830131560e8 9
manumaet 28:a830131560e8 10 void MMRanging::callbackRX() {
manumaet 32:041dd02e0e3b 11 rangingframe RX;
manumaet 32:041dd02e0e3b 12 dw.readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)&RX, dw.getFramelength()); // get data from buffer
manumaet 31:6f76f3d518ac 13
manumaet 36:883de6f9a73b 14 if (RX.destination == address) // only if received packet is for me
manumaet 36:883de6f9a73b 15 switch (RX.type) {
manumaet 36:883de6f9a73b 16 case 1:
manumaet 36:883de6f9a73b 17 rangingtimingsReceiver[RX.source][0] = dw.getRXTimestamp();
manumaet 36:883de6f9a73b 18 sendRangingframe(RX.source, RX.sequence_number, 2, 0);
manumaet 36:883de6f9a73b 19 break;
manumaet 36:883de6f9a73b 20 case 2:
manumaet 36:883de6f9a73b 21 rangingtimingsSender[RX.source][1] = dw.getRXTimestamp();
manumaet 36:883de6f9a73b 22 sendRangingframe(RX.source, counter, 3, 0);
manumaet 36:883de6f9a73b 23 counter++;
manumaet 36:883de6f9a73b 24 break;
manumaet 36:883de6f9a73b 25 case 3:
manumaet 39:bb57aa77b015 26 //sendRangingframe(RX.source, RX.sequence_number, 4, rangingtimingsReceiver[RX.source][1] - rangingtimingsReceiver[RX.source][0]);
manumaet 39:bb57aa77b015 27 sendRangingframe(RX.source, RX.sequence_number, 4, timeDifference40Bit(rangingtimingsReceiver[RX.source][0], rangingtimingsReceiver[RX.source][1]));
manumaet 36:883de6f9a73b 28 break;
manumaet 36:883de6f9a73b 29 case 4:
manumaet 39:bb57aa77b015 30 //tofs[RX.source] = (rangingtimingsSender[RX.source][1] - rangingtimingsSender[RX.source][0] - RX.time_difference_receiver);
manumaet 39:bb57aa77b015 31 tofs[RX.source] = timeDifference40Bit(rangingtimingsSender[RX.source][0], rangingtimingsSender[RX.source][1]) - RX.time_difference_receiver;
manumaet 36:883de6f9a73b 32 break;
manumaet 36:883de6f9a73b 33 default : break;
manumaet 36:883de6f9a73b 34 }
manumaet 36:883de6f9a73b 35
manumaet 39:bb57aa77b015 36 sprintf(event[event_i], "!R %d>%d / %d %d", RX.source, RX.destination, RX.sequence_number, RX.type);
manumaet 28:a830131560e8 37 if (event_i == 8)
manumaet 28:a830131560e8 38 event_i = 0;
manumaet 28:a830131560e8 39 else
manumaet 39:bb57aa77b015 40 event_i++;
manumaet 31:6f76f3d518ac 41
manumaet 28:a830131560e8 42 dw.startRX();
manumaet 28:a830131560e8 43 }
manumaet 28:a830131560e8 44
manumaet 28:a830131560e8 45 void MMRanging::callbackTX() {
manumaet 31:6f76f3d518ac 46 switch (TX.type) {
manumaet 31:6f76f3d518ac 47 case 1:
manumaet 36:883de6f9a73b 48 rangingtimingsSender[TX.destination][0] = dw.getTXTimestamp();
manumaet 31:6f76f3d518ac 49 break;
manumaet 31:6f76f3d518ac 50 case 2:
manumaet 36:883de6f9a73b 51 rangingtimingsReceiver[TX.destination][1] = dw.getTXTimestamp();
manumaet 31:6f76f3d518ac 52 break;
manumaet 31:6f76f3d518ac 53 default: break;
manumaet 31:6f76f3d518ac 54 }
manumaet 31:6f76f3d518ac 55
manumaet 39:bb57aa77b015 56 sprintf(event[event_i], "!S %d>%d / %d %d", TX.source, TX.destination, TX.sequence_number, TX.type);
manumaet 28:a830131560e8 57 if (event_i == 8)
manumaet 28:a830131560e8 58 event_i = 0;
manumaet 28:a830131560e8 59 else
manumaet 39:bb57aa77b015 60 event_i++;
manumaet 28:a830131560e8 61 }
manumaet 28:a830131560e8 62
manumaet 36:883de6f9a73b 63 void MMRanging::requestRanging(uint8_t destination) {
manumaet 36:883de6f9a73b 64 sendRangingframe(destination, counter, 1, 0);
manumaet 31:6f76f3d518ac 65 }
manumaet 31:6f76f3d518ac 66
manumaet 36:883de6f9a73b 67 void MMRanging::sendRangingframe(uint8_t destination, uint8_t sequence_number, uint8_t type, uint64_t time_difference_receiver) {
manumaet 36:883de6f9a73b 68 TX.source = address;
manumaet 36:883de6f9a73b 69 TX.destination = destination;
manumaet 36:883de6f9a73b 70 TX.sequence_number = sequence_number;
manumaet 34:f56962030c5c 71 TX.type = type;
manumaet 34:f56962030c5c 72 TX.time_difference_receiver = time_difference_receiver;
manumaet 34:f56962030c5c 73 dw.sendFrame((uint8_t*)&TX, sizeof(TX));
manumaet 39:bb57aa77b015 74 }
manumaet 39:bb57aa77b015 75
manumaet 39:bb57aa77b015 76 uint64_t MMRanging::timeDifference40Bit(uint64_t early, uint64_t late) {
manumaet 39:bb57aa77b015 77 int64_t difference = late - early;
manumaet 39:bb57aa77b015 78 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 79 return difference + MMRANGING_2POWER40;
manumaet 39:bb57aa77b015 80 if ((difference < 0) || (difference > 10000000000))
manumaet 39:bb57aa77b015 81 return 10000000000;
manumaet 39:bb57aa77b015 82 return (uint64_t)difference;
manumaet 28:a830131560e8 83 }