README 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. Task Scheduler – cooperative multitasking for Arduino and ESP8266 microcontrollers
  2. Version 2.5.2: 2018-01-09
  3. If you find TaskScheduler useful for your Arduino project, please drop me an email: arkhipenko@hotmail.com
  4. ----------------------------------------------------------------------------------------------------------
  5. OVERVIEW:
  6. A lightweight implementation of cooperative multitasking (task scheduling) supporting:
  7. 1. Periodic task execution (with dynamic execution period in milliseconds or microseconds)
  8. 2. Number of iterations (n times)
  9. 3. Execution of tasks in predefined sequence
  10. 4. Dynamic change of task execution parameters (frequency, number of iterations, callback function)
  11. 5. Power saving via entering IDLE sleep mode between tasks are scheduled to run
  12. 6. Task invocation via Status Request object
  13. 7. Task IDs and Control Points for error handling and watchdog timer
  14. 8. Local Task Storage pointer (allowing use of same callback code for multiple tasks)
  15. 9. Layered task prioritization
  16. 10. Support for std::functions (ESP8266 only)
  17. Scheduling overhead: between 15 and 18 microseconds per scheduling pass (check the benchmark example).
  18. Tested on the following microcontrollers:
  19. - Arduino Uno R3
  20. - Arduino Nano
  21. - Arduino Micro
  22. - ATtiny85
  23. - ESP8266 (Node MCU v2.0)
  24. - Teensy (tested on Teensy 3.6)
  25. For detailed functionality overview please refer to TaskScheduler documentation in the 'extras' folder.
  26. =======================================================================================================
  27. Check out what TaskScheduler can do:
  28. ====================================
  29. Endeavor - build a space capable craft, with the ability to maneuver both in and out of atmosphere.
  30. (by Radical Space Technologies
  31. http://radicalspacetechnologies.com/2015/10/01/endeavor-phase-i/
  32. "So after some digging I found TaskScheduler an awesome little library developed by Anatoli Arkhipenko,
  33. his github is here and the documentation is here. Its fantastic, so far I’ve been able to replicate
  34. some things I’ve seen in Ardupilot. ..."
  35. link: http://radicalspacetechnologies.com/2015/10/25/endeavors-code-definitely-progress/
  36. 3 Devo - Quality 3D printing filament, now made accessible and affordable
  37. http://3devo.eu/ (http://3devo.eu/license-information/)
  38. Houston midi clock project - TaskScheduler with microseconds resolution
  39. (by chaffneue: My first arduino project. It's a multi-master midi controller with a shared clock and
  40. auto count in behaviour. Project files on github: https://github.com/chaffneue/houston
  41. youtube: https://www.youtube.com/watch?v=QRof550TtXo)
  42. Hackabot Nano - Compact Plug and Play Arduino compatible robotic kit
  43. (by Funnyvale: http://hackarobot.com/
  44. also: https://www.kickstarter.com/projects/hackarobot/hackabot-nano-compact-plug-and-play-arduino-robot)
  45. Arduino Nano based Hexbug Scarab Robotic Spider
  46. (by arkhipenko: http://www.instructables.com/id/Arduino-Nano-based-Hexbug-Scarab-Robotic-Spider/)
  47. Wave your hand to control OWI Robotic Arm... no strings attached
  48. (by arkhipenko: http://www.instructables.com/id/Wave-your-hand-to-control-OWI-Robotic-Arm-no-strin/)
  49. APIS - Automated Plant Irrigation System
  50. (by arkhipenko: http://www.instructables.com/id/APIS-Automated-Plant-Irrigation-System/)
  51. IoT APIS v2 - Autonomous IoT-enabled Automated Plant Irrigation System
  52. (by arkhipenko: http://www.instructables.com/id/IoT-APIS-V2-Autonomous-IoT-enabled-Automated-Plant/)
  53. Interactive Halloween Pumpkin
  54. (by arkhipenko: http://www.instructables.com/id/Interactive-Halloween-Pumpkin/)
  55. Changelog:
  56. =========
  57. v2.6.0:
  58. 2018-01-30 - _TASK_TIMEOUT compilation directive: Task overall timeout functionality
  59. 2018-01-30 - Support for ESP32
  60. v2.5.2:
  61. 2018-01-09 - _TASK_INLINE compilation directive making all methods declared "inline" (issue #42)
  62. v2.5.1:
  63. 2018-01-06 - support for IDLE sleep on Teensy boards (tested on Teensy 3.6)
  64. v2.5.0:
  65. 2017-04-27 - added optional support for std::functions via _TASK_STD_FUNCTION
  66. (Contributed by Edwin van Leeuwen [BlackEdder - https://github.com/BlackEdder)
  67. 2017-08-30 - add _TASK_DEBUG making all methods and variables public FOR DEBUGGING PURPOSES ONLY!
  68. *** USE AT YOUR OWN RISK ***
  69. 2017-08-30 - bug fix: Scheduler::addTask() checks if task is already part of an execution chain (github issue #37)
  70. 2017-08-30 - support for multi-tab sketches (Contributed by Adam Ryczkowski - https://github.com/adamryczkowski)
  71. v2.4.0:
  72. 2017-04-27 - added destructor to the Task class to ensure tasks are disables and taken off the execution chain
  73. upon destruction. (Contributed by Edwin van Leeuwen [BlackEdder - https://github.com/BlackEdder)
  74. v2.3.0:
  75. 2017-02-24 - new timeUntilNextIteration() method within Scheduler class - inquire when a particlar task is
  76. scheduled to run next time
  77. v2.2.1:
  78. 2016-11-30 - inlined constructors. Added "yield()" and "yieldOnce()" functions to easily break down and
  79. chain back together long running callback methods
  80. 2016-12-16 - added "getCount()" to StatusRequest objects, made every task StatusRequest enabled.
  81. Internal StatusRequest objects are accessible via "getInternalStatusRequest()" method.
  82. v2.2.0:
  83. 2016-11-17 - all methods made 'inline' to support inclusion of TaskSchedule.h file into other header files
  84. v2.1.0:
  85. 2016-02-01 - support for microsecond resolution
  86. 2016-02-02 - added Scheduler baseline start time reset method: startNow()
  87. v2.0.1:
  88. 2016-01-02 - bug fix: issue#11 Xtensa compiler (esp8266): Declaration of constructor does not match implementation
  89. v2.0.0:
  90. 2015-12-22 - _TASK_PRIORITY - support for layered task prioritization
  91. v1.9.2:
  92. 2015-11-28 - _TASK_ROLLOVER_FIX is deprecated (not necessary)
  93. 2015-12-16 - bug fixes: automatic millis rollover support for delay methods
  94. 2015-12-17 - new method for _TASK_TIMECRITICAL option: getStartDelay()
  95. v1.9.0:
  96. 2015-11-24 - packed three byte-long status variables into one byte-long bit array structure data type - saving 2 bytes per each task instance
  97. v1.8.5:
  98. 2015-11-23 - bug fix: incorrect calculation of next task invocation in case callback changed the interval
  99. 2015-11-23 - bug fix: Task::set() method calls setInterval() explicitly, therefore delaying the task in the same manner
  100. v1.8.4:
  101. 2015-11-15 - bug fix: Task alignment with millis() for scheduling purposes should be done after OnEnable, not before. Especially since OnEnable method can change the interval
  102. v1.8.3:
  103. 2015-11-05 - support for task activation on a status request with arbitrary interval and number of iterations (0 and 1 are still default values)
  104. 2015-11-05 - implement waitForDelayed() method to allow task activation on the status request completion delayed for one current interval
  105. 2015-11-09 - added callback methods prototypes to all examples for Arduino IDE 1.6.6 compatibility
  106. 2015-11-14 - added several constants to be used as task parameters for readability (e.g, TASK_FOREVER, TASK_SECOND, etc.)
  107. 2015-11-14 - significant optimization of the scheduler's execute loop, including millis() rollover fix option
  108. v1.8.2:
  109. 2015-10-27 - implement Local Task Storage Pointer (allow use of same callback code for different tasks)
  110. 2015-10-27 - bug: currentTask() method returns incorrect Task reference if called within OnEnable and OnDisable methods
  111. 2015-10-27 - protection against infinite loop in OnEnable (if enable() methods are called within OnEnable)
  112. 2015-10-29 - new currentLts() method in the scheduler class returns current task's LTS pointer in one call
  113. v1.8.1:
  114. 2015-10-22 - implement Task id and control points to support identification of failure points for watchdog timer logging
  115. v1.8.0:
  116. 2015-10-13 - support for status request objects allowing tasks waiting on requests
  117. 2015-10-13 - moved to a single header file to allow compilation control via #defines from the main sketch
  118. v1.7.0:
  119. 2015-10-08 - introduced callback run counter - callback functions can branch on the iteration number.
  120. 2015-10-11 - enableIfNot() - enable a task only if it is not already enabled. Returns true if was already enabled, false if was disabled.
  121. 2015-10-11 - disable() returns previous enable state (true if was enabled, false if was already disabled)
  122. 2015-10-11 - introduced callback functions "on enable" and "on disable". On enable runs every time enable is called, on disable runs only if task was enabled
  123. 2015-10-12 - new Task method: forceNextIteration() - makes next iteration happen immediately during the next pass regardless how much time is left
  124. v1.6.0:
  125. 2015-09-22 - revert back to having all tasks disable on last iteration.
  126. 2015-09-22 - deprecated disableOnLastIteration method as a result
  127. 2015-10-01 - made version numbers semver compliant (documentation only)
  128. v1.5.1:
  129. 2015-09-21 - bug fix: incorrect handling of active tasks via set() and setIterations().
  130. Thanks to Hannes Morgenstern for catching this one
  131. v1.5.0:
  132. 2015-09-20 - access to currently executing task (for callback functions)
  133. 2015-09-20 - pass scheduler as a parameter to the task constructor to append the task to the end of the chain
  134. 2015-09-20 - option to create a task already enabled
  135. v1.4.1:
  136. 2015-09-15 - more careful placement of AVR-specific includes for sleep functions (compatibility with DUE)
  137. sleep on idle run is no longer a default and should be explicitly compiled with _TASK_SLEEP_ON_IDLE_RUN defined
  138. v1.0.0:
  139. 2015-02-24 - Initial release
  140. 2015-02-28 - added delay() and disableOnLastIteration() functions
  141. 2015-03-25 - changed scheduler execute() function for a more precise delay calculation:
  142. 1. Do not delay if any of the tasks ran (making request for immediate execution redundant)
  143. 2. Delay is invoked only if none of the tasks ran
  144. 3. Delay is based on the min anticipated wait until next task _AND_ the runtime of execute function itself.
  145. 2015-05-11 - added restart() and restartDelayed() functions to restart tasks which are on hold after running all iterations
  146. 2015-05-19 - completely removed delay from the scheduler since there are no power saving there. using 1 ms sleep instead