-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
321 lines (276 loc) · 10.1 KB
/
main.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Things to be done:
1) Add timestamp to messages;
2) Add SD Card logging;
Output example (distance values are in mm):
{
"nodeId": 192C,
"distances":
[
{
"anchorId": D68B,
"distance": 956
},
{
"anchorId": C8B4,
"distance": 2313
},
{
"anchorId": 921F,
"distance": 4038
},
{
"anchorId": 489B,
"distance": 4933
}
]
}
{
"nodeId": 192C,
"position":
{
"x":4.198,
"y":2.284,
"z":0.763,
"quality":68
}
}^B01^arduinoTest^23^room
*/
#include <SPI.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
SPISettings settingsA(8000000, MSBFIRST, SPI_MODE0);
//A replacement for timestamp (not a good one, just so it is possible to differentiate messages)
// could be replaced with millis()
int msgCounter = 0;
void setup() {
pinMode(10,OUTPUT);
Serial.begin(115200);
SPI.begin();
}
void loop() {
char locationBuffer[500];
char distanceBuffer[300];
char anchorDistanceInfo[70];
char testName[12] = "arduinoTest\0";
char arduinoId[4] = "B01\0";
char zoneId[5] = "room\0";
// For debugging purposes
uint8_t wholeData[1500]; // Contains the whole received message
uint32_t iterator = 0;
uint8_t data[10][50]; // User data message array
uint8_t rxSize = 0x00; // Transfer size (0 - 255)
uint8_t rxNum = 0x00; // Transfer count (0 - n)
uint8_t dummy = 0x00;
uint16_t anchorDistance = 0x0000;
int32_t xCoordinate = 0x00000000;
int32_t yCoordinate = 0x00000000;
int32_t zCoordinate = 0x00000000;
uint8_t userMessageCount = 0; // How many user messages are in one transfer
uint8_t userDataLength = 29; // Length of user data to work with
uint8_t userDataLengthIt = 29;
uint8_t checkByteCount = 0x05; // Count of check bytes (sign of user data)
uint8_t checkByte = 0x99; // The byte to check by.
uint8_t dataCheckByteOccurance = 0x00; // Contains counter for check bytes (0x00 - 0x05)
SPI.beginTransaction(settingsA);
digitalWrite(10, LOW);
delay(1);
dummy = SPI.transfer(0x37);
dummy = SPI.transfer(0x02);
dummy = SPI.transfer(0x01);
dummy = SPI.transfer(0x00);
digitalWrite(10,HIGH);
delay(1);
// Get the transfer size and the number of transfers to be held
while (rxSize == 0x00) {
digitalWrite(10,LOW);
delay(1);
rxSize = SPI.transfer(0xFF);
rxNum = SPI.transfer(0xFF);
digitalWrite(10,HIGH);
delay(1);
}
// For debugging purposes
/*wholeData[iterator] = rxSize;
iterator++;
wholeData[iterator] = rxNum;
iterator++;*/
// Make rxNum transfers with rxSize byte count.
// Format for working with transfers, but
// only the first transfer contains tag data. Transfers go from 6E + rxNum.
// For defined request it is 6E + 0x05, which means that there
// will be 5 transfers with msg types from 6E to 72.
// More info in the Decawave documentation "DWM1001 Firmware API Guide"
for (byte j = 0; j < rxNum; j++) {
digitalWrite(10,LOW);
delay(1);
for (byte i = 0; i < rxSize; i++) {
wholeData[iterator] = SPI.transfer(0xFF);
if (dataCheckByteOccurance == checkByteCount && userDataLengthIt != 0) {
// read 34 bytes of user data
data[userMessageCount][userDataLength - userDataLengthIt] = wholeData[iterator];
userDataLengthIt--;
} else if (wholeData[iterator] == checkByte && dataCheckByteOccurance != checkByteCount && userDataLengthIt == userDataLength) {
dataCheckByteOccurance++;
} else if (dataCheckByteOccurance != checkByteCount && wholeData[iterator] != checkByte){
dataCheckByteOccurance = 0x00;
userDataLengthIt = userDataLength;
}
if (userDataLengthIt == 0x00){
userMessageCount++;
userDataLengthIt = userDataLength;
dataCheckByteOccurance = 0x00;
}
iterator++;
}
digitalWrite(10,HIGH);
delay(1);
}
delay(100);
SPI.endTransaction();
// The message should always have the checked rxSize and rxNum
// but it's checked just in case. The only transfer that is
// being logged is the first one, which holds the tag data. Other transfer
// data is being ignored, because for now it is only 0x00.
if(rxSize == 0xFF && rxNum == 0x05){
// For debugging
/*Serial.print("\nWhole data start....\n");
for (int i = 0; i < iterator; i++) {
Serial.print(wholeData[i],HEX);
Serial.print(" ");
if (i % 12 == 0) {
Serial.print("\n");
}
}
Serial.print("\nWhole data end....\n");*/
//Serial.print("\n");
/*Serial.print("Start....");
Serial.print("\n");*/
for (int it_msg = 0; it_msg < userMessageCount; it_msg++) {
/*Serial.print("Node user start....\n");
Serial.print("Node id: ");
Serial.print(data[it_msg][1], HEX);
Serial.print(data[it_msg][0], HEX);
Serial.print("\n");
Serial.print("Anchor count: ");
Serial.print(data[it_msg][2], HEX);
Serial.print("\n");*/
sprintf(distanceBuffer, "{\"nodeId\": %02X%02X, \"distances\": [", data[it_msg][1], data[it_msg][0]);
uint8_t anchorIt = 4;
uint8_t distanceIt = 5;
// Loop through anchor count
for (uint8_t i = 0; i < data[it_msg][2]; i++){
/*Serial.print("Anchor id: ");
Serial.print(data[it_msg][anchorIt], HEX);
Serial.print(data[it_msg][anchorIt-1], HEX);
Serial.print("\n");*/
/*Serial.print("Distance to anchor: ");
Serial.print(data[it_msg][distanceIt], HEX);*/
anchorDistance = data[it_msg][distanceIt+1] << 8;
//Serial.print(data[it_msg][distanceIt+1], HEX);
anchorDistance += data[it_msg][distanceIt];
//Serial.print("\n");
sprintf(anchorDistanceInfo, "{\"anchorId\": %02X%02X, \"distance\": %d }", data[it_msg][anchorIt], data[it_msg][anchorIt-1],
(int) anchorDistance);
strcat(distanceBuffer, anchorDistanceInfo);
strcat(distanceBuffer, ", ");
//delay(100);
/*Serial.print(anchorDistance, DEC);
Serial.print("\n");*/
anchorIt += 4;
distanceIt += 4;
anchorDistance = 0x0000;
}
Serial.print("\n");
distanceBuffer[strlen(distanceBuffer) - 2] = ']';
distanceBuffer[strlen(distanceBuffer) - 1] = '}';
// Print tag anchor distances
// This should be logged in an sd card
Serial.print(distanceBuffer);
//Serial.print("\n");
/*Serial.print("x coordinate: ");
Serial.print(data[it_msg][19], HEX);
Serial.print(data[it_msg][20], HEX);
Serial.print(data[it_msg][21], HEX);
Serial.print("\n");*/
xCoordinate = (int16_t) data[it_msg][21] << 16;
xCoordinate += (int16_t) data[it_msg][20] << 8;
xCoordinate += (int16_t) data[it_msg][19];
//Serial.print(xCoordinate, DEC);
//Serial.print("\n");
/*Serial.print("y coordinate: ");
Serial.print(data[it_msg][22], HEX);
Serial.print(data[it_msg][23], HEX);
Serial.print(data[it_msg][24], HEX);
Serial.print("\n");*/
yCoordinate = (int16_t) data[it_msg][24] << 16;
yCoordinate += (int16_t) data[it_msg][23] << 8;
yCoordinate += (int16_t) data[it_msg][22];
//Serial.print(yCoordinate, DEC);
//Serial.print("\n");
/*Serial.print("z coordinate: ");
Serial.print(data[it_msg][25], HEX);
Serial.print(data[it_msg][26], HEX);
Serial.print(data[it_msg][27], HEX);
Serial.print("\n");*/
zCoordinate = (int16_t) data[it_msg][27] << 16;
zCoordinate += (int16_t) data[it_msg][26] << 8;
zCoordinate += (int16_t) data[it_msg][25];
//Serial.print(zCoordinate, DEC);
Serial.print("\n");
/*Serial.print("Quality factor: ");
Serial.print(data[it_msg][userDataLength-1], DEC);
Serial.print("\n");*/
// Workaround for printing floating point numbers
uint16_t decimalValue = abs(xCoordinate) % 1000;
uint16_t metres = abs(xCoordinate)/1000;
char xReal[20];
if (xCoordinate < 0){
sprintf(xReal, "-%" PRIu16 ".%" PRIu16, metres, decimalValue);
} else {
sprintf(xReal, "%" PRIu16 ".%" PRIu16, metres, decimalValue);
}
decimalValue = abs(yCoordinate) % 1000;
metres = abs(yCoordinate)/1000;
char yReal[20];
if (yCoordinate < 0){
sprintf(yReal, "-%" PRIu16 ".%" PRIu16, metres, decimalValue);
} else {
sprintf(yReal, "%" PRIu16 ".%" PRIu16, metres, decimalValue);
}
decimalValue = abs(zCoordinate) % 1000;
metres = abs(zCoordinate)/1000;
char zReal[20];
if (zCoordinate < 0){
sprintf(zReal, "-%" PRIu16 ".%" PRIu16, metres, decimalValue);
} else {
sprintf(zReal, "%" PRIu16 ".%" PRIu16, metres, decimalValue);
}
sprintf(locationBuffer, "{\"nodeId\": %02X%02X, \"position\":{\"x\":%s,\"y\":%s,\"z\":%s,\"quality\":%d}}^%s^%s^%d^%s",
data[it_msg][1],
data[it_msg][0],
xReal,
yReal,
zReal,
(int) data[it_msg][userDataLength-1],
arduinoId, testName, msgCounter, zoneId);
msgCounter++;
// Print tag location information
// This should be logged in an sd card
Serial.print(locationBuffer);
/*Serial.print("\n");
for (int it = 0; it < userDataLength; it++) {
Serial.print(data[it_msg][it],HEX);
Serial.print(" ");
if (it % 10 == 0 && it != 0) {
Serial.print("\n");
}
}*/
//Serial.print("Node user end....\n");
}
/*Serial.print("\n");
Serial.print("End....");
Serial.print("\n");*/
}
}