Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
bhepp
Date:
Tue Nov 24 16:41:23 2015 +0000
Revision:
48:5999e510f154
Parent:
47:b6120c152ad1
Child:
58:b824bdac7cd8
Multiple Receivers implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhepp 48:5999e510f154 1 // Adapted from Matthias Grob & Manuel Stalder - ETH Zürich - 2015
manumaet 44:2e0045042a59 2
bhepp 48:5999e510f154 3 #pragma once
manumaet 44:2e0045042a59 4
manumaet 44:2e0045042a59 5 #include "mbed.h"
manumaet 44:2e0045042a59 6 #include "DW1000.h"
manumaet 44:2e0045042a59 7
manumaet 44:2e0045042a59 8 #define TIMEUNITS_TO_US (1/(128*499.2)) // conversion between the decawave timeunits (ca 15.65ps) to microseconds.
manumaet 44:2e0045042a59 9 #define US_TO_TIMEUNITS (128*499.2) // conversion between microseconds to the decawave timeunits (ca 15.65ps).
manumaet 45:01a33363bc21 10 #define MMRANGING_2POWER40 1099511627776 // decimal value of 2^40 to correct timeroverflow between timestamps
manumaet 45:01a33363bc21 11
bhepp 48:5999e510f154 12 #define NUM_OF_UWB_ADDRESSES 256
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
bhepp 48:5999e510f154 25 void requestRanging(uint8_t remote_address);
manumaet 45:01a33363bc21 26
manumaet 45:01a33363bc21 27 //TODO: Better capsulation on those?
manumaet 45:01a33363bc21 28 bool isAnchor;
bhepp 48:5999e510f154 29 uint8_t address; // Identifies the nodes as address and remote_address in rangingframes
manumaet 44:2e0045042a59 30
manumaet 45:01a33363bc21 31 //TODO: Make those PRIVATE!
bhepp 48:5999e510f154 32 float roundtriptimes[NUM_OF_UWB_ADDRESSES]; // Array containing the round trip times to the anchors or the timeout which occured
bhepp 48:5999e510f154 33 float distances[NUM_OF_UWB_ADDRESSES]; // Array containing the finally calculated Distances to the anchors
bhepp 48:5999e510f154 34
bhepp 48:5999e510f154 35 struct __attribute__((packed, aligned(1))) ReceptionStats {
bhepp 48:5999e510f154 36 uint16_t std_noise;
bhepp 48:5999e510f154 37 uint16_t preamble_acc_count;
bhepp 48:5999e510f154 38 uint16_t first_path_index;
bhepp 48:5999e510f154 39 uint16_t first_path_amp_1;
bhepp 48:5999e510f154 40 uint16_t first_path_amp_2;
bhepp 48:5999e510f154 41 uint16_t first_path_amp_3;
bhepp 48:5999e510f154 42 uint16_t channel_impulse_response_power;
bhepp 48:5999e510f154 43 uint8_t prf;
bhepp 48:5999e510f154 44 };
bhepp 48:5999e510f154 45 ReceptionStats reception_stats[NUM_OF_UWB_ADDRESSES][3];
manumaet 45:01a33363bc21 46
manumaet 45:01a33363bc21 47 bool overflow; // TRUE if counter overflows while ranging
manumaet 45:01a33363bc21 48
manumaet 45:01a33363bc21 49 private:
manumaet 45:01a33363bc21 50
manumaet 45:01a33363bc21 51
manumaet 45:01a33363bc21 52 DW1000& dw;
manumaet 45:01a33363bc21 53 Timer LocalTimer;
manumaet 45:01a33363bc21 54
manumaet 45:01a33363bc21 55 void callbackRX();
manumaet 45:01a33363bc21 56 void callbackTX();
bhepp 48:5999e510f154 57 void sendPingFrame(uint8_t remote_address);
bhepp 48:5999e510f154 58 void sendDelayedAnswer(uint8_t remote_address, uint8_t type, uint64_t rxTimestamp);
bhepp 48:5999e510f154 59 void sendTransferFrame(uint8_t remote_address, int timestamp);
manumaet 45:01a33363bc21 60
bhepp 48:5999e510f154 61 inline float calibratedDistance(uint8_t remote_address);
manumaet 44:2e0045042a59 62
manumaet 45:01a33363bc21 63 /**
manumaet 45:01a33363bc21 64 * These two functions correct the timestamps if the counter had an overflow between measurements
manumaet 45:01a33363bc21 65 */
bhepp 48:5999e510f154 66 void correctReceiverTimestamps(uint8_t address);
bhepp 48:5999e510f154 67 void correctSenderTimestamps(uint8_t address);
manumaet 45:01a33363bc21 68
manumaet 45:01a33363bc21 69 int timediffRec;
manumaet 45:01a33363bc21 70 int timediffSend;
manumaet 45:01a33363bc21 71
manumaet 45:01a33363bc21 72 enum FrameType{
bhepp 48:5999e510f154 73 PING = 1,
manumaet 45:01a33363bc21 74 ANCHOR_RESPONSE,
manumaet 45:01a33363bc21 75 BEACON_RESPONSE,
manumaet 46:6398237672a0 76 TRANSFER_FRAME,
manumaet 46:6398237672a0 77 DISTANCES_FRAME
manumaet 45:01a33363bc21 78 };
manumaet 45:01a33363bc21 79
manumaet 47:b6120c152ad1 80 //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 81 //IT IS A GCC SPECIFIC DIRECTIVE
manumaet 46:6398237672a0 82 struct __attribute__((packed, aligned(1))) RangingFrame {
bhepp 48:5999e510f154 83 uint8_t address;
bhepp 48:5999e510f154 84 uint8_t remote_address;
manumaet 45:01a33363bc21 85 uint8_t type;
manumaet 45:01a33363bc21 86 };
manumaet 45:01a33363bc21 87
manumaet 46:6398237672a0 88 struct __attribute__((packed, aligned(1))) ExtendedRangingFrame : RangingFrame{
manumaet 45:01a33363bc21 89 int signedTime;
bhepp 48:5999e510f154 90 ReceptionStats stats1;
bhepp 48:5999e510f154 91 ReceptionStats stats2;
manumaet 45:01a33363bc21 92 };
manumaet 45:01a33363bc21 93
manumaet 45:01a33363bc21 94
manumaet 45:01a33363bc21 95 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 96 ExtendedRangingFrame transferFrame;
manumaet 45:01a33363bc21 97 ExtendedRangingFrame receivedFrame;
bhepp 48:5999e510f154 98
manumaet 45:01a33363bc21 99 uint64_t rxTimestamp;
bhepp 48:5999e510f154 100 uint64_t senderTimestamps[NUM_OF_UWB_ADDRESSES][3];
bhepp 48:5999e510f154 101 uint64_t receiverTimestamps[NUM_OF_UWB_ADDRESSES][3];
bhepp 48:5999e510f154 102 bool acknowledgement[NUM_OF_UWB_ADDRESSES]; // flag to indicate if ranging has succeeded
bhepp 48:5999e510f154 103 uint32_t tofs[NUM_OF_UWB_ADDRESSES]; // Array containing time of flights for each node (index is address of node)
manumaet 44:2e0045042a59 104
manumaet 44:2e0045042a59 105 };