-
Notifications
You must be signed in to change notification settings - Fork 15
/
Sender.cpp
368 lines (311 loc) · 8.4 KB
/
Sender.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
/***************************************************************************
* *
* Copyright (C) 2017 by University of Illinois *
* *
* http://illinois.edu *
* *
***************************************************************************/
/**
* @file Sender.cpp
*
* A Sender object reads Vicon data from a Station object, processes the data
* with Kalman filtering and computes a GPS MAVLink data pack, sends MAVLink
* messages to a robot via UDP.
*
* This is the Model of the
* Model(Sender)-View(SenderWindow)-Controller(SenderController) pattern.
*
* @author Bo Liu <boliu1@illinois.edu>
*
*/
#include "Sender.h"
#include <cmath>
#include <chrono>
using Eigen::VectorXd;
using Eigen::MatrixXd;
Sender::Sender(const QString &name, std::unique_ptr<Station> &station, QObject *parent) : QObject(parent),
isInitialized {false},
isRunning {false},
station {station},
name {name},
udpSocket { new QUdpSocket(this)}
{
initialize();
setupConnections();
}
Sender::~Sender()
{
delete udpSocket;
qDebug() << "Sender destructor.";
}
void Sender::setupConnections()
{
connect(&timer, SIGNAL(timeout()),
this, SLOT(timerHandler()));
}
void Sender::start()
{
startTimer(rate);
}
void Sender::stop()
{
stopTimer();
}
void Sender::initialize()
{
// init x, dt
updateMeas();
// init Kalmen Filter using newly-fetched x, dt
init_KF();
/* the following will appear on UI */
rate = 10;
useGPS = true;
useLocPos = true;
useViconEst = true;
sysid = 1;
compid = 1;
remoteAddress = QHostAddress("192.168.1.2");
remotePort = 45454;
}
void Sender::init_KF()
{
VectorXd x_in(6);
x_in << viconEstMsg.x/1000.0, viconEstMsg.y/1000.0, viconEstMsg.z/1000.0,
0.0, 0.0, 0.0;
MatrixXd P_in(6,6);
P_in << 0.1, 0, 0, 0, 0, 0,
0, 0.1, 0, 0, 0, 0,
0, 0, 0.1, 0, 0, 0,
0, 0, 0, 10., 0, 0,
0, 0, 0, 0, 10., 0,
0, 0, 0, 0, 0, 10.;
MatrixXd F_in(6,6);
F_in << 1, 0, 0, dt, 0, 0,
0, 1, 0, 0, dt, 0,
0, 0, 1, 0, 0, dt,
0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1;
MatrixXd Q_in(6,6);
Q_in << 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 1;
MatrixXd H_in(3,6);
H_in << 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0;
MatrixXd R_in(3,3);
R_in << 0.01, 0, 0,
0, 0.01, 0,
0, 0, 0.01;
kf.init(x_in, P_in, F_in, Q_in, H_in, R_in);
}
void Sender::updateTimer(uint8_t r)
{
rate = r;
if (isRunning) {
stopTimer();
startTimer(rate);
}
}
void Sender::startTimer(uint8_t rate)
{
auto msecTime = 1.0/rate * 1000;
timer.start(msecTime);
isRunning = true;
}
void Sender::stopTimer()
{
timer.stop();
isRunning = false;
}
QHostAddress Sender::getRemoteAddress() const
{
return remoteAddress;
}
quint16 Sender::getRemotePort() const
{
return remotePort;
}
uint8_t Sender::getSysID() const
{
return sysid;
}
uint8_t Sender::getCompID() const
{
return compid;
}
bool Sender::getUseGps() const
{
return useGPS;
}
bool Sender::getUseLocPos() const
{
return useLocPos;
}
bool Sender::getUseVicon() const
{
return useViconEst;
}
uint8_t Sender::getRate() const
{
return rate;
}
void Sender::setRate(uint8_t rate)
{
this->rate = rate;
updateTimer(rate);
}
void Sender::setUseGps(bool use)
{
useGPS = use;
}
void Sender::setUseVicon(bool use)
{
useViconEst = use;
}
void Sender::setUseLocPos(bool use)
{
useLocPos = use;
}
void Sender::setSysID(uint8_t id)
{
sysid = id;
}
void Sender::setCompID(uint8_t id)
{
compid = id;
}
void Sender::timerHandler()
{
updateMeas();
updateLocPosFromMeas();
updateGpsFromeLocPos();
sendDatagram();
emit measUpdated();
}
void Sender::setRemoteAddress(const QString &value)
{
remoteAddress = QHostAddress(value);
}
void Sender::setRemotePort(quint16 port)
{
remotePort = port;
}
void Sender::updateMeas()
{
auto now = std::chrono::system_clock::now();
auto microsec = std::chrono::time_point_cast<std::chrono::microseconds>(now);
auto epoch = microsec.time_since_epoch();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(epoch);
ustime = duration.count();
qDebug() << "ustime: " << ustime;
if (!isInitialized)
{
viconEstMsg = station->getMeas(name, frame);
dt = 1.0/rate;
isInitialized = true;
return;
}
// update frame number and measurement
auto prev_frame = frame;
viconEstMsg = station->getMeas(name, frame);
// update dt
if (frame > prev_frame)
{
dt = (frame - prev_frame) / station->getRate();
}
else
{
dt = 1.0/rate;
}
qDebug() << "updating Vicon, time: " << ustime << " dt = "<< dt;
qDebug() << "Vicon pos:" << viconEstMsg.x << "," << viconEstMsg.y << "," << viconEstMsg.z;
}
/* call this function after calling updateMeas() */
void Sender::updateLocPosFromMeas()
{
kf.update_dt(dt);
kf.predict();
VectorXd z(3);
z << viconEstMsg.x / 1000.0,
viconEstMsg.y / 1000.0,
viconEstMsg.z / 1000.0; // unit is meter
kf.update(z);
locPosMsg.x = kf.x_[0];
locPosMsg.y = kf.x_[1];
locPosMsg.z = kf.x_[2];
locPosMsg.vx = kf.x_[3];
locPosMsg.vy = kf.x_[4];
locPosMsg.vz = kf.x_[5];
qDebug() << "updateing locPos";
qDebug() << "locPos state vector: " << locPosMsg.x << ","
<< locPosMsg.y << ","
<< locPosMsg.z << ","
<< locPosMsg.vx << ","
<< locPosMsg.vy << ","
<< locPosMsg.vz;
}
/* call this function after calling updateLocPosFromMeas() */
void Sender::updateGpsFromeLocPos()
{
GeographicLib::LocalCartesian geoLocal = station->getGpsToLocal();
double lat, lon, alt;
/* geographiclib uses east-north-up coordination */
geoLocal.Reverse(locPosMsg.y, locPosMsg.x, -locPosMsg.z, lat, lon, alt);
double groundSpeed = sqrt(pow(locPosMsg.vx, 2) + pow(locPosMsg.vy, 2));
double courseOverGround = atan(locPosMsg.vy / locPosMsg.vx) * 180 / M_PI;
gpsMsg.time_usec = ustime; // in microsecond
gpsMsg.lat = lat * 10000000; // [degrees * 1E7]
gpsMsg.lon = lon * 10000000; // [degrees * 1E7]
gpsMsg.alt = alt * 1000; // [m * 1000] AMSL
gpsMsg.vel = groundSpeed * 100; // [cm/s]
gpsMsg.vn = locPosMsg.vx * 100; // [cm/s]
gpsMsg.ve = locPosMsg.vy * 100; // [cm/s]
gpsMsg.vd = locPosMsg.vz * 100; // [cm/s]
gpsMsg.cog = courseOverGround * 100; // [degrees * 100]
gpsMsg.eph = 0.01; // <1: ideal
gpsMsg.epv = 0.01; // <1: ideal
gpsMsg.fix_type = 3;
gpsMsg.satellites_visible = 10;
qDebug() << "updating GPS";
qDebug() << "GPS time: " << gpsMsg.time_usec;
qDebug() << "GPS coord: " << gpsMsg.lat << "," << gpsMsg.lon << ", " << gpsMsg.alt;
qDebug() << "GPS velocity: " << gpsMsg.vn << ", " << gpsMsg.ve << ", " << gpsMsg.vd;
}
void Sender::sendDatagram()
{
uint8_t len = 0;
constexpr int BUFSIZE = 512;
if (useLocPos) {
qDebug() << "sending locPos";
mavlink_message_t msg_loc;
uint8_t send_buf_loc[BUFSIZE];
memset(send_buf_loc, 0, BUFSIZE);
mavlink_msg_local_position_ned_encode(sysid, compid, &msg_loc, &locPosMsg);
len = mavlink_msg_to_send_buffer(send_buf_loc, &msg_loc);
qDebug() << "sending to " << remoteAddress << "," << remotePort;
udpSocket->writeDatagram(reinterpret_cast<const char*>(send_buf_loc), len, remoteAddress, remotePort);
}
if (useGPS) {
qDebug() << "sending GPS";
mavlink_message_t msg_gps;
uint8_t send_buf_gps[BUFSIZE];
memset(send_buf_gps, 0, BUFSIZE);
mavlink_msg_hil_gps_encode(sysid, compid, &msg_gps, &gpsMsg);
len = mavlink_msg_to_send_buffer(send_buf_gps, &msg_gps);
udpSocket->writeDatagram(reinterpret_cast<const char*>(send_buf_gps), len, remoteAddress, remotePort);
}
if (useViconEst) {
qDebug() << "sending Vicon";
mavlink_message_t msg_mocap;
uint8_t send_buf_mocap[BUFSIZE];
memset(send_buf_mocap, 0, BUFSIZE);
mavlink_msg_att_pos_mocap_encode(sysid, compid, &msg_mocap, &viconEstMsg);
len = mavlink_msg_to_send_buffer(send_buf_mocap, &msg_mocap);
udpSocket->writeDatagram(reinterpret_cast<const char*>(send_buf_mocap), len, remoteAddress, remotePort);
}
}