Timer Elapsed vs Remaining AOI, 1s off issue

JZerb

Member
Join Date
Oct 2016
Location
Here
Posts
421
So I'm assuming this is a rounding issue somewhere, but I don't know for sure so I figured someone else would.

Made an AOI that just gives me a Timers remaining Minutes and Seconds as well as its elapsed Minutes and Seconds. Issue is the seconds are always 1 second off. So if say its a 2 minute timer and 10 seconds has elapsed, it will tell me there is 1:50 remaining but only 9 seconds elapsed.

TimerOne.JPG TimerTwo.JPG
 
I think it is nature of time, you can't be on same full second with remaining and elapsed time. Unless milliseconds equals 0.
 
It's a truncation issue, not a rounding issue, but that is semantics.



By truncating (rounding) elapsed time in ms toward its zero, i.e. toward the TimerPRE value, and truncating the remaining time in ms toward zero, the code is "throwing away" 1000 milliseconds between the two DIV instructions.


In the example shown, the top rung generates a remaining time of 5m41s = 581s = 581000ms, which is 961ms less than the 581961ms remaining, while the bottom rung generates an elapsed time of 18s = 18000ms, which is 39ms less than the actual elapsed time .ACC = 18-39. So the example leaves 961ms+39ms = 1000ms = one second on the table; Cinderella!


One possible fix, among many, with minimal change to existing AOI, would be to calculate the leftover milliseconds remaining, via a [MOD TotalTimeRemaining 1000 ModuloTransferMs] instruction on the first rung, and transfer (add) those to the second rung [ADD TimerACC ModulTransferMs TimerACClocal] before the first DIV and use TimerACClocal in place of TimerACC after that; as it stands now, that would make elapsed time start at 1 (after the first millisecond, anyway), and always be rounding up; reversing the rungs, but still transferring from first to second, would make the remaining time essentially start at 10m0s while elapsed time stayed at 0s through the first second.



Of course there are better approaches that would involve refactoring the entire AOI (but it's an AOI, so who cares?), and better handling of edge cases. E.g. one approach would involve rounding so the calculated whole-second transitions happen on the half-second (at 500ms remainder); one edge case is what to do if the .PRE value is not a multiple of 1000ms.
 
It's a truncation issue, not a rounding issue, but that is semantics.



By truncating (rounding) elapsed time in ms toward its zero, i.e. toward the TimerPRE value, and truncating the remaining time in ms toward zero, the code is "throwing away" 1000 milliseconds between the two DIV instructions.


In the example shown, the top rung generates a remaining time of 5m41s = 581s = 581000ms, which is 961ms less than the 581961ms remaining, while the bottom rung generates an elapsed time of 18s = 18000ms, which is 39ms less than the actual elapsed time .ACC = 18-39. So the example leaves 961ms+39ms = 1000ms = one second on the table; Cinderella!


One possible fix, among many, with minimal change to existing AOI, would be to calculate the leftover milliseconds remaining, via a [MOD TotalTimeRemaining 1000 ModuloTransferMs] instruction on the first rung, and transfer (add) those to the second rung [ADD TimerACC ModulTransferMs TimerACClocal] before the first DIV and use TimerACClocal in place of TimerACC after that; as it stands now, that would make elapsed time start at 1 (after the first millisecond, anyway), and always be rounding up; reversing the rungs, but still transferring from first to second, would make the remaining time essentially start at 10m0s while elapsed time stayed at 0s through the first second.



Of course there are better approaches that would involve refactoring the entire AOI (but it's an AOI, so who cares?), and better handling of edge cases. E.g. one approach would involve rounding so the calculated whole-second transitions happen on the half-second (at 500ms remainder); one edge case is what to do if the .PRE value is not a multiple of 1000ms.

I figured it to be some sort of truncating/rounding issue from the get go. After posting I did the long math like you did in your third paragraph and that makes sense to me now. It wasn’t immediately obvious when I just quickly wrote the code for the AOI. I would like to take the approach you outlined in your last paragraph, so I will just re write what I need in the AOI and accordingly. Other using something like you said above, or doing some sort of forced rounding depending on the first decimal place.
 
nice, glad you sussed it out. Take care of the bits, and the bytes will take care of themselves. ;)

I set it up as you said using the MOD on the end of the first rung and it does work better then before. But i still see your point when I put even a 1 into the ACC of the timer, it reads that 1 second has elapsed as well as 9 minutes and 59 seconds remain.

I will have to tweak it some more so that it doesn't read that unless the entire 1000ms has elapsed.
 
I set it up as you said using the MOD on the end of the first rung and it does work better then before. But i still see your point when I put even a 1 into the ACC of the timer, it reads that 1 second has elapsed as well as 9 minutes and 59 seconds remain.

I will have to tweak it some more so that it doesn't read that unless the entire 1000ms has elapsed.




Reverse the order of the rungs so the elapsed time is giving its excess ms to the remaining time calculation.
 
Reverse the order of the rungs so the elapsed time is giving its excess ms to the remaining time calculation.

I see. Only issue thats still there, a small one at that, is as soon as the timer acc value becomes 1ms the time remaining value reads 9m 60s, aka 10m. Thanks for the help, its better then I had when i started and I will have to use this to just create a new AOI when I have more time. For now, this will suffice, long as no one stops the timer on a full minute or else they'll see 60s in the remaining tag.
 
...the time remaining value reads 9m 60s, aka 10m....


Heh, I left you out to dry there by not being more specific ;).


