Roomba App  1.0
mainwindow.cpp
Go to the documentation of this file.
1 
14 #include <QCloseEvent>
15 #include <QColor>
16 #include <QDateTime>
17 #include <QDebug>
18 #include <QGraphicsDropShadowEffect>
19 #include <QList>
20 #include <QObject>
21 #include <QSerialPortInfo>
22 #include <QSignalMapper>
23 
24 #include "mainwindow.hh"
25 #include "ui_mainwindow.h"
26 
27 MainWindow::MainWindow(QWidget* parent)
28  : QMainWindow(parent), ui(new Ui::MainWindow), _communication(new Communication),
29  _frameThread(new FrameThread(_communication, this)), _parser(new Parser())
30 
31 {
32  ui->setupUi(this);
33  ui->serialStatus->setAlignment(Qt::AlignCenter);
34  ui->statusbar->addPermanentWidget(ui->serialStatus, 1);
35 
36  _serialTimer.setInterval(200);
37  _serialTimer.setSingleShot(true);
38 
39  connect(&_serialTimer, &QTimer::timeout, this, &MainWindow::serialTimeout);
48  connect(_parser, &Parser::changedJoystick, ui->widgetJoystick, &Joystick::movePos);
49  connect(_parser, &Parser::changedLidar, ui->openGLWidget, &GLWidget::obstacleDataReady);
51 
52  addDropShadow(ui->groupBoxLidar, 4, 4, 4);
53  addDropShadow(ui->groupBoxAnalogJoystick, 4, 4, 4);
54  addDropShadow(ui->groupBoxBattery, 4, 4, 4);
55  addDropShadow(ui->groupBoxMotors, 4, 4, 4);
56  addDropShadow(ui->groupBoxLogs, 4, 4, 4);
57 
58  ui->lineEditAccelerationValueL->setText(QString("0.00"));
59  ui->lineEditSpeedValueL->setText(QString("0.00"));
60  ui->lineEditGSpeedValueL->setText(QString("0.00"));
61 
62  ui->lineEditAccelerationValueR->setText(QString("0.00"));
63  ui->lineEditSpeedValueR->setText(QString("0.00"));
64  ui->lineEditGSpeedValueR->setText(QString("0.00"));
65 }
66 
68 {
69  delete ui;
70 }
71 
72 void MainWindow::closeEvent(QCloseEvent* event)
73 {
75  _frameThread->wait();
76  event->accept();
77 }
78 
80 {
82  _frameThread->wait();
83  QApplication::quit();
84 }
85 
87 {
89  {
90  addMessageToLogs("Zamknięto połączenie.");
91  _communication->setStatus(ui->serialStatus);
92  _frameThread->wait();
93  }
94  else
95  {
96  addMessageToLogs("Port nie jest otwarty!");
97  return;
98  }
99 }
100 
102 {
103  // TODO: Repair bug when connect menu is hovered first and do not show founded devices
104  ui->menuConnect->clear();
105  addMessageToLogs("Szukam urządzeń...");
106 
107  QList<QSerialPortInfo> devices;
108  devices = QSerialPortInfo::availablePorts();
109 
110  QSignalMapper* mapper = new QSignalMapper;
111  connect(mapper, SIGNAL(mappedString(QString)), this, SLOT(connectSerialDevice(QString)));
112 
113  for(int i = 0; i < devices.count(); i++)
114  {
115  addMessageToLogs("Znalazłem urządzenie: " + devices.at(i).portName() + " " + devices.at(i).description());
116  QAction* deviceName = new QAction(QString(devices.at(i).portName() + "\t" + devices.at(i).description()), this);
117  QObject::connect(deviceName, SIGNAL(triggered()), mapper, SLOT(map()));
118  mapper->setMapping(deviceName, deviceName->text());
119  ui->menuConnect->addAction(deviceName);
120  }
121 }
122 
123 void MainWindow::connectSerialDevice(const QString& actionName)
124 {
125  QString portName = actionName.split("\t").first();
126 
127  _communication->setDeviceName(portName.toStdString().data());
128 
129  if(!_communication->isOpen())
130  {
131  _frameThread->start();
132  _communication->setStatus(ui->serialStatus);
133  _serialTimer.start();
134  }
135  else
136  {
137  addMessageToLogs("Port już jest otwarty!");
138  return;
139  }
140 }
141 
143 {
144  std::string line;
145 
147  {
148  if(!_communication->useFrameBuffer().getFrame(line)) continue;
149  addMessageToLogs(QString(line.c_str()));
150 
151  if(_parser->parseFrame(line))
152  {
153  _parser->processData();
154  }
155  }
156 
157  _serialTimer.start();
158 }
159 
160 void MainWindow::showErrorMessage(const QString& message)
161 {
162  ui->statusbar->showMessage(message, 100);
163 }
164 
165 void MainWindow::addMessageToLogs(const QString& message)
166 {
167  QString currentDate = QDateTime::currentDateTime().toString("yyyy.MM.dd");
168  QString currentTime = QDateTime::currentDateTime().toString("hh:mm:ss");
169  ui->textEditLogs->append(currentDate + " | " + currentTime + " | " + message);
170 }
171 
172 void MainWindow::addDropShadow(QGroupBox* group, int xOffset, int yOffset, int radius)
173 {
174  QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect();
175  shadow->setBlurRadius(radius);
176  shadow->setOffset(xOffset, yOffset);
177  group->setGraphicsEffect(shadow);
178 }
179 
180 void MainWindow::addValueSpeedL(const QString& message)
181 {
182  ui->lineEditSpeedValueL->setText(message);
183 }
184 
185 void MainWindow::addValueGSpeedL(const QString& message)
186 {
187  ui->lineEditGSpeedValueL->setText(message);
188 }
189 
190 void MainWindow::addValueAccelerationL(const QString& message)
191 {
192  ui->lineEditAccelerationValueL->setText(message);
193 }
194 
195 void MainWindow::addValueSpeedR(const QString& message)
196 {
197  ui->lineEditSpeedValueR->setText(message);
198 }
199 
200 void MainWindow::addValueGSpeedR(const QString& message)
201 {
202  ui->lineEditGSpeedValueR->setText(message);
203 }
204 
205 void MainWindow::addValueAccelerationR(const QString& message)
206 {
207  ui->lineEditAccelerationValueR->setText(message);
208 }
209 
210 void MainWindow::addValueBaterry(const int message)
211 {
212  ui->progressBarBattery->setValue(message);
213 }
The Communication class.
const FrameBuffer & getFrameBuffer() const
Get unmodifiable frame buffer.
FrameBuffer & useFrameBuffer()
Get modifiable frame buffer.
bool closeSerialPort()
Close serial port.
void setDeviceName(const std::string &name)
Set serial device name.
void setStatus(QLabel *statusBar)
Set status of port openning on status bar.
void endCommunication()
End communicaiton with serial device.
bool isOpen()
Check wether serial poet is open.
bool isFrame() const
Check if list contain any frame.
Definition: frameBuffer.cpp:45
bool getFrame(std::string &frame)
Get one frame from the top of the list.
Definition: frameBuffer.cpp:33
The FrameThread class.
Definition: frameThread.hh:23
void info(const QString &info)
Signal to be emmited to add information to logs.
void error(const QString &error)
Signal to be emmited when some error occured when running thread.
void obstacleDataReady(const std::map< float, int > &lidarData)
Slot to get lidar data when parser sent signal that this data is ready.
Definition: glWidget.cpp:16
void movePos(const QPoint &coordinates)
Change knop coordinates.
Definition: joystick.cpp:94
The MainWindow class.
Definition: mainwindow.hh:39
void showErrorMessage(const QString &message)
Show error message as status bar message.
Definition: mainwindow.cpp:160
void closeEvent(QCloseEvent *event) override
Reimplemented to savely close reading thread.
Definition: mainwindow.cpp:72
void on_actionDisconnect_triggered()
Disconnect from serial device.
Definition: mainwindow.cpp:86
FrameThread * _frameThread
Serial communication thread handle.
Definition: mainwindow.hh:174
void on_actionClose_triggered()
Close application.
Definition: mainwindow.cpp:79
Communication * _communication
Communication handle.
Definition: mainwindow.hh:164
Ui::MainWindow * ui
Pointer to all ui widgets.
Definition: mainwindow.hh:150
void addValueGSpeedL(const QString &message)
addValueSetSpeedL
Definition: mainwindow.cpp:185
void addValueSpeedR(const QString &message)
addValueVelocityR
Definition: mainwindow.cpp:195
void addMessageToLogs(const QString &message)
Add message to log widget.
Definition: mainwindow.cpp:165
Parser * _parser
_parser
Definition: mainwindow.hh:179
void addValueSpeedL(const QString &message)
addValueVelocityL
Definition: mainwindow.cpp:180
MainWindow(QWidget *parent=nullptr)
Constrict a new MainWindow object.
Definition: mainwindow.cpp:27
void addValueBaterry(const int message)
addValueBaterry
Definition: mainwindow.cpp:210
void addDropShadow(QGroupBox *group, int xOffset, int yOffset, int radius)
addDropShadow
Definition: mainwindow.cpp:172
void serialTimeout()
Slot to handle serial timeout which signals to update data from serial.
Definition: mainwindow.cpp:142
void addValueAccelerationR(const QString &message)
addValueAccelR
Definition: mainwindow.cpp:205
QTimer _serialTimer
Serial communication timer to get new frames.
Definition: mainwindow.hh:169
void on_actionSearch_triggered()
Search serial devices.
Definition: mainwindow.cpp:101
~MainWindow()
Delete MainWindow object.
Definition: mainwindow.cpp:67
void connectSerialDevice(const QString &actionName)
Connect to serial device.
Definition: mainwindow.cpp:123
void addValueAccelerationL(const QString &message)
addValueAccelL
Definition: mainwindow.cpp:190
void addValueGSpeedR(const QString &message)
addValueSetSpeedR
Definition: mainwindow.cpp:200
The Parser class.
Definition: parser.hh:32
void changedRightMotorAcceleration(const QString &accelerationValue)
Signal emitted when parsed data of right motor acceleration is ready.
void changedLidar(const std::map< float, int > lidarData)
Signal emitted when parsed data of lidar is ready.
void changedLeftMotorGSpeed(const QString &speedValue)
Signal emitted when parsed data of left motor given speed is ready.
void changedBattery(int battery)
Signal emitted when parsed data of battery voltage is ready.
void processData()
processData
Definition: parser.cpp:76
bool parseFrame(const std::string &frame)
Parse line from serial port into managed variables.
Definition: parser.cpp:25
void changedRightMotorSpeed(const QString &speedValue)
Signal emitted when parsed data of right motor speed is ready.
void changedLeftMotorSpeed(const QString &speedValue)
Signal emitted when parsed data of left motor speed is ready.
void changedJoystick(const QPoint &coordinates)
Signal emitted when parsed data of joystick swing is ready.
void changedRightMotorGSpeed(const QString &speedValue)
Signal emitted when parsed data of right motor given speed is ready.
void changedLeftMotorAcceleration(const QString &accelerationValue)
Signal emitted when parsed data of left motor acceleration is ready.
Header file for MainWindow class.
Definition: mainwindow.hh:30