Time between two events, Studio5000

Hey All, I have a question that i have been bugging me for about a year now. I think it is time i tackle this problem head on and try to produce a solution. You help would be appreciated.

1716274768246.png

So imagine i have some reoccurring events. Such as scheduled events that occur every week. Like a garden timer. Specifically a garden timer.

For the sake of this explanation i have drawn up a couple of random events, and "NOW".

So the image shows EVENT 1 occurs every Tue, 2313hrs. EVENT 2 occurs every Fri, 2330hrs. The third relevent event is NOW, which is this moment of time.

I want to create like a countdown timer between NOW and the NEXT event. In this example, the next event is EVENT1.

Task 1: Create a code to identify which event, EVENT 1 or EVENT 2, is the NEXT event.

One of the problems i face around creating a countdown timer is that the events are reoccurring. So for example, now is Saturday, the next event is Tuesday, then the next event after that is Friday. After friday's event, the next event is Tuesday, then Fri, and so on forever.

Task 2: Create a code that counts down the time between NOW and the NEXT event.

I have a very crude idea as to what i plan to do. I would love to try and explain it here but it would take forever to explain, and it would likely not be the final solution anyhow.

Has anyone tried to do something like this in the past?
 
Hey All, I have a question that i have been bugging me for about a year now. I think it is time i tackle this problem head on and try to produce a solution. You help would be appreciated.

View attachment 70072

So imagine i have some reoccurring events. Such as scheduled events that occur every week. Like a garden timer. Specifically a garden timer.

For the sake of this explanation i have drawn up a couple of random events, and "NOW".

So the image shows EVENT 1 occurs every Tue, 2313hrs. EVENT 2 occurs every Fri, 2330hrs. The third relevent event is NOW, which is this moment of time.

I want to create like a countdown timer between NOW and the NEXT event. In this example, the next event is EVENT1.

Task 1: Create a code to identify which event, EVENT 1 or EVENT 2, is the NEXT event.

One of the problems i face around creating a countdown timer is that the events are reoccurring. So for example, now is Saturday, the next event is Tuesday, then the next event after that is Friday. After friday's event, the next event is Tuesday, then Fri, and so on forever.

Task 2: Create a code that counts down the time between NOW and the NEXT event.

I have a very crude idea as to what i plan to do. I would love to try and explain it here but it would take forever to explain, and it would likely not be the final solution anyhow.

Has anyone tried to do something like this in the past?
First question, Is this homework?
 
How accurate does it have to be, e.g. +/-1s?

What programming environment (make, model, ladder vs. ST vs. ?, etc.)?
Hey Dr.B. I would have thought by now you would know what programming environment i am using.

Ladder Logic. RSLogix5000. Compactlogix 1769 L35E.

Accuracy. It does not need to be very accurate at all. +/- 15 min
 
I have never done this either but getting the wall clock time (converted to hours in a month or week) and comparing it to a set date (converted to hours in a month or week) would give you a reasonable number to compare for your countdown.
 
It matters if they are independent events or dependent events.

The reason being if they are independent, eventually there will be a sequence like so: e₁ e₂ e₁ e₁ e₂. If this is the case, initialize two timestamps T₁ T₂ to the current time (using unix-like timestamps). When e₁ occurs, set T₁ := T + (T - T₁) where T is the current time. Do the same for e₂: T₂ := T + (T - T₂). Then just keep comparing the current time to the lesser of T₁ and T₂

For dependent events, they will always alternate. Actually the above method works for that too, so it turns out it doesn't really matter and was only useful as a topic of discussion.
 
It matters if they are independent events or dependent events.

The reason being if they are independent, eventually there will be a sequence like so: e₁ e₂ e₁ e₁ e₂. If this is the case, initialize two timestamps T₁ T₂ to the current time (using unix-like timestamps). When e₁ occurs, set T₁ := T + (T - T₁) where T is the current time. Do the same for e₂: T₂ := T + (T - T₂). Then just keep comparing the current time to the lesser of T₁ and T₂

