Roomba App  1.0
mainwindow.cpp
Go to the documentation of this file.
1 
15 #include <QCloseEvent>
16 #include <QColor>
17 #include <QDateTime>
18 #include <QDebug>
19 #include <QGraphicsDropShadowEffect>
20 #include <QList>
21 #include <QObject>
22 #include <QSerialPortInfo>
23 #include <QSignalMapper>
24 #include <QTranslator>
25 
26 #include "mainwindow.hh"
27 #include "ui_mainwindow.h"
28 
29 MainWindow::MainWindow(QWidget* parent)
30  : QMainWindow(parent), ui(new Ui::MainWindow), _communication(new Communication),
31  _frameThread(new FrameThread(_communication, this)), _parser(new Parser())
32 {
33  ui->setupUi(this);
34  ui->serialStatus->setAlignment(Qt::AlignCenter);
35  ui->statusbar->addPermanentWidget(ui->serialStatus, 1);
36 
37  _serialTimer.setInterval(200);
38  _serialTimer.setSingleShot(true);
39 
40  connect(&_serialTimer, &QTimer::timeout, this, &MainWindow::serialTimeout);
41 
45 
52  connect(_parser, &Parser::changedJoystick, ui->widgetJoystick, &Joystick::movePos);
53  connect(_parser, &Parser::changedLidar, ui->openGLWidget, &GLWidget::obstacleDataReady);
59 
60  addDropShadow(ui->groupBoxLidar, 4, 4, 4);
61  addDropShadow(ui->groupBoxAnalogJoystick, 4, 4, 4);
62  addDropShadow(ui->groupBoxBattery, 4, 4, 4);
63  addDropShadow(ui->groupBoxMotors, 4, 4, 4);
64  addDropShadow(ui->groupBoxLogs, 4, 4, 4);
65 
66  ui->lineEditAccelerationValueL->setText(QString("0.00"));
67  ui->lineEditSpeedValueL->setText(QString("0.00"));
68  ui->lineEditGSpeedValueL->setText(QString("0.00"));
69 
70  ui->lineEditAccelerationValueR->setText(QString("0.00"));
71  ui->lineEditSpeedValueR->setText(QString("0.00"));
72  ui->lineEditGSpeedValueR->setText(QString("0.00"));
73 
74  QStringList languages;
75  const QStringList uiLanguages = QLocale::system().uiLanguages();
76  for(const QString& locale : uiLanguages)
77  {
78  const QString baseName = "roomba-app_" + QLocale(locale).name();
79 
80  if(_translator.load(":/i18n/" + baseName))
81  {
82  qApp->installTranslator(&_translator);
83  break;
84  }
85  }
86  ui->retranslateUi(this);
87 }
88 
90 {
91  delete ui;
92 }
93 
94 void MainWindow::closeEvent(QCloseEvent* event)
95 {
97  _frameThread->wait();
98  event->accept();
99 }
100 
102 {
104  _frameThread->wait();
105  QApplication::quit();
106 }
107 
109 {
111  _frameThread->wait();
112 }
113 
115 {
116  ui->menuConnect->clear();
117  addMessageToLogs(tr("Szukam urządzeń..."));
118 
119  QList<QSerialPortInfo> devices;
120  devices = QSerialPortInfo::availablePorts();
121 
122  QSignalMapper* mapper = new QSignalMapper;
123  connect(mapper, SIGNAL(mappedString(QString)), this, SLOT(connectSerialDevice(QString)));
124 
125  for(int i = 0; i < devices.count(); i++)
126  {
127  QString message = tr("Znalazłem urządzenie: ");
128  addMessageToLogs(message + devices.at(i).portName() + " " + devices.at(i).description());
129  QAction* deviceName = new QAction(QString(devices.at(i).portName() + "\t" + devices.at(i).description()), this);
130  QObject::connect(deviceName, SIGNAL(triggered()), mapper, SLOT(map()));
131  mapper->setMapping(deviceName, deviceName->text());
132  ui->menuConnect->addAction(deviceName);
133  }
134 }
135 
136 void MainWindow::connectSerialDevice(const QString& actionName)
137 {
138  QString portName = actionName.split("\t").first();
139 
140  _communication->setDeviceName(portName.toStdString().data());
141 
142  if(!_communication->isOpen())
143  {
144  _frameThread->start();
145  _serialTimer.start();
146  }
147  else
148  {
149  addMessageToLogs(tr("Port już jest otwarty!"));
150  return;
151  }
152 }
153 
155 {
156  std::string line;
157 
159  {
160  if(!_communication->useFrameBuffer().getFrame(line)) continue;
161 
162  if(_parser->parseFrame(line))
163  {
164  _parser->processData();
165  }
166  else
167  {
168  addMessageToLogs(tr("Niepoprawna ramka danych"));
169  }
170  }
171 
172  _serialTimer.start();
173 }
174 
175 void MainWindow::showErrorMessage(const QString& message)
176 {
177  ui->statusbar->showMessage(message, 100);
178 }
179 
180 void MainWindow::addMessageToLogs(const QString& message)
181 {
182  QString currentDate = QDateTime::currentDateTime().toString("yyyy.MM.dd");
183  QString currentTime = QDateTime::currentDateTime().toString("hh:mm:ss");
184  ui->textEditLogs->append(currentDate + " | " + currentTime + " | " + message);
185 }
186 
187 void MainWindow::addDropShadow(QGroupBox* group, int xOffset, int yOffset, int radius)
188 {
189  QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect();
190  shadow->setBlurRadius(radius);
191  shadow->setOffset(xOffset, yOffset);
192  group->setGraphicsEffect(shadow);
193 }
194 
195 void MainWindow::addValueSpeedL(const QString& message)
196 {
197  ui->lineEditSpeedValueL->setText(message);
198 }
199 
200 void MainWindow::addValueGSpeedL(const QString& message)
201 {
202  ui->lineEditGSpeedValueL->setText(message);
203 }
204 
205 void MainWindow::addValueAccelerationL(const QString& message)
206 {
207  ui->lineEditAccelerationValueL->setText(message);
208 }
209 
210 void MainWindow::addValueSpeedR(const QString& message)
211 {
212  ui->lineEditSpeedValueR->setText(message);
213 }
214 
215 void MainWindow::addValueGSpeedR(const QString& message)
216 {
217  ui->lineEditGSpeedValueR->setText(message);
218 }
219 
220 void MainWindow::addValueAccelerationR(const QString& message)
221 {
222  ui->lineEditAccelerationValueR->setText(message);
223 }
224 
225 void MainWindow::addValueBattery(const int message)
226 {
227  ui->progressBarBattery->setValue(message);
228 }
229 
230 void MainWindow::setSerialStatus(const QString& message)
231 {
232  ui->serialStatus->setText(message);
233 }
234 
236 {
237  ui->labelSkiddingInfo->setStyleSheet("QLabel#labelSkiddingInfo {color: #FF0000;}");
238  ui->labelSkiddingInfo->setText(tr("Zwolnij przeszkoda jest blisko!"));
239 }
240 
242 {
243  ui->labelSkiddingInfo->setStyleSheet("QLabel#labelSkiddingInfo {color: #00AA00;}");
244  ui->labelSkiddingInfo->setText(tr("Jazda normalna!"));
245 }
246 
248 {
249  if(!_translator.isEmpty())
250  {
251  qApp->removeTranslator(&_translator);
252  }
253  ui->retranslateUi(this);
254 }
255 
257 {
258  if(_translator.load(":/i18n/roomba-app_en_US"))
259  {
260  qApp->installTranslator(&_translator);
261  }
262  ui->retranslateUi(this);
263 }
The class connecting main application and serial port.
const FrameBuffer & getFrameBuffer() const
Get a unmodifiable frame from the circular buffer.
FrameBuffer & useFrameBuffer()
Get a modifiable frame from the circular buffer.
void setDeviceName(const std::string &name)
Set the serial device name.
void endCommunication()
Set the end of communication flag to end communication.
bool isOpen() const
Check if the serial port is open.
bool isFrame() const
Check if there is any frame in a circular buffer.
Definition: frameBuffer.cpp:42
bool getFrame(std::string &frame)
Get one frame from the top of the list.
Definition: frameBuffer.cpp:30
Thread wrapper class for communicating with the serial device on a separate thread.
Definition: frameThread.hh:27
void setSerialStatus(const QString &message)
Signal emmitted to change connection status with the serial device on the status bar.
void info(const QString &info)
Signal emmitted to add informations to logs.
void error(const QString &error)
Signal emmitted to add errors on the status bar.
void obstacleDataReady(const std::map< float, int > &lidarData)
A slot to get a lidar data when the parser sent a signal that this data is ready.
Definition: glWidget.cpp:33
void rightWheelDirection(const int &direction)
A slot to get a right wheel spining direction.
Definition: glWidget.cpp:63
void leftWheelDirection(const int &direction)
A slot to get the left wheel spining direction.
Definition: glWidget.cpp:68
void movePos(const QPoint &coordinates)
Change the knop coordinates.
Definition: joystick.cpp:94
The class connecting all modules through the UI interface.
Definition: mainwindow.hh:41
void showErrorMessage(const QString &message)
Show error message as status bar message.
Definition: mainwindow.cpp:175
void closeEvent(QCloseEvent *event) override
Closing event function.
Definition: mainwindow.cpp:94
void on_actionDisconnect_triggered()
Slot triggered when the disconnecting signal was emitted.
Definition: mainwindow.cpp:108
FrameThread * _frameThread
Handle for communicating on different thread.
Definition: mainwindow.hh:221
void on_actionClose_triggered()
Slot triggered when the closing signal was emitted.
Definition: mainwindow.cpp:101
void setObstacleCloseStatus()
Set the close distance status with the obstacle on the motor information widget.
Definition: mainwindow.cpp:235
Communication * _communication
Communication handle.
Definition: mainwindow.hh:219
Ui::MainWindow * ui
Pointer to all UI widgets.
Definition: mainwindow.hh:218
void addValueGSpeedL(const QString &message)
Set the value of the left motor given speed.
Definition: mainwindow.cpp:200
void addValueSpeedR(const QString &message)
Set the value of the right motor speed.
Definition: mainwindow.cpp:210
void resetObstacleCloseStatus()
Set the normal distance status with the obstacle on the motor information widget.
Definition: mainwindow.cpp:241
void setSerialStatus(const QString &message)
Set the connection status with the serial port on the status bar.
Definition: mainwindow.cpp:230
void addMessageToLogs(const QString &message)
Add a message to the logs widget.
Definition: mainwindow.cpp:180
Parser * _parser
Serial data processor.
Definition: mainwindow.hh:222
void addValueSpeedL(const QString &message)
Set the value of the left motor speed.
Definition: mainwindow.cpp:195
void on_actionEnglish_US_triggered()
Change UI language to English (US).
Definition: mainwindow.cpp:256
MainWindow(QWidget *parent=nullptr)
Construct a new MainWindow object.
Definition: mainwindow.cpp:29
void addValueBattery(const int message)
Set the battery voltage value.
Definition: mainwindow.cpp:225
QTranslator _translator
Handle to translator object.
Definition: mainwindow.hh:223
void addDropShadow(QGroupBox *group, int xOffset, int yOffset, int radius)
Add drop shadow under given GroupBox widget.
Definition: mainwindow.cpp:187
void serialTimeout()
Update information read from the serial port.
Definition: mainwindow.cpp:154
void addValueAccelerationR(const QString &message)
Set the value of the right motor acceleration.
Definition: mainwindow.cpp:220
void on_actionPolish_triggered()
Change UI language to Polish.
Definition: mainwindow.cpp:247
QTimer _serialTimer
Serial data updating timer.
Definition: mainwindow.hh:220
void on_actionSearch_triggered()
Slot triggered when the searching signal was emitted.
Definition: mainwindow.cpp:114
~MainWindow()
Destroy the MainWindow object.
Definition: mainwindow.cpp:89
void connectSerialDevice(const QString &actionName)
Connect to the serial device.
Definition: mainwindow.cpp:136
void addValueAccelerationL(const QString &message)
Set the value of the left motor acceleration.
Definition: mainwindow.cpp:205
void addValueGSpeedR(const QString &message)
Set the value of the right motor given speed.
Definition: mainwindow.cpp:215
Parsing class for a processing data from the serial port frame.
Definition: parser.hh:29
void changedRightMotorAcceleration(const QString &accelerationValue)
A signal emitted when parsed data of the right motor acceleration is ready.
void setObstacleCloseStatus()
Set the Obstacle Close Status.
void changedLidar(const std::map< float, int > lidarData)
A signal emitted when parsed data of the lidar is ready.
void leftMotorDirection(const int &direction)
A signal emitted to pass wheel rotating direction to the openGL widget.
void changedLeftMotorGSpeed(const QString &speedValue)
A signal emitted when parsed data of the left motor given speed is ready.
void changedBattery(int battery)
A signal emitted when parsed data of the battery voltage is ready.
void processData()
Process data from a one frame.
Definition: parser.cpp:72
bool parseFrame(const std::string &frame)
Parse line from the serial port into managed variables.
Definition: parser.cpp:21
void changedRightMotorSpeed(const QString &speedValue)
A signal emitted when parsed data of the right motor speed is ready.
void changedLeftMotorSpeed(const QString &speedValue)
A signal emitted when parsed data of the left motor speed is ready.
void changedJoystick(const QPoint &coordinates)
A signal emitted when parsed data of a joystick swing is ready.
void changedRightMotorGSpeed(const QString &speedValue)
A signal emitted when parsed data of the right motor given speed is ready.
void resetObstacleCloseStatus()
Reset the Obstacle Close Status.
void rightMotorDirection(const int &direction)
A signal emitted to pass wheel rotating direction to the openGL widget.
void changedLeftMotorAcceleration(const QString &accelerationValue)
A signal emitted when parsed data of the left motor acceleration is ready.
MainWindow class declaration.
Definition: mainwindow.hh:32