Huseyin Berkay Berabi / GA-Final

Dependencies:   mbed-dev

Fork of GA-Berkay_Alex by Alejandro Ungria Hirte

Committer:
aungriah
Date:
Wed Feb 28 16:10:21 2018 +0000
Revision:
3:8bee1711d186
Parent:
2:5adf0b785944
Child:
4:120ff05a7c27
ILBE

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
bberabi 2:5adf0b785944 17 #define ADRESSES_COUNT 128 // 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)
bberabi 2:5adf0b785944 20 #define MAX_TRIES 0 // 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 3:8bee1711d186 24 #define RANGE_ALL 2
aungriah 3:8bee1711d186 25 #define RANGE_ONE 0
aungriah 3:8bee1711d186 26 #define RANGE_INT 1
aungriah 0:a3b83d366423 27 #define NOT_USED 0xff
aungriah 0:a3b83d366423 28
aungriah 0:a3b83d366423 29 // Constant for Switch Types Orders
aungriah 0:a3b83d366423 30 #define BECOME_BEACON 1
aungriah 0:a3b83d366423 31 #define BECOME_ANCHOR 0
aungriah 0:a3b83d366423 32
aungriah 0:a3b83d366423 33
aungriah 0:a3b83d366423 34 class Node {
aungriah 0:a3b83d366423 35 public:
aungriah 0:a3b83d366423 36 Node(DecaWave& DW);
aungriah 0:a3b83d366423 37
aungriah 0:a3b83d366423 38 void setAnchor(bool anc);
aungriah 0:a3b83d366423 39 bool isAnchor();
aungriah 0:a3b83d366423 40 bool isBaseStation();
aungriah 0:a3b83d366423 41 void setAddress(uint8_t adr);
aungriah 0:a3b83d366423 42 uint8_t getAddress();
aungriah 0:a3b83d366423 43
aungriah 0:a3b83d366423 44 virtual uint8* getRecFrameRef() = 0;
aungriah 0:a3b83d366423 45 virtual uint16 getRecFrameLength() = 0;
aungriah 0:a3b83d366423 46
aungriah 0:a3b83d366423 47 virtual void callbackRX(uint64_t RxTime) = 0;
aungriah 0:a3b83d366423 48 virtual void callbackTX(uint64_t TxTime) = 0;
aungriah 0:a3b83d366423 49
aungriah 0:a3b83d366423 50 //Frametype of the form 1001XXXX. First 4 Bits for uniqueness
aungriah 0:a3b83d366423 51 enum FrameType{
aungriah 0:a3b83d366423 52 PING = 0x91,
aungriah 0:a3b83d366423 53 ANCHOR_RESPONSE = 0x92,
aungriah 0:a3b83d366423 54 BEACON_RESPONSE = 0x93,
aungriah 0:a3b83d366423 55 TRANSFER_FRAME = 0x94,
aungriah 0:a3b83d366423 56 DISTANCES_FRAME = 0x95,
aungriah 0:a3b83d366423 57 STREAM_TO_BASE = 0x96,
aungriah 0:a3b83d366423 58 BASE_ORDER = 0x97,
aungriah 0:a3b83d366423 59 SWITCH_TYPE = 0x98,
aungriah 0:a3b83d366423 60 BASE_ORDER_ACK = 0x99
aungriah 0:a3b83d366423 61 };
aungriah 0:a3b83d366423 62
aungriah 0:a3b83d366423 63 protected:
aungriah 0:a3b83d366423 64 DecaWave& dw;
aungriah 0:a3b83d366423 65 uint8_t address;
aungriah 0:a3b83d366423 66 bool Anchor;
aungriah 0:a3b83d366423 67 };
aungriah 0:a3b83d366423 68
aungriah 0:a3b83d366423 69 class BaseStationNode : public Node {
aungriah 0:a3b83d366423 70 public:
aungriah 0:a3b83d366423 71 BaseStationNode(DecaWave& DW);
aungriah 0:a3b83d366423 72
aungriah 0:a3b83d366423 73 virtual uint8* getRecFrameRef();
aungriah 0:a3b83d366423 74 virtual uint16 getRecFrameLength();
aungriah 0:a3b83d366423 75
aungriah 0:a3b83d366423 76 virtual void callbackRX(uint64_t RxTime);
aungriah 0:a3b83d366423 77 virtual void callbackTX(uint64_t TxTime);
aungriah 0:a3b83d366423 78
aungriah 0:a3b83d366423 79 void sendOrder(uint8_t beacon_destination, uint8_t anchor_destination, uint8_t action, uint16_t repetitions, uint8_t type);
aungriah 0:a3b83d366423 80
aungriah 0:a3b83d366423 81 private:
aungriah 0:a3b83d366423 82 Timer LocalTimer;
aungriah 0:a3b83d366423 83 bool ack;
aungriah 0:a3b83d366423 84 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 85 public:
aungriah 0:a3b83d366423 86 StreamFrame receivedStreamFrame;
aungriah 0:a3b83d366423 87 };
aungriah 0:a3b83d366423 88
aungriah 0:a3b83d366423 89 class AnchorNode : public Node {
aungriah 0:a3b83d366423 90 public:
aungriah 0:a3b83d366423 91 AnchorNode(DecaWave& DW);
aungriah 0:a3b83d366423 92
aungriah 0:a3b83d366423 93 virtual uint8* getRecFrameRef();
aungriah 0:a3b83d366423 94 virtual uint16 getRecFrameLength();
aungriah 0:a3b83d366423 95
aungriah 0:a3b83d366423 96 virtual void callbackRX(uint64_t RxTime);
aungriah 0:a3b83d366423 97 virtual void callbackTX(uint64_t TxTime);
aungriah 0:a3b83d366423 98
aungriah 0:a3b83d366423 99 private:
aungriah 0:a3b83d366423 100 uint64_t receiverTimestamps[ADRESSES_COUNT][3];
aungriah 0:a3b83d366423 101
aungriah 0:a3b83d366423 102 void sendAnswer(uint8_t destination, uint8_t type);
aungriah 0:a3b83d366423 103 void sendTransferFrame(uint8_t destination, int timestamp);
aungriah 0:a3b83d366423 104 void sendBaseAck();
aungriah 0:a3b83d366423 105
aungriah 0:a3b83d366423 106 void correctReceiverTimestamps(uint8_t source);
aungriah 0:a3b83d366423 107
aungriah 0:a3b83d366423 108 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 109
aungriah 0:a3b83d366423 110 public:
aungriah 0:a3b83d366423 111 ExtendedRangingFrame receivedFrame;
aungriah 0:a3b83d366423 112 };
aungriah 0:a3b83d366423 113
aungriah 0:a3b83d366423 114
aungriah 0:a3b83d366423 115
aungriah 0:a3b83d366423 116 class BeaconNode : public Node {
aungriah 0:a3b83d366423 117 public:
aungriah 0:a3b83d366423 118 BeaconNode(DecaWave& DW);
aungriah 0:a3b83d366423 119
aungriah 0:a3b83d366423 120 virtual void requestRanging(uint8_t destination);
aungriah 0:a3b83d366423 121 virtual void requestRangingAll();
aungriah 3:8bee1711d186 122 virtual void requestRangingInt(uint8_t from, uint8_t to);
aungriah 0:a3b83d366423 123
aungriah 0:a3b83d366423 124 float getDistance(uint8_t index);
aungriah 0:a3b83d366423 125 float getSignalStrength(uint8_t index);
aungriah 0:a3b83d366423 126 int getMode();
aungriah 0:a3b83d366423 127
aungriah 0:a3b83d366423 128 virtual uint8* getRecFrameRef();
aungriah 0:a3b83d366423 129 virtual uint16 getRecFrameLength();
aungriah 0:a3b83d366423 130
aungriah 0:a3b83d366423 131 virtual void callbackRX(uint64_t RxTime);
aungriah 0:a3b83d366423 132 virtual void callbackTX(uint64_t TxTime);
aungriah 0:a3b83d366423 133
aungriah 0:a3b83d366423 134 uint16_t getRepetitions();
aungriah 0:a3b83d366423 135 void decreaseRepetitions();
aungriah 0:a3b83d366423 136 uint8_t getDestination();
aungriah 0:a3b83d366423 137 void clearRec();
aungriah 0:a3b83d366423 138
aungriah 0:a3b83d366423 139
aungriah 0:a3b83d366423 140 private:
aungriah 0:a3b83d366423 141 Timer LocalTimer;
aungriah 0:a3b83d366423 142 float distances[ADRESSES_COUNT]; // Array containing the finally calculated Distances to the anchors
aungriah 0:a3b83d366423 143 float signalStrength[ADRESSES_COUNT]; // Array containing the finally calculated Distances to the anchors
aungriah 0:a3b83d366423 144 uint64_t senderTimestamps[ADRESSES_COUNT][3];
aungriah 0:a3b83d366423 145 volatile bool acknowledgement[3]; // flag to indicate if ranging has started (0) and succeeded (1)
aungriah 0:a3b83d366423 146 int32_t tofs[ADRESSES_COUNT]; // Array containing time of flights for each node (index is address of node)
aungriah 0:a3b83d366423 147 int8_t noRec[ADRESSES_COUNT];
aungriah 0:a3b83d366423 148
aungriah 0:a3b83d366423 149 void sendPingFrame(uint8_t destination);
aungriah 0:a3b83d366423 150 void sendAnswer(uint8_t destination, uint8_t type);
aungriah 0:a3b83d366423 151 void sendStreamFrame(uint8_t anchor_addr);
aungriah 0:a3b83d366423 152 void sendBaseAck();
aungriah 0:a3b83d366423 153
aungriah 0:a3b83d366423 154 inline float calibratedDistance(uint8_t destination);
aungriah 0:a3b83d366423 155 void correctSenderTimestamps(uint8_t source);
aungriah 0:a3b83d366423 156
aungriah 0:a3b83d366423 157 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 158
aungriah 0:a3b83d366423 159 uint8_t mode;
aungriah 0:a3b83d366423 160 uint8_t destination;
aungriah 0:a3b83d366423 161 uint16_t repetitions;
aungriah 0:a3b83d366423 162
aungriah 0:a3b83d366423 163 public:
aungriah 0:a3b83d366423 164 ExtendedRangingFrame receivedFrame;
aungriah 0:a3b83d366423 165
aungriah 0:a3b83d366423 166
aungriah 0:a3b83d366423 167 };
aungriah 0:a3b83d366423 168
aungriah 0:a3b83d366423 169 #endif
aungriah 0:a3b83d366423 170