Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Fri Feb 27 11:47:51 2015 +0000
Revision:
40:5ce51b7e3118
Parent:
39:bb57aa77b015
Child:
41:0a3bb028d4ba
distance to 4 anchors works and is visible in MATLAB, before cleanup and calibration

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