-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
201 lines (141 loc) · 5.35 KB
/
mainwindow.cpp
File metadata and controls
201 lines (141 loc) · 5.35 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPort>
#include<QSerialPortInfo>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/QmlMap.qml")));
ui->quickWidget->show();
serialPort = new QSerialPort(this);
// Find the correct serial port name for your Arduino Mega
foreach(const QSerialPortInfo &port, QSerialPortInfo::availablePorts()) {
qDebug() << port.description();
if (port.description().contains("Arduino Mega 2560")) {
serialPort->setPort(port);
serialPort->setBaudRate(QSerialPort::Baud57600);
break;
}
}
if (serialPort->open(QIODevice::ReadOnly)) {
qDebug() << "Serial port opened successfully.";
} else {
qDebug() << "Failed to open serial port: " << serialPort->errorString();
}
// Create a timer to check for data every 1000 milliseconds (1 second)
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::checkForData);
timer->start(200); // Start the timer
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow :: timerEvent(QTimerEvent *event)
{
QMainWindow :: timerEvent(event);
ui->graphicsView_AttitudeIndicator->redraw();
}
void MainWindow::on_doubleSpinBoxPitch_valueChanged(double arg1)
{
// ui->graphicsEADI->setPitch(arg1);
// ui->graphicsEADI->redraw();
// ui->doubleSpinBoxPitch->setValue(25);
ui->graphicsView_AttitudeIndicator->setPitch(arg1);
ui->graphicsView_AttitudeIndicator->redraw();
ui->statusbar->showMessage("Pitch value is changing");
}
void MainWindow::on_doubleSpinBoxRoll_valueChanged(double arg1)
{
ui->graphicsView_AttitudeIndicator->setRoll(arg1);
ui->graphicsView_AttitudeIndicator->redraw();
ui->statusbar->showMessage("Pitch value is changing");
}
void MainWindow::on_doubleSpinBoxAltitude_valueChanged(double arg1)
{
ui->graphicsView_AttitudeIndicator->setAltitude(arg1);
ui->graphicsView_AttitudeIndicator->redraw();
ui->statusbar->showMessage("Altitude is changing");
}
void MainWindow::on_doubleSpinBoxHeading_valueChanged(double arg1)
{
ui->graphicsView_AttitudeIndicator->setHeading(arg1);
// ui->graphicsView->setAirspeed(arg1);
ui->graphicsView_AttitudeIndicator->redraw();
ui->statusbar->showMessage("Plane is moving forward");
}
void MainWindow::on_doubleSpinBoxHeadingSpeed_valueChanged(double arg1)
{
ui->graphicsView_AttitudeIndicator->setAirspeed(arg1);
// ui->graphicsView->set
ui->graphicsView_AttitudeIndicator->redraw();
ui->statusbar->showMessage("Plane speed is changing");
}
void MainWindow::on_doubleSpinBoxTR_valueChanged(double arg1)
{
ui->graphicsView_TurnCoord->setTurnRate(arg1);
ui->graphicsView_TurnCoord->redraw();
// ui->graphicsView_TC
}
void MainWindow::checkForData()
{
if (serialPort->bytesAvailable() > 0) {
// Data is available to be read, so call readData to process it
readData();
}
}
void MainWindow::readData()
{
// =====================================================================================================
QByteArray data = serialPort->readLine();
QString dataString = QString::fromUtf8(data);
// bool ok;
// double gyroValue = dataString.toDouble(&ok); // This is for only one value
qDebug() << dataString;
// if (ok) {
// // Update the QDoubleSpinBox with the received gyroValue
// ui->graphicsView->setHeading(gyroValue);
// }
// ========================================================================================================
QList<QByteArray> values = data.split(',');
if (values.size() == 6) {
gyroX = values[0].toDouble();
gyroY = values[1].toDouble();
gyroZ = values[2].toDouble();
accX = values[3].toDouble();
accY = values[4].toDouble();
accZ = values[5].toDouble();
accelerationX = (signed int)(((signed int)accX) * 3.9);
accelerationY = (signed int)(((signed int)accY) * 3.9);
accelerationZ = (signed int)(((signed int)accZ) * 3.9);
// Note: M_PI = 3.14159265358979323846 it is constant defined in math.h
pitch = 180 * atan (accelerationX/sqrt(accelerationY*accelerationY + accelerationZ*accelerationZ))/M_PI;
roll = atan2(accelerationY, accelerationZ) * 180.0 / M_PI;
yaw = atan2(accelerationZ, accelerationX)*180.0 / M_PI - 90.0;
double gyro_sensitivity_x = 250;
double gyro_bias_x = 0.5;
/* yaw = (gyroX+gyroY+gyroZ)*0.2;
currYaw = prevYaw + yaw;
turnRate = yaw/0.2;
prevYaw = currYaw;
*/
// turnRate = (gyroZ/gyro_sensitivity_x) - gyro_bias_x;
// Update UI elements here with the received data
ui->doubleSpinBoxPitch->setValue(pitch);
ui->doubleSpinBoxRoll->setValue(roll);
ui->doubleSpinBoxHeading->setValue(yaw);
ui->doubleSpinBoxTR->setValue(gyroX);
ui->doubleSpinBoxHeadingSpeed->setValue(accelerationX);
// ui->doubleSpinBoxTR->setValue(turnRate);
}
}
/*
void MainWindow::on_doubleSpinBox_Yaw_valueChanged(double arg1)
{
ui->doubleSpinBoxheading->setValue(arg1);
ui->graphicsView->redraw();
ui->statusbar->showMessage("Yaw is changing");
}
*/