From 5e34ebe508a67016f0f259db1e15bb5326d49d3b Mon Sep 17 00:00:00 2001
From: zeisele <zeisele@iastate.edu>
Date: Tue, 29 Mar 2022 01:29:49 +0200
Subject: [PATCH] add real time logging

---
 crazyflie_groundstation/src/CTOC.cpp          |   3 +-
 .../src/ccrazyradio/CCrazyRadio_loopFuncs.cpp |   2 +-
 .../gui/MicroCART/crazyflieworker.cpp         |   1 -
 groundStation/gui/MicroCART/logworker.cpp     |  31 +++-
 groundStation/gui/MicroCART/logworker.h       |   2 +-
 groundStation/gui/MicroCART/mainwindow.cpp    | 167 +++++++++---------
 groundStation/gui/MicroCART/mainwindow.h      |  15 +-
 7 files changed, 127 insertions(+), 94 deletions(-)

diff --git a/crazyflie_groundstation/src/CTOC.cpp b/crazyflie_groundstation/src/CTOC.cpp
index 039b0d951..745e05ff5 100644
--- a/crazyflie_groundstation/src/CTOC.cpp
+++ b/crazyflie_groundstation/src/CTOC.cpp
@@ -493,10 +493,9 @@ bool CTOC::startLogging(std::string strName, std::string strBlockName) {
 			if (crtpReceived) {
 				delete crtpReceived;
 			}
+			this->addElementToBlock(lbCurrent.nID, teCurrent.nID);
 
 			if (bCreateOK) {
-				this->addElementToBlock(lbCurrent.nID, teCurrent.nID);
-
 				return true;
 			} else {
 				std::cout << "Unable to add element " << strName << " to " << strBlockName << std::endl;
diff --git a/crazyflie_groundstation/src/ccrazyradio/CCrazyRadio_loopFuncs.cpp b/crazyflie_groundstation/src/ccrazyradio/CCrazyRadio_loopFuncs.cpp
index 5772cf441..1d2d7d0e1 100644
--- a/crazyflie_groundstation/src/ccrazyradio/CCrazyRadio_loopFuncs.cpp
+++ b/crazyflie_groundstation/src/ccrazyradio/CCrazyRadio_loopFuncs.cpp
@@ -23,7 +23,7 @@ void CCrazyRadio::mainLoop() {
 			(*it)->writeLogData();
 		}
 
-		usleep(500);
+		//usleep(100);
 
 		// Check if the thread has been told to terminate
 		if (this->m_exitThread) {
diff --git a/groundStation/gui/MicroCART/crazyflieworker.cpp b/groundStation/gui/MicroCART/crazyflieworker.cpp
index 5412e4d8b..85d57d5c6 100644
--- a/groundStation/gui/MicroCART/crazyflieworker.cpp
+++ b/groundStation/gui/MicroCART/crazyflieworker.cpp
@@ -590,7 +590,6 @@ std::vector<int8_t> CrazyflieWorker::getLogColumns(QString log1, QString log2, Q
 				struct toc_info teCurrent = lbCurrent.variableNames.at(j);
 				element = teCurrent.strGroup + "." + teCurrent.strName;
 				if(!element.compare(log1.toStdString())) {
-                    qInfo() << column;
                     columns[0] = column;
                 }
                 else if(!element.compare(log2.toStdString())) {
diff --git a/groundStation/gui/MicroCART/logworker.cpp b/groundStation/gui/MicroCART/logworker.cpp
index 50ae9b26c..4cdfccdac 100644
--- a/groundStation/gui/MicroCART/logworker.cpp
+++ b/groundStation/gui/MicroCART/logworker.cpp
@@ -46,7 +46,7 @@ void LogWorker::disconnectBackend()
 void LogWorker::startLogging(QString filename) {
     std::string logFileName = filename.toStdString();
     std::ifstream logFile;
-    logFile.open(logFileName.c_str());
+    logFile.open(logFileName.c_str(), std::fstream::ate);
     //std::streamoff p = 0;
     std::string line;
     std::vector<std::string> tokens;
@@ -54,6 +54,8 @@ void LogWorker::startLogging(QString filename) {
     std::stringstream stream;
     QStringList results;
     QString wantedLine;
+    uint8_t lineCnt = 0;
+    uint8_t nanCnt = 0;
 
     // while(true) {
     //     logFile.seekg(p);
@@ -77,8 +79,12 @@ void LogWorker::startLogging(QString filename) {
             if(logFlag == 0) {
                 usleep(1000);
                 continue;
+            }
+            else if(line.at(0) == '#') {
+
             }
             else if(logFlag == 1) {
+                lineCnt++;
                 stream.str(line);
                 while(getline(stream, temp, '\t')) {
                     tokens.push_back(temp);
@@ -90,21 +96,36 @@ void LogWorker::startLogging(QString filename) {
                         wantedLine.append("NULL,");
                     }
                     else {
-                        qInfo() << "append";
                         wantedLine.append(tokens.at(logColumns.at(i)).c_str());
                         wantedLine.append(",");
-                        qInfo() << "append2";
+                        if(!(tokens.at(logColumns.at(i)).compare("nan"))) {
+                            nanCnt++;
+                        }
                     }
                 }
-                qInfo() << "results";
+                if(nanCnt >= 5) {
+                    nanCnt = 0;
+                    wantedLine = "";
+                    tokens.clear();
+                    stream.str("");
+                    stream.clear();
+                    lineCnt--;
+                    continue;
+                }
                 results.append(wantedLine);
                 wantedLine = "";
                 tokens.clear();
                 stream.str("");
                 stream.clear();
+                nanCnt = 0;
+                if(lineCnt > 8) {
+                    emit(gotLogData(results, false));
+                    lineCnt = 0;
+                    results.clear();
+                }  
             }
             else if(logFlag == 2) {
-                emit(gotLogData(results));
+                emit(gotLogData(results, true));
                 results.clear();
                 logFlag = 0;
             }
diff --git a/groundStation/gui/MicroCART/logworker.h b/groundStation/gui/MicroCART/logworker.h
index 88525b2b4..667e229c6 100644
--- a/groundStation/gui/MicroCART/logworker.h
+++ b/groundStation/gui/MicroCART/logworker.h
@@ -28,7 +28,7 @@ public:
     
 
 signals:
-    void gotLogData(QStringList results);
+    void gotLogData(QStringList results, bool lastSet);
     void connected();
     void disconnected();
 
diff --git a/groundStation/gui/MicroCART/mainwindow.cpp b/groundStation/gui/MicroCART/mainwindow.cpp
index 27a7dd3fb..3e5529b4c 100644
--- a/groundStation/gui/MicroCART/mainwindow.cpp
+++ b/groundStation/gui/MicroCART/mainwindow.cpp
@@ -84,7 +84,7 @@ MainWindow::MainWindow(QWidget *parent) :
     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()), this, SLOT (sendStartLog()));
-    connect(logWorker, SIGNAL (gotLogValues(QStringList)), this, SLOT (graphLogs(QStringList)));
+    connect(logWorker, SIGNAL (gotLogValues(QStringList)), this, SLOT (graphLogs(QStringList, bool)));
     connect(ui->pb_logBlockFile, SIGNAL (clicked()), this, SLOT(openLogBlockFile()));
     connect(ui->pb_logBlockRefresh, SIGNAL (clicked()), this, SLOT(on_pb_lbRefresh()));
     connect(ui->pb_logBlockStop, SIGNAL (clicked()), this, SLOT(on_pb_lbStop()));
@@ -93,7 +93,7 @@ MainWindow::MainWindow(QWidget *parent) :
     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(logWorker, SIGNAL (gotLogData(QStringList, bool)), this, SLOT(graphLogData(QStringList, bool)));
     //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)));
@@ -630,7 +630,7 @@ void MainWindow::on_gamepadPitchScale_valueChanged(int arg1)
     ui->pitchVizBar->setMaximum(std::abs(arg1));
 }
 
-void MainWindow::graphLogs(QStringList logs)
+void MainWindow::graphLogs(QStringList logs, bool lastSet)
 {   
     ui->dataPlot->addGraph();
     ui->dataPlot->addGraph();
@@ -652,12 +652,11 @@ void MainWindow::graphLogs(QStringList logs)
     ui->logEntriesComboBox5->setStyleSheet("QComboBox { color: darkMagenta; }");
     
     //parse data and put into vector
-    QVector<double> x0(logs.size()), y0(logs.size());
-    QVector<double> x1(logs.size()), y1(logs.size());
-    QVector<double> x2(logs.size()), y2(logs.size());
-    QVector<double> x3(logs.size()), y3(logs.size());
-    QVector<double> x4(logs.size()), y4(logs.size());
-
+    QVector<double> x0, y0;
+    QVector<double> x1, y1;
+    QVector<double> x2, y2;
+    QVector<double> x3, y3;
+    QVector<double> x4, y4;
     QVector<std::string> tempConv;
     
     for(int i=0; i<logs.size(); i++) {
@@ -666,10 +665,6 @@ void MainWindow::graphLogs(QStringList logs)
     // 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;
     bool log0 = true;
     bool log1 = true;
     bool log2 = true;
@@ -694,6 +689,13 @@ void MainWindow::graphLogs(QStringList logs)
         // qInfo() << QString::fromStdString(tokens[4]) << endl;
         // qInfo() << QString::fromStdString(tokens[5]) << endl;
 
+        if(i == 0 && std::stod(tokens[0]) < xmin) {
+            xmin = std::stod(tokens[0]);
+        }
+        if(i == logs.size() - 1 && std::stod(tokens[0]) > xmax) {
+            xmax = std::stod(tokens[0]);
+        }
+
         //assign x and y values for log variables
         if(tokens[1] == "nan"){
             //do not include point
@@ -701,22 +703,15 @@ void MainWindow::graphLogs(QStringList logs)
             //disable this variable if encounter "NULL"
             log0 = false;
         }else {
-            x0[i] = std::stod(tokens[0]);
-            y0[i] = std::stod(tokens[1]);
-            if(y0[i]<ymin) {
-                ymin = y0[i];
+            x0.append(std::stod(tokens[0]));
+            y0.append(std::stod(tokens[1]));
+            if(y0.back()<ymin) {
+                ymin = y0.back(); 
             }
-            if(y0[i]>ymax) {
-                ymax = y0[i];
-            }
-            if(i == logs.size()-1) {
-                xmax = x0[i];
-            }
-            if(i == 0) {
-                xmin = x0[i];
+            if(y0.back()>ymax) {
+                ymax = y0.back();
             }
         }
-        qInfo() << ymin << endl;
 
         if(tokens[2] == "nan"){
             //do not include point
@@ -724,19 +719,13 @@ void MainWindow::graphLogs(QStringList logs)
             //disable this variable if encounter "NULL"
             log1 = false;
         }else {
-            x1[i] = std::stod(tokens[0]);
-            y1[i] = std::stod(tokens[2]);
-            if(y1[i]<ymin) {
-                ymin = y1[i];
-            }
-            if(y1[i]>ymax) {
-                ymax = y1[i];
+            x1.append(std::stod(tokens[0]));
+            y1.append(std::stod(tokens[2]));
+            if(y1.back()<ymin) {
+                ymin = y1.back();
             }
-            if(i == logs.size()-1) {
-                xmax = x1[i];
-            }
-            if(i == 0) {
-                xmin = x0[i];
+            if(y1.back()>ymax) {
+                ymax = y1.back();
             }
         }
 
@@ -746,20 +735,14 @@ void MainWindow::graphLogs(QStringList logs)
             //disable this variable if encounter "NULL"
             log2 = false;
         }else {
-            x2[i] = std::stod(tokens[0]);
-            y2[i] = std::stod(tokens[3]);
-            if(y2[i]<ymin) {
-                ymin = y2[i];
+            x2.append(std::stod(tokens[0]));
+            y2.append(std::stod(tokens[3]));
+            if(y2.back()<ymin) {
+                ymin = y2.back();
              }
-             if(y2[i]>ymax) {
-                ymax = y2[i];
+             if(y2.back()>ymax) {
+                ymax = y2.back();
              }
-            if(i == logs.size()-1) {
-                xmax = x2[i];
-            }
-            if(i == 0) {
-                xmin = x0[i];
-            }
         }
 
         if(tokens[4] == "nan"){
@@ -768,43 +751,30 @@ void MainWindow::graphLogs(QStringList logs)
             //disable this variable if encounter "NULL"
             log3 = false;
         }else {
-            x3[i] = std::stod(tokens[0]);
-            y3[i] = std::stod(tokens[4]);
-            if(y3[i]<ymin) {
-                ymin = y3[i];
+            x3.append(std::stod(tokens[0]));
+            y3.append(std::stod(tokens[4]));
+            if(y3.back()<ymin) {
+                ymin = y3.back();
             }
-            if(y3[i]>ymax) {
-                ymax = y3[i];
-            }
-            if(i == logs.size()-1) {
-                xmax = x3[i];
-            }
-            if(i == 0) {
-                xmin = x0[i];
+            if(y3.back()>ymax) {
+                ymax = y3.back();
             }
         }
 
-
         if(tokens[5] == "nan"){
             //do not include point
         }else if(tokens[5] == "NULL"){
             //disable this variable if encounter "NULL"
             log4 = false;
         }else {
-            x4[i] = std::stod(tokens[0]);
-            y4[i] = std::stod(tokens[5]);
-            if(y4[i]<ymin) {
-                ymin = y4[i];
+            x4.append(std::stod(tokens[0]));
+            y4.append(std::stod(tokens[5]));
+            if(y4.back()<ymin) {
+                ymin = y4.back();
             }
-            if(y4[i]>ymax) {
+            if(y4.back()>ymax) {
                 ymax = y4[i];
             }
-            if(i == logs.size()-1) {
-                xmax = x4[i];
-            }
-            if(i == 0) {
-                xmin = x0[i];
-            }
         }
     }
 
@@ -816,19 +786,47 @@ void MainWindow::graphLogs(QStringList logs)
 
     //set data
     if(log0) {
-        ui->dataPlot->graph(0)->setData(x0, y0);
+        if(firstSet) {
+            ui->dataPlot->graph(0)->setData(x0, y0);
+        }
+        else {
+            ui->dataPlot->graph(0)->addData(x0, y0);
+        }
     }
     if(log1) {
-        ui->dataPlot->graph(1)->setData(x1, y1);
+        if(firstSet) {
+            ui->dataPlot->graph(1)->setData(x1, y1);
+        }
+        else {
+            ui->dataPlot->graph(1)->addData(x1, y1);
+        } 
     }
     if(log2) {
-        ui->dataPlot->graph(2)->setData(x2, y2);
+        if(firstSet) {
+            ui->dataPlot->graph(2)->setData(x2, y2);
+        }
+        else {
+            ui->dataPlot->graph(2)->addData(x2, y2);
+        }
     }
     if(log3) {
-        ui->dataPlot->graph(3)->setData(x3, y3);
+        if(firstSet) {
+            ui->dataPlot->graph(3)->setData(x3, y3);
+        }
+        else {
+            ui->dataPlot->graph(3)->addData(x3, y3);
+        }
     }
     if(log4) {
-        ui->dataPlot->graph(4)->setData(x4, y4);
+        if(firstSet) {
+            ui->dataPlot->graph(4)->setData(x4, y4);
+        }
+        else {
+            ui->dataPlot->graph(4)->addData(x4, y4);
+        }
+    }
+    if(firstSet) {
+        firstSet = false;
     }
 
     //set lables and boundaries
@@ -838,6 +836,13 @@ void MainWindow::graphLogs(QStringList logs)
     ui->dataPlot->yAxis->setRange(ymin, ymax);
     ui->dataPlot->replot();
     ui->dataPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
+    if(lastSet) {
+        firstSet = true;
+        xmin = INFINITY;
+        xmax = -INFINITY;
+        ymax = -INFINITY;
+        ymin = INFINITY;
+    }
 }
 
 void MainWindow::openLogBlockFile() {
@@ -1064,9 +1069,9 @@ void MainWindow::getLogEntryBoxChange5(QString text) {
     }
 }
 
-void MainWindow::graphLogData(QStringList results)
+void MainWindow::graphLogData(QStringList results, bool lastSet)
 {
-    graphLogs(results);
+    graphLogs(results, lastSet);
 }
 
 void MainWindow::receiveLogFile(QString filename) {
diff --git a/groundStation/gui/MicroCART/mainwindow.h b/groundStation/gui/MicroCART/mainwindow.h
index 28a220315..c0834c6e1 100644
--- a/groundStation/gui/MicroCART/mainwindow.h
+++ b/groundStation/gui/MicroCART/mainwindow.h
@@ -123,7 +123,7 @@ private slots:
 
     void on_gamepadPitchScale_valueChanged(int arg1);
 
-    void graphLogs(QStringList logs);
+    void graphLogs(QStringList logs, bool lastSet);
 
     void openLogBlockFile();
 
@@ -161,7 +161,7 @@ private slots:
 
     void sendStartLog();
     void sendStopLog();
-    void graphLogData(QStringList results);
+    void graphLogData(QStringList results, bool lastSet);
 
 private:
     Ui::MainWindow *ui;
@@ -179,7 +179,16 @@ private:
     CrazyflieWorker *crazyflieWorker;
     LogWorker *logWorker;
 public:
-    int logFlag = 0;
+    // QVector<double> x0, y0;
+    // QVector<double> x1, y1;
+    // QVector<double> x2, y2;
+    // QVector<double> x3, y3;
+    // QVector<double> x4, y4;
+    float xmin = INFINITY;
+    float xmax = -INFINITY;
+    float ymax = -INFINITY;
+    float ymin = INFINITY;
+    bool firstSet = true;
 };
 
 #endif // MAINWINDOW_H
-- 
GitLab