-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauodomwidget.cpp
264 lines (229 loc) · 10.4 KB
/
lauodomwidget.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
#include "lauodomwidget.h"
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUOdomWidget::LAUOdomWidget(QString portString, QWidget *parent) : QWidget(parent), object(NULL)
{
// SET THE WINDOWS LAYOUT
this->setWindowTitle("LAUOdomWidget");
this->setLayout(new QVBoxLayout());
this->layout()->setContentsMargins(0, 0, 0, 0);
// CREATE LIDAR LABEL TO DISPLAY LIDAR DATA AS IT ARRIVES
label = new LAUOdomLabel();
label->setMinimumHeight(200);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->layout()->addWidget(label);
// CREATE A ROBOT OBJECT FOR CONTROLLING ROBOT
object = new LAUOdomObject(portString);
// NOW THAT WE'VE MADE OUR CONNECTIONS, TELL ROBOT OBJECT TO CONNECT OVER SERIAL/TCP
if (object->connectPort()) {
connect(object, SIGNAL(emitOdometry(QQuaternion, QVector3D)), label, SLOT(onUpdateOdometry(QQuaternion, QVector3D)), Qt::DirectConnection);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUOdomWidget::LAUOdomWidget(QString ipAddr, int portNum, QWidget *parent) : QWidget(parent), object(NULL)
{
// SET THE WINDOWS LAYOUT
this->setWindowTitle("LAUOdomWidget");
this->setLayout(new QVBoxLayout());
this->layout()->setContentsMargins(0, 0, 0, 0);
// CREATE LIDAR LABEL TO DISPLAY LIDAR DATA AS IT ARRIVES
label = new LAUOdomLabel();
label->setMinimumHeight(200);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
label->onEnableSavePoints(true);
this->layout()->addWidget(label);
// CREATE A ROBOT OBJECT FOR CONTROLLING ROBOT
object = new LAUOdomObject(ipAddr, portNum);
// NOW THAT WE'VE MADE OUR CONNECTIONS, TELL ROBOT OBJECT TO CONNECT OVER SERIAL/TCP
if (object->connectPort()) {
connect(object, SIGNAL(emitOdometry(QQuaternion, QVector3D)), label, SLOT(onUpdateOdometry(QQuaternion, QVector3D)), Qt::DirectConnection);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUOdomWidget::~LAUOdomWidget()
{
if (object) {
delete object;
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUOdomLabel::LAUOdomLabel(QWidget *parent) : QLabel(parent), savePointsFlag(false), counter(0)
{
// RESTART THE TIMER
time.restart();
// CREATE A CONTEXT MENU FOR TOGGLING TEXTURE
contextMenu = new QMenu();
QAction *enableTextureAction = contextMenu->addAction(QString("Save"));
connect(enableTextureAction, SIGNAL(triggered()), this, SLOT(onSavePoints()));
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomLabel::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
if (contextMenu) {
contextMenu->popup(event->globalPos());
}
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomLabel::onEnableSavePoints(bool state)
{
if (state) {
points.clear();
savePointsFlag = true;
} else {
savePointsFlag = false;
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomLabel::onUpdateOdometry(QQuaternion pose, QVector3D position)
{
qDebug() << pose << position;
// REPORT FRAME RATE TO THE CONSOLE
if (++counter >= 200) {
//qDebug() << QString("%1 sps").arg(1000.0 * (float)counter / (float)time.elapsed());
time.restart();
counter = 0;
}
// ONLY ADD POINT IS THE SAVE FLAG IS TRUE
if (savePointsFlag) {
// SET THE LIMITS TO INCLUDE THE INCOMING POINT
topLeft.setX(qMin(topLeft.x(), position.x()));
topLeft.setY(qMin(topLeft.y(), position.y()));
topLeft.setZ(qMin(topLeft.z(), position.z()));
bottomRight.setX(qMax(bottomRight.x(), position.x()));
bottomRight.setY(qMax(bottomRight.y(), position.y()));
bottomRight.setZ(qMax(bottomRight.z(), position.z()));
// ADD THE NEW POINT TO OUR POINT LIST
points.append(position);
poses.append(pose);
// UPDATE THE LABEL ON SCREEN FOR THE USER
if (points.count() % 100 == 0) {
//qDebug() << "Number of points:" << points.count();
update();
}
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomLabel::onSavePoints()
{
// SAVE THE SCANNING DATA TO DISK
QSettings settings;
QString directory = settings.value("LAUOdomWidget::lastDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
QString filename = QFileDialog::getSaveFileName(this, QString("Save tracking data to disk (*.csv)"), directory, QString("*.csv"));
if (filename.isEmpty() == false) {
if (filename.toLower().endsWith(QString(".csv")) == false) {
filename.append(QString(".csv"));
}
settings.setValue("LAUOdomWidget::lastDirectory", QFileInfo(filename).absolutePath());
// OPEN A FILE TO SAVE THE RESULTS
QFile file(filename);
if (file.open(QIODevice::WriteOnly)) {
for (int n = 0; n < points.count(); n++) {
QVector3D pt = points.at(n);
file.write(QString("%1, %2, %3\n").arg(pt.x()).arg(pt.y()).arg(pt.z()).toLatin1());
}
file.close();
}
}
return;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomLabel::paintEvent(QPaintEvent *)
{
// CREATE A PAINTER OBJECT TO DRAW ON SCREEN
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing, false);
// DRAW THE BOUNDING BOX OF THE LABEL IN THE BACKGROUND
painter.setPen(Qt::black);
painter.setBrush(Qt::white);
painter.drawRect(0, 0, this->width(), this->height());
if (points.count() > 100) {
// CALCULATE SCALE FACTOR TO MAINTAIN 1:1 ASPECT RATIO
float xScale = (float)this->width() / (float)(bottomRight.x() - topLeft.x());
float yScale = (float)this->height() / (float)(bottomRight.y() - topLeft.y());
// SET A TRANSFORM TO KEEP ALL THE POINTS IN THE FIELD OF VIEW OF THE LABEL
QTransform transformA, transformB, transformC;
transformA.translate(-(topLeft.x() + bottomRight.x()) / 2, -(topLeft.y() + bottomRight.y()) / 2);
if (xScale < yScale) {
transformB.scale(xScale, xScale);
} else {
transformB.scale(yScale, yScale);
}
transformC.translate(this->width() / 2.0, this->height() / 2.0);
painter.setTransform(transformA * transformB * transformC);
// DRAW OUR LIST OF POINTS
QPen pen(Qt::red, 1.0f, Qt::SolidLine);
pen.setCosmetic(true);
painter.setPen(pen);
for (int n = 1; n < points.count() && n < 10000; n++) {
painter.drawPoint(points.at(points.count() - n).x(), points.at(points.count() - n).y());
}
}
// END DRAWING
painter.end();
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUOdomObject::~LAUOdomObject()
{
;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomObject::onSendMessage(int message, void *argument)
{
Q_UNUSED(message);
Q_UNUSED(argument);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUOdomObject::onReadyRead()
{
static QByteArray byteArray;
// KEEP READING FROM THE PORT UNTIL THERE ARE NO MORE BYTES TO READ
while (bytesAvailable() > 0) {
byteArray.append(readAll());
byteArray = processMessage(byteArray);
}
return;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
QByteArray LAUOdomObject::processMessage(QByteArray byteArray)
{
// MAKE SURE WE HAVE ENOUGH BYTES FOR A COMPLETE MESSAGE
while (byteArray.length() > (int)(7 * sizeof(double))) {
// EXTRACT THE DOUBLE FLOATING POINT VALUES FROM THE INCOMING MESSAGE
double *buffer = (double *)byteArray.data();
QQuaternion pose(buffer[0], buffer[1], buffer[2], buffer[3]);
QVector3D position(buffer[4], buffer[5], buffer[6]);
// EMIT THE POSE AND POSITION TO THE USER
emit emitOdometry(pose, position);
// REMOVE THE POSE AND POSITION FROM THE INCOMING MESSAGE FOR THE NEXT ITERATION
byteArray = byteArray.right(byteArray.length() - 7 * sizeof(double));
}
return (byteArray);
}