Clear Filters
Clear Filters

Custom timer function timing query

3 views (last 30 days)
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 Comments
Guillaume
Guillaume on 10 Jul 2018
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?
Emerson Butler
Emerson Butler on 10 Jul 2018
Yes, the function would be called at regular intervals, every 4.167 ms. To be frank, I need to do more research on modeling continuous systems. The function output would be read by another function at the same interval, every 4.167 ms. The only thing being passed to the function would be the latest sample, so I probably need to devise a way to store the previous x number of inputs (5 seconds @ 240 samples/second = 1200 previous inputs). Maybe I could use some kind of loop to do that. The function wouldn't be passed the time since last call, the whole system will be guaranteed an accurate 240Hz sampling frequency, so essentially the inputs and outputs would be updated every 4.167 ms.
I also would like to note that I am an intern, I have a long time to do this, and I just got started on this aspect. My boss has tasked me with modeling this system as a learning exercise, to learn about the system itself and Matlab as I go along. My actual output won't have any impact on anything this year, but it may next year when I try to implement my model on actual hardware. The questions you have asked so far have made me realize how much more I need to learn about the overall system. This function is just a small part of what I will be doing, but it all needs to fit together

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 10 Jul 2018
Edited: Guillaume on 10 Jul 2018
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

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!