That happens because the elapsed leftover ms is being added to SecondsRemaining. It is better to add the time remaining to TotalTimeRemaining, i.e. between the [SUB TimerPRE TimerAcc TotalTimeRemaining] but before the [DIV TotalTimeRemaining 1000 MinutesRemaining]; that will eliminate the Nm60s anomaly.
 
drbitboy, are you sure you aren't your brother who is the PLC wiz, man for someone that had not even programmed a PLC until a 6 months ago, you sure know some stuff (l know you were a programmer in other environments) keep up the good work, or maybe your Lance01 back for another 10,000 post round.🍻
 
drbitboy, are you sure you aren't your brother who is the PLC wiz, man for someone that had not even programmed a PLC until a 6 months ago ...




No, I am drbitboy: I am facile moving the bits around; the language is irrelevant. However, that is not process knowledge: if I ever programmed a physical system, then I would be concerned that someone could get hurt.
 
Heh, I left you out to dry there by not being more specific ;).


That happens because the elapsed leftover ms is being added to SecondsRemaining. It is better to add the time remaining to TotalTimeRemaining, i.e. between the [SUB TimerPRE TimerAcc TotalTimeRemaining] but before the [DIV TotalTimeRemaining 1000 MinutesRemaining]; that will eliminate the Nm60s anomaly.

You are absolutely correct. Thank you! I'm comfortable with writing code with the process knowledge I have for the field I'm in, but with things like this I seem to get to a point where my brain just gets locked with analysis paralysis.
 

Similar Topics

Hi guys! Good day! I would like to ask, how can I possibly map the elapsed time of a simatic IEC timer? I already configured the preset time...
Replies
0
Views
2,535
I have been using Crimson 2.0 and have been using the remote view to show active run speed of equipment. I now need to display and elapsed timer...
Replies
4
Views
2,832
Hi all, I have a simple question that I have overcomplicated and gotten stuck on. I have a variable, we can call it "light" that I need to stay...
Replies
4
Views
295
Question to anyone with ideas about my thoughts on upgrading a very vintage timer, which is being used to switch between 2 5hp domestic water...
Replies
14
Views
429
why my timer only executes once here? After first time i use it, ET stays at 0ms all the time. It is in FB
Replies
5
Views
301
Back
Top Bottom