-
Notifications
You must be signed in to change notification settings - Fork 6
/
mainwindow.cpp
423 lines (365 loc) · 12.6 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPortInfo>
#include <QtDebug>
#include <QtCore>
#include <QMessageBox>
#include <QCloseEvent>
#include <QLayout>
#include <QFile>
#include "linkmanager.h"
#include "QuadApplication.h"
#include "VehicleManager.h"
#include "Vehicle.h"
#include "PX4AutopioltPlugins/PX4AutopilotPlugin.h"
static MainWindow* _instance = NULL;
MainWindow* MainWindow::_create()
{
Q_ASSERT(_instance == NULL);
_instance = new MainWindow();
Q_ASSERT(_instance);
return _instance;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
, port_open(false)
, connectionLost(true)
, bool_flying(false)
, _armed(0)
, _status(0)
, _fly_distance(0.0)
, _fly_velocity(0.0)
, systemId(DEFAULT_SYSTEM_ID)
, _vehicle(NULL)
{
ui->setupUi(this);
setFixedSize(960,600);
this->setStyleSheet("QPushButton {background: #cfe2f3}"
"QPushButton:pressed {background: 6699cc}");
ui->pushButton_Connect->setFixedWidth(100);
c_palette.setColor(QPalette::WindowText,Qt::black);
w_palette.setColor(QPalette::WindowText,Qt::red);
// Invalidate the timer to signal first announce
_lowBatteryAnnounceTimer.invalidate();
_attitude_indicator = new Attitude_Indicator();
_audio = new Audio_Worker();
ui->gridLayout_Gauge->setSpacing( 2 );
ui->gridLayout_Gauge->setMargin( 0 );
ui->gridLayout_Gauge->addWidget(_attitude_indicator,0,0,1,1);
_calWidget = new SetupWidget(this);
ui->stackedWidget_Summary->addWidget(_calWidget);
ui->stackedWidget_Summary->setCurrentWidget(_calWidget);
connect(&_portListTimer, &QTimer::timeout, this, &MainWindow::init);
_portListTimer.start(1000);
_linkMgr = qgcApp()->toolbox()->linkManager();
connect(qgcApp()->toolbox()->vehicleManager(),&VehicleManager::activeVehicleChanged,this,&MainWindow::_activeVehicleChanged);
connect(qgcApp()->toolbox()->vehicleManager(),&VehicleManager::activeVehicleChanged,_calWidget,&SetupWidget::_activeVehicleChanged);
show();
}
void MainWindow::_activeVehicleChanged(Vehicle* vehicle)
{
if(vehicle) {
_vehicle = vehicle;
connect(_vehicle, &Vehicle::modeChanged,this,&MainWindow::_modeChange);
connect(_vehicle, &Vehicle::attitudeChanged,this, &MainWindow::_attitudeChange);
connect(_vehicle, &Vehicle::telemetryChanged,this, &MainWindow::_telemetryChange);
connect(_vehicle, &Vehicle::batteryChanged,this, &MainWindow::_batteryChange);
connect(_vehicle, &Vehicle::GPSStatusChanged,this,&MainWindow::_GPSStatusChange);
connect(_vehicle, &Vehicle::globalPositionChanged,this,&MainWindow::_globalPositionChange);
connect(_vehicle, &Vehicle::localPositionChanged,this,&MainWindow::_localPositionChange);
connect(_vehicle, &Vehicle::groundSpeedChanged,this,&MainWindow::_groundSpeedChange);
connect(_vehicle, &Vehicle::statusChanged,this,&MainWindow::_statusChange);
connect(_vehicle, &Vehicle::armedChanged,this,&MainWindow::_armedChange);
}
}
MainWindow* MainWindow::instance(void)
{
return _instance;
}
//display config description and baudrate
void MainWindow::init()
{
_linkMgr->_updateConfigurationList();
QList<SerialConfiguration*> configs = _linkMgr->getSerialConfigurationList();
foreach (SerialConfiguration* config, configs)
{
if(ui->label_Portname->text()!=config->description())
{
ui->label_Portname->setText(config->description());
ui->label_Baudrate->setText(QString::number(config->baud()));
qDebug()<<config->description()<<config->baud();
}
_portListTimer.stop();
_config = config;
ui->pushButton_Connect->setEnabled(true);
}
/*SerialConfiguration* config = new SerialConfiguration("SIASUN");
config->setPortName("COM4");
config->setBaud(57600);
ui->label_Portname->setText(config->portName());
ui->label_Baudrate->setText(QString::number(config->baud()));
_portListTimer.stop();
_config = config;
ui->pushButton_Connect->setEnabled(true);
*/
}
//create serial link, start thread, open serial port
void MainWindow::on_pushButton_Connect_clicked()
{
Q_ASSERT(_currentLink);
if(port_open == false)
{
if(_config) {
_linkMgr->createConnectedLink(_config);
if(_currentLink=_linkMgr->getLink()) {
port_open = true;
ui->pushButton_Connect->setText("断开连接");
//enableAllDataTransmission(_currentLink);
connect(&_statusTimer,&QTimer::timeout,this,&MainWindow::_updateState);
_statusTimer.start(500);
}
else {
qDebug()<<"can‘t create link!";
}
}
}
else{
if (_currentLink->isConnected()) {
_linkMgr->disconnectLink(_currentLink);
port_open = false;
ui->pushButton_Connect->setText("连接");
}
}
}
void MainWindow::closeEvent(QCloseEvent* event)
{
Q_UNUSED(event)
/*QMessageBox::StandardButton button;
button = QMessageBox::question(this,tr("关闭程序"),
tr("确认退出程序?"),
QMessageBox::Yes | QMessageBox::No);
if(button == QMessageBox::Yes){
event->accept();
}
if(button == QMessageBox::No){
event->ignore();
}
*/
}
void MainWindow::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
}
void MainWindow::_updateState()
{
quint64 curTime =static_cast<quint64>(QDateTime::currentMSecsSinceEpoch());
quint64 lastHeartbeat = _linkMgr->getHeartbeatTime();
quint64 heartbeatInterval = (curTime - lastHeartbeat)/1000.0;
//qDebug()<<"last time"<<lastHeartbeat<<"now time: "<<curTime<<"interval"<<heartbeatInterval;
// Check if heartbeat timed out
if(!connectionLost && (heartbeatInterval>timeoutIntervalHeartbeat))
{
connectionLost = true;
ui->label_Message->setPalette(w_palette);
ui->label_Message->setText("链接丢失!");
if (_currentLink->isConnected()) {
_linkMgr->disconnectLink(_currentLink);
port_open = false;
ui->pushButton_Connect->setText("连接");
}
}
// Update connection loss time on each iteration
if (connectionLost && (heartbeatInterval > timeoutIntervalHeartbeat))
{
quint64 connectionLossTime = heartbeatInterval;
ui->label_Message->setPalette(w_palette);
ui->label_Message->setText("链接丢失: "+QString::number(connectionLossTime));
}
// Connection gained
if(connectionLost && (heartbeatInterval<timeoutIntervalHeartbeat))
{
connectionLost = false;
ui->label_Message->setPalette(c_palette);
ui->label_Message->setText("连接成功!");
}
}
MainWindow::~MainWindow()
{
_instance = NULL;
delete ui;
if (_currentLink) {
_linkMgr->disconnectLink(_currentLink);
}
}
void MainWindow::_telemetryChange(uint8_t rssi)
{
ui->progressBar_RC->setValue(rssi);
ui->label_RC->setText(QString::number(rssi));
}
void MainWindow::_modeChange(QString shortModeText)
{
//qDebug()<<"_mode Change";
ui->label_Mode->setText(shortModeText);
}
void MainWindow::_statusChange(uint8_t status)
{
_status = status;
//flying
if(_status == 4) {
if(!bool_flying) {
bool_flying = true;
_lastTakeoffTime = static_cast<quint64>(QDateTime::currentMSecsSinceEpoch());
}
_showFlytime();
}
else {
if(bool_flying) {
//bool_flying = false;
// hide bool_flying = false ,time
_showFlytime();
}
}
if(status == 0||status==1) {
ui->tabWidget->setTabEnabled(0,true);
}
else {
ui->tabWidget->setTabEnabled(0,false);
}
QString fltStatus;
switch (status) {
case 0 :
fltStatus = "Landed";
break;
case 1:
fltStatus = "Initiating";
break;
case 2:
fltStatus = "Taking off";
break;
case 3:
fltStatus = "Landing";
break;
case 4:
fltStatus = "Flying";
break;
case 5:
fltStatus = "Hovering";
break;
case 6:
fltStatus = "FreeFall";
break;
case 7:
fltStatus = "UpsideDown";
break;
case 8:
fltStatus = "Tumble";
break;
}
ui->label_Status->setText(fltStatus);
}
void MainWindow::_showFlytime(void)
{
quint64 currentTime= static_cast<quint64>(QDateTime::currentMSecsSinceEpoch());
quint64 flytime = currentTime-_lastTakeoffTime;
uint32_t min = flytime/(1000*60);
uint32_t sec = flytime/1000 - min*60;
QTime time(0,min,sec);
ui->label_Fly_Time->setText(time.toString("mm:ss"));
}
void MainWindow::_armedChange(uint32_t armed)
{
ui->label_Armed->setText(QString("%1").arg((armed==1)? QStringLiteral("Armed"):QStringLiteral("Disarmed")));
_armed = armed;
}
void MainWindow::_batteryChange(int8_t battery_remaining)
{
ui->progressBar_Battery->setValue(battery_remaining);
QString battery =QString::number(battery_remaining)+"%";
if(battery_remaining<=battery_lowbound)
{
QPalette palette;
palette.setColor(QPalette::WindowText,Qt::red);
if (!_lowBatteryAnnounceTimer.isValid() || _lowBatteryAnnounceTimer.elapsed() > 5 * 1000) { // 5s, amount of time in between each low battery announcement
_lowBatteryAnnounceTimer.restart();
}
//_audio->say(tr("电量过低"));
ui->label_Message->setText(tr("警告! 电量不足, 请立即返航!"));
}
ui->label_Battery_Volt->setText(battery);
}
void MainWindow::_attitudeChange(mavlink_attitude_t* attitude)
{
//qDebug()<<"mainwindow: attitude";
_attitude_indicator->rollLP = attitude->roll;
_attitude_indicator->pitchLP = attitude->pitch;
_attitude_indicator->yaw = attitude->yaw;
QString attitude_text;
attitude_text = QString::number(attitude->roll*180.0/M_PI,'f',1)+",";
attitude_text +=QString::number(attitude->pitch*180.0/M_PI,'f',1);
attitude_text +=",";
if((attitude->yaw*180/M_PI)<0){
attitude_text +=QString::number((attitude->yaw*180.0/M_PI + 360),'f',1);
}
else{
attitude_text +=QString::number(attitude->yaw*180.0/M_PI ,'f',1);
}
ui->label_Attitude->setText(attitude_text);
}
void MainWindow::_GPSStatusChange(mavlink_gps_raw_int_t* gps_raw_int)
{
//qDebug()<<"GPS satellites visible: "<<gps_raw_int->satellites_visible;
uint8_t fix_type = gps_raw_int->fix_type;
QString fix;
switch(fix_type){
//fix 0: lost, 1: 2D local position hold, 2: 2D localization, 3: 3D localization
case 0:
case 1: fix = "No Fix"; break;
case 2:fix = "2D Fix"; break;
case 3:fix = "3D Fix"; break;
case 4:fix = "DGPS"; break;
case 5:fix = "RTK"; break;
default: break;
}
uint8_t val = gps_raw_int->satellites_visible;
ui->label_GPS_Fix->setText(QString::number(val));
ui->progressBar_GPS->setValue(val);
}
void MainWindow::_globalPositionChange(mavlink_global_position_int_t *global_position_int)
{
Q_UNUSED(global_position_int)
//ui->label_Rel_Alt->setText(QString::number(global_position_int->relative_alt/1000.0));
}
void MainWindow::_localPositionChange(mavlink_local_position_ned_t *local_position_t)
{
//Q_UNUSED(local_position_t)
//qDebug()<<"local position x y z :"<<local_position_t->x<<local_position_t->y<<local_position_t->z;
//判断是否起飞
if(!connectionLost) {
if(_armed == 1) {
ui->label_Rel_Alt->setText(QString::number(local_position_t->z));
_fly_velocity = qSqrt(qPow(local_position_t->vx,2)+qPow(local_position_t->vy,2)+qPow(local_position_t->vz,2));
ui->label_Speed->setText(QString::number(_fly_velocity));
_fly_distance = qSqrt(qPow(local_position_t->x,2)+qPow(local_position_t->y,2));
ui->label_Distance->setText(QString::number(_fly_distance));
/*if(fabs(local_position_t->z)>=1.0) {
if(!bool_flying) {
bool_flying = true;
_lastTakeoffTime = static_cast<quint64>(QDateTime::currentMSecsSinceEpoch());
}
_showFlytime();
}
else {
if(bool_flying) {
bool_flying = false;
_showFlytime();
}
} */
}
}
}
void MainWindow::_groundSpeedChange(float fly_speed)
{
Q_UNUSED(fly_speed)
//ui->label_Speed->setText(QString::number(fly_speed,'g',6));
}