Huseyin Berkay Berabi / GA-Final

Dependencies:   mbed-dev

Fork of GA-Berkay_Alex by Alejandro Ungria Hirte

Committer:
aungriah
Date:
Wed Dec 06 21:35:45 2017 +0000
Revision:
0:a3b83d366423
Child:
2:5adf0b785944
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aungriah 0:a3b83d366423 1 // Lukas Bieri, Matthias, Manuel Meier & Noa Melchior
aungriah 0:a3b83d366423 2 // based on Work by Matthias Grob & Manuel Stalder
aungriah 0:a3b83d366423 3 // ETH 2017
aungriah 0:a3b83d366423 4
aungriah 0:a3b83d366423 5 #ifndef _NODES_H
aungriah 0:a3b83d366423 6 #define _NODES_H
aungriah 0:a3b83d366423 7
aungriah 0:a3b83d366423 8 #include "mbed.h"
aungriah 0:a3b83d366423 9 #include "DecaWave.h"
aungriah 0:a3b83d366423 10 #include "frames.h"
aungriah 0:a3b83d366423 11
aungriah 0:a3b83d366423 12 #include "globals.h"
aungriah 0:a3b83d366423 13
aungriah 0:a3b83d366423 14 #define TIMEUNITS_TO_US (1/(128*499.2)) // conversion between the decawave timeunits (ca 15.65ps) to microseconds.
aungriah 0:a3b83d366423 15 #define US_TO_TIMEUNITS (128*499.2) // conversion between microseconds to the decawave timeunits (ca 15.65ps).
aungriah 0:a3b83d366423 16 #define MMRANGING_2POWER40 1099511627776 // decimal value of 2^40 to correct timeroverflow between timestamps
aungriah 0:a3b83d366423 17 #define ADRESSES_COUNT 10 // Defines the Adress Space that is covered when Ranging for all
aungriah 0:a3b83d366423 18
aungriah 0:a3b83d366423 19 //static int ADRESSES_COUNT=1; // Adress Space: 0 - (ADRESSES_COUNT - 1)
aungriah 0:a3b83d366423 20 #define MAX_TRIES 5 // Number of times a Anchor is pinged until determined it is not in range
aungriah 0:a3b83d366423 21 #define BASE_STATION_ADDR 15 // Defines the Adress of the Base Station (reserved Adress)
aungriah 0:a3b83d366423 22
aungriah 0:a3b83d366423 23 // Constants for Base_Orders
aungriah 0:a3b83d366423 24 #define RANGE_ALL 1
aungriah 0:a3b83d366423 25 #define RANGE_ONE 0
aungriah 0:a3b83d366423 26 #define NOT_USED 0xff
aungriah 0:a3b83d366423 27
aungriah 0:a3b83d366423 28 // Constant for Switch Types Orders
aungriah 0:a3b83d366423 29 #define BECOME_BEACON 1
aungriah 0:a3b83d366423 30 #define BECOME_ANCHOR 0
aungriah 0:a3b83d366423 31
aungriah 0:a3b83d366423 32
aungriah 0:a3b83d366423 33 class Node {
aungriah 0:a3b83d366423 34 public:
aungriah 0:a3b83d366423 35 Node(DecaWave& DW);
aungriah 0:a3b83d366423 36
aungriah 0:a3b83d366423 37 void setAnchor(bool anc);
aungriah 0:a3b83d366423 38 bool isAnchor();
aungriah 0:a3b83d366423 39 bool isBaseStation();
aungriah 0:a3b83d366423 40 void setAddress(uint8_t adr);
aungriah 0:a3b83d366423 41 uint8_t getAddress();
aungriah 0:a3b83d366423 42
aungriah 0:a3b83d366423 43 virtual uint8* getRecFrameRef() = 0;
aungriah 0:a3b83d366423 44 virtual uint16 getRecFrameLength() = 0;
aungriah 0:a3b83d366423 45
aungriah 0:a3b83d366423 46 virtual void callbackRX(uint64_t RxTime) = 0;
aungriah 0:a3b83d366423 47 virtual void callbackTX(uint64_t TxTime) = 0;
aungriah 0:a3b83d366423 48
aungriah 0:a3b83d366423 49 //Frametype of the form 1001XXXX. First 4 Bits for uniqueness
aungriah 0:a3b83d366423 50 enum FrameType{
aungriah 0:a3b83d366423 51 PING = 0x91,
aungriah 0:a3b83d366423 52 ANCHOR_RESPONSE = 0x92,
aungriah 0:a3b83d366423 53 BEACON_RESPONSE = 0x93,
aungriah 0:a3b83d366423 54 TRANSFER_FRAME = 0x94,
aungriah 0:a3b83d366423 55 DISTANCES_FRAME = 0x95,
aungriah 0:a3b83d366423 56 STREAM_TO_BASE = 0x96,
aungriah 0:a3b83d366423 57 BASE_ORDER = 0x97,
aungriah 0:a3b83d366423 58 SWITCH_TYPE = 0x98,
aungriah 0:a3b83d366423 59 BASE_ORDER_ACK = 0x99
aungriah 0:a3b83d366423 60 };
aungriah 0:a3b83d366423 61
aungriah 0:a3b83d366423 62 protected:
aungriah 0:a3b83d366423 63 DecaWave& dw;
aungriah 0:a3b83d366423 64 uint8_t address;
aungriah 0:a3b83d366423 65 bool Anchor;
aungriah 0:a3b83d366423 66 };
aungriah 0:a3b83d366423 67
aungriah 0:a3b83d366423 68 class BaseStationNode : public Node {
aungriah 0:a3b83d366423 69 public:
aungriah 0:a3b83d366423 70 BaseStationNode(DecaWave& DW);
aungriah 0:a3b83d366423 71
aungriah 0:a3b83d366423 72 virtual uint8* getRecFrameRef();
aungriah 0:a3b83d366423 73 virtual uint16 getRecFrameLength();
aungriah 0:a3b83d366423 74
aungriah 0:a3b83d366423 75 virtual void callbackRX(uint64_t RxTime);
aungriah 0:a3b83d366423 76 virtual void callbackTX(uint64_t TxTime);
aungriah 0:a3b83d366423 77
aungriah 0:a3b83d366423 78 void sendOrder(uint8_t beacon_destination, uint8_t anchor_destination, uint8_t action, uint16_t repetitions, uint8_t type);
aungriah 0:a3b83d366423 79
aungriah 0:a3b83d366423 80 private:
aungriah 0:a3b83d366423 81 Timer LocalTimer;
aungriah 0:a3b83d366423 82 bool ack;
aungriah 0:a3b83d366423 83 RangingFrame rangingFrame; // buffer in class for sending a frame (not made locally because then we can recall in the interrupt what was sent)
aungriah 0:a3b83d366423 84 public:
aungriah 0:a3b83d366423 85 StreamFrame receivedStreamFrame;
aungriah 0:a3b83d366423 86 };
aungriah 0:a3b83d366423 87
aungriah 0:a3b83d366423 88 class AnchorNode : public Node {
aungriah 0:a3b83d366423 89 public:
aungriah 0:a3b83d366423 90 AnchorNode(DecaWave& DW);
aungriah 0:a3b83d366423 91
aungriah 0:a3b83d366423 92 virtual uint8* getRecFrameRef();
aungriah 0:a3b83d366423 93 virtual uint16 getRecFrameLength();
aungriah 0:a3b83d366423 94
aungriah 0:a3b83d366423 95 virtual void callbackRX(uint64_t RxTime);
aungriah 0:a3b83d366423 96 virtual void callbackTX(uint64_t TxTime);
aungriah 0:a3b83d366423 97
aungriah 0:a3b83d366423 98 private:
aungriah 0:a3b83d366423 99 uint64_t receiverTimestamps[ADRESSES_COUNT][3];
aungriah 0:a3b83d366423 100
aungriah 0:a3b83d366423 101 void sendAnswer(uint8_t destination, uint8_t type);
aungriah 0:a3b83d366423 102 void sendTransferFrame(uint8_t destination, int timestamp);
aungriah 0:a3b83d366423 103 void sendBaseAck();
aungriah 0:a3b83d366423 104
aungriah 0:a3b83d366423 105 void correctReceiverTimestamps(uint8_t source);
aungriah 0:a3b83d366423 106
aungriah 0:a3b83d366423 107 RangingFrame rangingFrame; // buffer in class for sending a frame (not made locally because then we can recall in the interrupt what was sent)
aungriah 0:a3b83d366423 108
aungriah 0:a3b83d366423 109 public:
aungriah 0:a3b83d366423 110 ExtendedRangingFrame receivedFrame;
aungriah 0:a3b83d366423 111 };
aungriah 0:a3b83d366423 112
aungriah 0:a3b83d366423 113
aungriah 0:a3b83d366423 114
aungriah 0:a3b83d366423 115 class BeaconNode : public Node {
aungriah 0:a3b83d366423 116 public:
aungriah 0:a3b83d366423 117 BeaconNode(DecaWave& DW);
aungriah 0:a3b83d366423 118
aungriah 0:a3b83d366423 119 virtual void requestRanging(uint8_t destination);
aungriah 0:a3b83d366423 120 virtual void requestRangingAll();
aungriah 0:a3b83d366423 121
aungriah 0:a3b83d366423 122 float getDistance(uint8_t index);
aungriah 0:a3b83d366423 123 float getSignalStrength(uint8_t index);
aungriah 0:a3b83d366423 124 int getMode();
aungriah 0:a3b83d366423 125
aungriah 0:a3b83d366423 126 virtual uint8* getRecFrameRef();
aungriah 0:a3b83d366423 127 virtual uint16 getRecFrameLength();
aungriah 0:a3b83d366423 128
aungriah 0:a3b83d366423 129 virtual void callbackRX(uint64_t RxTime);
aungriah 0:a3b83d366423 130 virtual void callbackTX(uint64_t TxTime);
aungriah 0:a3b83d366423 131
aungriah 0:a3b83d366423 132 uint16_t getRepetitions();
aungriah 0:a3b83d366423 133 void decreaseRepetitions();
aungriah 0:a3b83d366423 134 uint8_t getDestination();
aungriah 0:a3b83d366423 135 void clearRec();
aungriah 0:a3b83d366423 136
aungriah 0:a3b83d366423 137
aungriah 0:a3b83d366423 138 private:
aungriah 0:a3b83d366423 139 Timer LocalTimer;
aungriah 0:a3b83d366423 140 float distances[ADRESSES_COUNT]; // Array containing the finally calculated Distances to the anchors
aungriah 0:a3b83d366423 141 float signalStrength[ADRESSES_COUNT]; // Array containing the finally calculated Distances to the anchors
aungriah 0:a3b83d366423 142 uint64_t senderTimestamps[ADRESSES_COUNT][3];
aungriah 0:a3b83d366423 143 volatile bool acknowledgement[3]; // flag to indicate if ranging has started (0) and succeeded (1)
aungriah 0:a3b83d366423 144 int32_t tofs[ADRESSES_COUNT]; // Array containing time of flights for each node (index is address of node)
aungriah 0:a3b83d366423 145 int8_t noRec[ADRESSES_COUNT];
aungriah 0:a3b83d366423 146
aungriah 0:a3b83d366423 147 void sendPingFrame(uint8_t destination);
aungriah 0:a3b83d366423 148 void sendAnswer(uint8_t destination, uint8_t type);
aungriah 0:a3b83d366423 149 void sendStreamFrame(uint8_t anchor_addr);
aungriah 0:a3b83d366423 150 void sendBaseAck();
aungriah 0:a3b83d366423 151
aungriah 0:a3b83d366423 152 inline float calibratedDistance(uint8_t destination);
aungriah 0:a3b83d366423 153 void correctSenderTimestamps(uint8_t source);
aungriah 0:a3b83d366423 154
aungriah 0:a3b83d366423 155 RangingFrame rangingFrame; // buffer in class for sending a frame (not made locally because then we can recall in the interrupt what was sent)
aungriah 0:a3b83d366423 156
aungriah 0:a3b83d366423 157 uint8_t mode;
aungriah 0:a3b83d366423 158 uint8_t destination;
aungriah 0:a3b83d366423 159 uint16_t repetitions;
aungriah 0:a3b83d366423 160
aungriah 0:a3b83d366423 161 public:
aungriah 0:a3b83d366423 162 ExtendedRangingFrame receivedFrame;
aungriah 0:a3b83d366423 163
aungriah 0:a3b83d366423 164
aungriah 0:a3b83d366423 165 };
aungriah 0:a3b83d366423 166
aungriah 0:a3b83d366423 167 #endif
aungriah 0:a3b83d366423 168