-
Notifications
You must be signed in to change notification settings - Fork 4
/
widget.cpp
353 lines (318 loc) · 9.67 KB
/
widget.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
#include "widget.h"
#include "ui_widget.h"
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QtCore/QDebug>
#include <QtWidgets/QFormLayout>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QtCore/QtGlobal>
#include <QtCharts/QXYSeries>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLegend>
#include <QtCharts/QLineSeries>
#include <QtCharts/QAbstractAxis>
#include <QtCharts/QSplineSeries>
using namespace QtCharts;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),
index_x(0),
index_y(0),
count(0),
sum_data(0),
flow_SQUAL(0),
frame_rate(0),
//indexX_range(1000),
indexY_range(100),
index2Y_range(500),
index_max(500)
{
ui->setupUi(this);
timer = new QTimer();
timer->setInterval(500); //定时触发显示环境质量和帧率
timer->start();
QLabel *frameshow = new QLabel();
frameLable = new QLabel();
frameshow->setText(tr("frame rate:"));
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
QLabel *showsum = new QLabel();
showsum->setText(tr("data count:"));
sum_label = new QLabel();
QLabel * showdial = new QLabel();
showdial->setText(tr("speed Y:"));
QLabel * showdial1 = new QLabel();
showdial1->setText(tr("distance Y:"));
serial = new QSerialPort();
m_mainLayout = new QGridLayout();
m_seriesX = new QLineSeries();
m_seriesX->setName(tr("X speed"));
m_seriesY = new QLineSeries();
m_seriesY->setName(tr("Y speed"));
m_distX = new QLineSeries();
m_distX->setName(tr("X distance"));
m_distY = new QLineSeries();
m_distY->setName(tr("Y distance"));
m_chart = new QChart();
QChartView *chartView = new QChartView(m_chart);
chartView->setMinimumSize(800, 600);
m_chart->addSeries(m_seriesX);
m_chart->addSeries(m_seriesY);
m_chart->addSeries(m_distX);
m_chart->addSeries(m_distY);
axisX = new QValueAxis; //设置图标坐标和文字等
axisX->setRange(0, index_max);
axisX->setLabelFormat("%g");
axisX->setTitleText("sample");
axisY = new QValueAxis;
axisY->setRange(-indexY_range, indexY_range);
axisY->setTitleText("speed");
axis2Y = new QValueAxis();
axis2Y->setRange(-50,50);
axis2Y->setLabelFormat("%g");
axis2Y->setTitleText("distance");
axisY->setTickCount(11);
m_chart->setAxisX(axisX,m_seriesX);
m_chart->setAxisY(axisY,m_seriesX);
m_chart->setAxisX(axisX,m_seriesY);
m_chart->setAxisY(axisY,m_seriesY);
m_chart->addAxis(axis2Y,Qt::AlignRight);
m_chart->setAxisY(axis2Y,m_distX);
m_chart->setAxisY(axis2Y,m_distY);
m_chart->setAxisX(axisX,m_distX);
m_chart->setAxisX(axisX,m_distY);
m_chart->legend()->setVisible(true);
m_chart->legend()->setAlignment(Qt::AlignBottom);
m_chart->setTitle("Optcial Flow Data");
QLabel * showSQUAL = new QLabel(); //显示环境质量
SQUAL = new QLabel();
showSQUAL->setText(tr("SQUAL:"));
QFormLayout *SQUALlayout = new QFormLayout(); //子layout
SQUALlayout->addWidget(showSQUAL);
SQUALlayout->addWidget(SQUAL);
SQUALlayout->addWidget(frameshow);
SQUALlayout->addWidget(frameLable);
SQUALlayout->addWidget(showsum);
SQUALlayout->addWidget(sum_label);
SQUALlayout->addWidget(showdial); //adjust speed axis
SQUALlayout->addWidget(ui->dial);
SQUALlayout->addWidget(showdial1); //adjust distance axis
SQUALlayout->addWidget(ui->dial_2);
chartView->setRenderHint(QPainter::Antialiasing);
m_mainLayout->addWidget(chartView,0,0,8,1);
m_mainLayout->addWidget(ui->comboBox,0,2,1,1);
m_mainLayout->addWidget(ui->pushButton,1,2,1,1);
m_mainLayout->addWidget(ui->checkX,2,2,1,1);
m_mainLayout->addWidget(ui->checkY,3,2,1,1);
m_mainLayout->addWidget(ui->checkdistX,4,2,1,1);
m_mainLayout->addWidget(ui->checkdistY,5,2,1,1);
m_mainLayout->addLayout(SQUALlayout,6,2,8,1);
axis2Y->setTickCount(11);
ui->checkX->setChecked(true);
ui->checkY->setChecked(true);
ui->checkdistX->setChecked(true);
ui->checkdistY->setChecked(true);
ui->dial->setRange(10,indexY_range);
ui->dial->setValue(100);
ui->dial_2->setRange(10,index2Y_range);
ui->dial_2->setValue(100);
setLayout(m_mainLayout);
}
Widget::~Widget()
{
serial->close();
delete ui;
}
/*打开串口*/
void Widget::on_pushButton_clicked(bool checked)
{
Q_UNUSED(checked)
static int flag=0;
if(flag ==0)
{
foreach( const QSerialPortInfo &Info,QSerialPortInfo::availablePorts())//读取串口信息
{
qDebug() << "portName :" << Info.portName();
qDebug() << "Description :" << Info.description();
qDebug() << "Manufacturer:" << Info.manufacturer();
ui->comboBox->addItem(Info.portName());
}
if(ui->comboBox->count()>0)
{
flag = 1;
ui->pushButton->setText(tr("打开串口"));
}
}
else if(flag == 1)
{
serial->setPortName(ui->comboBox->currentText());
serial->open(QIODevice::ReadOnly);
serial->setBaudRate(19200);
connect(serial,SIGNAL(readyRead()),this,SLOT(SerialReceive()));
qDebug()<<"打开串口:"<<serial->portName();
ui->pushButton->setText(tr("关闭串口"));
ui->comboBox->setEnabled(false);
flag =2;
}
else
{
qDebug()<<"关闭串口";
serial->close();
ui->pushButton->setText(tr("刷新"));
ui->comboBox->clear();
ui->comboBox->setEnabled(true);
disconnect(serial, &QSerialPort::readyRead, this, &Widget::SerialReceive);
flag = 0;
}
}
/*串口数据接收处理槽函数*/
void Widget::SerialReceive()
{
requestData = serial->readAll();
uint8_t head,tail,sum;
#define USE_UAV 0 //通过无人机发数据还是直接用光流计
#if USE_UAV
head = requestData[0];
tail = requestData[12];
flow_sum = requestData[10];
count++; /*统计帧率*/
if(index_x>index_max)
{
index_x=0;
m_seriesX->clear();
m_distX->clear();
}
if(index_y>index_max)
{
index_y=0;
m_seriesY->clear();
m_distY->clear();
}
if(head == 0xFE && tail ==0xAA)
{
sum = requestData[2]+requestData[3]+requestData[4]+requestData[5]
+requestData[6]+requestData[7]+requestData[8]+requestData[9];
if(sum == flow_sum)
{
flow_SQUAL = requestData[11];
dist_x = (int16_t)(requestData[2]<<8 | requestData[3]);
dist_y = (int16_t)(requestData[4]<<8 | requestData[5]);
flow_x = (int16_t)(requestData[6]<<8 | requestData[7]);
flow_y = (int16_t)(requestData[8]<<8 | requestData[9]);
m_seriesX->append(index_x,flow_x);
m_seriesY->append(index_y,flow_y);
m_distX->append(index_x,dist_x);
m_distY->append(index_y,dist_y);
index_x++;
index_y++;
sum_data++;
sum_label->setNum(sum_data);
}
}
// qDebug()<<"head:"<<head<<"tail:"<<tail<<"sum:"<<sum<<"SQUAL:"<<SQUAL;
// qDebug()<<"head:"<<head<<"tail:"<<tail<<"flow_sum:"<<flow_sum<<"sum:"<<sum;
qDebug()<<"head:"<<head<<"tail:"<<tail<<"flow_x:"<<flow_x<<"flow_y:"<<flow_y;
#else
head = requestData[0];
tail = requestData[8];
flow_sum = requestData[6];
count++; /*统计帧率*/
if(index_x>index_max)
{
index_x=0;
m_seriesX->clear();
}
if(index_y>index_max)
{
index_y=0;
m_seriesY->clear();
}
if(head == 0xFE && tail ==0xAA)
{
sum = requestData[2]+requestData[3]+requestData[4]+requestData[5];
if(sum == flow_sum)
{
flow_SQUAL = requestData[7];
flow_x = (int16_t)(requestData[3]<<8 | requestData[2]);
flow_y = (int16_t)(requestData[5]<<8 | requestData[4]);
m_seriesX->append(index_x++,flow_x);
m_seriesY->append(index_y++,flow_y);
sum_data++;
sum_label->setNum(sum_data);
}
}
// qDebug()<<"head:"<<head<<"tail:"<<tail<<"sum:"<<sum<<"SQUAL:"<<SQUAL;
// qDebug()<<"head:"<<head<<"tail:"<<tail<<"flow_sum:"<<flow_sum<<"sum:"<<sum;
qDebug()<<"head:"<<head<<"tail:"<<tail<<"flow_x:"<<flow_x<<"flow_y:"<<flow_y;
#endif
}
/*显示隐藏坐标槽函数*/
void Widget::on_checkX_clicked(bool checked)
{
if(checked)
{
m_seriesX->setVisible(true);
}
else
{
m_seriesX->setVisible(false);
}
}
void Widget::on_checkY_clicked(bool checked)
{
if(checked)
{
m_seriesY->setVisible(true);
}
else
{
m_seriesY->setVisible(false);
}
}
/*刷新帧率和环境质量*/
void Widget::handleTimeout()
{
static int state=0;
state++;
if(state==2)
{
frame_rate=count;
count=0;
frameLable->setNum(frame_rate);
state=0;
}
SQUAL->setNum((flow_SQUAL));
}
void Widget::on_dial_valueChanged(int value)
{
indexY_range=value;
axisY->setRange(-indexY_range,indexY_range);
}
void Widget::on_dial_2_valueChanged(int value)
{
index2Y_range = value;
axis2Y->setRange(-index2Y_range,index2Y_range);
}
void Widget::on_checkdistX_clicked(bool checked)
{
if(checked)
{
m_distX->setVisible(true);
}
else
{
m_distX->setVisible(false);
}
}
void Widget::on_checkdistY_clicked(bool checked)
{
if(checked)
{
m_distY->setVisible(true);
}
else
{
m_distY->setVisible(false);
}
}