-
Notifications
You must be signed in to change notification settings - Fork 15
/
Station.cpp
438 lines (367 loc) · 10.9 KB
/
Station.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/***************************************************************************
* *
* Copyright (C) 2017 by University of Illinois *
* *
* http://illinois.edu *
* *
***************************************************************************/
/**
* @file Station.cpp
*
* A Station object organizes drones and senders, communicates with Vicon and
* updates measurements for drones using a separate thread.
*
* A StationWindow is the main GUI.
*
* This is the Model of the
* Model(Station)-View(StationWindow)-Controller(StationController) pattern.
*
* @author Bo Liu <[email protected]>
*
*/
#include "Station.h"
#include <QMessageBox>
#include <iostream>
#include <vector>
#include <sstream>
#ifdef WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <utility>
#include <set>
#include <algorithm>
#include <iterator>
using namespace ViconDataStreamSDK::CPP;
void Station::_connectVicon()
{
shouldExit = false;
if (vicon_.IsConnected().Connected) {
QMessageBox msgBox;
msgBox.setText("Vicon client is running.");
msgBox.exec();
return;
}
while( !vicon_.IsConnected().Connected && !shouldExit)
{
// Direct connection
bool ok = false;
QString host = hostAddress.toString();
host += QString(":");
host += QString::number(hostPort);
std::cout << "connecting to ..." << host.toStdString() << std::endl;
ok =( vicon_.Connect( host.toStdString() ).Result == Result::Success );
if(!ok)
{
std::cout << "Warning - connect failed..." << std::endl;
}
std::cout << ".";
#ifdef WIN32
Sleep( 1000 );
#else
sleep(1);
#endif
}
if (!vicon_.IsConnected().Connected)
{
std::cout << "connecting to Vicon failed." << std::endl;
return;
}
vicon_.EnableSegmentData();
vicon_.EnableMarkerData();
vicon_.SetStreamMode(StreamMode::ServerPush);
// MavLink uses NED coornidate frame, the following map assumes
// Vicon is calibrated according to manual using the Wand with z up.
if (north == "+x") {
vicon_.SetAxisMapping(Direction::Forward,Direction::Right,Direction::Down);
}
else if (north == "-x") {
vicon_.SetAxisMapping(Direction::Backward,Direction::Left,Direction::Down);
}
else if (north == "+y") {
vicon_.SetAxisMapping(Direction::Left,Direction::Forward,Direction::Down);
}
else if (north == "-y") {
vicon_.SetAxisMapping(Direction::Right,Direction::Backward,Direction::Down);
}
std::cout << "North mapped to " << north.toStdString() << std::endl;
auto _Output_GetAxisMapping = vicon_.GetAxisMapping();
qDebug() << "Axis Mapping: X-" << _Adapt( _Output_GetAxisMapping.XAxis )
<< " Y-" << _Adapt( _Output_GetAxisMapping.YAxis )
<< " Z-" << _Adapt( _Output_GetAxisMapping.ZAxis );
vicon_.GetFrame();
auto frame_rate = vicon_.GetFrameRate();
rate = frame_rate.FrameRateHz;
std::cout << "frame rate = " << frame_rate.FrameRateHz << std::endl;
std::cout << "Vicon origin GPS: " << originGPS.lat << "," << originGPS.lon << "," << originGPS.alt << std::endl;
setdt(1.0/rate); // initial value for dt (second/frame)
emit ViconConnected();
}
void Station::setupConnections()
{
connect(this, &Station::ViconConnected,
this, &Station::dataStream);
}
Station::Station(QObject *parent) : QObject(parent),
vicon_(ViconDataStreamSDK::CPP::Client())
{
initialize();
setupConnections();
}
void Station::initialize()
{
isInitialized = false;
shouldExit = false;
/* the following values will appear in UI */
hostAddress = QHostAddress("192.168.1.3");
hostPort = 801;
originGPS.lat = 40.1156786;
originGPS.lon = -88.2243507;
originGPS.alt = 222.196;
originGPS.gps_to_local = GeographicLib::LocalCartesian(originGPS.lat,
originGPS.lon,
originGPS.alt);
north = "+x";
}
Station::~Station()
{
disconnectVicon();
}
QHostAddress Station::getHostAddress() const
{
return hostAddress;
}
void Station::setHostAddress(const QHostAddress &value)
{
hostAddress = value;
}
quint16 Station::getHostPort() const
{
return hostPort;
}
void Station::setHostPort(const quint16 &value)
{
hostPort = value;
}
double Station::getRate() const
{
return rate;
}
void Station::setRate(double value)
{
rate = value;
}
void Station::connectVicon()
{
isInitialized = false;
viconConnectFuture = QtConcurrent::run(this, &Station::_connectVicon);
}
void Station::disconnectVicon()
{
shouldExit = true;
dataStreamFuture.cancel();
viconConnectFuture.cancel();
dataStreamFuture.waitForFinished();
viconConnectFuture.waitForFinished();
if (!vicon_.IsConnected().Connected) {
QMessageBox msgBox;
msgBox.setText("Vicon client is NOT connected.");
msgBox.exec();
return;
}
vicon_.DisableMarkerData();
vicon_.DisableSegmentData();
std::cout << " Disconnecting Vicon..." << std::endl;
vicon_.Disconnect();
std::cout << " Disconnected! " << std::endl;
emit ViconDisconnected();
}
void Station::addDrone(const QString &name)
{
if (droneCollection.contains(name)) {
std::cout << "Drone: " << name.toStdString() << " already exists." << std::endl;
return;
}
droneCollection.insert(name, Drone(name));
emit droneAdded(name);
}
void Station::addDrone(const QString &name, double x, double y, double z, double q_0, double q_1, double q_2, double q_3)
{
if (droneCollection.contains(name)) {
std::cout << "Drone: " << name.toStdString() << " already exists." << std::endl;
return;
}
droneCollection.insert(name, Drone(name, x, y, z, q_0, q_1, q_2, q_3));
emit droneAdded(name);
}
void Station::removeDrone(const QString &name)
{
if (droneCollection.contains(name)) {
droneCollection.remove(name);
emit droneRemoved(name);
}
}
void Station::_dataStream()
{
int counter = 0;
while (!shouldExit) {
if (counter > 1000) {
qDebug() << "Streaming ... ";
counter = 0;
} else {
++counter;
}
while( vicon_.GetFrame().Result != Result::Success )
{
// Sleep a little so that we don't lumber the CPU with a busy poll
#ifdef WIN32
Sleep( 200 );
#else
sleep(1);
#endif
}
Output_GetFrameNumber _Output_GetFrameNumber = vicon_.GetFrameNumber();
// update drone collection
auto subCount = vicon_.GetSubjectCount().SubjectCount;
std::set<QString> activeDroneNames;
for (uint i = 0; i != subCount; ++i) {
auto subName = QString::fromStdString(std::string(vicon_.GetSubjectName(i).SubjectName));
activeDroneNames.insert(subName);
if (!droneCollection.contains(subName)) {
addDrone(subName);
}
}
std::vector<QString> inactiveDroneNames;
for (const auto& x : droneCollection) {
if (activeDroneNames.count(x.getName()) == 0)
inactiveDroneNames.push_back(x.getName());
}
for (const auto& x : inactiveDroneNames)
removeDrone(x);
auto frameNew = _Output_GetFrameNumber.FrameNumber; // use frame number as a hardware time
if (!isInitialized) { // do not update dt in initialization
setFrame(frameNew);
for (auto& x : droneCollection) {
auto drone_translation = vicon_.GetSegmentLocalTranslation(x.getName().toStdString(), x.getName().toStdString());
auto drone_rotation = vicon_.GetSegmentLocalRotationQuaternion(x.getName().toStdString(), x.getName().toStdString());
mavlink_att_pos_mocap_t pos;
// ---- writing data start ---- //
lock.lockForWrite();
pos.x = drone_translation.Translation[0] / 1000.0;
pos.y = drone_translation.Translation[1] / 1000.0;
pos.z = drone_translation.Translation[2] / 1000.0;
pos.q[0] = drone_rotation.Rotation[0];
pos.q[1] = drone_rotation.Rotation[1];
pos.q[2] = drone_rotation.Rotation[2];
pos.q[3] = drone_rotation.Rotation[3];
x.setPos(pos);
x.setTime(frameNew);
lock.unlock();
// ---- writing data end ---- //
}
isInitialized = true;
}
auto frameDiff = frameNew - getFrame();
setFrame(frameNew);
setdt(frameDiff/rate);
emit dtUpdated(dt);
// if ( frameDiff > 1 )
// qDebug() << frameDiff-1 << " frames dropped, transient dt = " << dt*1000 << " ms";
for (auto& x : droneCollection) {
auto drone_translation = vicon_.GetSegmentLocalTranslation(x.getName().toStdString(), x.getName().toStdString());
auto drone_rotation = vicon_.GetSegmentLocalRotationQuaternion(x.getName().toStdString(), x.getName().toStdString());
mavlink_att_pos_mocap_t pos;
// ---- writing data start ---- //
lock.lockForWrite();
pos.x = drone_translation.Translation[0];
pos.y = drone_translation.Translation[1];
pos.z = drone_translation.Translation[2];
pos.q[0] = drone_rotation.Rotation[0];
pos.q[1] = drone_rotation.Rotation[1];
pos.q[2] = drone_rotation.Rotation[2];
pos.q[3] = drone_rotation.Rotation[3];
x.setPos(pos);
x.setTime(frameNew);
lock.unlock();
// ---- writing data end ---- //
}
}
}
void Station::dataStream()
{
dataStreamFuture = QtConcurrent::run(this, &Station::_dataStream);
}
double Station::getdt() const
{
return dt;
}
void Station::setdt(double value)
{
dt = value;
}
long long Station::getFrame() const
{
return frame;
}
void Station::setFrame(long long value)
{
frame = value;
}
mavlink_att_pos_mocap_t Station::getMeas(const QString& name, long long &frame)
{
lock.lockForRead();
auto ret = droneCollection[name].getPos(frame);
lock.unlock();
return ret;
}
QString Station::getOriginGPS() const
{
std::string gps = std::to_string(originGPS.lat);
gps = gps + ',';
gps = gps + std::to_string(originGPS.lon);
gps = gps + ',';
gps = gps + std::to_string(originGPS.alt);
return QString::fromStdString(gps);
}
void Station::setOriginGPS(const QString &q_gps)
{
std::istringstream stream(q_gps.toStdString());
std::vector<double> entries;
std::string entry;
while(getline(stream, entry, ',')) {
entries.push_back(std::atof(entry.c_str()));
}
originGPS.gps_to_local = GeographicLib::LocalCartesian(entries[0],entries[1],entries[2]);
originGPS.lat = entries[0];
originGPS.lon = entries[1];
originGPS.alt = entries[2];
}
GeographicLib::LocalCartesian Station::getGpsToLocal() const
{
return originGPS.gps_to_local;
}
void Station::setNorth(const QString &axis)
{
north = axis;
}
QString Station::_Adapt( const Direction::Enum i_Direction )
{
switch( i_Direction )
{
case Direction::Forward:
return QString("Forward");
case Direction::Backward:
return QString("Backward");
case Direction::Left:
return QString("Left");
case Direction::Right:
return QString("Right");
case Direction::Up:
return QString("Up");
case Direction::Down:
return QString("Down");
default:
return QString("Unknown");
}
}