-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawingman.cpp
More file actions
141 lines (111 loc) · 3.98 KB
/
Copy pathdrawingman.cpp
File metadata and controls
141 lines (111 loc) · 3.98 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
#include "drawingman.h"
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTimer>
DrawingMan::DrawingMan(QString appDir, QObject *parent) : QObject{parent} {
this->appDir = appDir;
channelFilePath = appDir + "/channel.csv";
// Connect signals for asynchronous reading
connect(&process, &QProcess::readyReadStandardOutput, this,
&DrawingMan::readOutput);
connect(&process, &QProcess::readyReadStandardError, this,
&DrawingMan::readError);
connect(&process, &QProcess::finished, this, &DrawingMan::processFinished);
connect(&timer, &QTimer::timeout, this, &DrawingMan::onTimeout);
}
void DrawingMan::runScript() {
QString runnerFile;
#ifdef Q_OS_WIN
runnerFile = "/runner.bat";
#else
runnerFile = "/runner.sh";
#endif
process.start(appDir + runnerFile, QStringList() << channelFilePath << QString::number(numSensors));
process.waitForStarted();
timer.start(10);
}
void DrawingMan::stopScript() { process.close(); }
void DrawingMan::readOutput() {
QProcess *process = qobject_cast<QProcess *>(sender());
if (process) {
QByteArray output = process->readAllStandardOutput();
// Process the output as needed
qDebug() << "Output: " << output;
}
}
void DrawingMan::readError() {
QProcess *process = qobject_cast<QProcess *>(sender());
if (process) {
QByteArray error = process->readAllStandardError();
// Process the error as needed
qDebug() << "Error: " << error;
}
}
void DrawingMan::processFinished(int exitCode,
QProcess::ExitStatus exitStatus) {
QProcess *process = qobject_cast<QProcess *>(sender());
if (process) {
// Process has finished
qDebug() << "Process finished with exit code: " << exitCode;
qDebug() << "Exit status: " << exitStatus;
}
QFile::remove(channelFilePath);
timer.stop();
}
void DrawingMan::onTimeout() {
// Writing to the text file
QFile fileOut(channelFilePath);
if (fileOut.open(QIODevice::Append | QIODevice::Text)) {
QTextStream out(&fileOut);
// // Parse JSON string
// QJsonDocument jsonDocument =
// QJsonDocument::fromJson(latestJsonReading.toUtf8());
// // Check if the document is an object
// if (!jsonDocument.isObject()) {
// qDebug() << "Invalid JSON object." << jsonDocument;
// return;
// }
// // Get the JSON object
// QJsonObject jsonObject = jsonDocument.object();
// QStringList keys = jsonObject.keys();
// // Create arrays
// QList<double> valArray;
// // Iterate through keys
// for (const QString &key : keys) {
// // Get the value for each key
// double value = QString(jsonObject.value(key).toString()).toDouble();
// if(value < 0) value *= -1;
// valArray.append(value * 100);
// }
QList<double> valArray;
valArray << rng.bounded(400) << rng.bounded(400) << rng.bounded(400) << rng.bounded(400) << rng.bounded(400) << rng.bounded(400);
// sensor 1, Center Front
out << "0," << valArray[0] + 20;
out << ",";
// sensor 2, Center Right
out << valArray[1] + 10 << ",0";
out << ",";
// sensor 3, Center Left
out << -valArray[2] - 10 << ",0";
out << ",";
// sensor 4, Center Back
out << "0," << -valArray[3] - 20;
out << ",";
// sensor 5, Left Back
out << -valArray[4] - 10 << "," << -valArray[4] - 20;
out << ",";
// sensor 6, Right Back
out << valArray[5] + 10 << "," << -valArray[5] - 20;
out << ",";
// Constant Translation and rotation
out << 5 << "," << 10;
out << '\n';
} else {
qDebug() << "Failed to open the file for writing.";
}
}
void DrawingMan::updateLatestReading(QString message) {
latestJsonReading = message;
}