Simulating a spring/damper system in Yampa
Asked Answered
C

1

10

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?

Chloramphenicol answered 19/10, 2013 at 1:14 Comment(0)
M
7

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.

Mullock answered 20/10, 2013 at 19:40 Comment(5)
Excellent, thanks heaps for that! I had done some simple tests with integral so I didn't think that was an issue. By 'the scale is off', do you mean the horizontal axis? My code doesn't plot time, just the # of each data point.Chloramphenicol
darn, nevermind about that, when I was comparing matlab and simulink I had something off that made the diference in the y scale.Mullock
Adding a transport delay in simulink makes the system unstable similarly to the one you've found, so that's must be it.Mullock
Oh, of course. Try simulating a system that's just time >>> integral >>> integral. The first couple of samples are 0 while the integrators warm up. I had no idea that small delay would be enough to destroy the system stability.Chloramphenicol
It is! Since you are adding a transport delay in the feedback loop, you add a lot of phase in your system, which is similar to throwing the poles around. edit: yeah, just simulated that code, it shows a 2 samples delay, one for each integrator.Mullock

© 2022 - 2024 — McMap. All rights reserved.