This commit is contained in:
guangzong 2023-11-08 09:35:59 -05:00
parent dad61488a1
commit 0f77f98fe4
Signed by: guangzong
GPG Key ID: 095389BACAE97D19
5 changed files with 110 additions and 169 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target /target
.idea/ .idea/
/cmake-build-* /cmake-build-*
build/

View File

@ -15,6 +15,7 @@ find_package(Qt6 COMPONENTS
Charts Charts
REQUIRED) REQUIRED)
include_directories(./)
add_executable(com main.cpp main_window.cpp main_window.h GChartView.cpp GChartView.h) add_executable(com main.cpp main_window.cpp main_window.h GChartView.cpp GChartView.h)
#add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h) #add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h)
target_link_libraries(com target_link_libraries(com

View File

@ -5,16 +5,33 @@
// You may need to build the project (run Qt uic code generator) to get "ui_gchartview.h" resolved // You may need to build the project (run Qt uic code generator) to get "ui_gchartview.h" resolved
#include "GChartView.h" #include "GChartView.h"
#include "QtCharts/QScatterSeries"
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QValueAxis>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#define MAX_DATA 200
GChartView::GChartView(QWidget *parent) : QChartView(parent) { GChartView::GChartView(QWidget *parent) : QChartView(parent) {
data = new QLineSeries(); data = new QSplineSeries();
// Customize spline series
data->setPen(QPen(Qt::blue)); // Set the color of the spline line
// Customize scatter series
// data->setMarkerSize(10.0); // Set the size of the scatter points
data->setPointsVisible(true); // Set the scatter points visible or not
data->setBrush(QBrush(Qt::red)); // Set the color of the scatter points
axis_x = new QValueAxis(); axis_x = new QValueAxis();
axis_y = new QValueAxis(); axis_y = new QValueAxis();
chart = new QChart(); chart = new QChart();
chart->setTitle("WIP"); chart->setTitle("WIP");
axis_x->setRange(0, 5); axis_x->setRange(0, MAX_DATA);
axis_y->setRange(-2.6, 2.6); axis_y->setRange(-6, 7);
// axis_x->setTickType(QValueAxis::TickType::TicksDynamic); // axis_x->setTickType(QValueAxis::TickType::TicksDynamic);
axis_x->setTickInterval(1); axis_x->setTickInterval(1);
axis_x->setLabelFormat("%d"); axis_x->setLabelFormat("%d");
@ -31,53 +48,49 @@ GChartView::~GChartView() {
} }
void GChartView::wheelEvent(QWheelEvent *event) { void GChartView::wheelEvent(QWheelEvent *event) {
auto degrees = event->angleDelta(); // auto degrees = event->angleDelta();
auto cursor_y = event->position().y(); // auto cursor_y = event->position().y();
auto cur_range_max = axis_y->max(); // auto cur_range_max = axis_y->max();
auto cur_range_min = axis_y->min(); // auto cur_range_min = axis_y->min();
//
// left and top corner is origin // // left and top corner is origin
auto pos_max = chart->mapToPosition(QPointF(0, cur_range_min)).y(); // auto pos_max = chart->mapToPosition(QPointF(0, cur_range_min)).y();
auto pos_min = chart->mapToPosition(QPointF(0, cur_range_max)).y(); // auto pos_min = chart->mapToPosition(QPointF(0, cur_range_max)).y();
auto new_range = 5.0; // auto new_range = 5.0;
auto center = (qreal) 0.0; // auto center = (qreal) 0.0;
if (cursor_y < pos_max && cursor_y > pos_min) { // if (cursor_y < pos_max && cursor_y > pos_min) {
center = chart->mapToValue(QPointF(0, cursor_y)).y(); // center = chart->mapToValue(QPointF(0, cursor_y)).y();
auto cur_range = (cur_range_max - cur_range_min); // auto cur_range = (cur_range_max - cur_range_min);
if (degrees.y() > 0) // if (degrees.y() > 0)
new_range = cur_range * 0.8; // new_range = cur_range * 0.8;
else // else
new_range = cur_range / 0.8; // new_range = cur_range / 0.8;
} // }
// setup new axis // // setup new axis
auto new_max = (center + new_range / 2.0); // auto new_max = (center + new_range / 2.0);
auto new_min = (center - new_range / 2.0); // auto new_min = (center - new_range / 2.0);
if (new_max > 2.5) { // if (new_max > 2.5) {
new_min -= (new_max - 2.5); // new_min -= (new_max - 2.5);
new_max = 2.5; // new_max = 2.5;
} // }
if (new_min < -2.5) { // if (new_min < -2.5) {
new_max += (-2.5 - new_min); // new_max += (-2.5 - new_min);
new_min = -2.5; // new_min = -2.5;
new_max = new_max > 2.5 ? 2.5 : new_max; // new_max = new_max > 2.5 ? 2.5 : new_max;
} // }
//
axis_y->setRange(new_min, new_max); // axis_y->setRange(new_min, new_max);
qDebug() << "new_min: " << new_min << " " << new_max; // qDebug() << "new_min: " << new_min << " " << new_max;
} }
void GChartView::append(qreal x, qreal y) { void GChartView::append(qreal x, qreal y) {
data->append(x, y); data->append(x, y);
// while (data->count() > 5000) { while (data->count() > MAX_DATA) {
// data->remove(0); data->remove(0);
// } }
// qDebug() << x << y;
// auto beg = data->at(0);
auto end = data->at(data->count()-1); auto end = data->at(data->count()-1);
if (end.x() > 5) { if (end.x() > MAX_DATA) {
axis_x->setRange(end.x()-5, end.x()); axis_x->setRange(end.x()-MAX_DATA, end.x());
} }
} }

View File

@ -10,6 +10,7 @@
#include <QLineSeries> #include <QLineSeries>
#include <QValueAxis> #include <QValueAxis>
#include <QChart> #include <QChart>
#include <QSplineSeries>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class gchartview; } namespace Ui { class gchartview; }
@ -24,7 +25,8 @@ public:
void wheelEvent(QWheelEvent *even) override; void wheelEvent(QWheelEvent *even) override;
private: private:
QLineSeries* data; // QLineSeries* data;
QSplineSeries *data;
QValueAxis* axis_x; QValueAxis* axis_x;
QValueAxis* axis_y; QValueAxis* axis_y;
QChart* chart; QChart* chart;

View File

@ -5,8 +5,6 @@
#include "main_window.h" #include "main_window.h"
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo> #include <QtSerialPort/QSerialPortInfo>
#include <queue>
#include <cstdint>
#include <QChartView> #include <QChartView>
#include <QWidget> #include <QWidget>
@ -46,7 +44,6 @@ void main_window::config_enable(bool ena) {
} }
void main_window::setUiComponent() { void main_window::setUiComponent() {
this->ui = new Ui::MainWindow; this->ui = new Ui::MainWindow;
ui->setupUi(this); ui->setupUi(this);
@ -130,6 +127,7 @@ void main_window::open_port() {
auto cur_port = portsComboBox->currentText(); auto cur_port = portsComboBox->currentText();
if (!check_exist(cur_port)) return; if (!check_exist(cur_port)) return;
port.setPortName(cur_port); port.setPortName(cur_port);
auto stopbits = stopbitsComboBox->currentText(); auto stopbits = stopbitsComboBox->currentText();
if (stopbits == QString("1")) port.setStopBits(QSerialPort::OneStop); if (stopbits == QString("1")) port.setStopBits(QSerialPort::OneStop);
else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop); else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop);
@ -139,9 +137,16 @@ void main_window::open_port() {
return; return;
} }
// TODO: read info from config // TODO: read info from config
port.setBaudRate(115200); //get baudrate
port.setDataBits(QSerialPort::Data8); port.setBaudRate(baudLineEdit->text().toInt());
port.setParity(QSerialPort::NoParity);
// get databits
auto databits = dataBitsComboBox->currentText();
if (databits == QString("5")) port.setDataBits(QSerialPort::Data5);
else if (databits == QString("6")) port.setDataBits(QSerialPort::Data6);
else if (databits == QString("7")) port.setDataBits(QSerialPort::Data7);
else if (databits == QString("8"))
port.setParity(QSerialPort::NoParity);
port.setFlowControl(QSerialPort::NoFlowControl); port.setFlowControl(QSerialPort::NoFlowControl);
if (port.open(QIODevice::ReadWrite)) { if (port.open(QIODevice::ReadWrite)) {
config_enable(false); config_enable(false);
@ -153,126 +158,45 @@ void main_window::open_port() {
} }
static int cnt = 0; double t = 0;
QByteArray first_frame;
bool first = true;
#include <deque>
static int receive_num_cnt = -2;
static int receive_byte_cnt = 0;
#define MAX_BUF_LENGTH 1e3
const double b[4] = {1.0000, -2.6236, 2.3147, -0.6855};
const double a[4] = {0.0007, 0.0021, 0.0021, 0.0007};
std::deque<double> in_data;
std::deque<double> out_data;
void main_window::read_data() { void main_window::read_data() {
static double sum_b = 0;
static double sum_a = 0;
for(int i=0;i<4;i++){
sum_a += a[i];
sum_b += b[i];
}
static qint64 val = 0; static qint64 val = 0;
auto data = port.readAll(); auto data = port.readAll();
auto pre_cursor = hexTextEdit->textCursor(); // save data to file
QString new_text = QString(data.toHex(' ').toUpper()) + " "; receivefileStream << data;
hexTextEdit->moveCursor(QTextCursor::End); // auto pre_cursor = hexTextEdit->textCursor();
bool move_to_end = pre_cursor == hexTextEdit->textCursor(); // QString new_text = QString(data.toHex(' ').toUpper()) + " ";
hexTextEdit->insertPlainText(new_text); // hexTextEdit->moveCursor(QTextCursor::End);
if (!move_to_end) hexTextEdit->setTextCursor(pre_cursor); // bool move_to_end = pre_cursor == hexTextEdit->textCursor();
// pre_cursor = asciiTextEdit->textCursor(); // hexTextEdit->insertPlainText(new_text);
//// asciiTextEdit->moveCursor(QTextCursor::End); // if (!move_to_end) hexTextEdit->setTextCursor(pre_cursor);
//// asciiTextEdit->insertPlainText(QString(data)); // auto tmp = hexTextEdit->toPlainText();
// if (!move_to_end) asciiTextEdit->setTextCursor(pre_cursor); //
// asciiTextEdit->setText(asciiTextEdit->toPlainText() + QString(data));
auto tmp = hexTextEdit->toPlainText(); // // move cursor to end
if (tmp.length() > MAX_BUF_LENGTH) { // asciiTextEdit->moveCursor(QTextCursor::End);
// hexTextEdit->setPlainText(tmp.last((int) MAX_BUF_LENGTH)); // // clean up ascii text nad hex text if too long
// hexTextEdit->moveCursor(QTextCursor::End); // if (asciiTextEdit->toPlainText().size() > 10000) {
hexTextEdit->clear(); // asciiTextEdit->setText(asciiTextEdit->toPlainText().right(1000));
}
// tmp = asciiTextEdit->toPlainText();
// if (tmp.length() > MAX_BUF_LENGTH) {
// asciiTextEdit->setPlainText(tmp.last(int(MAX_BUF_LENGTH)));
// asciiTextEdit->moveCursor(QTextCursor::End);
// } // }
// data processing // if (hexTextEdit->toPlainText().size() > 10000) {
if (first) { // hexTextEdit->setText(hexTextEdit->toPlainText().right(1000));
if (first_frame.length() < 50) { // }
first_frame += data; // parse data to line (ascii data)
return; // auto lines = QString(data).split('\n');
}
int zero_cnt = 0;
do {
zero_cnt = 0;
first_frame.remove(0, 1);
// qDebug() << first_frame;
while (first_frame.size() > 0 && first_frame[0] != 0) {
first_frame.remove(0, 1);
}
for (int i = 0; i < 4 && i < first_frame.size(); i++) { // // if line contains 5 float number, then give the first one to plot
zero_cnt += (first_frame[i] == 0 ? 1 : 0); // for (auto &line: lines) {
} // auto tmp = line.split(' ');
// qDebug() <<"zero_cnt "<< zero_cnt ; //// if (tmp.size() != 5) continue;
// qDebug() << first_frame; // // convert first number to float (check if it is a number)
} while (zero_cnt != 1); // bool ok = false;
data = first_frame; // auto data = tmp[0].toFloat(&ok);
first = false; // if (!ok) continue;
} // if (data > 6) data=0.0;
// TODO: BUG int data overflow // t += 1;
rawDataFile.write(data); // plotChartView->append(t, data);
for (int i = 0; i < data.size(); i++) { // qDebug() << data;
if (receive_byte_cnt % 4 == 0) { // }
if (data[i] != 0) {
msgBox.setText("Error occurred, please restart transmission.");
msgBox.exec();
this->open_port();
first = true;
receive_num_cnt = -2;
receive_byte_cnt = 0;
return;
}
receive_num_cnt++;
qint64 real_number = val;
if ((real_number & 0x800000) != 0) {
// negative number
real_number &= 0x7fffff;
real_number = -(((real_number ^ 0x7fffff) + 1));
}
// qDebug() << "val: " << val;
// qDebug() << "real_number: " << real_number;
receivefileStream << real_number << " ";
double scaled = (double) real_number / (double) (0x7fffff) * 2.5;
// in_data.push_front(scaled);
in_data.push_back(scaled);
// out_data.push_back(0);
if (in_data.size() < 5)
out_data.push_back(0);
else {
double tmp = b[0] * in_data[0] + b[1] * in_data[1] + b[2] * in_data[2] + b[3] * in_data[3];
tmp = tmp - a[1] * out_data[0] - a[2] * out_data[1] - a[3] * out_data[2];
// tmp = tmp * sum_b/ sum_a;
out_data.push_back(tmp);
qDebug() << tmp;
out_data.pop_front();
in_data.pop_front();
}
// scaled = (double) out_data[0] / (double) (0x7fffff) * 2.5;
if (receive_num_cnt %50 == 0)
plotChartView->append((double) receive_num_cnt / 1000.0, out_data[0]);
val = 0;
}
val <<= 8;
val |= (data[i] & 0xFF);
receive_byte_cnt++;
}
} }