Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
MM2WayRanging/MM2WayRanging.h@47:b6120c152ad1, 2015-03-19 (annotated)
- Committer:
- manumaet
- Date:
- Thu Mar 19 12:54:28 2015 +0000
- Revision:
- 47:b6120c152ad1
- Parent:
- 46:6398237672a0
- Child:
- 48:5999e510f154
final release with the code used in the verification test case
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 | 47:b6120c152ad1 | 71 | //the packed attribute makes sure the types only use their respective size in memory (8 bit for uint8_t), otherwise they would always use 32 bit |
manumaet | 47:b6120c152ad1 | 72 | //IT IS A GCC SPECIFIC DIRECTIVE |
manumaet | 46:6398237672a0 | 73 | struct __attribute__((packed, aligned(1))) RangingFrame { |
manumaet | 45:01a33363bc21 | 74 | uint8_t source; |
manumaet | 45:01a33363bc21 | 75 | uint8_t destination; |
manumaet | 45:01a33363bc21 | 76 | uint8_t type; |
manumaet | 45:01a33363bc21 | 77 | }; |
manumaet | 45:01a33363bc21 | 78 | |
manumaet | 46:6398237672a0 | 79 | struct __attribute__((packed, aligned(1))) ExtendedRangingFrame : RangingFrame{ |
manumaet | 45:01a33363bc21 | 80 | int signedTime; |
manumaet | 45:01a33363bc21 | 81 | }; |
manumaet | 45:01a33363bc21 | 82 | |
manumaet | 45:01a33363bc21 | 83 | |
manumaet | 45:01a33363bc21 | 84 | 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 | 85 | ExtendedRangingFrame transferFrame; |
manumaet | 45:01a33363bc21 | 86 | ExtendedRangingFrame receivedFrame; |
manumaet | 45:01a33363bc21 | 87 | uint64_t rxTimestamp; |
manumaet | 45:01a33363bc21 | 88 | uint64_t senderTimestamps[10][3]; |
manumaet | 45:01a33363bc21 | 89 | uint64_t receiverTimestamps[10][3]; |
manumaet | 45:01a33363bc21 | 90 | bool acknowledgement[10]; // flag to indicate if ranging has succeeded |
manumaet | 45:01a33363bc21 | 91 | uint32_t tofs[10]; // Array containing time of flights for each node (index is address of node) |
manumaet | 44:2e0045042a59 | 92 | |
manumaet | 44:2e0045042a59 | 93 | }; |
manumaet | 44:2e0045042a59 | 94 | |
manumaet | 45:01a33363bc21 | 95 | #endif |