Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
MM2WayRanging/MM2WayRanging.h@46:6398237672a0, 2015-03-10 (annotated)
- Committer:
- manumaet
- Date:
- Tue Mar 10 12:11:13 2015 +0000
- Revision:
- 46:6398237672a0
- Parent:
- 45:01a33363bc21
- Child:
- 47:b6120c152ad1
Final commit with altCallbackRX for the testcase and improved power.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manumaet | 44:2e0045042a59 | 1 | // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015 |
manumaet | 44:2e0045042a59 | 2 | |
manumaet | 44:2e0045042a59 | 3 | #ifndef MM2WAYRANGING_H |
manumaet | 44:2e0045042a59 | 4 | #define MM2WAYRANGING_H |
manumaet | 44:2e0045042a59 | 5 | |
manumaet | 44:2e0045042a59 | 6 | #include "mbed.h" |
manumaet | 44:2e0045042a59 | 7 | #include "DW1000.h" |
manumaet | 44:2e0045042a59 | 8 | |
manumaet | 44:2e0045042a59 | 9 | #define TIMEUNITS_TO_US (1/(128*499.2)) // conversion between the decawave timeunits (ca 15.65ps) to microseconds. |
manumaet | 44:2e0045042a59 | 10 | #define US_TO_TIMEUNITS (128*499.2) // conversion between microseconds to the decawave timeunits (ca 15.65ps). |
manumaet | 45:01a33363bc21 | 11 | #define MMRANGING_2POWER40 1099511627776 // decimal value of 2^40 to correct timeroverflow between timestamps |
manumaet | 45:01a33363bc21 | 12 | |
manumaet | 45:01a33363bc21 | 13 | |
manumaet | 44:2e0045042a59 | 14 | |
manumaet | 44:2e0045042a59 | 15 | //Predefined delay for the critical answers in the ranging algorithm |
manumaet | 44:2e0045042a59 | 16 | //HAS TO BE BIGGER THAN THE PROCESSING TIME OF THE FRAME ON THE NODE |
manumaet | 45:01a33363bc21 | 17 | #define ANSWER_DELAY_US 2500 //2500 works for 110kbps, 900 for 6.8Mbps |
manumaet | 45:01a33363bc21 | 18 | #define ANSWER_DELAY_TIMEUNITS ANSWER_DELAY_US * (128*499.2) |
manumaet | 44:2e0045042a59 | 19 | |
manumaet | 44:2e0045042a59 | 20 | class MM2WayRanging { |
manumaet | 45:01a33363bc21 | 21 | |
manumaet | 45:01a33363bc21 | 22 | public: |
manumaet | 45:01a33363bc21 | 23 | MM2WayRanging(DW1000& DW); |
manumaet | 45:01a33363bc21 | 24 | |
manumaet | 45:01a33363bc21 | 25 | void requestRanging(uint8_t destination); |
manumaet | 45:01a33363bc21 | 26 | void requestRangingAll(); |
manumaet | 45:01a33363bc21 | 27 | |
manumaet | 45:01a33363bc21 | 28 | |
manumaet | 45:01a33363bc21 | 29 | |
manumaet | 45:01a33363bc21 | 30 | //TODO: Better capsulation on those? |
manumaet | 45:01a33363bc21 | 31 | bool isAnchor; |
manumaet | 45:01a33363bc21 | 32 | uint8_t address; // Identifies the nodes as source and destination in rangingframes |
manumaet | 44:2e0045042a59 | 33 | |
manumaet | 45:01a33363bc21 | 34 | //TODO: Make those PRIVATE! |
manumaet | 45:01a33363bc21 | 35 | float roundtriptimes[10]; // Array containing the round trip times to the anchors or the timeout which occured |
manumaet | 45:01a33363bc21 | 36 | float distances[10]; // Array containing the finally calculated Distances to the anchors |
manumaet | 45:01a33363bc21 | 37 | |
manumaet | 45:01a33363bc21 | 38 | bool overflow; // TRUE if counter overflows while ranging |
manumaet | 45:01a33363bc21 | 39 | |
manumaet | 45:01a33363bc21 | 40 | private: |
manumaet | 45:01a33363bc21 | 41 | |
manumaet | 45:01a33363bc21 | 42 | |
manumaet | 45:01a33363bc21 | 43 | DW1000& dw; |
manumaet | 45:01a33363bc21 | 44 | Timer LocalTimer; |
manumaet | 45:01a33363bc21 | 45 | |
manumaet | 45:01a33363bc21 | 46 | void callbackRX(); |
manumaet | 45:01a33363bc21 | 47 | void callbackTX(); |
manumaet | 45:01a33363bc21 | 48 | void sendPingFrame(uint8_t destination); |
manumaet | 45:01a33363bc21 | 49 | void sendDelayedAnswer(uint8_t destination, uint8_t type, uint64_t rxTimestamp); |
manumaet | 45:01a33363bc21 | 50 | void sendTransferFrame(uint8_t destination, int timestamp); |
manumaet | 45:01a33363bc21 | 51 | |
manumaet | 45:01a33363bc21 | 52 | inline float calibratedDistance(uint8_t destination); |
manumaet | 44:2e0045042a59 | 53 | |
manumaet | 45:01a33363bc21 | 54 | /** |
manumaet | 45:01a33363bc21 | 55 | * These two functions correct the timestamps if the counter had an overflow between measurements |
manumaet | 45:01a33363bc21 | 56 | */ |
manumaet | 45:01a33363bc21 | 57 | void correctReceiverTimestamps(uint8_t source); |
manumaet | 45:01a33363bc21 | 58 | void correctSenderTimestamps(uint8_t source); |
manumaet | 45:01a33363bc21 | 59 | |
manumaet | 45:01a33363bc21 | 60 | int timediffRec; |
manumaet | 45:01a33363bc21 | 61 | int timediffSend; |
manumaet | 45:01a33363bc21 | 62 | |
manumaet | 45:01a33363bc21 | 63 | enum FrameType{ |
manumaet | 45:01a33363bc21 | 64 | PING=1, |
manumaet | 45:01a33363bc21 | 65 | ANCHOR_RESPONSE, |
manumaet | 45:01a33363bc21 | 66 | BEACON_RESPONSE, |
manumaet | 46:6398237672a0 | 67 | TRANSFER_FRAME, |
manumaet | 46:6398237672a0 | 68 | DISTANCES_FRAME |
manumaet | 45:01a33363bc21 | 69 | }; |
manumaet | 45:01a33363bc21 | 70 | |
manumaet | 46:6398237672a0 | 71 | struct __attribute__((packed, aligned(1))) RangingFrame { |
manumaet | 45:01a33363bc21 | 72 | uint8_t source; |
manumaet | 45:01a33363bc21 | 73 | uint8_t destination; |
manumaet | 45:01a33363bc21 | 74 | uint8_t type; |
manumaet | 45:01a33363bc21 | 75 | }; |
manumaet | 45:01a33363bc21 | 76 | |
manumaet | 46:6398237672a0 | 77 | struct __attribute__((packed, aligned(1))) ExtendedRangingFrame : RangingFrame{ |
manumaet | 45:01a33363bc21 | 78 | int signedTime; |
manumaet | 45:01a33363bc21 | 79 | }; |
manumaet | 45:01a33363bc21 | 80 | |
manumaet | 45:01a33363bc21 | 81 | |
manumaet | 45:01a33363bc21 | 82 | RangingFrame rangingFrame; // buffer in class for sending a frame (not made locally because then we can recall in the interrupt what was sent) |
manumaet | 45:01a33363bc21 | 83 | ExtendedRangingFrame transferFrame; |
manumaet | 45:01a33363bc21 | 84 | ExtendedRangingFrame receivedFrame; |
manumaet | 45:01a33363bc21 | 85 | uint64_t rxTimestamp; |
manumaet | 45:01a33363bc21 | 86 | uint64_t senderTimestamps[10][3]; |
manumaet | 45:01a33363bc21 | 87 | uint64_t receiverTimestamps[10][3]; |
manumaet | 45:01a33363bc21 | 88 | bool acknowledgement[10]; // flag to indicate if ranging has succeeded |
manumaet | 45:01a33363bc21 | 89 | uint32_t tofs[10]; // Array containing time of flights for each node (index is address of node) |
manumaet | 44:2e0045042a59 | 90 | |
manumaet | 44:2e0045042a59 | 91 | }; |
manumaet | 44:2e0045042a59 | 92 | |
manumaet | 45:01a33363bc21 | 93 | #endif |