Simulink-每30秒输出1,否则输出0 [英] Simulink - Output 1 every 30 seconds, 0 otherwise
问题描述
我需要一个子系统,该子系统需要每隔30秒或30秒或更短的时间输出1.
I need a subsystem that needs to output 1 at interval or 30 seconds or slightly over 30 seconds.
用matlab代码编写,应该可以这样工作
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
其中ulplinkTimeInterval是30秒.当然,我尝试将此代码与matlab功能块一起使用,但是由于某种原因它不起作用(在调试模式下,我可以看到y取值1,但它只是不将值输出到块外),因此我想使用积木,但我不知道该怎么做.
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.
非常感谢您的帮助
推荐答案
您可以使用代码或块使此逻辑相对容易.当您请求使用块的解决方案时,就在这里!
You can make this logic relatively easily with code or blocks. As you requested a solution using blocks, here it is!
- 使用
clock
块来跟踪时间,并使用某些constant
块来确定给出1而不是0的间隔(以秒为单位). - 使用
memory
块将clock
信号延迟1个时间步长,因此我们可以比较连续步长的值. - 将时间除以间隔,然后四舍五入,以给出许多间隔的过去时间.
- 最后,使用
relational operator
比较连续的经过的间隔数".如果更多的间隔已通过上限,则您刚刚超过了间隔阈值.
- Use the
clock
block to keep track of time, and someconstant
block to determine the interval (in seconds) at which to give 1 instead of 0. - Use the
memory
block to delay theclock
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.
注意:对于尚未跨越新间隔的每个时间步,这将返回0,而对于每个单独的时间步,将返回1.输出的准确性将取决于模型的步长.
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.
将memory
块添加到floor
块之后可能会更清楚/更容易,因此您只需进行除法/舍入一次.它仍然可以让您与上一个时间步进行比较.看起来像:
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:
这篇关于Simulink-每30秒输出1,否则输出0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!