diff --git a/.gitignore b/.gitignore index 6b39d31..53bd18d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -.idea/ \ No newline at end of file +.idea/ +/cmake-build-* diff --git a/GChartView.cpp b/GChartView.cpp index f31b773..0ddcb27 100644 --- a/GChartView.cpp +++ b/GChartView.cpp @@ -14,11 +14,11 @@ GChartView::GChartView(QWidget *parent) : QChartView(parent) { chart = new QChart(); chart->setTitle("WIP"); axis_x->setRange(0, 5); - axis_y->setRange(-2.5, 2.5); - axis_x->setTickType(QValueAxis::TickType::TicksDynamic); + axis_y->setRange(-2.6, 2.6); +// axis_x->setTickType(QValueAxis::TickType::TicksDynamic); axis_x->setTickInterval(1); axis_x->setLabelFormat("%d"); - axis_y->setTickCount(21); +// axis_y->setTickCount(21); chart->addAxis(axis_x, Qt::AlignBottom); chart->addAxis(axis_y, Qt::AlignLeft); chart->addSeries(data); @@ -70,13 +70,14 @@ void GChartView::wheelEvent(QWheelEvent *event) { void GChartView::append(qreal x, qreal y) { data->append(x, y); - while (data->count() > 5000) { - data->remove(0); - } - auto beg = data->at(0); - auto end = data->at(data->count()); +// while (data->count() > 5000) { +// data->remove(0); +// } +// qDebug() << x << y; +// auto beg = data->at(0); + auto end = data->at(data->count()-1); if (end.x() > 5) { - axis_x->setRange(beg.x(), end.x()); + axis_x->setRange(end.x()-5, end.x()); } } diff --git a/main_window.cpp b/main_window.cpp index d4a15ab..10a3497 100755 --- a/main_window.cpp +++ b/main_window.cpp @@ -38,10 +38,10 @@ void main_window::config_enable(bool ena) { portOpenBtn->setText("close"), is_open = true; auto filename = tmpFilenameEdit->text(); receivefile.setFileName(filename); - receivefile.open(QIODevice::Append); + receivefile.open(QIODevice::WriteOnly); receivefileStream.setDevice(&receivefile); rawDataFile.setFileName(filename + "raw"); - rawDataFile.open(QIODevice::Append); + rawDataFile.open(QIODevice::WriteOnly); } } @@ -157,84 +157,122 @@ static int cnt = 0; QByteArray first_frame; bool first = true; -#define MAX_BUF_LENGTH 1e4 +#include + +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 in_data; +std::deque out_data; + + void main_window::read_data() { - static int receive_num_cnt = -2; - static int receive_byte_cnt = 0; - static int val = 0; + 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; auto data = port.readAll(); - auto pre_cursor =hexTextEdit->textCursor(); + auto pre_cursor = hexTextEdit->textCursor(); QString new_text = QString(data.toHex(' ').toUpper()) + " "; hexTextEdit->moveCursor(QTextCursor::End); bool move_to_end = pre_cursor == hexTextEdit->textCursor(); hexTextEdit->insertPlainText(new_text); if (!move_to_end) hexTextEdit->setTextCursor(pre_cursor); - pre_cursor = asciiTextEdit->textCursor(); - asciiTextEdit->moveCursor(QTextCursor::End); - asciiTextEdit->insertPlainText(QString(data)); - if (!move_to_end) asciiTextEdit->setTextCursor(pre_cursor); +// pre_cursor = asciiTextEdit->textCursor(); +//// asciiTextEdit->moveCursor(QTextCursor::End); +//// asciiTextEdit->insertPlainText(QString(data)); +// if (!move_to_end) asciiTextEdit->setTextCursor(pre_cursor); auto tmp = hexTextEdit->toPlainText(); if (tmp.length() > MAX_BUF_LENGTH) { - hexTextEdit->setPlainText(tmp.last((int) MAX_BUF_LENGTH)); - hexTextEdit->moveCursor(QTextCursor::End); +// hexTextEdit->setPlainText(tmp.last((int) MAX_BUF_LENGTH)); +// hexTextEdit->moveCursor(QTextCursor::End); + hexTextEdit->clear(); } - tmp = asciiTextEdit->toPlainText(); - if (tmp.length() > MAX_BUF_LENGTH) { - asciiTextEdit->setPlainText(tmp.last(int(MAX_BUF_LENGTH))); - asciiTextEdit->moveCursor(QTextCursor::End); +// tmp = asciiTextEdit->toPlainText(); +// if (tmp.length() > MAX_BUF_LENGTH) { +// asciiTextEdit->setPlainText(tmp.last(int(MAX_BUF_LENGTH))); +// asciiTextEdit->moveCursor(QTextCursor::End); +// } +// data processing + if (first) { + if (first_frame.length() < 50) { + first_frame += data; + return; + } + 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++) { + zero_cnt += (first_frame[i] == 0 ? 1 : 0); + } +// qDebug() <<"zero_cnt "<< zero_cnt ; +// qDebug() << first_frame; + } while (zero_cnt != 1); + data = first_frame; + first = false; + } + // TODO: BUG int data overflow + rawDataFile.write(data); + for (int i = 0; i < data.size(); i++) { + 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++; } - // data processing -// if (first) { -// if (data.length() < 10) { -// first_frame += data; -// return; -// } -// while (first_frame.size() > 0 && first_frame[0] != 0) { -// first_frame.remove(0, 1); -// } -// int zero_cnt = 0; -// do { -// zero_cnt = 0; -// 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++) { -// zero_cnt += first_frame[i] == 0 ? 1 : 0; -// } -// first_frame.remove(0, 1); -// } while (zero_cnt != 0); -// data = first_frame; -// first = false; -// } -// -// // TODO: BUG int data overflow -// rawDataFile.write(data); -// for (int i = 0; i < data.size(); i++) { -// if (receive_byte_cnt % 4 == 0) { -// if (data[i] != 0) { -// msgBox.setText("Error occurred, please restart transmission."); -// msgBox.exec(); -// this->open_port(); -// return; -// } -// receive_num_cnt++; -// if (receive_num_cnt == -1) -// continue; -// int real_number = val; -// if (real_number & 0x800000) { -// // negative number -// real_number = -(~real_number & 0x7fffff + 1); -// } -// receivefileStream << real_number << " "; -// double scaled = (double) real_number / (double) (0x7fffff) * 2.5; -// plotChartView->append ((double) receive_num_cnt / 1000.0, scaled); -// val = 0; -// continue; -// } -// val <<= 8; -// val |= data[i]; -// } } diff --git a/main_windows.cpp b/main_windows.cpp index e20de72..a720ccb 100755 --- a/main_windows.cpp +++ b/main_windows.cpp @@ -1,75 +1,75 @@ +//// +//// Created by zong on 5/18/22. +//// // -// Created by zong on 5/18/22. +//#include "main_windows.h" +//#include +//#include +//#include +//#include // - -#include "main_windows.h" -#include -#include -#include -#include - -static bool check_exist(QString str) { - for (auto &port: QSerialPortInfo::availablePorts()) { - if (str == port.portName()) - return true; - } - return false; -} - -main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - ui->setupUi(this); - QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data())); - QSerialPortInfo info; - auto tmp = QSerialPortInfo::availablePorts(); - ui->comboBox->clear(); - for (auto &port: tmp) { - qDebug() << port.portName(); - ui->comboBox->addItem(port.portName()); - } - -} - -main_windows::~main_windows() { - delete ui; -} - -void main_windows::update_ui_port() { - ui->comboBox->clear(); - for (auto &port: QSerialPortInfo::availablePorts()) { - ui->comboBox->addItem(port.portName()); - } -} - -void main_windows::open_port() { - if (is_open) { - port.close(); - is_open = false; - ui->pushButton_2->setText("open"); - return; - } - if (ui->comboBox->count() == 0) { return; } - auto cur_port = ui->comboBox->currentText(); - if (!check_exist(cur_port)) return; - port.setPortName(cur_port); - port.setStopBits(QSerialPort::OneStop); - port.setBaudRate(115200); - port.setDataBits(QSerialPort::Data8); - port.setParity(QSerialPort::NoParity); - port.setFlowControl(QSerialPort::NoFlowControl); - if (port.open(QIODevice::ReadWrite)) { - is_open = true; - ui->pushButton_2->setText("close"); - qDebug() << "open port success"; - } else - is_open = false; - -} - -void main_windows::read_data() { - auto data = port.readAll(); - qDebug() << data; - auto tmp = data.toHex(' ').toUpper(); - auto new_text = QString(tmp); - ui->textEdit->insertPlainText(new_text); -// auto text = ui->textEdit- -} +//static bool check_exist(QString str) { +// for (auto &port: QSerialPortInfo::availablePorts()) { +// if (str == port.portName()) +// return true; +// } +// return false; +//} +// +//main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { +// ui->setupUi(this); +// QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data())); +// QSerialPortInfo info; +// auto tmp = QSerialPortInfo::availablePorts(); +// ui->comboBox->clear(); +// for (auto &port: tmp) { +// qDebug() << port.portName(); +// ui->comboBox->addItem(port.portName()); +// } +// +//} +// +//main_windows::~main_windows() { +// delete ui; +//} +// +//void main_windows::update_ui_port() { +// ui->comboBox->clear(); +// for (auto &port: QSerialPortInfo::availablePorts()) { +// ui->comboBox->addItem(port.portName()); +// } +//} +// +//void main_windows::open_port() { +// if (is_open) { +// port.close(); +// is_open = false; +// ui->pushButton_2->setText("open"); +// return; +// } +// if (ui->comboBox->count() == 0) { return; } +// auto cur_port = ui->comboBox->currentText(); +// if (!check_exist(cur_port)) return; +// port.setPortName(cur_port); +// port.setStopBits(QSerialPort::OneStop); +// port.setBaudRate(115200); +// port.setDataBits(QSerialPort::Data8); +// port.setParity(QSerialPort::NoParity); +// port.setFlowControl(QSerialPort::NoFlowControl); +// if (port.open(QIODevice::ReadWrite)) { +// is_open = true; +// ui->pushButton_2->setText("close"); +// qDebug() << "open port success"; +// } else +// is_open = false; +// +//} +// +//void main_windows::read_data() { +// auto data = port.readAll(); +// qDebug() << data; +// auto tmp = data.toHex(' ').toUpper(); +// auto new_text = QString(tmp); +// ui->textEdit->insertPlainText(new_text); +//// auto text = ui->textEdit- +//} diff --git a/main_windows.h b/main_windows.h index d8a1e0a..7212c13 100755 --- a/main_windows.h +++ b/main_windows.h @@ -1,36 +1,36 @@ +//// +//// Created by zong on 5/18/22. +//// // -// Created by zong on 5/18/22. +//#ifndef UNTITLED_MAIN_WINDOWS_H +//#define UNTITLED_MAIN_WINDOWS_H // - -#ifndef UNTITLED_MAIN_WINDOWS_H -#define UNTITLED_MAIN_WINDOWS_H - -#include "ui_main.h" -#include -#include - -class main_windows : public QMainWindow { -Q_OBJECT -public: - explicit main_windows(QWidget *parent = nullptr); - - ~main_windows() override; - -private: - Ui::MainWindow *ui; - bool is_open = false; - - - QSerialPort port; - -public slots: - - void update_ui_port(); - - void open_port(); - - void read_data(); -}; - - -#endif //UNTITLED_MAIN_WINDOWS_H +//#include "ui_main.h" +//#include +//#include +// +//class main_windows : public QMainWindow { +//Q_OBJECT +//public: +// explicit main_windows(QWidget *parent = nullptr); +// +// ~main_windows() override; +// +//private: +// Ui::MainWindow *ui; +// bool is_open = false; +// +// +// QSerialPort port; +// +//public slots: +// +// void update_ui_port(); +// +// void open_port(); +// +// void read_data(); +//}; +// +// +//#endif //UNTITLED_MAIN_WINDOWS_H