For dependent events, they will always alternate. Actually the above method works for that too, so it turns out it doesn't really matter and was only useful as a topic of discussion.
Hey Chris, thank you for your feedback. All events are independant. They are literally a timer start time entered by a user (the gardener in this case is me).

Your suggestion regarding time stamps is what i had in mind. I am still trying to get my head around how i am going to achieve this using ladder logic in a Logix5000 environment.

Acquiring time stamps in logix5000 has never been straight forward.

"Using unix-like timestamps". I do not know what this means. Can you elaborate?
 
Unix systems keep time as an integer count of seconds since midnight on January 1st, 1970. The PLC has a similar system but they chose 1972, and instead of seconds they accumulate microseconds.

1716304269066.png

You need to use 64-bit instructions for this however, but typically you just need to divide by 10^6 to get it back to seconds, which will be a 32-bit number until 2038.
 
Unix time tracks time as the total seconds since January 1st, 1970
~
When I need time and date I use a GSV (Get SystemValue)
Class Name = WallClockTime
Instance Name = (Blank)
Attribute Name = LocalDateTime
Dest = YourArray[0]

You just need to make a Dint Array with 7 elements.
 
Unix time tracks time as the total seconds since January 1st, 1970
~
When I need time and date I use a GSV (Get SystemValue)
Class Name = WallClockTime
Instance Name = (Blank)
Attribute Name = LocalDateTime
Dest = YourArray[0]

You just need to make a Dint Array with 7 elements.
The unix time is a good idea.

I have the GSV set up. It is critical for any kind of reasonable scheduling.
 
Another option is to calculate the day of the week. Logix Designer includes a sample program from Rockwell to do that.
To get to it easily, open Logix Designer. On the splash screen pick "Sample Project" under "Open" and open the "Samples" directory. Find DayOfWeek.L5X and import it into a new project.

It may be easier to do that than convert your event time setpoint to the PLC's version of Unix time.
 
(you are most of the way there with the "Eunuchs(tm)" time; I was helping with grandchildren.)

Are the events actual inputs (XIC Local:I.0.0 => event 1 occured on this scan cycle, and will occur at the same time next week), or are they [weekday, time of day] stored in the PLC?

Background and other considerations
  • "A microfortnight is about one second."
  • All time measurements are more or less linear, continuous, and relative to some zero epoch (time).
  • The "Unix(tm)" time is seconds since the Unis(tm) epoch of 1970-JAN-01T00:00:00,
    • although the Logix/GSV version of Unix(tm) time is integer microseconds since that epoch.
    • 1970-JAN-01 was a Thursday.
  • There are roughly PI x 107 seconds in a year,
    • so that Unix(tm) time is currently between 1.5Gs and 2Gs; 32-bit signed integers roll over at 2G+ (2038 A.D.),
      • unsigned integers at 4G+ (2106 A.D.).
    • The Logix/GSV value is 64-bit,
      • and will not rollover in a timeframe we care about;
      • if 1769-L35E is in the 5370 family, then those 64-bit GSV values will comprise two 32-bit integers, probably signed (DINT, not UDINT)
  • The Earth's rotation is slightly slower than 86,400 linear (SI i.e. atomic-timed) seconds per day, slower by very close to the same amount as the New Horizons spacecraft clock, so there is a leap second adjustment between linear seconds and civil time roughly every third year.
604,800

This is the number of seconds in a week. Add six zeros to get the number of microseconds in a week. If the PLC reduces the current Unix(tm) time MODULO* this number (or reduces the 64-bit GSV WALLCLOCKTIME.CurrentValue MODULO 604,800,000,000) using the MOD instruction and DINTs, we get the number of seconds (or microseconds) since the most recent midnight between Wednesday and Thursday. Let's call this value Tnow, which will always be a value in the range [0:604,800).

* remainder after division

To simplify this explanation let's drop the microseconds, and work with seconds (e.g. assume we divided the 64-bit WALLCLOCKTIME.CurrentValue value in unsigned form by a million) and have the vallue in a DINT.

