Arduino driver library for Decawave DW1000  Dec 20 2016
DW1000Ranging.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 by Thomas Trojer <thomas@trojer.net> and Leopold Sayous <leosayous@gmail.com>
3  * Decawave DW1000 library for arduino.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * @file DW1000Ranging.h
18  * Arduino global library (header file) working with the DW1000 library
19  * for the Decawave DW1000 UWB transceiver IC.
20  *
21  * @TODO
22  * - remove or debugmode for Serial.print
23  * - move strings to flash to reduce ram usage
24  * - do not safe duplicate of pin settings
25  * - maybe other object structure
26  * - use enums instead of preprocessor constants
27  */
28 
29 #include "DW1000.h"
30 #include "DW1000Time.h"
31 #include "DW1000Device.h"
32 #include "DW1000Mac.h"
33 
34 // messages used in the ranging protocol
35 #define POLL 0
36 #define POLL_ACK 1
37 #define RANGE 2
38 #define RANGE_REPORT 3
39 #define RANGE_FAILED 255
40 #define BLINK 4
41 #define RANGING_INIT 5
42 
43 #define LEN_DATA 90
44 
45 //Max devices we put in the networkDevices array ! Each DW1000Device is 74 Bytes in SRAM memory for now.
46 #define MAX_DEVICES 4
47 
48 //Default Pin for module:
49 #define DEFAULT_RST_PIN 9
50 #define DEFAULT_SPI_SS_PIN 10
51 
52 //Default value
53 //in ms
54 #define DEFAULT_RESET_PERIOD 200
55 //in us
56 #define DEFAULT_REPLY_DELAY_TIME 7000
57 
58 //sketch type (anchor or tag)
59 #define TAG 0
60 #define ANCHOR 1
61 
62 //default timer delay
63 #define DEFAULT_TIMER_DELAY 80
64 
65 //debug mode
66 #ifndef DEBUG
67 #define DEBUG false
68 #endif
69 
70 
72 public:
73  //variables
74  // data buffer
75  static byte data[LEN_DATA];
76 
77  //initialisation
78  static void initCommunication(uint8_t myRST = DEFAULT_RST_PIN, uint8_t mySS = DEFAULT_SPI_SS_PIN, uint8_t myIRQ = 2);
79  static void configureNetwork(uint16_t deviceAddress, uint16_t networkId, const byte mode[]);
80  static void generalStart();
81  static void startAsAnchor(char address[], const byte mode[]);
82  static void startAsTag(char address[], const byte mode[]);
83  static boolean addNetworkDevices(DW1000Device* device, boolean shortAddress);
84  static boolean addNetworkDevices(DW1000Device* device);
85  static void removeNetworkDevices(int16_t index);
86 
87  //setters
88  static void setReplyTime(uint16_t replyDelayTimeUs);
89  static void setResetPeriod(uint32_t resetPeriod);
90 
91  //getters
92  static byte* getCurrentAddress() { return _currentAddress; };
93 
94  static byte* getCurrentShortAddress() { return _currentShortAddress; };
95 
96  static uint8_t getNetworkDevicesNumber() { return _networkDevicesNumber; };
97 
98  //ranging functions
99  static int16_t detectMessageType(byte datas[]); // TODO check return type
100  static void loop();
101  static void useRangeFilter(boolean enabled);
102  // Used for the smoothing algorithm (Exponential Moving Average). newValue must be >= 2. Default 15.
103  static void setRangeFilterValue(uint16_t newValue);
104 
105  //Handlers:
106  static void attachNewRange(void (* handleNewRange)(void)) { _handleNewRange = handleNewRange; };
107 
108  static void attachBlinkDevice(void (* handleBlinkDevice)(DW1000Device*)) { _handleBlinkDevice = handleBlinkDevice; };
109 
110  static void attachNewDevice(void (* handleNewDevice)(DW1000Device*)) { _handleNewDevice = handleNewDevice; };
111 
112  static void attachInactiveDevice(void (* handleInactiveDevice)(DW1000Device*)) { _handleInactiveDevice = handleInactiveDevice; };
113 
114 
115 
116  static DW1000Device* getDistantDevice();
117  static DW1000Device* searchDistantDevice(byte shortAddress[]);
118 
119  //FOR DEBUGGING
120  static void visualizeDatas(byte datas[]);
121 
122 
123 private:
124  //other devices in the network
125  static DW1000Device _networkDevices[MAX_DEVICES];
126  static uint8_t _networkDevicesNumber;
127  static int16_t _lastDistantDevice;
128  static byte _currentAddress[8];
129  static byte _currentShortAddress[2];
130  static byte _lastSentToShortAddress[2];
131  static DW1000Mac _globalMac;
132  static int32_t timer;
133  static int16_t counterForBlink;
134 
135  //Handlers:
136  static void (* _handleNewRange)(void);
137  static void (* _handleBlinkDevice)(DW1000Device*);
138  static void (* _handleNewDevice)(DW1000Device*);
139  static void (* _handleInactiveDevice)(DW1000Device*);
140 
141  //sketch type (tag or anchor)
142  static int16_t _type; //0 for tag and 1 for anchor
143  // TODO check type, maybe enum?
144  // message flow state
145  static volatile byte _expectedMsgId;
146  // message sent/received state
147  static volatile boolean _sentAck;
148  static volatile boolean _receivedAck;
149  // protocol error state
150  static boolean _protocolFailed;
151  // reset line to the chip
152  static uint8_t _RST;
153  static uint8_t _SS;
154  // watchdog and reset period
155  static uint32_t _lastActivity;
156  static uint32_t _resetPeriod;
157  // reply times (same on both sides for symm. ranging)
158  static uint16_t _replyDelayTimeUS;
159  //timer Tick delay
160  static uint16_t _timerDelay;
161  // ranging counter (per second)
162  static uint16_t _successRangingCount;
163  static uint32_t _rangingCountPeriod;
164  //ranging filter
165  static volatile boolean _useRangeFilter;
166  static uint16_t _rangeFilterValue;
167  //_bias correction
168  static char _bias_RSL[17]; // TODO remove or use
169  //17*2=34 bytes in SRAM
170  static int16_t _bias_PRF_16[17]; // TODO remove or use
171  //17 bytes in SRAM
172  static char _bias_PRF_64[17]; // TODO remove or use
173 
174 
175  //methods
176  static void handleSent();
177  static void handleReceived();
178  static void noteActivity();
179  static void resetInactive();
180 
181  //global functions:
182  static void checkForReset();
183  static void checkForInactiveDevices();
184  static void copyShortAddress(byte address1[], byte address2[]);
185 
186  //for ranging protocole (ANCHOR)
187  static void transmitInit();
188  static void transmit(byte datas[]);
189  static void transmit(byte datas[], DW1000Time time);
190  static void transmitBlink();
191  static void transmitRangingInit(DW1000Device* myDistantDevice);
192  static void transmitPollAck(DW1000Device* myDistantDevice);
193  static void transmitRangeReport(DW1000Device* myDistantDevice);
194  static void transmitRangeFailed(DW1000Device* myDistantDevice);
195  static void receiver();
196 
197  //for ranging protocole (TAG)
198  static void transmitPoll(DW1000Device* myDistantDevice);
199  static void transmitRange(DW1000Device* myDistantDevice);
200 
201  //methods for range computation
202  static void computeRangeAsymmetric(DW1000Device* myDistantDevice, DW1000Time* myTOF);
203 
204  static void timerTick();
205 
206  //Utils
207  static float filterValue(float value, float previousValue, uint16_t numberOfElements);
208 };
209 
Definition: DW1000Device.h:37
static DW1000Device * getDistantDevice()
Definition: DW1000Ranging.cpp:308
static boolean addNetworkDevices(DW1000Device *device, boolean shortAddress)
Definition: DW1000Ranging.cpp:211
static void startAsAnchor(char address[], const byte mode[])
Definition: DW1000Ranging.cpp:162
Definition: DW1000Mac.h:45
#define MAX_DEVICES
Definition: DW1000Ranging.h:46
DW1000RangingClass DW1000Ranging
Definition: DW1000Ranging.cpp:33
static byte * getCurrentAddress()
Definition: DW1000Ranging.h:92
static void attachNewRange(void(*handleNewRange)(void))
Definition: DW1000Ranging.h:106
static void removeNetworkDevices(int16_t index)
Definition: DW1000Ranging.cpp:267
static void generalStart()
Definition: DW1000Ranging.cpp:119
static void setReplyTime(uint16_t replyDelayTimeUs)
Definition: DW1000Ranging.cpp:291
static void setResetPeriod(uint32_t resetPeriod)
Definition: DW1000Ranging.cpp:293
static byte data[LEN_DATA]
Definition: DW1000Ranging.h:75
static uint8_t getNetworkDevicesNumber()
Definition: DW1000Ranging.h:96
static void visualizeDatas(byte datas[])
Definition: DW1000Ranging.cpp:948
Definition: DW1000Ranging.h:71
static void attachBlinkDevice(void(*handleBlinkDevice)(DW1000Device *))
Definition: DW1000Ranging.h:108
static DW1000Device * searchDistantDevice(byte shortAddress[])
Definition: DW1000Ranging.cpp:296
#define LEN_DATA
Definition: DW1000Ranging.h:43
Definition: DW1000Time.h:43
static void configureNetwork(uint16_t deviceAddress, uint16_t networkId, const byte mode[])
Definition: DW1000Ranging.cpp:108
static void useRangeFilter(boolean enabled)
Definition: DW1000Ranging.cpp:662
static void initCommunication(uint8_t myRST=DEFAULT_RST_PIN, uint8_t mySS=DEFAULT_SPI_SS_PIN, uint8_t myIRQ=2)
Definition: DW1000Ranging.cpp:92
#define DEFAULT_SPI_SS_PIN
Definition: DW1000Ranging.h:50
static int16_t detectMessageType(byte datas[])
Definition: DW1000Ranging.cpp:345
static void attachInactiveDevice(void(*handleInactiveDevice)(DW1000Device *))
Definition: DW1000Ranging.h:112
static byte * getCurrentShortAddress()
Definition: DW1000Ranging.h:94
static void setRangeFilterValue(uint16_t newValue)
Definition: DW1000Ranging.cpp:666
static void attachNewDevice(void(*handleNewDevice)(DW1000Device *))
Definition: DW1000Ranging.h:110
static void startAsTag(char address[], const byte mode[])
Definition: DW1000Ranging.cpp:188
#define DEFAULT_RST_PIN
Definition: DW1000Ranging.h:49
static void loop()
Definition: DW1000Ranging.cpp:359