tic toc in embedded matlab function showing strange results?
6 views (last 30 days)
Show older comments
I have created a matlab function in Simulink (partially) containing the code below. The model has an input X. Based on this input a switch is operated by the output y. A value of 1 switches it on and a value of 0 switches it off. What I want the function to do is the following:
- If y = 0, and X is still within the threshold, at the first occurence of an outside value I want to start a real-time timer. If that timer reaches a value larger than t_init I want it to switch on.
- If y = 0, and X is already outside the threshold, I want the timer to keep counting till it reaches t_init.
- If y = 1, and X is outside the threshold, at the first occurence of an inside value I want to start a real-time timer. If that timer reaches a value larger than t_term I want it to switch off.
- If y = 1, and X is already inside the threshold, I want the timer to keep counting till it reached t_term.
I added coder.extrinsic(tic) and coder.extrinsic(toc). I also tried using clock in combination with etime but that didn't work either.
The problem for now is that the value for elapsedTime_init and elapsedTime_term keep switching between ~ 3.5*10^5 and 0. It might be something obvious but I can't seem to figure out what the problem is. Please help me, cause I'm seriously stuck.
t_term = 0;
t_init = 0;
elapsedTime_init = 0;
elapsedTime_term = 0;
t_start = tic;
elapsedTime_init = double(toc(t_start));
elapsedTime_term = double(toc(t_start));
k = zeros(10,1);
i = length(k);
y = 0;
for ii = 2:i
k(ii,1) = k_old(ii-1,1); % remove 1 value and make space for new value at first row
end
if X <= Lower_threshold_value && X => Upper_threshold_value
k(1) = 0;
if sum(k_old) >= 7 && y_old == 1
t_term = typecast(tic,'double');
else
t_term = t_term_old;
end
elapsedTime_term = toc(typecast(t_term,'uint64'));
if elapsedTime_term >= Time_term
y = 0;
else
y = y_old;
end
else
k(1) = 1;
if sum(k_old) <= 3 && y_old == 0
t_init = typecast(tic,'double');
else
t_init = t_init_old;
end
elapsedTime_init = toc(typecast(t_term,'uint64'));
if elapsedTime_init >= Time_init
y = 1;
else
y = y_old;
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!