-
Notifications
You must be signed in to change notification settings - Fork 117
/
TwoWayRangingResponder.ino
275 lines (247 loc) · 7.71 KB
/
TwoWayRangingResponder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
@file TwoWayRangingResponder.ino
@author rakwireless.com
@brief TwoWay Ranging Responder
@version 0.1
@date 2022-1-28
@copyright Copyright (c) 2022
**/
#include <SPI.h>
#include <DW1000Ng.hpp>
#include <DW1000NgUtils.hpp>
#include <DW1000NgRanging.hpp>
int antenna_delay = 16500; //16500
// connection pins
const uint8_t PIN_SS = SS; // spi select pin
const uint8_t PIN_RST = WB_IO6;
const uint8_t PIN_IRQ = WB_IO5;
// messages used in the ranging protocol
// TODO replace by enum
#define POLL 0
#define POLL_ACK 1
#define RANGE 2
#define RANGE_REPORT 3
#define RANGE_FAILED 255
// message flow state
volatile byte expectedMsgId = POLL;
// message sent/received state
volatile boolean sentAck = false;
volatile boolean receivedAck = false;
// protocol error state
boolean protocolFailed = false;
// timestamps to remember
uint64_t timePollSent;
uint64_t timePollReceived;
uint64_t timePollAckSent;
uint64_t timePollAckReceived;
uint64_t timeRangeSent;
uint64_t timeRangeReceived;
uint64_t timeComputedRange;
// last computed range/time
// data buffer
#define LEN_DATA 16
byte data[LEN_DATA];
// watchdog and reset period
uint32_t lastActivity;
uint32_t resetPeriod = 250;
// reply times (same on both sides for symm. ranging)
uint16_t replyDelayTimeUS = 0;
// ranging counter (per second)
uint16_t successRangingCount = 0;
uint32_t rangingCountPeriod = 0;
float samplingRate = 0;
device_configuration_t DEFAULT_CONFIG = {
false,
true,
true,
true,
false,
SFDMode::STANDARD_SFD,
Channel::CHANNEL_5,
DataRate::RATE_850KBPS,
PulseFrequency::FREQ_16MHZ,
PreambleLength::LEN_256,
PreambleCode::CODE_3
};
interrupt_configuration_t DEFAULT_INTERRUPT_CONFIG = {
true,
true,
true,
false,
true,
true
};
void setup() {
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH); //Turn on the power switch
delay(300);
time_t serial_timeout = millis();
// DEBUG monitoring
Serial.begin(115200);
while (!Serial) {
if ((millis() - serial_timeout) < 5000) {
delay(100);
} else {
break;
}
}
Serial.println(F("### DW1000Ng-arduino-ranging-anchor ###"));
// initialize the driver
DW1000Ng::initialize(PIN_SS, PIN_IRQ, PIN_RST);
// DW1000Ng::initializeNoInterrupt(PIN_SS, PIN_RST);
Serial.println(F("DW1000Ng initialized ..."));
// general configuration
DW1000Ng::applyConfiguration(DEFAULT_CONFIG);
DW1000Ng::applyInterruptConfiguration(DEFAULT_INTERRUPT_CONFIG);
DW1000Ng::setDeviceAddress(1);
// DW1000Ng::setNetworkId(10);
DW1000Ng::setAntennaDelay(antenna_delay); //16551
Serial.println(F("Committed configuration ..."));
// DEBUG chip info and registers pretty printed
char msg[128];
DW1000Ng::getPrintableDeviceIdentifier(msg);
Serial.print("Device ID: ");
Serial.println(msg);
DW1000Ng::getPrintableExtendedUniqueIdentifier(msg);
Serial.print("Unique ID: ");
Serial.println(msg);
DW1000Ng::getPrintableNetworkIdAndShortAddress(msg);
Serial.print("Network ID & Device Address: ");
Serial.println(msg);
DW1000Ng::getPrintableDeviceMode(msg);
Serial.print("Device mode: ");
Serial.println(msg);
// attach callback for (successfully) sent and received messages
DW1000Ng::attachSentHandler(handleSent);
DW1000Ng::attachReceivedHandler(handleReceived);
// anchor starts in receiving mode, awaiting a ranging poll message
receiver();
noteActivity();
// for first time ranging frequency computation
rangingCountPeriod = millis();
}
void noteActivity() {
// update activity timestamp, so that we do not reach "resetPeriod"
lastActivity = millis();
}
void resetInactive() {
// anchor listens for POLL
expectedMsgId = POLL;
receiver();
noteActivity();
}
void handleSent() {
// status change on sent success
sentAck = true;
}
void handleReceived() {
// status change on received success
receivedAck = true;
}
void transmitPollAck() {
data[0] = POLL_ACK;
DW1000Ng::setTransmitData(data, LEN_DATA);
DW1000Ng::startTransmit();
}
void transmitRangeReport(float curRange) {
data[0] = RANGE_REPORT;
// write final ranging result
memcpy(data + 1, &curRange, 4);
DW1000Ng::setTransmitData(data, LEN_DATA);
DW1000Ng::startTransmit();
}
void transmitRangeFailed() {
data[0] = RANGE_FAILED;
DW1000Ng::setTransmitData(data, LEN_DATA);
DW1000Ng::startTransmit();
}
void receiver() {
DW1000Ng::forceTRxOff();
usb_reset();
// so we don't need to restart the receiver manually
DW1000Ng::startReceive();
}
void loop() {
int32_t curMillis = millis();
if (!sentAck && !receivedAck) {
// check if inactive
if (curMillis - lastActivity > resetPeriod) {
resetInactive();
}
return;
}
// continue on any success confirmation
if (sentAck) {
sentAck = false;
byte msgId = data[0];
if (msgId == POLL_ACK) {
timePollAckSent = DW1000Ng::getTransmitTimestamp();
noteActivity();
}
DW1000Ng::startReceive();
}
if (receivedAck) {
receivedAck = false;
// get message and parse
DW1000Ng::getReceivedData(data, LEN_DATA);
byte msgId = data[0];
if (msgId != expectedMsgId) {
// unexpected message, start over again (except if already POLL)
protocolFailed = true;
}
if (msgId == POLL) {
protocolFailed = false;
timePollReceived = DW1000Ng::getReceiveTimestamp();
expectedMsgId = RANGE;
transmitPollAck();
noteActivity();
} else if (msgId == RANGE) {
timeRangeReceived = DW1000Ng::getReceiveTimestamp();
expectedMsgId = POLL;
if (!protocolFailed) {
timePollSent = DW1000NgUtils::bytesAsValue(data + 1, LENGTH_TIMESTAMP);
timePollAckReceived = DW1000NgUtils::bytesAsValue(data + 6, LENGTH_TIMESTAMP);
timeRangeSent = DW1000NgUtils::bytesAsValue(data + 11, LENGTH_TIMESTAMP);
// (re-)compute range as two-way ranging is done
double distance = DW1000NgRanging::computeRangeAsymmetric(timePollSent,
timePollReceived,
timePollAckSent,
timePollAckReceived,
timeRangeSent,
timeRangeReceived);
/* Apply simple bias correction */
distance = DW1000NgRanging::correctRange(distance);
String rangeString = "Range: ";
rangeString += distance;
rangeString += " m";
rangeString += "\t RX power: ";
rangeString += DW1000Ng::getReceivePower();
rangeString += " dBm";
rangeString += "\t Sampling: ";
rangeString += samplingRate;
rangeString += " Hz";
Serial.println(rangeString);
//Serial.print("FP power is [dBm]: "); Serial.print(DW1000Ng::getFirstPathPower());
//Serial.print("RX power is [dBm]: "); Serial.println(DW1000Ng::getReceivePower());
//Serial.print("Receive quality: "); Serial.println(DW1000Ng::getReceiveQuality());
// update sampling rate (each second)
transmitRangeReport(distance * DISTANCE_OF_RADIO_INV);
successRangingCount++;
if (curMillis - rangingCountPeriod > 1000) {
samplingRate = (1000.0f * successRangingCount) / (curMillis - rangingCountPeriod);
rangingCountPeriod = curMillis;
successRangingCount = 0;
}
} else {
transmitRangeFailed();
}
noteActivity();
}
}
}
void usb_reset(void) {
// DW1000Ng::clearReceiveStatus();
DW1000Ng::applyConfiguration(DEFAULT_CONFIG);
DW1000Ng::applyInterruptConfiguration(DEFAULT_INTERRUPT_CONFIG);
DW1000Ng::setAntennaDelay(antenna_delay);
}