From 530448915faafbc9340175f76f6e79551fde6844 Mon Sep 17 00:00:00 2001
From: zeisele <zeisele@iastate.edu>
Date: Mon, 28 Mar 2022 02:54:40 +0200
Subject: [PATCH] allow users to log data

---
 .../gui/MicroCART/crazyflieworker.cpp         | 37 ++++++++
 groundStation/gui/MicroCART/crazyflieworker.h |  3 +
 groundStation/gui/MicroCART/logworker.cpp     | 42 +++++++-
 groundStation/gui/MicroCART/logworker.h       |  5 +
 groundStation/gui/MicroCART/mainwindow.cpp    | 95 +++++++++++++------
 groundStation/gui/MicroCART/mainwindow.h      |  8 +-
 6 files changed, 158 insertions(+), 32 deletions(-)

diff --git a/groundStation/gui/MicroCART/crazyflieworker.cpp b/groundStation/gui/MicroCART/crazyflieworker.cpp
index b34028768..199cc1c95 100644
--- a/groundStation/gui/MicroCART/crazyflieworker.cpp
+++ b/groundStation/gui/MicroCART/crazyflieworker.cpp
@@ -545,4 +545,41 @@ void CrazyflieWorker::logBlockBoxChanged(QString text) {
         }
     }
     emit(gotBlockisEnabled(enabled));
+}
+
+bool compare_id(const struct logBlock& first, const struct logBlock& second) {
+	return (first.id < second.id);
+}
+
+std::vector<int8_t> CrazyflieWorker::getLogColumns(QString log1, QString log2, QString log3, QString log4, QString log5) {
+    std::vector<int8_t> columns(5, -1);
+    int column = 1;
+    std::sort(logBlocks.begin(), logBlocks.end(), compare_id);
+    std::string element;
+    for (int i = 0; i < logBlocks.size(); i++) {
+		struct logBlock lbCurrent = logBlocks.at(i);
+		if(lbCurrent.isEnabled) {
+			for(int j = 0; j < lbCurrent.variableNames.size(); j++) {
+				struct toc_info teCurrent = lbCurrent.variableNames.at(j);
+				element = teCurrent.strGroup + "." + teCurrent.strName;
+				if(!element.compare(log1.toStdString())) {
+                    columns[0] = column;
+                }
+                else if(!element.compare(log2.toStdString())) {
+                    columns[1] = column;
+                }
+                else if(!element.compare(log3.toStdString())) {
+                    columns[2] = column;
+                }
+                else if(!element.compare(log4.toStdString())) {
+                    columns[3] = column;
+                }
+                else if(!element.compare(log5.toStdString())) {
+                    columns[4] = column;
+                }
+                column++;
+			}
+		}			
+	}
+    return columns;
 }
\ No newline at end of file
diff --git a/groundStation/gui/MicroCART/crazyflieworker.h b/groundStation/gui/MicroCART/crazyflieworker.h
index a68502486..5d3ee3f28 100644
--- a/groundStation/gui/MicroCART/crazyflieworker.h
+++ b/groundStation/gui/MicroCART/crazyflieworker.h
@@ -99,6 +99,9 @@ private:
     std::vector<struct toc_group> logGroups;
     std::vector<struct logBlock> logBlocks;
     bool isBackendConnected = false;
+
+public:
+    std::vector<int8_t> getLogColumns(QString log1, QString log2, QString log3, QString log4, QString log5);
 };
 
 #endif // CRAZYFLIEWORKER_H
