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.