From 4f6749584ee6418854617eb9179a9b97a6dde568 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <nicolo.caruso@skywarder.eu>
Date: Fri, 14 Mar 2025 13:06:22 +0100
Subject: [PATCH] [ARP] LogAngles with timestamp and logdecoding

LogAnlgesData now has the timestamp.
Minor improvement in the test to have log number printed and log the timestamp
Logdecoder: Added the correct logAnglesData structure
---
 src/shared/algorithms/Follower/Follower.cpp   | 13 +++++----
 src/shared/algorithms/Follower/Follower.h     |  4 +--
 src/shared/logger/LogTypes.h                  |  1 -
 src/shared/sensors/logAnglesData.h            | 15 ++++++----
 src/tests/sensors/logAnglesData.h             | 29 -------------------
 .../sensors/test-vn300_gyro_to_angles.cpp     | 18 ++++++++----
 6 files changed, 33 insertions(+), 47 deletions(-)
 delete mode 100644 src/tests/sensors/logAnglesData.h

diff --git a/src/shared/algorithms/Follower/Follower.cpp b/src/shared/algorithms/Follower/Follower.cpp
index 6c9e71d28..bc5b45f8e 100644
--- a/src/shared/algorithms/Follower/Follower.cpp
+++ b/src/shared/algorithms/Follower/Follower.cpp
@@ -50,9 +50,8 @@ float minimizeRotation(float angle)
     return angle;
 }
 
-Follower::Follower(std::chrono::milliseconds updatePeriod)
-    : updatePeriod(static_cast<float>(updatePeriod.count()) / 1000),
-      targetAngles({0, 0, 0})
+Follower::Follower(std::chrono::milliseconds updPeriod)
+    : updatePeriod{updPeriod}, targetAngles({0, 0, 0})
 {
 }
 
@@ -168,9 +167,13 @@ void Follower::step()
 
     // Calculate angular velocity for moving the antennas toward position
     float horizontalSpeed =
-        std::abs((diffAngles.yaw * 1000) / (360 * updatePeriod));
+        std::abs((diffAngles.yaw) /
+                 (360 * (static_cast<float>(updatePeriod.count()) / 1000)));
+    TRACE("[Follower] horizontalSpeed is: %f\n", horizontalSpeed);
     float verticalSpeed =
-        std::abs((diffAngles.pitch * 1000) / (360 * updatePeriod));
+        std::abs((diffAngles.pitch) /
+                 (360 * (static_cast<float>(updatePeriod.count()) / 1000)));
+    TRACE("[Follower] Vertical speed is: %f\n", horizontalSpeed);
 
     // Update the state of the follower
     FollowerState newState;
diff --git a/src/shared/algorithms/Follower/Follower.h b/src/shared/algorithms/Follower/Follower.h
index 3a6fcda47..850a63f90 100644
--- a/src/shared/algorithms/Follower/Follower.h
+++ b/src/shared/algorithms/Follower/Follower.h
@@ -144,8 +144,8 @@ private:
      */
     Eigen::Vector3f getRocketNASOrigin();
 
-    // actuation update period [s]
-    float updatePeriod;
+    // actuation update period [ms]
+    std::chrono::milliseconds updatePeriod;
     // Initialization flag
     std::atomic<bool> isInit{false};
 
diff --git a/src/shared/logger/LogTypes.h b/src/shared/logger/LogTypes.h
index 85c4c1d5e..7a48471f6 100644
--- a/src/shared/logger/LogTypes.h
+++ b/src/shared/logger/LogTypes.h
@@ -80,7 +80,6 @@
 #include <sensors/analog/pressure/nxp/MPXH6400AData.h>
 #include <sensors/analog/pressure/nxp/MPXHZ6130AData.h>
 #include <sensors/logAnglesData.h>
-#include <test/AnnoyingScalar.h>
 
 #include <fstream>
 #include <iostream>
diff --git a/src/shared/sensors/logAnglesData.h b/src/shared/sensors/logAnglesData.h
index 40247bf96..9935ce036 100644
--- a/src/shared/sensors/logAnglesData.h
+++ b/src/shared/sensors/logAnglesData.h
@@ -1,9 +1,11 @@
-#include <iostream>
 #include <cmath>
+#include <iostream>
 
