arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.33k stars 243 forks source link

wrong calculation of overrun? #104

Closed kwrtz closed 4 years ago

kwrtz commented 4 years ago
                iCurrent->iPreviousMillis += iCurrent->iDelay;

#ifdef _TASK_TIMECRITICAL
                // Updated_previous+current interval should put us into the future, so iOverrun should be positive or zero.
                // If negative - the task is behind (next execution time is already in the past)
                unsigned long p = iCurrent->iPreviousMillis;
                iCurrent->iOverrun = (long)(p + i - m);

In the first line you calculate the future when the task should run again. In iOverrun you add the interval again. Is that correct? Because now you have two times delay added.

arkhipenko commented 4 years ago

It might be a bit confusing but is correct. iPreviousMillis is always a point in time of the last start. So iPrevious + iDelay should be right after or equal to now, and another interval should put you in the future. If that next interval does not put you in the future, you know you are in the overrun situation.