Skip to content

Commit

Permalink
WIP make sinceLast a maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
turion committed Jul 18, 2024
1 parent 2de85f0 commit 79fb327
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
25 changes: 14 additions & 11 deletions rhine/src/FRP/Rhine/ClSF/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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 ::
Expand All @@ -182,19 +185,19 @@ 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 ::
( Monad m
, 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.
Expand All @@ -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'.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'.
Expand Down
2 changes: 1 addition & 1 deletion rhine/src/FRP/Rhine/Clock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rhine/src/FRP/Rhine/Clock/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
, ..
}

0 comments on commit 79fb327

Please sign in to comment.