Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
MMRanging/MMRanging.cpp@36:883de6f9a73b, 2015-02-20 (annotated)
- Committer:
- manumaet
- Date:
- Fri Feb 20 09:07:52 2015 +0000
- Revision:
- 36:883de6f9a73b
- Parent:
- 35:95d838d33bf5
- Child:
- 37:40f94c634c3e
before changing data rate (disabled resetAll)
Who changed what in which revision?
User | Revision | Line number | New 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 | 28:a830131560e8 | 11 | RX_timestamp = dw.getRXTimestamp(); |
manumaet | 31:6f76f3d518ac | 12 | |
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 | 36:883de6f9a73b | 28 | sendRangingframe(RX.source, RX.sequence_number, 4, rangingtimingsReceiver[RX.source][1] - rangingtimingsReceiver[RX.source][0]); |
manumaet | 36:883de6f9a73b | 29 | break; |
manumaet | 36:883de6f9a73b | 30 | case 4: |
manumaet | 36:883de6f9a73b | 31 | tofs = (rangingtimingsSender[RX.source][1] - rangingtimingsSender[RX.source][0] - RX.time_difference_receiver); |
manumaet | 36:883de6f9a73b | 32 | break; |
manumaet | 36:883de6f9a73b | 33 | default : break; |
manumaet | 36:883de6f9a73b | 34 | } |
manumaet | 36:883de6f9a73b | 35 | |
manumaet | 28:a830131560e8 | 36 | event[event_i][0] = '!'; |
manumaet | 28:a830131560e8 | 37 | event[event_i][1] = 'R'; |
manumaet | 28:a830131560e8 | 38 | event[event_i][2] = ' '; |
manumaet | 36:883de6f9a73b | 39 | sprintf(&event[event_i][3], "%d>%d / %d %d", RX.source, RX.destination, RX.sequence_number, RX.type); |
manumaet | 28:a830131560e8 | 40 | if (event_i == 8) |
manumaet | 28:a830131560e8 | 41 | event_i = 0; |
manumaet | 28:a830131560e8 | 42 | else |
manumaet | 28:a830131560e8 | 43 | event_i++; |
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 | 28:a830131560e8 | 49 | TX_timestamp = dw.getTXTimestamp(); |
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 | 28:a830131560e8 | 60 | event[event_i][0] = '!'; |
manumaet | 28:a830131560e8 | 61 | event[event_i][1] = 'S'; |
manumaet | 28:a830131560e8 | 62 | event[event_i][2] = ' '; |
manumaet | 36:883de6f9a73b | 63 | sprintf(&event[event_i][3], "%d>%d / %d %d", TX.source, TX.destination, TX.sequence_number, TX.type); |
manumaet | 28:a830131560e8 | 64 | if (event_i == 8) |
manumaet | 28:a830131560e8 | 65 | event_i = 0; |
manumaet | 28:a830131560e8 | 66 | else |
manumaet | 28:a830131560e8 | 67 | event_i++; |
manumaet | 28:a830131560e8 | 68 | } |
manumaet | 28:a830131560e8 | 69 | |
manumaet | 36:883de6f9a73b | 70 | void MMRanging::requestRanging(uint8_t destination) { |
manumaet | 36:883de6f9a73b | 71 | sendRangingframe(destination, counter, 1, 0); |
manumaet | 31:6f76f3d518ac | 72 | } |
manumaet | 31:6f76f3d518ac | 73 | |
manumaet | 36:883de6f9a73b | 74 | void MMRanging::sendRangingframe(uint8_t destination, uint8_t sequence_number, uint8_t type, uint64_t time_difference_receiver) { |
manumaet | 36:883de6f9a73b | 75 | TX.source = address; |
manumaet | 36:883de6f9a73b | 76 | TX.destination = destination; |
manumaet | 36:883de6f9a73b | 77 | TX.sequence_number = sequence_number; |
manumaet | 34:f56962030c5c | 78 | TX.type = type; |
manumaet | 34:f56962030c5c | 79 | TX.time_difference_receiver = time_difference_receiver; |
manumaet | 34:f56962030c5c | 80 | dw.sendFrame((uint8_t*)&TX, sizeof(TX)); |
manumaet | 28:a830131560e8 | 81 | } |