-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
98 lines (89 loc) · 2.97 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initGUI();
pid = new PIDThread;
connect(ui->sldPosition, SIGNAL(valueChanged(int)), pid, SLOT(setCurrentPosition(int)));
connect(pid, SIGNAL(updatePosition(int)), ui->sldPosition, SLOT(setValue(int)));
connect(pid, SIGNAL(updatePosition(int)), this, SLOT(updateChart(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initGUI()
{
// set title
setWindowTitle("Simple PID Emulator");
// move window to center
move((QApplication::desktop()->width() - width()) / 2,
(QApplication::desktop()->height() - height()) / 2);
// input constraints
ui->editPos->setValidator(new QIntValidator(this));
ui->editP->setValidator(new QDoubleValidator(this));
ui->editI->setValidator(new QDoubleValidator(this));
ui->editD->setValidator(new QDoubleValidator(this));
// connections
connect(ui->action_Quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::on_btnSetPos_clicked()
{
ui->lblCurTarget->setText(ui->editPos->text());
ui->lblWarning->setText("");
if (ui->lblCurTarget->text().isEmpty())
{
ui->lblWarning->setText("<font color=\"red\">Warning: Empty position target!</font>");
return;
}
if (ui->lblPosCurP->text().isEmpty()
|| ui->lblPosCurI->text().isEmpty()
|| ui->lblPosCurD->text().isEmpty())
{
ui->lblWarning->setText("<font color=\"red\">Warning: Empty PID parameter(s)!</font>");
return;
}
if (abs(ui->lblCurTarget->text().toInt()) > 1000)
{
ui->lblWarning->setText("<font color=\"red\">Warning: Position out of range!</font>");
return;
}
pid->clearData();
pid->setTargetPosition(ui->lblCurTarget->text().toInt());
ui->widPosChart->clearRecord();
// ui->sldPosition->setValue(ui->lblCurTarget->text().toInt());
pid->start();
}
void MainWindow::on_btnSetPID_clicked()
{
ui->lblPosCurP->setText(ui->editP->text());
ui->lblPosCurI->setText(ui->editI->text());
ui->lblPosCurD->setText(ui->editD->text());
pid->setPIDParameters(ui->lblPosCurP->text().toDouble(),
ui->lblPosCurI->text().toDouble(),
ui->lblPosCurD->text().toDouble());
}
void MainWindow::on_btnQuit_clicked()
{
qApp->quit();
}
void MainWindow::on_sldPosition_valueChanged(int value)
{
ui->lblOrigin->setText(QString::number(value / 10.0, 'f', 1));
}
void MainWindow::DrawPosChart(int newPos)
{
int position = -newPos / 10000.0 * ui->widPosChart->height() / 2
+ ui->widPosChart->height() / 2;
// qDebug("pos: %d, %d", position, newPos);
ui->widPosChart->insertPosValue(position);
ui->widPosChart->update();
}
void MainWindow::updateChart(int newPos)
{
DrawPosChart(newPos);
}