diff --git a/groundStation/gui/MicroCART/logworker.cpp b/groundStation/gui/MicroCART/logworker.cpp
index 37d3f98aa..c96f67056 100644
--- a/groundStation/gui/MicroCART/logworker.cpp
+++ b/groundStation/gui/MicroCART/logworker.cpp
@@ -49,6 +49,11 @@ void LogWorker::startLogging(QString filename) {
     logFile.open(logFileName.c_str());
     //std::streamoff p = 0;
     std::string line;
+    std::vector<std::string> tokens;
+    std::string temp;
+    std::stringstream stream;
+    QStringList results;
+    QString wantedLine;
 
     // while(true) {
     //     logFile.seekg(p);
@@ -67,13 +72,44 @@ void LogWorker::startLogging(QString filename) {
 
     while(true) {
         while(std::getline(logFile, line)) {
-            QString result(line.c_str());
-            qInfo() << result;
+            //QString result(line.c_str());
+            //qInfo() << result;
+            if(logFlag == 0) {
+                usleep(1000);
+                continue;
+            }
+            else if(logFlag == 1) {
+                stream.str(line);
+                while(getline(stream, temp, '\t')) {
+                    tokens.push_back(temp);
+                }
+                wantedLine.append(tokens.at(0).c_str());
+                wantedLine.append(",");
+                for(int i = 0; i < 5; i++) {
+                    if(logColumns.at(i) == -1) {
+                        wantedLine.append("NULL,");
+                    }
+                    else {
+                        wantedLine.append(tokens.at(logColumns.at(i)).c_str());
+                        wantedLine.append(",");
+                    }
+                }
+                results.append(wantedLine);
+                wantedLine = "";
+                tokens.clear();
+                stream.str("");
+                stream.clear();
+            }
+            else if(logFlag == 2) {
+                emit(gotLogData(results));
+                results.clear();
+                logFlag = 0;
+            }
         }
         if(!logFile.eof()) {
             break;
         }
         logFile.clear();
-        usleep(500000);
+        usleep(150000);
     }
 }
\ No newline at end of file
diff --git a/groundStation/gui/MicroCART/logworker.h b/groundStation/gui/MicroCART/logworker.h
index a59d06b90..88525b2b4 100644
--- a/groundStation/gui/MicroCART/logworker.h
+++ b/groundStation/gui/MicroCART/logworker.h
@@ -28,6 +28,7 @@ public:
     
 
 signals:
+    void gotLogData(QStringList results);
     void connected();
     void disconnected();
 
@@ -39,6 +40,10 @@ public slots:
 
 private:
     struct backend_conn * conn;
+
+public:
+    int logFlag = 0;
+    std::vector<int8_t> logColumns;
 };
 
 #endif // LOGWORKER_H
diff --git a/groundStation/gui/MicroCART/mainwindow.cpp b/groundStation/gui/MicroCART/mainwindow.cpp
index 5e5759df9..d457ed40d 100644
--- a/groundStation/gui/MicroCART/mainwindow.cpp
+++ b/groundStation/gui/MicroCART/mainwindow.cpp
@@ -116,7 +116,7 @@ MainWindow::MainWindow(QWidget *parent) :
     connect(ui->logEntriesComboBox3, SIGNAL (currentTextChanged(QString)), this, SLOT (getLogEntryBoxChange3(QString)));
     connect(ui->logEntriesComboBox4, SIGNAL (currentTextChanged(QString)), this, SLOT (getLogEntryBoxChange4(QString)));
     connect(ui->logEntriesComboBox5, SIGNAL (currentTextChanged(QString)), this, SLOT (getLogEntryBoxChange5(QString)));
-    connect(ui->pb_startLog, SIGNAL (clicked()), logWorker, SLOT (getLogValues(QString, QString, QString, QString, QString)));
+    connect(ui->pb_startLog, SIGNAL (clicked()), this, SLOT (sendStartLog()));
     connect(logWorker, SIGNAL (gotLogValues(QStringList)), this, SLOT (graphLogs(QStringList)));
     connect(ui->pb_logBlockFile, SIGNAL (clicked()), this, SLOT(openLogBlockFile()));
     connect(ui->pb_logBlockRefresh, SIGNAL (clicked()), this, SLOT(on_pb_lbRefresh()));
@@ -124,7 +124,9 @@ MainWindow::MainWindow(QWidget *parent) :
     connect(ui->pb_logBlockResume, SIGNAL (clicked()), this, SLOT(on_pb_lbResume()));
     connect(ui->pb_logBlockPause, SIGNAL (clicked()), this, SLOT(on_pb_lbPause()));
     connect(ui->comboBox_logBlocks, SIGNAL (currentTextChanged(QString)), crazyflieWorker, SLOT (logBlockBoxChanged(QString)));
+    connect(ui->pb_stopLog, SIGNAL (clicked()), this, SLOT (sendStopLog()));
     connect(crazyflieWorker, SIGNAL (gotBlockisEnabled(bool)), this, SLOT(setLogButtons(bool)));
+    connect(logWorker, SIGNAL (gotLogData(QStringList)), this, SLOT(graphLogData(QStringList)));
     //connect(ui->pb_logBlockDelete, SIGNAL (clicked()), this, SLOT(on_pb_lbDelete()));
     connect(this, SIGNAL(sendLogBlockCommand(int8_t, int8_t)), crazyflieWorker, SLOT(frontendLogBlockCommand(int8_t, int8_t)));
     connect(this, SIGNAL (requestLogBlockID(QString, int8_t)), crazyflieWorker, SLOT(findLogBlockID(QString, int8_t)));
