-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDW1000Positioning.cpp
executable file
·262 lines (215 loc) · 8.43 KB
/
DW1000Positioning.cpp
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
/**
* DW1000Positioning.cpp
* Based on DW1000Ranging library by Thomas Trojer
* Written by Morten Jansrud
* https://mortenjansrud.com
* Feel free to modify and redistribute
*/
#include "DW1000Positioning.h"
DW1000PositioningClass DW1000Positioning;
void DW1000PositioningClass::startAsAnchor(uint8_t _address){
_isTag = false;
_device = &_devices[_address];
}
void DW1000PositioningClass::startAsTag(uint8_t _address){
_isTag = true;
_device = &_devices[_address];
}
void DW1000PositioningClass::initTestDevices(){
for (uint8_t i = 0; i < _NUM_DEVICES; i++){
struct _Node _node;
_node.address = i;
_node.distance = random(2, 100);
for (uint8_t j = 0; j < _NUM_DEVICES; j++){
_node.distances[j].distance = random(2, 100);
}
_devices[i] = _node;
}
}
void DW1000PositioningClass::initDevices(){
for (uint8_t i = 0; i < _NUM_DEVICES; i++){
struct _Node _node;
_node.address = i;
_devices[i] = _node;
}
}
void DW1000PositioningClass::activeDevice(uint8_t _address){
_devices[_address].active = true;
}
void DW1000PositioningClass::inactiveDevice(uint8_t _address){
_devices[_address].active = false;
_devices[_address].distance = 0.0;
}
void DW1000PositioningClass::setDistance(uint8_t _address, float _distance){
_devices[_address].distance = _distance;
}
void DW1000PositioningClass::setDistanceBetweenDevices(uint8_t _from, uint8_t _to, float _distance){
_devices[_from].distances[_to].distance = _distance;
Serial.print("Msg,from:");
Serial.print(_from);
Serial.print(",to:");
Serial.print(_to);
Serial.print(",range:");
Serial.println(_distance);
}
void DW1000PositioningClass::calculatePositions(){
calculateAnchorPositions();
calculateTagPositions();
}
void DW1000PositioningClass::calculateAnchorPositions(){
//We define anchor 1 with position [0,0,0];
//Define X axis between 1 -> 2.
//Then we know that anchor 2 has position [X,0,0];
_devices[2].position.x = _devices[1].distances[2].distance;
//We use pytagoras to calculate position in C, make two triangles and solve two equations for X and Y.
float A = _devices[1].distances[2].distance;
float B = _devices[2].distances[3].distance;
float C = _devices[1].distances[3].distance;
float X = (pow(A,2)+pow(B,2)-pow(C,2))/(2*A);
float Y = sqrt((-pow(A, 4)+2*pow(A,2)*(pow(B,2)+pow(C,2))-pow(pow(B,2)-pow(C,2),2))/(2*A));
_devices[3].position.x = X;
_devices[3].position.y = Y;
}
void DW1000PositioningClass::calculateTagPositions(){
//We use pytagoras to calculate position in C, make two triangles and solve two equations for X and Y.
float A = _devices[1].distances[2].distance;
float B = _devices[1].distance;
float C = _devices[2].distance;
float X = (pow(A,2)+pow(B,2)-pow(C,2))/(2*A);
float Y = sqrt((-pow(A, 4)+2*pow(A,2)*(pow(B,2)+pow(C,2))-pow(pow(B,2)-pow(C,2),2))/(2*A));
_device->position.x = X;
_device->position.y = Y;
}
void DW1000PositioningClass::calculatePositionsAndDraw(){
calculatePositions();
serialDrawDistances();
serialSendPosititions();
}
String DW1000PositioningClass::createJsonDistance(struct _Node _node){
/*
TODO: Send device information more effiecient than json
*/
String json;
json += "{\"type\":";
json += DISTANCE;
json += ",\"from\":";
json += _device->address;
json += ",\"to\":";
json += _node.address;
json += ",\"range\":";
json += _node.distance;
json += "}";
return json;
}
void DW1000PositioningClass::serialSendPositition(struct _Node _node){
Serial.print("Dev:");
Serial.print(_node.address);
Serial.print("[");
Serial.print(_node.position.x);
Serial.print(",");
Serial.print(_node.position.y);
Serial.print(",");
Serial.print(_node.position.z);
Serial.println("]");
}
void DW1000PositioningClass::serialSendDistance(struct _Node _node){
Serial.println(createJsonDistance(_node));
}
void DW1000PositioningClass::serialSendPosititions(){
for (uint8_t i = 0; i < _NUM_DEVICES; i++){
serialSendPositition(_devices[i]);
}
}
void DW1000PositioningClass::serialSendDistances(){
for (uint8_t i = 0; i < _NUM_DEVICES; i++){
//Prevent sending own distance (0m) over network
if(_device->address != _devices[i].address){
serialSendDistance(_devices[i]);
}
}
}
void DW1000PositioningClass::serialDrawDistances(){
Serial.println("Y" );
Serial.println("▲" );
Serial.println("3" );
Serial.println("| \\" );
Serial.println("| \\ \\" );
Serial.println("| \\ \\" );
Serial.println("| \\ \\" );
Serial.println("| \\ \\" );
Serial.print( "| " ); Serial.print(_devices[3].distance); Serial.println( " \\" );
Serial.print( "| \\ " ); Serial.println( " \\" );
Serial.print( "| \\ " ); Serial.println(_devices[2].distances[3].distance);
Serial.print(_devices[1].distances[3].distance);
Serial.print( " 0 " ); Serial.println( " \\" );
Serial.print( "| / " ); Serial.println(" \\ \\" );
Serial.print( "| " ); Serial.print(_devices[1].distance);
Serial.print( " " ); Serial.print(_devices[2].distance); Serial.println( " \\" );
Serial.print( "| / " ); Serial.println( " \\ \\" );
Serial.print( "1 ------ " ); Serial.print(_devices[1].distances[2].distance); Serial.println( " ------------ 2►X" );
/*
Y
|
3
| \
| \ \
| \ \
| \ \
| \ \
| 51.00 \
| \ \
| \ 17.00
13.00 0 \
| / \ \
| 8.00 90.00 \
| / \ \
1 ------ 53.00 ------------ 2 -> X
// Alternative
Serial.print( "1 ------ " ); Serial.print(_devices[1].distances[2].distance); Serial.println( " ------ 2");
Serial.print( "| \\ " ); Serial.println( " //" );
Serial.print( "| " ); Serial.print(_devices[1].distance);
Serial.print( " " ); Serial.print(_devices[2].distance); Serial.println( " /" );
Serial.print( "| \\" ); Serial.println( " / /" );
Serial.print(_devices[1].distances[3].distance);
Serial.print( " 0 " ); Serial.println( " /" );
Serial.print( "| / "); Serial.println(_devices[2].distances[3].distance);
Serial.print( "| / " ); Serial.println( " /" );
Serial.print( "| " ); Serial.print(_devices[3].distance); Serial.println( " /" );
Serial.println("| / /" );
Serial.println("| / /" );
Serial.println("| / /" );
Serial.println("| / /" );
Serial.println("| //" );
Serial.println("3");
1 ------ 53.00 ------ 2
| \ //
| 8.00 90.00 /
| \ / /
13.00 0 /
| / 17.00
| / /
| 51.00 /
| / /
| / /
| / /
| / /
| //
3
*/
}
/*
Setters and getters
*/
struct _Node DW1000PositioningClass::getNextDevice(){
_nextDevice == 2 ? _nextDevice = 0 : _nextDevice++;
return _devices[_nextDevice];
}
struct _Node* DW1000PositioningClass::getDevice(){
return _device;
}
_STATES DW1000PositioningClass::getState(){
return _state;
}
void DW1000PositioningClass::setState(_STATES state){
_state = state;
}