I'm trying to use Yampa for some basic system simulation like I'd do in Simulink. In this case I want to simulate a spring and damper system, introduced by this simulink tutorial. I've written the following signal functions to represent the system:
system = time >>> force >>> displacement
force = constant (m * g)
displacement = feedback (-) (velocity >>> integral) (gain $ k / m) 0
velocity = feedback (-) integral (gain $ c / m) 0
Where the feedback
function creates a basic feedback loop and is implemented like this:
feedback op a b b0 = loopPre b0 inner
where inner = arr (uncurry op) >>> a >>> (identity &&& b)
Oh, and:
gain x = arr (*x)
With sensible positive constants, I get a wildly unstable system:
Is there something obviously wrong in the way I'm constructing feedback loops or applying the integration?
Change integral
to imIntegral 0
displacement = feedback (-) (velocity >>> imIntegral 0) (gain $ k / m) 0
velocity = feedback (-) (imIntegral 0) (gain $ c / m) 0
From spring.hs:
Using Simulink:
Something funny is happening in the integral function, changing to imIntegral 0
gives the same curve as in matlab.
My guess is that Integral
is delayed by one sample, since it doesn't have a starting value, changing the behaviour of the loop.