-namespace Boardcore{
+namespace Boardcore
+{
 struct LogAngles
 {
+    uint64_t timestamp;
     float roll, pitch, yaw;
 
     LogAngles() : roll{0}, pitch{0}, yaw(0) {}
@@ -19,11 +21,14 @@ struct LogAngles
         this->yaw   = z * (180 / M_PI);
     }
 
-    static std::string header() { return "Roll,Pitch,Yaw,quaternionZ\n"; }
+    static std::string header()
+    {
+        return "Timestamp,Roll,Pitch,Yaw,quaternionZ\n";
+    }
 
     void print(std::ostream& os) const
     {
-        os << roll << "," << pitch << "," << yaw << "\n";
+        os << timestamp << "," << roll << "," << pitch << "," << yaw << "\n";
     }
 };
-}
\ No newline at end of file
+}  // namespace Boardcore
diff --git a/src/tests/sensors/logAnglesData.h b/src/tests/sensors/logAnglesData.h
deleted file mode 100644
index 40247bf96..000000000
--- a/src/tests/sensors/logAnglesData.h
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <iostream>
-#include <cmath>
-
-namespace Boardcore{
-struct LogAngles
-{
-    float roll, pitch, yaw;
-
-    LogAngles() : roll{0}, pitch{0}, yaw(0) {}
-    LogAngles(float Roll, float Pitch, float Yaw)
-        : roll{Roll}, pitch{Pitch}, yaw(Yaw)
-    {
-    }
-
-    void getAngles(float x, float y, float z)
-    {
-        this->roll  = x * (180 / M_PI);
-        this->pitch = y * (180 / M_PI);
-        this->yaw   = z * (180 / M_PI);
-    }
-
-    static std::string header() { return "Roll,Pitch,Yaw,quaternionZ\n"; }
-
-    void print(std::ostream& os) const
-    {
-        os << roll << "," << pitch << "," << yaw << "\n";
-    }
-};
-}
\ No newline at end of file
diff --git a/src/tests/sensors/test-vn300_gyro_to_angles.cpp b/src/tests/sensors/test-vn300_gyro_to_angles.cpp
index f8f2e96a2..a783af6af 100644
--- a/src/tests/sensors/test-vn300_gyro_to_angles.cpp
+++ b/src/tests/sensors/test-vn300_gyro_to_angles.cpp
@@ -21,9 +21,9 @@
  */
 
 #include <drivers/timer/TimestampTimer.h>
-#include <sensors/logAnglesData.h>
 #include <logger/Logger.h>
 #include <sensors/Vectornav/VN300/VN300.h>
+#include <sensors/logAnglesData.h>
 
 #include <Eigen/Dense>
 #include <cmath>
@@ -50,13 +50,14 @@ void computeEulerAngles(const Vector3d& gyro, double dt, Vector3d& angles)
             cos(ass(1));  // rotation matrix angular_rate --> body_rate
 
     Vector3d angular_rate = G.transpose() * body_rate;
-    printf("angles -->%f,%f,%f\n",angles(0),angles(1),angles(2));
-    printf("angular_rate -->%f,%f,%f\n",angular_rate(0),angular_rate(1),angular_rate(2));
+    printf("angles -->%f,%f,%f\n", angles(0), angles(1), angles(2));
+    printf("angular_rate -->%f,%f,%f\n", angular_rate(0), angular_rate(1),
+           angular_rate(2));
     printf("dt: %f", (dt / 1000000));
     Vector3d new_angles =
         angles + angular_rate * (dt / 1000000);  // integration
     angles = new_angles;
-    printf("new_angles -->%f,%f,%f\n",angles(0),angles(1),angles(2));
+    printf("new_angles -->%f,%f,%f\n", angles(0), angles(1), angles(2));
     // rad --> deg
 }
 
@@ -91,6 +92,10 @@ int main()
 
     Logger::getInstance().start();
 
+    printf("[GYRO] LOG NR %d \n", Logger::getInstance().getStats().logNumber);
+
+    Thread::sleep(100);
+
     uint64_t t0     = TimestampTimer::getTimestamp();
     Vector3d angles = {0, 0, 0};
 
@@ -110,8 +115,9 @@ int main()
             Vector3d gyro = {sample.angularSpeedX, sample.angularSpeedY,
                              sample.angularSpeedZ};
 
-            computeEulerAngles(gyro, dt, angles);
+            computeEulerAngles(gyro, t1 - t0, angles);
 
+            angles_log.timestamp = t1;
             angles_log.getAngles(angles(0), angles(1), angles(2));
             printf("Angles: %f, %f, %f\n", angles_log.pitch, angles_log.roll,
                    angles_log.yaw);
@@ -119,6 +125,8 @@ int main()
 
             t0 = TimestampTimer::getTimestamp();
         }
+        printf("[GYRO] LOG NR %d \n",
+               Logger::getInstance().getStats().logNumber);
 
         Thread::sleep(20);
     }
-- 
GitLab