Go Down

Topic: Timing events mechanism. (Read 532 times) previous topic - next topic

kostbill

Hello.
I have a general programming question, not necessarily for arduino.

I want to create a mechanism based on a timer-counter, that will register events for some time in the future.
I must be able to register events many times, meaning that the timer expiry may be adjusted every time I register a new event.

Example:
1. register a timing event, 50 msec from now.
2. after 10 msec, register another timing event, 5 msec from now.

Now, the first registering will set the timer to 50 msec.
The second registering, will have to re-schedule the timer to 5 msec.
When the event occurs, the timer should be re-scheduled to set the timer to 35 msec.

This is a fairly complicated mechanism and I would like to adopt something already built and proven.

Do you know of such a mechanism?
Thanks.

Robin2

#1
Jan 10, 2020, 03:30 pm Last Edit: Jan 10, 2020, 03:31 pm by Robin2
Example:
1. register a timing event, 50 msec from now.
2. after 10 msec, register another timing event, 5 msec from now.
That reads like you want to go back in time - we're working on a library for that but we're not quite there yet :)

Do you mean that you want to register the second event 5msecs after the first event. Or, perhaps it should be 10 + 5 = 15 msecs after the first event.

On the other hand if you just want to create an array of times at which things should happen that should be perfectly feasible. There would be no need for all the times to be entered into the array at the start.

Humans won't be entering things at 10 msec intervals so it would be a big help if you describe the whole project so we can understand your question in its context.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kostbill

Hi,
I didn't make the question clear. I will try again with the same example and I will try to be more clear:

- Current time = time zero = 0.

1) I want a timing event to occur in 50 msec from time zero. This means that I set up the timer to overflow in 50 msec.

2) After 10 msec from time 0, I want to register another timing event, that will occur in 5 msec from now, so the timer value MUST now change:
  -- instead of overflowing in 40 msec from now, for the event I registered at step 1,
  -- it MUST overflow in 5 msec from now, for the event I registered at step 2.

3) Now I have two events that MUST occur in the future. The first event to occur will be at time 15 msec from time zero, 5 from now. The second event to occur will be at time 50 msec from time zero, 40 from now and 35 after the first event occurs.


These events are not from human interface so they are quite fast.

I hope I explained it better this time!

Robin2

The hardware timer triggers when its counter matches the value in the register (can't remember the correct name). If you change the value in the register AND if the new value is higher than the counter it will trigger when the counter gets to the new value. The old register value will be gone forever. If the counter is already higher than the new value then IIRC it will not trigger until the counter overflows and gets back to the register value.

Your program could restore the old value after the shorter event has triggered.

As I said earlier, if you describe the project it will be much easier to help.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kostbill

Hi,

I don't have a specific project in mind, I just want such a time-event mechanism.

I know how the time works, but this is not about just one timer in one chip.
I am looking for the higher layer software framework.

Of course I will have to save the previous value, then when the timer expires, I will have to have a sorted list, in a stack form (perhaps) that will pop the next time event etc.

However it seems to me that it is a difficult job, with lots of details and a lot of bugs.
That is why I don't want to re-invent the wheel and asking if there is something ready.

For example, if one needs to implement an observer-observable pattern, one will actually read lots of existing code.

Robin2

#5
Jan 13, 2020, 01:40 pm Last Edit: Jan 13, 2020, 01:41 pm by Robin2
That is why I don't want to re-invent the wheel and asking if there is something ready.
As I am the only person who has responded (so far) it may be that your Title is not attracting the attention of people with specific knowledge to help you OR there isn't an existing library.

You can change the Title by modifying your Original Post but in this case I can't think of an obvious alternative.

Quote
However it seems to me that it is a difficult job, with lots of details and a lot of bugs.
I think you have already outlined the principal mechanism and I don't think it would be so difficult to implement - but perhaps there are complications in your head that you have not shared with us. That's why it is much easier to deal with a specific requirement rather than a general concept. Having created a system to meet a specific need it may be obvious that part of the system could be extracted into a library for general use.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

westfw

For events timed on the order of 10 to 50 milliseconds, you should probably poll millis() similar to the "blink without delay" examples.  Just make sure nothing else blocks for longer than you can tolerate.

You can speed up the timer, within limits.

Trying to do something optimized with a hardware time is rarely worth it - there are intermediate optimizations if you have a lot of tasks (sorting end time so that you only have to look at the "next" event.)



dougp

A possibility:   Make a structure with two elements - an unsigned long for a timer value and a byte for a flag.  Make an array of these structures with n slots.  When a new time is to be started push the preset value into the UL and an ID to the byte.  When another timer starts push its values into the array, etc. 

Every ms make a pass through the array and decrement each non-zero UL value.  If that value becomes zero the time is expired, load the ID to some flag location to indicate completion and remove the expired timer by shifting all succeeding elements down, thus overwriting it.

An alternative to the shifting might be to let the timer value go to zero and instead of shifting have a small piece of code which locates the next empty (time expired) slot.  Pass time and ID to a function and insert where the empty flag dictates.
I don't trust atoms.  They make up everything.

No private consultations undertaken!

Go Up