For any event type, we know the day of week, hour, minute, and second of the next event of that type, we can calculate that event's next time "second past" its previous Thursday, call that number Tevent, which will again be a value in the range [0:604,800).

So the number of seconds from now (Tnow) until the next occurence of an event (Tevent) will be (Tevent - Tnow), with the caveat that, if Tevent is less than Tnow, that means that the event occurs after the next week rollover (Wednesday-Thursday midnight), and Tevent should be increased by 6048000 to account for that; call this Tevent*. Here are some examples:
  • Current time is Thursday night, Friday morning midnight; event time is Monday night, Tuesday morning midnight
    • Tnow = 86,400s (= 1d x 24h/d x 60min/h x 60s/min, one day after Wednesday night, Thursday morning midnight)
    • Tevent = 432,000s (= 5d x 86400s/d, five days after Wednesday night, Thursday morning midnight)
    • Tevent <= Tnow
      • Tevent* = Tevent = 432,000s
    • Tremaining = Tevent* - Tnow = 345,600s (= 4d x 86,400s/d)
  • Event time is Thursday night, Friday morning midnight; current time is Monday night, Tuesday morning midnight
    • Tnow = 432,000s
    • Tevent = 86,400s
    • Tevent > Tnow, add 604800 to Tevent to get Tevent*:
      • Tevent* = Tevent + 604,800a = 619,200a
    • Tremaining = Tevent* - Tnow = 259200s (= 3d x 86,400s/d)
So the PLC can convert from GSV WALLCLOCKTIME.CurrentValue to Tnow on each scan cycle, and quickly calculate a Tremaining for each event type, and finally select and display whichever event has the lowest Tremaining.

That part is simple. The bulk of the code will revolve around handling edge cases:
  • the 64-bit unsigned WALLCLOCKTIME.CurrentValue comprising two 32-bit DINTs, assuming the 1769 L35E does not have the 64-bit LINT data type.
    • I think Rockwell has an AOI that can do 64-bit math with pairs of 32-bit DINTs.
  • Daylight Savings Time
    • See WALLCLOCKTIME.ApplyDST
 
I have come up with something i believe is workable. The core idea of comparing the dates from a reference point was the key to this solution.
I will break up my explaination over a few posts as the diagrams will likely take up a lot of room.

To add clarity to the problem, the events, (eg. Event 1 and Event 2) are scheduled into a weekly timer and it repeats as you might expect from a weekly timer.

This is the HMI screen to enter the event dates and times. This screen allows the user to select the days that an event occurs, and the times events occur on the chosen days. for example, if you wanted to water your roses on monday, wed, Fri, at 0100hrs and 1500hrs, then you would select the respective days, and you would enter the times on the available numeric input enabled buttons. Take note of the "Save" button, this will be mentioned later. Each day can accommodate up to 4 event times.

1716350392249.png


In my original post, i uploaded a linear timeline. I realise now i should not be thinking of the timeline as linear as much as i should be thinking of it as more circular. The reason for this is because the events repeat link a sine wave.

Here is the new timeline that includes 4 off scheduled events and 2 off Now events.

1716350424310.png
 

Similar Topics

Hi everyone, I want to know if in S7 300, there is a standard watch or something like that to measure the elapsed time between two events. And...
1
Replies
4,788
Views
I'm on site with an old TSX PLC and I have a rewritable card available for online debugging, but I can't figure out how to use the "MEM EXTRACT"...
1
Replies
82
Views
Hello everyone, I am working on a project to retrofit a harvesting head for log bucking onto a Terex Fuchs excavator. I have built a new...
3
Replies
188
Views
I want to connect a Schneider PLC (model TSX P57103) to a Delta HMI (model DOP-107DV). Which communication protocol should I choose? I believe...
0
Replies
121
Views
Hello, Earlier this week, one of our technicians powered down a cabinet containing a PanelMate Plus 1000 and an AEG‑A984‑145 PLC for several...
5
Replies
279
Views
Top