0
votes

I'm brand new to Simulink, but I have a lot of programming experience, so I'm not used to thinking about problems in the Simulink mindset. Either I'm missing something very obvious or something very deep.

I'd like to implement a simple model of a linear sliding table according to the following equations:

v  = model input = table velocity
dt = simulation timestep
x  = internal displacement variable = initially zero
x at current timestep = (x at last timestep) + v*dt

In C or C++ I would declare a variable x and update it explicitly, but I can't figure out how to implement this kind of persistent state variable in Simulink. My model is a discrete time model with a fixed timestep.

I know I can read variables set in the MATLAB workspace- is the right approach here to read/write to the matlab workspace once per simulation step? If so, how do I do that? If not, what's the "Simulink way" of doing this?

Thanks.

Edit 1 in response to comment:

The code I would write in MATLAB for the above task would be the following, with x and dt declared in the MATLAB workspace, and then the update() function called once per timestep. The function updates the variable in the MATLAB workspace and returns the new value as well for convenience.

function xNew = update( v )
    global x;
    global dt;
    x = x + v*dt;
    xNew = x;
end

Edit 2: What I'm really trying to achieve is a simple simulation and experimentation with feedback PID control, but I have zero experience in control theory other than some things I've picked up here and there along the way. I'm just trying to come up with a simple, representative problem for me to play around with. I've attached a picture of my model below. The table is displacement-controlled with the target displacement being the sine wave.

Essentially my question is about implementing the highlighted "Motor Model" block.

enter image description here

1
It would perhaps be useful to show how you would do this in MATLAB or pseudo code (or C/C++ if it's easy enough to "read"). Sounds like you just want an integrator block though. I appreciate your explanations for what each variable means, but example values / inputs / expected outputs are always handy. Reading from and writing to the workspace each timestep is almost definitely not the solution.Wolfie
@Wolfie I've added some more context. Thanks again!David
This added info is really useful, and makes the question clear. Unfortunately I don't have Simulink at home, so will look to answer next week if nobody else already has.Wolfie

1 Answers

1
votes

Memory like this is implemented using the Unit Delay block ( or the Integrator block if working in continuous time). In the diagram below there needs to be a MATLAB Workspace variable called dt (for the Gain block to work) and the Sample Time of the Unit Delay block also needs to be set to be dt.

enter image description here

Under almost no circumstance is writing to the Workspace at each time step the right approach when using Simulink.