-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
312 lines (255 loc) · 10.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
#include <QtGui\QtGui>
#include "mainwindow.hpp"
MainWindow::MainWindow()
{
bConnected = false;
textEdit = new QTextEdit;
textEdit->setReadOnly (true);
setCentralWidget(textEdit);
label = new QLabel(this);
if (Network.InitNetwork () == 1) {
QMessageBox::critical(this, tr("System error"), tr("Failed to initialize the Network ressources!"));
}
createActions();
createMenus();
createDockWindow ();
pixmap = new QPixmap (1000, 1000);
m_nNetworkUpdatesperSecond = 0;
setWindowTitle(tr("Robot Control Tool"));
}
void MainWindow::SetText (const QString text) {
textEdit->setText (text);
}
void MainWindow::Info()
{
QMessageBox::about(this, tr("About this program"),
tr("This tool was programmed by "
"<b> Lennart Haller </b> to control and debug "
"the robot. "));
}
void MainWindow::createActions()
{
qshowScanAct = new QAction(tr("&Show scan"), this);
qshowScanAct->setStatusTip(tr("Show the LiDAR scan"));
connect(qshowScanAct, SIGNAL(triggered()), this, SLOT(ShowScan()));
qshowMotorDataAct = new QAction(tr("&Show motor data"), this);
qshowMotorDataAct->setStatusTip(tr("Show motor data"));
connect(qshowMotorDataAct, SIGNAL(triggered()), this, SLOT(ShowMotorData()));
qshowGeneralAct = new QAction(tr("&Show general data"), this);
qshowGeneralAct->setStatusTip(tr("Show general data"));
connect(qshowGeneralAct, SIGNAL(triggered()), this, SLOT(ShowGeneralData()));
qshowCDDAct = new QAction(tr("&Show CDD"), this);
qshowCDDAct->setStatusTip(tr("Show Calculated driving direction"));
qshowCDDAct->setCheckable(true);
connect (qshowCDDAct, SIGNAL (changed()), this, SLOT (toggle()));
qshowOriginOfScanAct = new QAction(tr("&Show origin of scan"), this);
qshowOriginOfScanAct->setStatusTip(tr("Display the origin of the laser scan"));
qshowOriginOfScanAct->setCheckable(true);
connect (qshowOriginOfScanAct, SIGNAL (changed()), this, SLOT (toggle()));
quitAct = new QAction(tr("&Quit"), this);
quitAct->setShortcuts(QKeySequence::Quit);
quitAct->setStatusTip(tr("Quit the application"));
connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutAct = new QAction(tr("&Info"), this);
aboutAct->setStatusTip(tr("Show some information"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(Info()));
qconnectAct = new QAction(tr("&Connect"), this);
qconnectAct->setStatusTip(tr("Connect with the robot"));
connect(qconnectAct, SIGNAL(triggered()), this, SLOT(ConnectToRobot()));
}
void MainWindow::createMenus()
{
ShowData = menuBar()->addMenu(tr("&Show data"));
ShowData->addAction (qshowScanAct);
ShowData->addAction (qshowMotorDataAct);
ShowData->addAction (qshowGeneralAct);
Settings = menuBar()->addMenu (tr("&Settings"));
Settings->addAction (qshowCDDAct);
Settings->addAction (qshowOriginOfScanAct);
GeneralMenu = menuBar()->addMenu(tr("&General"));
GeneralMenu->addAction(qconnectAct);
GeneralMenu->addAction(aboutAct);
GeneralMenu->addAction(quitAct);
}
void MainWindow::ConnectToRobot () {
if(bConnected == false) {
if (Network.Connect () != 0) {
QMessageBox::critical(this, tr("Network error"), tr("Failed to connect to the robot!"));
}else{
QMessageBox::information(this, tr("Connection status"), tr("You are now connected to the robot."));
UpdateTimer.start(UPDATEFREQUENCY);
NetworkUpdateCounterTimer.start(1000);
connect(&UpdateTimer, SIGNAL (timeout()), this, SLOT (UpdateData()));
connect(&NetworkUpdateCounterTimer, SIGNAL (timeout()), this, SLOT (CountNetworkUpdates()));
bConnected = true;
}
}else{
QMessageBox::information(this, tr("Connection status"), tr("You are already connected to the robot."));
}
}
void MainWindow::ShowScan () {
if (bConnected == true) {
timer.start(50);
disconnect(&timer, SIGNAL (timeout()), this, SLOT (DisplayMotorData()));
disconnect(&timer, SIGNAL (timeout()), this, SLOT (DisplayGeneralData()));
connect(&timer, SIGNAL (timeout()), this, SLOT (DisplayScan()));
dock->show();
}else{
QMessageBox::information(this, tr("Information"), tr("You need to connect to the robot first."));
}
}
void MainWindow::ShowMotorData () {
if (bConnected == true) {
timer.start(20);
disconnect(&timer, SIGNAL (timeout()), this, SLOT (DisplayScan()));
disconnect(&timer, SIGNAL (timeout()), this, SLOT (DisplayGeneralData()));
connect(&timer, SIGNAL (timeout()), this, SLOT (DisplayMotorData()));
dock->hide();
}else{
QMessageBox::information(this, tr("Information"), tr("You need to connect to the robot first."));
}
}
void MainWindow::ShowGeneralData () {
if (bConnected == true) {
timer.start(20);
disconnect(&timer, SIGNAL (timeout()), this, SLOT (DisplayScan()));
disconnect(&timer, SIGNAL (timeout()), this, SLOT (DisplayMotorData()));
connect(&timer, SIGNAL (timeout()), this, SLOT (DisplayGeneralData()));
dock->hide();
}else{
QMessageBox::information(this, tr("Information"), tr("You need to connect to the robot first."));
}
}
void MainWindow::DisplayMotorData () {
textEdit->clear();
textEdit->setText ("Motor 3: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetMotorPower()+2)));
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (" || Motor 2: "); //10 leerzeichen zwischen den | und Motor
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetMotorPower()+1)));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("Odometry 3: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetOdometryTicks()+2)));
textEdit->insertPlainText (" Odometry 4: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetOdometryTicks()+3)));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("\n\n\n\n\n\n\n\n\n\n\n");
textEdit->insertPlainText ("Motor 1: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetMotorPower())));
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (" Motor 4: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetMotorPower()+3)));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("Odometry 2: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetOdometryTicks()+1)));
textEdit->insertPlainText (" Odometry 1: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetOdometryTicks())));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("\n\n\n\n\n\n\n");
textEdit->insertPlainText ("X Pos from Odometry: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(Network.OdometryPosition()->nX));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("Y Pos from Odometry: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(Network.OdometryPosition()->nY));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("Theta from Odometry: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(Network.OdometryPosition()->fTheta));
textEdit->moveCursor (QTextCursor::End);
}
void MainWindow::DisplayGeneralData () {
textEdit->clear();
textEdit->setText ("Operating Voltage: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(Network.GetVoltage()));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("Main loop ticks per second: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(Network.GetLoopTicks()));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("Network Updates per second: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(m_nNetworkUpdatesperSecond));
textEdit->moveCursor (QTextCursor::End);
textEdit->append ("\n\nCalculated driving direction: ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(Network.GetCalculatedDrivingDirection()));
textEdit->moveCursor (QTextCursor::End);
}
void MainWindow::createDockWindow() {
dock = new QDockWidget(tr("LiDAR scan"), this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
dock->setWidget(label);
addDockWidget(Qt::RightDockWidgetArea, dock);
dock->hide();
}
void MainWindow::DisplayScan () {
if (Network.GetWasUpdated() == true) {
textEdit->clear();
/*
for (int i=0; i<271; i++) {
textEdit->insertPlainText ("Messung ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(i));
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (": ");
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText (QString::number(*(Network.GetScannerData()+i)));
textEdit->moveCursor (QTextCursor::End);
textEdit->insertPlainText ("\n");
textEdit->moveCursor (QTextCursor::End);
}
*/
pixmap->fill (QColor (160, 160, 160, 255));
QPainter *paint = new QPainter (pixmap);
double x, y;
for (int i=0; i<271;i++) { //draw data points
if (*(Network.GetScannerData()+i) != 0) { //only draw the data point if its != 0
x = (((*(Network.GetScannerData()+i)) * cosd (i-45)) / 23)-4; //divide by 23
y = (((*(Network.GetScannerData()+i)) * sind (i-45)) / 23)-4;
paint->fillRect (500 + static_cast<int>(x), 800 - static_cast<int>(y), 8, 8, QColor (0,0,0));
}
}
if (qshowOriginOfScanAct->isChecked() == true) {//Draw origin if selected by the user
paint->fillRect (500 -8, 800 - 8, 16, 16, QColor (255,0,0));
}
if (qshowCDDAct->isChecked() == true) {//Draw calculated driving direction if selected by the user
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
paint->setPen (pen);
x = (cosd (Network.GetCalculatedDrivingDirection()+90) * 10); // * 8.7
y = (sind (Network.GetCalculatedDrivingDirection()+90) * 10);
paint->drawLine (500, 800, x+500, 800-y);
}
label->clear();
delete paint;
label->setPixmap(*pixmap);
Network.SetWasUpdated (false);
}
}
double MainWindow::sind(double angle)
{
double angleradians = angle * M_PI / 180.0f;
return sin(angleradians) * 180.0f / M_PI;
}
double MainWindow::cosd(double angle)
{
double angleradians = angle * M_PI / 180.0f;
return cos(angleradians) * 180.0f / M_PI;
}
void MainWindow::UpdateData () {
Network.UpdateData ();
}
void MainWindow::CountNetworkUpdates () {
m_nNetworkUpdatesperSecond = Network.GetUpdateCounter ();
Network.ResetUpdateCounter ();
}