From b66c05bfb00663b411b04e806d2adf79e1169851 Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Sat, 1 Oct 2022 16:52:14 +0800
Subject: [PATCH] reasonable oscillation

Only keeping every second iteratoin
---
 examples/rp/src/bin/usb_serial.rs | 51 +++++++++++++++++++------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs
index 92d35904..0d10d4fb 100644
--- a/examples/rp/src/bin/usb_serial.rs
+++ b/examples/rp/src/bin/usb_serial.rs
@@ -110,67 +110,80 @@ async fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, syst: &mut SYST) -> Result<(), D
         while pin.is_high() {}
     });
 
-    let mut up_x = 0u32;
-    let mut down_x = 0u32;
+    let mut up_x = 1u32;
+    let mut down_x = 1u32;
     let mut up = 0u32;
     let mut down = 0u32;
 
     loop {
-        const LOOP: usize = 20;
+        // number of loops per interrupt-free block
+        const LOOP: usize = 2000;
+
+        // count of cycles to target
+        const T: u32 = 256;
+
+        // BUF iterations from the start (or end?) will be printed
         const BUF: usize = 16;
-        const T: u32 = 8192;
-        let mut dels = [0u32; BUF];
         let mut ups = [0u32; BUF];
         let mut downs = [0u32; BUF];
         let mut upx = [0u32; BUF];
         let mut downx = [0u32; BUF];
 
+        // The main iteration function for experimentation.
+        // It calculates the next overshoot #cycles.
+        // `prev` is the #cycles returned from last step(), `meas` is the time
+        // taken for the return to centre value (which is targetting `T`).
+        // eg `up_x = step(up_x, down);`
         let step = |prev: u32, meas: u32| {
-            if meas < T || prev < T {
-                prev * 2
+            if meas < T {
+                (prev + meas)*2
             } else {
-                prev.saturating_sub(T / 2)
+                prev - prev/4 + meas % T
             }.max(1)
         };
 
         critical_section::with(|_cs| {
 
             for i in 0..LOOP {
-                cortex_m::asm::delay(down_x);
                 pin.set_pull(Pull::Up);
                 syst.clear_current();
                 let t1 = SYST::get_current();
                 while pin.is_low() {}
                 let t2 = SYST::get_current();
                 up = t1 - t2;
-
-                up_x = step(up_x, down);
-
+                if i % 2 == 0 {
+                    down_x = step(down_x, up);
+                }
                 cortex_m::asm::delay(up_x);
+
                 pin.set_pull(Pull::Down);
                 syst.clear_current();
                 let t1 = SYST::get_current();
                 while pin.is_high() {}
                 let t2 = SYST::get_current();
                 down = t1 - t2;
+                if i % 2 == 0 {
+                    up_x = step(up_x, down);
+                }
+                cortex_m::asm::delay(down_x);
 
-                down_x = step(down_x, up);
-
-                if i < BUF {
-                    let n = i;
+                if i >= LOOP-BUF {
+                    let n = i - (LOOP-BUF);
                     ups[n] = up;
                     upx[n] = up_x;
                     downs[n] = down;
                     downx[n] = down_x;
-                    dels[n] = up + down;
                 }
 
             }
             pin.set_pull(Pull::None);
         });
 
-        for i in 0..BUF {
-            info!("{}  U {} x {}  D {} x {}", dels[i], ups[i], upx[i], downs[i], downx[i]);
+        for i in 0..BUF-1 {
+            if i % 2 == 1 {
+                info!("U {},{} x {}  D {},{} x {} use", ups[i], ups[i+1], upx[i], downs[i], downs[i+1], downx[i]);
+            } else {
+            }
         }
         info!("===");
     }
-- 
GitLab