MATLAB: Simulink Ramp – On Demand

clockdemandfunctionMATLABmemorypersistentrampsimulink

Hello,
I would like to have a signal ramp from a min. value to a max value. and back down again, in a set amount of time. I initially did this with persistent variables which allowed me to reset the ramp signal and run it essentially on demand. Is there a simple work around that doesn't involve the use of persistent variables?
On a related note, is it possible to output the sample time as a variable? I've tried using a clock with a memory block, and subtracting to get the sample time, but for a fixed step size of 0.0001, the sample time is not constant but varies between 0.0009999 and 0.0001.
Thanks

Best Answer

You can set the model time step in the "model configuration parameters" to be a variable that you define in the Matlab workspace. This way, you can refer to this variable anywhere in the model. The entire model will run at this fixed timestep.
Another approach is to use a rate transition block to specify the rate that the model block runs at, and you can also define this rate as a variable in the Matlab workspace. (The rate transition sets the rate of the input signal to the block)
Build your ramp function as a function of elapsed time, (elapsed time = current time - start time) and then use a trigger or enable signal to set start time = current time (elapsed time =zero) to start the signal.
Here is another thought: you can construct the elapsed time using an integrator block (integral of time) and use an "external reset" (on the integrator block) to start over at zero. This will cause the ramp function to restart from the beginning.