From 30a26c5249631f149ee741f233e3bcdc5c11edc1 Mon Sep 17 00:00:00 2001
From: zeisele <zeisele@iastate.edu>
Date: Mon, 28 Mar 2022 19:53:59 +0200
Subject: [PATCH] add test stand data to logging

---
 crazyflie_groundstation/inc/CCrazyflie.h      |  3 +-
 .../ccrazyflie/CCrazyflie_loggingFuncs.cpp    |  7 +++++
 .../src/ccrazyradio/CCrazyRadio_loopFuncs.cpp |  2 +-
 .../src/crazyflieGroundStation.cpp            | 11 +++-----
 .../gui/MicroCART/crazyflieworker.cpp         | 28 +++++++++++++++++++
 groundStation/gui/MicroCART/logworker.cpp     |  3 ++
 6 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/crazyflie_groundstation/inc/CCrazyflie.h b/crazyflie_groundstation/inc/CCrazyflie.h
index dd7937812..8d92698a3 100644
--- a/crazyflie_groundstation/inc/CCrazyflie.h
+++ b/crazyflie_groundstation/inc/CCrazyflie.h
@@ -249,8 +249,6 @@ class CCrazyflie {
   bool readTOCParameters();
   bool readTOCLogs();
 
-  serial::Serial * test_stand;
-
   /*! \brief Send a set point to the copter controller
 
     Send the set point for the internal copter controllers. The
@@ -295,6 +293,7 @@ class CCrazyflie {
     CTOC *m_tocParameters;
     CTOC *m_tocLogs;
     int testStandError = 0;
+    serial::Serial * test_stand;
 
   CCrazyflie(CCrazyRadio *crRadio, int radioChannel, XferRate dataRate, int quadNum, double startTime);
   /*! \brief Destructor for the copter convenience class
diff --git a/crazyflie_groundstation/src/ccrazyflie/CCrazyflie_loggingFuncs.cpp b/crazyflie_groundstation/src/ccrazyflie/CCrazyflie_loggingFuncs.cpp
index e3b05418a..32fcc188a 100644
--- a/crazyflie_groundstation/src/ccrazyflie/CCrazyflie_loggingFuncs.cpp
+++ b/crazyflie_groundstation/src/ccrazyflie/CCrazyflie_loggingFuncs.cpp
@@ -117,6 +117,7 @@ void CCrazyflie::writeLogData() {
 	for(int i = 0; i < m_tocLogs->sizeOfActiveList(); i++) {
 		file_log << "\t" << this->sensorDoubleValue(m_tocLogs->activeLogName(i));
 	}
+	file_log << "\t" << this->sensorDoubleValue("testStand");
 }
 
 
@@ -238,7 +239,13 @@ double CCrazyflie::readTestStand() {
 	}
 	*/
 	std::string result = "";
+	
+	
+	//usleep(5);
+	this->test_stand->readline();
 	result = this->test_stand->readline();
+	this->test_stand->flushInput();
+	
 	double value;
 	try {
 		value = std::stod(result);
diff --git a/crazyflie_groundstation/src/ccrazyradio/CCrazyRadio_loopFuncs.cpp b/crazyflie_groundstation/src/ccrazyradio/CCrazyRadio_loopFuncs.cpp
index bc1dbc1d6..5772cf441 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(1000);
+		usleep(500);
 
 		// Check if the thread has been told to terminate
 		if (this->m_exitThread) {
diff --git a/crazyflie_groundstation/src/crazyflieGroundStation.cpp b/crazyflie_groundstation/src/crazyflieGroundStation.cpp
index 80d05ce35..5ac66203b 100644
--- a/crazyflie_groundstation/src/crazyflieGroundStation.cpp
+++ b/crazyflie_groundstation/src/crazyflieGroundStation.cpp
@@ -192,13 +192,9 @@ int main(int argc, char** argv) {
 		{
 			serial::PortInfo device = *iter++;
 
-			if(device.hardware_id.compare("n/a")) {
-				cout << "Port found, do you want to use " << device.port << ": " << device.description << " as your serial input to the ground station? y or n" << endl;
-				char response = getchar();
-				if(response == 'y') {
-					selectedPort = device.port;
-					break;
-				}
+			if(!device.port.compare("/dev/ttyUSB0")) {
+				selectedPort = device.port;
+				break;
 			}
 		}
 
@@ -498,6 +494,7 @@ void closeoutProgram() {
 		cout << "\tBattery" << ": " << crazyflie_info[i].cflieCopter->batteryLevel() << endl;
 		crazyflie_info[i].cflieCopter->cycle();
 		crazyflie_info[i].cflieCopter->resetLoggingBlocks();
+		crazyflie_info[i].cflieCopter->test_stand->close();
 
 		delete crazyflie_info[i].cflieCopter;
 	}
diff --git a/groundStation/gui/MicroCART/crazyflieworker.cpp b/groundStation/gui/MicroCART/crazyflieworker.cpp
index 199cc1c95..5412e4d8b 100644
--- a/groundStation/gui/MicroCART/crazyflieworker.cpp
+++ b/groundStation/gui/MicroCART/crazyflieworker.cpp
@@ -277,6 +277,7 @@ void CrazyflieWorker::loadLogIds() {
     //top line
     std::getline(logTOCFile, line);
     std::getline(logTOCFile, line);
+    int maxId = -1;
     //actual values;
     while(std::getline(logTOCFile, line)) {
         struct toc_info values;
@@ -286,6 +287,9 @@ void CrazyflieWorker::loadLogIds() {
             std::getline(ss, temp_str, '\t');
             if(i == 0) {
                 values.id = (int16_t) stoi(temp_str);
+                if(values.id > maxId) {
+                    maxId = values.id;
+                }
             }
             else if(i == 1) {
                 values.dataType = (int8_t) stoi(temp_str);
@@ -315,6 +319,18 @@ void CrazyflieWorker::loadLogIds() {
         logIds.insert(Qidentifier, values);
     }
     logTOCFile.close();
+    struct toc_group testGroup;
+    testGroup.groupName = "MicroCARTTest";
+    testGroup.entryNames.push_back("stand");
+    logGroups.push_back(testGroup);
+    struct toc_info testStand;
+    testStand.id = maxId+1;
+    testStand.dataType = 9;
+    testStand.strGroup = "MicroCARTTest";
+    testStand.strName = "stand";
+    std::string Midentifier = testStand.strGroup + "." + testStand.strName;
+    QString MQidentifier(Midentifier.c_str());
+    logIds.insert(MQidentifier, testStand);
 }
 
 int CrazyflieWorker::findParamGroup(std::string gName) {
@@ -422,6 +438,7 @@ void CrazyflieWorker::loadLogBlocks() {
 	int blockLinesRead = 0;
 	struct logBlock cur;
     std::string entryName;
+    int maxId = -1;
 
 	while(getline(blockFile, line)) {
 		if(!line.compare("START BLOCK")) {
@@ -430,6 +447,9 @@ void CrazyflieWorker::loadLogBlocks() {
 		}
 		else if(!line.compare("END BLOCK")) {
 			cur.isEnabled = true;
+            if(cur.id > maxId) {
+                maxId = cur.id;
+            }
             logBlocks.push_back(cur);
 			blockLinesRead = 0;
 			cur.id = -1;
@@ -496,6 +516,13 @@ void CrazyflieWorker::loadLogBlocks() {
 		}
 	}
 	blockFile.close();
+    struct logBlock stand;
+    stand.id = maxId+1;
+    stand.blockName = "MicroCART";
+    stand.isEnabled = true;
+    struct toc_info standInfo = tocElementForName("MicroCARTTest.stand");
+	stand.variableNames.push_back(standInfo);
+    logBlocks.push_back(stand);
 }
 
 void CrazyflieWorker::findLogBlockID(QString name, int8_t command) {
@@ -563,6 +590,7 @@ 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 c96f67056..50ae9b26c 100644
--- a/groundStation/gui/MicroCART/logworker.cpp
+++ b/groundStation/gui/MicroCART/logworker.cpp
@@ -90,10 +90,13 @@ void LogWorker::startLogging(QString filename) {
                         wantedLine.append("NULL,");
                     }
                     else {
+                        qInfo() << "append";
                         wantedLine.append(tokens.at(logColumns.at(i)).c_str());
                         wantedLine.append(",");
+                        qInfo() << "append2";
                     }
                 }
+                qInfo() << "results";
                 results.append(wantedLine);
                 wantedLine = "";
                 tokens.clear();
-- 
GitLab