@@ -1057,7 +1059,7 @@ void MainWindow::on_gamepadPitchScale_valueChanged(int arg1)
 
 void MainWindow::graphLogs(QStringList logs)
 {   
-    qInfo() << "start" << endl;
+    // qInfo() << "start" << endl;
 
     ui->dataPlot->addGraph();
     ui->dataPlot->addGraph();
@@ -1071,7 +1073,7 @@ void MainWindow::graphLogs(QStringList logs)
     ui->dataPlot->graph(3)->setPen(QPen(Qt::black, 2)); 
     ui->dataPlot->graph(4)->setPen(QPen(Qt::yellow, 2)); 
     
-    qInfo() << "color" << endl;
+    // qInfo() << "color" << endl;
 
     //parse data and put into vector
     QVector<double> x0(logs.size()), y0(logs.size());
@@ -1080,17 +1082,18 @@ void MainWindow::graphLogs(QStringList logs)
     QVector<double> x3(logs.size()), y3(logs.size());
     QVector<double> x4(logs.size()), y4(logs.size());
 
-    qInfo() << "vector size" << endl;
+    // qInfo() << "vector size" << endl;
 
     QVector<std::string> tempConv;
     
     for(int i=0; i<logs.size(); i++) {
-        qInfo() << "i:" << i << endl;
+        // qInfo() << "i:" << i << endl;
         tempConv.append(logs.at(i).toStdString());
     }
-    qInfo() << QString::fromStdString(tempConv.at(0)) << endl;
-    qInfo() << QString::fromStdString(tempConv.at(1)) << endl;
+    // qInfo() << QString::fromStdString(tempConv.at(0)) << endl;
+    // qInfo() << QString::fromStdString(tempConv.at(1)) << endl;
 
+    float xmin = INFINITY;
     float xmax = -INFINITY;
     float ymax = -INFINITY;
     float ymin = INFINITY;
@@ -1109,14 +1112,14 @@ void MainWindow::graphLogs(QStringList logs)
             tokens.push_back(intermediate);
         }
         
-        qInfo() << logs.size() << endl;
-        qInfo() << "tokens " << i << " :\n " << endl;
-        qInfo() << QString::fromStdString(tokens[0]) << endl;
-        qInfo() << QString::fromStdString(tokens[1]) << endl;
-        qInfo() << QString::fromStdString(tokens[2]) << endl;
-        qInfo() << QString::fromStdString(tokens[3]) << endl;
-        qInfo() << QString::fromStdString(tokens[4]) << endl;
-        qInfo() << QString::fromStdString(tokens[5]) << endl;
+        // qInfo() << logs.size() << endl;
+        // qInfo() << "tokens " << i << " :\n " << endl;
+        // qInfo() << QString::fromStdString(tokens[0]) << endl;
+        // qInfo() << QString::fromStdString(tokens[1]) << endl;
+        // qInfo() << QString::fromStdString(tokens[2]) << endl;
+        // qInfo() << QString::fromStdString(tokens[3]) << endl;
+        // qInfo() << QString::fromStdString(tokens[4]) << endl;
+        // qInfo() << QString::fromStdString(tokens[5]) << endl;
 
         //assign x and y values for log variables
         if(tokens[1] != "NULL") {
@@ -1131,12 +1134,15 @@ void MainWindow::graphLogs(QStringList logs)
             if(i == logs.size()-1) {
                 xmax = x0[i];
             }
+            if(i == 0) {
+                xmin = x0[i];
+            }
         }
         else {
             log0 = false;
         }
 
-        qInfo() << ymin << endl;
+        // qInfo() << ymin << endl;
 
         if(tokens[2] != "NULL") {
             x1[i] = std::stod(tokens[0]);
@@ -1150,6 +1156,9 @@ void MainWindow::graphLogs(QStringList logs)
             if(i == logs.size()-1) {
                 xmax = x1[i];
             }
+            if(i == 0) {
+                xmin = x0[i];
+            }
         }
         else {
             log1 = false;
@@ -1166,6 +1175,9 @@ void MainWindow::graphLogs(QStringList logs)
             if(i == logs.size()-1) {
                 xmax = x2[i];
             }
+            if(i == 0) {
+                xmin = x0[i];
+            }
         }
         else {
             log2 = false;
@@ -1182,6 +1194,9 @@ void MainWindow::graphLogs(QStringList logs)
             if(i == logs.size()-1) {
                 xmax = x3[i];
             }
+            if(i == 0) {
+                xmin = x0[i];
+            }
         }
         else {
             log3 = false;
@@ -1198,18 +1213,21 @@ void MainWindow::graphLogs(QStringList logs)
             if(i == logs.size()-1) {
                 xmax = x4[i];
             }
+            if(i == 0) {
+                xmin = x0[i];
+            }
         }
         else {
             log4 = false;
         }
