Jump to content

Recommended Posts

I have the following timer logic to display elapsed time in Hours.

Minute Timer to increment MI 0

If MI 0 = 6, Increment MI 1 and Store 0 in MI 0.

Hence MI 1 should be equal to tenths of elapsed time in hours.

Many of the units we shipped is off by almost an hour after about 1200 hours. Am I doing anything wrong?

Share this post


Link to post
Share on other sites

Let me guess. Your timer is running 60000 milliseconds. In the last program cycle, let us say, 59998ms, is still not elapsed. The first scan which "sees" the timer elapsed, is the next cycle, e.g. 60004ms. Now you have lost 4ms and you will start next instance of the timer. In the second minute the situation will be similar and the error will accumulate. After many loops of the timer you can get gross inaccuracy.

The better solution of your task is to use system real time clock: In the system integer SI30 you have actual second in minute. If your minute counter will be incremented if ((SI30 == 0) AND (SB13 == ON)), your task will be more accurate. (SB13 is on every second for one program cycle).

Next possible solution is using of SI36, minute in hour. If you put (((SI36 mod 6) == 0) AND (SI30 == 0) AND (SB13 == ON)), you will get one pulse every six minutes, i.e. every one tenth of hour. ("Mod" stands for modulo, the remainder after integer division).

Share this post


Link to post
Share on other sites

Thank you very much Stembera. That makes perfect sense. I may not completely use your solution since I want to calculate elapsed time that may not have any relation to the real time clock. I would rather use SB13 or SB3 to increment MI0 and increment MI1 when MI0=360.

Share this post


Link to post
Share on other sites

Why wouldn't "elaspsed time" have anything to do with the RTC??

When the start condition happens store the RTC in a variable.

Now have a Subtraction function block that takes the current value of the RTC, subtracts the Start value, and places into another variable (your elapsed time).

That is the most accurate elasped time you will be able to get.

Share this post


Link to post
Share on other sites

I just put up a Blog entry which touches on this exact issue - have a look at it here.

What you seem to be running into is the fact that when you try to accumulate time values, you have a small amount of error due to the scan time of the PLC. It's possible to get around this by using timed interrupt routines and counting up time in 2.5 millisecond increments - but for longer values, it's easier to use the RTC->UTC conversion function to get a value for "Now" in seconds, save that value as your "Start" time, then calculate elapsed time with ("Now" - "Start").

  • Upvote 1

Share this post


Link to post
Share on other sites

You are right. Thank you very much. Do you think the SB3 method will accumulate errors?

Why wouldn't "elaspsed time" have anything to do with the RTC??

When the start condition happens store the RTC in a variable.

Now have a Subtraction function block that takes the current value of the RTC, subtracts the Start value, and places into another variable (your elapsed time).

That is the most accurate elasped time you will be able to get.

Share this post


Link to post
Share on other sites

Considerably less than using a timer, which adds an average of 1/2 scan time each time the timer turns on. The SB3 method will add up to 1 timer period at the beginning of the timing, and 1/2 scan time to the overall amount, but since the time base is constant and not dependent on the scan of the program, it should work as well as using the RTC to UTC method, and almost as well as counting 2.5ms interrupts.

Share this post


Link to post
Share on other sites

Thank you very much Stembera. That makes perfect sense. I may not completely use your solution since I want to calculate elapsed time that may not have any relation to the real time clock. I would rather use SB13 or SB3 to increment MI0 and increment MI1 when MI0=360.

If you want to calculate elapsed time you can use the UTC time instead RTC, UTC is in seconds, this way if you substract the start value of the end value you will get the elapsed seconds.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...