jcsyshc 7 місяців тому
батько
коміт
e8d88bb7cc

+ 1 - 1
app/data/pose_collect.json

@@ -21,7 +21,7 @@
         "com_port": "/dev/ttyUSB0",
         "tool_list": [
           {
-            "rom_path": "/home/tpx/data/roms/NR_1_STCAM_20241115.rom",
+            "rom_path": "/home/tpx/data/roms/20241207Exp/Glass_4Ball_1Fix.rom",
             "outputs": {
               "transform": "target_in_tracker"
             }

+ 31 - 48
src/algorithm/measure_window.hpp

@@ -4,6 +4,7 @@
 #include "utility/dynamic_pool.hpp"
 
 #include <boost/core/noncopyable.hpp>
+#include <boost/circular_buffer.hpp>
 
 #include <cmath>
 #include <deque>
@@ -11,73 +12,55 @@
 
 namespace sophiar {
 
-    class measure_window : private boost::noncopyable {
+    class measure_window: boost::noncopyable {
     public:
+        static constexpr auto max_length = 1024;
 
-        static constexpr auto max_length = std::numeric_limits<size_t>::max();
+        explicit measure_window(size_t length = max_length)
+            : win(length) { }
 
-        explicit measure_window(size_t _length = max_length)
-                : win_length(_length) {}
-
-        void reset() {
-            while (!win.empty()) {
-                win.pop();
-            }
-            cur_n = 0;
-            sum_x = 0.0;
-            sum_x2 = 0.0;
+        void reset() { //
+            win.clear();
         }
 
-        void pop() {
-            assert(!win.empty());
-            auto val = win.front();
-            win.pop();
-            sum_x -= val;
-            sum_x2 -= val * val;
-            --cur_n;
+        void push(float val) { //
+            win.push_back(val);
         }
 
-        void push(double val) {
-            if (cur_n == win_length) {
-                pop();
-            }
-            win.push(val);
-            sum_x += val;
-            sum_x2 += val * val;
-            ++cur_n;
+        void pop() { //
+            win.pop_front();
         }
 
-        double calc_mean() const {
-            assert(cur_n >= 1);
-            return sum_x / static_cast<double>(cur_n);
+        float calc_mean() const {
+            if ( win.empty() ) return NAN;
+            auto sum = std::reduce(win.begin(), win.end());
+            return sum / win.size();
         }
 
-        double calc_std() const {
-            assert(cur_n >= 2);
-            auto mean_val = calc_mean();
-            return std::sqrt(sum_x2
-                             - 2 * mean_val * sum_x
-                             + static_cast<double>(cur_n) * mean_val * mean_val)
-                   / static_cast<double>(cur_n - 1);
+        float calc_std() const {
+            if ( win.size() < 2 ) return NAN;
+            auto mean     = calc_mean();
+            auto sum_std2 = std::transform_reduce(
+                win.begin(), win.end(), //
+                0.f, std::plus(),
+                [=](float x) {
+                return (x - mean) * (x - mean);
+            }
+            );
+            auto std2 = sum_std2 / (win.size() - 1);
+            return std::sqrt(std2);
         }
 
-        bool is_full() const {
-            return win.size() == win_length;
+        bool is_full() const { //
+            return win.full();
         }
 
-        size_t length() const {
+        size_t length() const { //
             return win.size();
         }
 
     private:
-        size_t win_length;
-        size_t cur_n = 0;
-        double sum_x = 0, sum_x2 = 0;
-
-        using dynamic_deque = std::deque<double, DYNAMIC_ALLOCATOR(double) >;
-        using dynamic_queue = std::queue<double, dynamic_deque>;
-        dynamic_queue win;
-
+        boost::circular_buffer<float> win;
     };
 
 }

+ 1 - 1
src/utility/coro_worker.hpp

@@ -131,7 +131,7 @@ namespace sophiar {
                 func = std::forward<FuncType>(func),
                 timer = boost::asio::high_resolution_timer(*ctx)]() mutable
                 -> boost::asio::awaitable<bool> {
-            timer.expires_from_now(interval);
+            timer.expires_after(interval);
             auto ret = co_await func();
             co_await timer.async_wait(boost::asio::use_awaitable);
             co_return ret;

+ 1 - 1
src/utility/debug_utility.hpp

@@ -86,7 +86,7 @@ namespace sophiar {
     template<typename DurationType>
     inline awaitable<void> coro_sleep(DurationType t) {
         boost::asio::high_resolution_timer timer(co_await boost::asio::this_coro::executor);
-        timer.expires_from_now(t);
+        timer.expires_after(t);
         co_await timer.async_wait(use_awaitable);
         co_return;
     }