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