-
Notifications
You must be signed in to change notification settings - Fork 15
/
SenderWindow.cpp
152 lines (138 loc) · 4.5 KB
/
SenderWindow.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
/***************************************************************************
* *
* Copyright (C) 2017 by University of Illinois *
* *
* http://illinois.edu *
* *
***************************************************************************/
/**
* @file SenderWindow.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 View of the
* Model(Sender)-View(SenderWindow)-Controller(SenderController) pattern.
*
* @author Bo Liu <[email protected]>
*
*/
#include "SenderWindow.h"
#include "ui_SenderWindow.h"
#include <math.h>
#include <QMessageBox>
SenderWindow::SenderWindow(const QString& name, std::unique_ptr<SenderController> &controller, QWidget *parent) :
QWidget(parent),
ui(new Ui::SenderWindow),
name(name),
controller(controller)
{
ui->setupUi(this);
setupConnections();
initialize();
}
void SenderWindow::initialize()
{
setStandBy();
ui->remoteIPLine->setText(controller->getRemoteAddress().toString());
ui->remotePortLine->setText(QString::number(controller->getRemotePort()));
ui->sysIDLine->setText(QString::number(controller->getSysID()));
ui->compIDLine->setText(QString::number(controller->getCompID()));
ui->gpsCheckBox->setChecked(controller->getUseGps());
ui->localPosCheckBox->setChecked(controller->getUseLocPos());
ui->viconCheckBox->setChecked(controller->getUseVicon());
ui->rateSlider->setTickPosition(QSlider::TicksBothSides);
ui->rateSlider->setMinimum(1);
ui->rateSlider->setMaximum(240);
ui->rateSlider->setSingleStep(1);
ui->rateSlider->setValue(controller->getRate());
auto r = ui->rateSlider->value();
auto rate = 2 * floor(r/2);
auto str = QString("Rate: ");
str += QString::number(rate);
str += QString(" Hz");
ui->Ratelabel->setText(str);
}
void SenderWindow::setupConnections()
{
connect(ui->rateSlider, &QSlider::sliderMoved,
this, &SenderWindow::updateRateLabel);
connect(ui->start, &QPushButton::released,
this, &SenderWindow::startSenderHandler);
connect(ui->stop, &QPushButton::released,
this, &SenderWindow::stopSenderHandler);
}
void SenderWindow::closeEvent(QCloseEvent *event)
{
event->accept();
emit closeSelf(name);
}
SenderWindow::~SenderWindow()
{
delete ui;
}
void SenderWindow::setStandBy()
{
auto str = name + QString("(stand by)");
ui->droneNameLabel->setText(str);
setLabelColor(ui->droneNameLabel, Qt::red);
}
void SenderWindow::setActive()
{
auto str = name + QString("(active)");
ui->droneNameLabel->setText(str);
setLabelColor(ui->droneNameLabel, Qt::darkGreen);
}
void SenderWindow::updateRateLabel()
{
auto r = ui->rateSlider->value();
auto rate = 2 * floor(r/2);
controller->setRate(rate);
qDebug() << "updating Rate Label: " << rate;
auto str = QString("Rate: ");
str += QString::number(rate);
str += QString(" Hz");
ui->Ratelabel->setText(str);
}
void SenderWindow::startSenderHandler()
{
if (!ui->remoteIPLine->isEnabled()) {
QMessageBox msgBox;
msgBox.setText("Sender is running!");
msgBox.exec();
return;
}
controller->setRemoteAddress(ui->remoteIPLine->text());
controller->setRemotePort(ui->remotePortLine->text().toUShort());
controller->setSysID(ui->sysIDLine->text().toInt());
controller->setCompID(ui->compIDLine->text().toInt());
controller->startSender();
ui->remoteIPLine->setEnabled(false);
ui->remotePortLine->setEnabled(false);
ui->sysIDLine->setEnabled(false);
ui->compIDLine->setEnabled(false);
setActive();
}
void SenderWindow::stopSenderHandler()
{
if (ui->remoteIPLine->isEnabled()) {
QMessageBox msgBox;
msgBox.setText("Sender is not running!");
msgBox.exec();
return;
}
controller->stopSender();
ui->remoteIPLine->setEnabled(true);
ui->remotePortLine->setEnabled(true);
ui->sysIDLine->setEnabled(true);
ui->compIDLine->setEnabled(true);
setStandBy();
}
void SenderWindow::setLabelColor(QLabel * label, QColor color)
{
QPalette palette;
palette.setColor(QPalette::Window, Qt::blue);
palette.setColor(QPalette::WindowText, color);
label->setPalette(palette);
}