-        qInfo() << "i: " << i << endl;
+        // qInfo() << "i: " << i << endl;
     }
 
-    qInfo() << "yMin: " << ymin << endl;
-    qInfo() << "yMax: " << ymax << endl;
-    qInfo() << "xMax: " << xmax << endl;
-    qInfo() << x0 << endl;
-    qInfo() << y0 << endl;
+    // qInfo() << "yMin: " << ymin << endl;
+    // qInfo() << "yMax: " << ymax << endl;
+    // qInfo() << "xMax: " << xmax << endl;
+    // qInfo() << x0 << endl;
+    // qInfo() << y0 << endl;
 
     //start graph and set data
     
@@ -1232,9 +1250,10 @@ void MainWindow::graphLogs(QStringList logs)
     //set lables and boundaries
     ui->dataPlot->xAxis->setLabel("Time");
     ui->dataPlot->yAxis->setLabel("Value");
-    ui->dataPlot->xAxis->setRange(0, xmax);
+    ui->dataPlot->xAxis->setRange(xmin, xmax);
     ui->dataPlot->yAxis->setRange(ymin, ymax);
     ui->dataPlot->replot();
+    ui->dataPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
 }
 
 void MainWindow::openLogBlockFile() {
@@ -1456,14 +1475,34 @@ void MainWindow::getLogEntryBoxChange5(QString text) {
     }
 }
 
-void MainWindow::on_pb_stopLog_clicked()
+void MainWindow::graphLogData(QStringList results)
 {
+    graphLogs(results);
+}
+
+// void MainWindow::on_pb_startLog_clicked()
+// {
     
-    qInfo() << "Hello?" << endl;
-    QStringList test = {"0,10,NULL,NULL,NULL,25","1,210,NULL,NULL,NULL,-10"};
-    graphLogs(test);
+//     qInfo() << "Hello?" << endl;
+//     QStringList test = {"0,10,NULL,NULL,NULL,25","1,210,NULL,NULL,NULL,-10"};
+//     graphLogs(test);
+
+// }
 
-}
 void MainWindow::receiveLogFile(QString filename) {
     emit(beginReadingLogFile(filename));
 }
+
+void MainWindow::sendStartLog() {
+    QString name1 = ui->logEntriesComboBox1->currentText();
+    QString name2 = ui->logEntriesComboBox2->currentText();
+    QString name3 = ui->logEntriesComboBox3->currentText();
+    QString name4 = ui->logEntriesComboBox4->currentText();
+    QString name5 = ui->logEntriesComboBox5->currentText();
+    logWorker->logColumns = crazyflieWorker->getLogColumns(name1, name2, name3, name4, name5);
+    logWorker->logFlag = 1;
+}
+
+void MainWindow::sendStopLog() {
+    logWorker->logFlag = 2;
+}
\ No newline at end of file
diff --git a/groundStation/gui/MicroCART/mainwindow.h b/groundStation/gui/MicroCART/mainwindow.h
index f6ac9d2dc..345a41ebc 100644
--- a/groundStation/gui/MicroCART/mainwindow.h
+++ b/groundStation/gui/MicroCART/mainwindow.h
@@ -175,7 +175,11 @@ private slots:
     void receiveLogFile(QString filename);
 
 
-    void on_pb_stopLog_clicked();
+    //void on_pb_stopLog_clicked();
+
+    void sendStartLog();
+    void sendStopLog();
+    void graphLogData(QStringList results);
 
 private:
     Ui::MainWindow *ui;
@@ -196,6 +200,8 @@ private:
     GamepadMonitor *gamepadMonitor;
     CrazyflieWorker *crazyflieWorker;
     LogWorker *logWorker;
+public:
+    int logFlag = 0;
 };
 
 #endif // MAINWINDOW_H
-- 
GitLab