Simulink - Output 1 every 30 seconds, 0 otherwise

こ雲淡風輕ζ 提交于 2019-12-11 00:16:41

问题


I need a subsystem that needs to output 1 at interval or 30 seconds or slightly over 30 seconds.

Written in matlab code it should work like that

function y = fcn(time,uplinkTimeInterval)

%#codegen

persistent lastTriggerTime

if isempty(lastTriggerTime)

    lastTriggerTime = 0;

end 

if time>=lastTriggerTime || time == 0

    y = 1;

    lastTriggerTime = time + uplinkTimeInterval;
else

    y = 0;

end

end

where ulplinkTimeInterval is 30 seconds. Of course I tried to use the matlab function block with this code but for some reason it does not work (in debug mode I can see that y takes value 1 as it should but it simply does not ouput the value outside the block), therefore I wanted to use blocks but I do not know how to do it.

Thanks very much for your help


回答1:


You can make this logic relatively easily with code or blocks. As you requested a solution using blocks, here it is!

  • Use the clock block to keep track of time, and some constant block to determine the interval (in seconds) at which to give 1 instead of 0.
  • Use the memory block to delay the clock signal by 1 timestep, so we can compare consecutive steps' values.
  • Divide the times by the interval, and round down, to give how many intervals have passed.
  • Finally, compare consecutive "number of intervals passed" using a relational operator. If more intervals have passed on the upper line, then you have just stepped over the interval threshold.

Note: this will return a 0 for every timestep where you have not crossed a new interval, and a 1 at each individual timestep where you have. The accuracy of the output will depend on the step size of your model.


Edit: It may be clearer / easier to just add the memory block after the floor block, so you are only doing the division / rounding once. It would still allow you to do a comparison to the previous time step. That would look like:




回答2:


Easiest way to do this is with a just a single Pulse Generator block, set to have a "high" of 1 every 30 seconds. That is shown as part of the image below. The signal will be high for whatever the percentage of the period is specified in the block dialog.

If for some reason you really need to use a subsystem then use a Triggered and Enabled Subsystem (See top right of image). Feed the same pulse signal into both the trigger and the enable port, and set the outport inside the subsystem to have Output when disabled to reset, and to have an Initial Output of 0 (See the lower right of the image).

The model below shows how to do this. In this instance the pulse has been set to have a period of 30s with the rising edge happening every 1% of that period (See the top left of the image).

The output signal will be high for one time step every time the input rises (assuming the trigger is set to rising edge.)



来源:https://stackoverflow.com/questions/45140022/simulink-output-1-every-30-seconds-0-otherwise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!