From 79fb3276c3f858e3fc0d75684606a58a51b1e344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20B=C3=A4renz?= Date: Thu, 18 Jul 2024 17:18:46 +0200 Subject: [PATCH] WIP make sinceLast a maybe --- rhine/src/FRP/Rhine/ClSF/Util.hs | 25 ++++++++++++++----------- rhine/src/FRP/Rhine/Clock.hs | 2 +- rhine/src/FRP/Rhine/Clock/Util.hs | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/rhine/src/FRP/Rhine/ClSF/Util.hs b/rhine/src/FRP/Rhine/ClSF/Util.hs index 797512a2..452eee27 100644 --- a/rhine/src/FRP/Rhine/ClSF/Util.hs +++ b/rhine/src/FRP/Rhine/ClSF/Util.hs @@ -33,9 +33,12 @@ import Data.VectorSpace import Data.TimeDomain -- rhine + +import Data.Functor ((<&>)) import FRP.Rhine.ClSF.Core import FRP.Rhine.ClSF.Except import FRP.Rhine.Clock +import Data.Maybe (fromMaybe) -- * Read time information @@ -54,7 +57,7 @@ timeInfoOf :: (Monad m) => (TimeInfo cl -> b) -> ClSF m cl a b timeInfoOf f = constM $ asks f -- | Continuously return the time difference since the last tick. -sinceLastS :: (Monad m) => ClSF m cl a (Diff (Time cl)) +sinceLastS :: (Monad m) => ClSF m cl a (Maybe (Diff (Time cl))) sinceLastS = timeInfoOf sinceLast -- | Continuously return the time difference since clock initialisation. @@ -161,7 +164,7 @@ integralFrom :: BehaviorF m td v v integralFrom v0 = proc v -> do _sinceLast <- timeInfoOf sinceLast -< () - sumFrom v0 -< _sinceLast *^ v + sumFrom v0 -< maybe zeroVector (*^ v) _sinceLast -- | Euler integration, with zero initial offset. integral :: @@ -182,11 +185,11 @@ derivativeFrom :: , s ~ Diff td ) => v -> - BehaviorF m td v v + BehaviorF m td v (Maybe v) derivativeFrom v0 = proc v -> do vLast <- delay v0 -< v TimeInfo {..} <- timeInfo -< () - returnA -< (v ^-^ vLast) ^/ sinceLast + returnA -< ((v ^-^ vLast) ^/) <$> sinceLast -- | Numerical derivative with input initialised to zero. derivative :: @@ -194,7 +197,7 @@ derivative :: , VectorSpace v s , s ~ Diff td ) => - BehaviorF m td v v + BehaviorF m td v (Maybe v) derivative = derivativeFrom zeroVector {- | Like 'derivativeFrom', but uses three samples to compute the derivative. @@ -208,11 +211,11 @@ threePointDerivativeFrom :: ) => -- | The initial position v -> - BehaviorF m td v v + BehaviorF m td v (Maybe v) threePointDerivativeFrom v0 = proc v -> do dv <- derivativeFrom v0 -< v - dv' <- delay zeroVector -< dv - returnA -< (dv ^+^ dv') ^/ 2 + dv' <- delay (Just zeroVector) -< dv -- FIXME think about this. Or just delay 2 samples? + returnA -< ((^+^) <$> dv <*> dv') <&> (^/ 2) {- | Like 'threePointDerivativeFrom', but with the initial position initialised to 'zeroVector'. @@ -223,7 +226,7 @@ threePointDerivative :: , s ~ Diff td , Num s ) => - BehaviorF m td v v + BehaviorF m td v (Maybe v) threePointDerivative = threePointDerivativeFrom zeroVector -- ** Averaging and filters @@ -269,7 +272,7 @@ averageFrom :: averageFrom v0 t = proc v -> do TimeInfo {..} <- timeInfo -< () let - weight = exp $ -(sinceLast / t) + weight = exp $ -(maybe 0 (/ t) sinceLast) weightedAverageFrom v0 -< (v, weight) -- | An average, or low pass, initialised to zero. @@ -303,7 +306,7 @@ averageLinFrom :: averageLinFrom v0 t = proc v -> do TimeInfo {..} <- timeInfo -< () let - weight = t / (sinceLast + t) + weight = t / (fromMaybe 0 sinceLast + t) weightedAverageFrom v0 -< (v, weight) -- | Linearised version of 'average'. diff --git a/rhine/src/FRP/Rhine/Clock.hs b/rhine/src/FRP/Rhine/Clock.hs index 62b1b302..455c38fb 100644 --- a/rhine/src/FRP/Rhine/Clock.hs +++ b/rhine/src/FRP/Rhine/Clock.hs @@ -67,7 +67,7 @@ class (TimeDomain (Time cl)) => Clock m cl where -- | An annotated, rich time stamp. data TimeInfo cl = TimeInfo - { sinceLast :: Diff (Time cl) + { sinceLast :: Maybe (Diff (Time cl)) -- ^ Time passed since the last tick , sinceInit :: Diff (Time cl) -- ^ Time passed since the initialisation of the clock diff --git a/rhine/src/FRP/Rhine/Clock/Util.hs b/rhine/src/FRP/Rhine/Clock/Util.hs index c0cf5cd3..6937649e 100644 --- a/rhine/src/FRP/Rhine/Clock/Util.hs +++ b/rhine/src/FRP/Rhine/Clock/Util.hs @@ -32,7 +32,7 @@ genTimeInfo _ = proc (absolute, tag) -> do returnA -< TimeInfo - { sinceLast = absolute `diffTime` fromMaybe initialTime lastTime + { sinceLast = (absolute `diffTime`) <$> lastTime , sinceInit = absolute `diffTime` initialTime , .. }