Custom timer function timing query
1 次查看(过去 30 天)
显示 更早的评论
I am trying to create a timer that accepts 3 inputs: An integer, and two floats representing times.
(input, time1, time2)
The timer will output, (we'll call it y), 1 if the input has been 1 for at least as long as the first time, and output 2 if the input has been 2 for at least as long as the second time. I have basically two levels of confirmation, where I need to know if my initial signal has been 1 long enough or 2 long enough. 1 and 2 are the only possibilities of input. The output, y, will start as 0. To describe the behavior a little more specifically, allow me to define two variables, t1 and t2.
t1 is true if the input has been 1 for at least as long as time1
t2 is true if the input has been 2 for at least as long as time2
And a quick logical chart:
t1 t2 y
0 0 No change
1 0 Set to 1
0 1 Set to 2
1 1 Not possible due to input being either 1 or 2
I need y to remain whatever it was for the 0 0 case. If t1 asserts and sets y to 1, but then t1 deasserts, I need y to remain as a 1.
Here is what I have so far:
function [y] = custom_timer(input, time1, time2)
y = 0; %y starts at 0
duration_1 = seconds(input == 1); %Set duration_1 to the number of seconds that input has equaled 1
duration_2 = seconds(input == 2);
if(duration_1 >= time1) %If we've been 1 long enough, set y to 1
y = 1;
elseif(duration_2 >= time2)
y = 2;
else
y = y; %Not really sure if I need this here, I just want Y to remain whatever it was if 0 0 case is met
end
end
When I set x to 1 directly, and the times to any arbitrary numbers, I always get y = 0. Is this because of the initial instantiation of y? Or is this due to my function not processing long enough for duration_1 to meet time1? If I comment out the initial instantiation of y entirely I get an error. I have also tried using the duration function instead of seconds but that gives me an error (duration(line 239)), so I switched to seconds(boolean) instead since I am measuring in seconds.
Eventually my input will not be just a number, it will be the output of some other function, and the value will change over time, it may be 1 for 10 seconds, and then be 2 for 30 seconds, etc.
Any help is appreciated, I have just started using Matlab and have little experience doing any other coding as well.
5 个评论
Guillaume
2018-7-10
I don't think the details of the hardware matter nor the properties of the signal, so there's no need for you to break confidentiality.
So, if I understood correctly, your function would be called at regular interval by ... something. Is this something you've got sorted out or are you asking help with that?
What would be passed to the function, just the latest sample(s) or the whole signal? Would the function be passed the time since last call, should it work it out, or is it guaranteed to be constant?
采纳的回答
Guillaume
2018-7-10
编辑:Guillaume
2018-7-10
I probably need to devise a way to store the previous x number of inputs
That's fairly easy to do. It does not involve loops but persistent variables. However, I don't think you need to keep x previous samples. Just the last one and a running count of how long the signal has been the same. You'd reset the count each time the signal changes.
So it'll be something like:
function state = signal_counter(sample, dt)
%sample: current value of signal
%dt: time since last call. Since it's a constant, it could just be hardcoded in the function
persistent lastsample runningtime currentstate;
if isempty(lastsample)
%first call to function, initialise all variables
lastsample = 0;
runningtime = 0;
currentstate = 0;
end
if sample == lastsample
%signal has not changed, increase running time
runningtime = runningtime + dt;
else
%signal changed, reset running time
lastsample = sample;
runningtime = 0;
end
%update currentstate if given conditions are met otherwise, leave as is
if runningtime >= 3 && sample == 2
currentstate = 2;
elseif runningtime >= 5 && sample == 1
currentstate = 1;
end
state = currentstate; %you cannot use a persistent variable as output variable.
end
You may want to keep the running count in term of samples or accumulate frequency instead of period so everything is integer to avoid accumulation errors on floating point values
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!