Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating a spring/damper system in Yampa

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:

plot of displacement/time

Is there something obviously wrong in the way I'm constructing feedback loops or applying the integration?

like image 600
Daniel Buckmaster Avatar asked Oct 19 '13 01:10

Daniel Buckmaster


1 Answers

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:

Yampa

Using Simulink:

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.

like image 127
MdxBhmt Avatar answered Nov 14 '22 02:11

MdxBhmt