Skip to content

Commit

Permalink
Add a qcustomplot widget to the window and complete initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuYanzhen1 committed Sep 10, 2021
1 parent 9f6679b commit fc9f04a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.
8 changes: 4 additions & 4 deletions tools/CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2021-09-10T01:16:40. -->
<!-- Written by QtCreator 4.11.1, 2021-09-10T14:00:45. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -98,7 +98,7 @@
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments">-j 14</value>
<valuelist type="QVariantList" key="CMakeProjectManager.MakeStep.BuildTargets">
<value type="QString">all</value>
</valuelist>
Expand Down Expand Up @@ -370,7 +370,7 @@
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments">-j 14</value>
<valuelist type="QVariantList" key="CMakeProjectManager.MakeStep.BuildTargets">
<value type="QString">all</value>
</valuelist>
Expand Down Expand Up @@ -617,7 +617,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/Desktop/Files/miniFOC/tools/build</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
80 changes: 53 additions & 27 deletions tools/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MainWindow::MainWindow(QWidget *parent)
serial = new QSerialPort();
ui->setupUi(this);
refresh_serial_port();
setup_custom_plot();
ui->serial_baudrate_txt->setText("115200");
}

Expand All @@ -17,6 +18,58 @@ MainWindow::~MainWindow(){
delete ui;
}

void MainWindow::mdtp_callback_handler(unsigned char pid, const unsigned char *data){
qDebug() << "pack:" << pid << " data:" << data[0] << data[1] << data[2] << data[3]
<< data[4] << data[5] << data[6] << data[7];
}

void MainWindow::on_open_btn_clicked(){
if(serial->isOpen() == false){
if(set_serial_badurate() == true){
serial->open(QSerialPort::ReadWrite);
ui->open_btn->setText("Close");
ui->serial_port_cb->setEnabled(false);
ui->serial_baudrate_txt->setEnabled(false);
connect(serial,SIGNAL(readyRead()),this,SLOT(serial_received()));
}
}
else{
serial->close();
ui->open_btn->setText("Open");
ui->serial_port_cb->setEnabled(true);
ui->serial_baudrate_txt->setEnabled(true);
}
}

void MainWindow::on_refresh_btn_clicked(){
refresh_serial_port();
}

void MainWindow::setup_custom_plot(){
QPen pen;
pen.setColor(Qt::blue);
ui->custom_plot->legend->setVisible(true);
ui->custom_plot->addGraph();
ui->custom_plot->graph(0)->setPen(pen);
ui->custom_plot->graph(0)->setName("Speed");

pen.setColor(Qt::red);
ui->custom_plot->addGraph();
ui->custom_plot->graph(1)->setPen(pen);
ui->custom_plot->graph(1)->setName("Angle");

ui->custom_plot->xAxis->setLabel("n(times)");
ui->custom_plot->yAxis->setLabel("y(rad/s rad)");

ui->custom_plot->xAxis->setRange(0,1);
ui->custom_plot->yAxis->setRange(0,1);

connect(ui->custom_plot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->custom_plot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->custom_plot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->custom_plot->yAxis2, SLOT(setRange(QCPRange)));

ui->custom_plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}

bool MainWindow::set_serial_badurate(){
if(ui->serial_port_cb->currentText() == NULL || ui->serial_baudrate_txt->toPlainText() == NULL){
QMessageBox::information(NULL, "Error", "Please select port number and baudrate first!");
Expand All @@ -40,36 +93,9 @@ void MainWindow::refresh_serial_port(){
ui->serial_port_cb->addItem(serialPortinfo.at(i).portName());
}

void MainWindow::mdtp_callback_handler(unsigned char pid, const unsigned char *data){
qDebug() << "pack:" << pid << " data:" << data[0] << data[1] << data[2] << data[3]
<< data[4] << data[5] << data[6] << data[7];
}

void MainWindow::serial_received(){
QByteArray info = serial->readAll();
char *buffer = info.data();
for (int counter = 0; counter < info.length(); counter++)
mdtp_receive_handler(buffer[counter]);
}

void MainWindow::on_open_btn_clicked(){
if(serial->isOpen() == false){
if(set_serial_badurate() == true){
serial->open(QSerialPort::ReadWrite);
ui->open_btn->setText("Close");
ui->serial_port_cb->setEnabled(false);
ui->serial_baudrate_txt->setEnabled(false);
connect(serial,SIGNAL(readyRead()),this,SLOT(serial_received()));
}
}
else{
serial->close();
ui->open_btn->setText("Open");
ui->serial_port_cb->setEnabled(true);
ui->serial_baudrate_txt->setEnabled(true);
}
}

void MainWindow::on_refresh_btn_clicked(){
refresh_serial_port();
}
1 change: 1 addition & 0 deletions tools/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public slots:
QSerialPort *serial;
void refresh_serial_port();
bool set_serial_badurate();
void setup_custom_plot();
void mdtp_receive_handler(unsigned char data);
void mdtp_data_transmit(unsigned char pid, const unsigned char *data);
void mdtp_callback_handler(unsigned char pid, const unsigned char *data);
Expand Down
23 changes: 15 additions & 8 deletions tools/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<width>762</width>
<height>444</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -122,19 +122,26 @@
</font>
</property>
</widget>
<widget class="QTextEdit" name="test_txt">
<widget class="QCustomPlot" name="custom_plot" native="true">
<property name="geometry">
<rect>
<x>40</x>
<y>70</y>
<width>681</width>
<height>451</height>
<x>20</x>
<y>60</y>
<width>561</width>
<height>361</height>
</rect>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>QCustomPlot</class>
<extends>QWidget</extends>
<header>qcustomplot.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit fc9f04a

Please sign in to comment.