mikaelpatel / Arduino-Scheduler

Portable Cooperative Multi-tasking Scheduler for Arduino

Home Page:https://mikaelpatel.github.io/Arduino-Scheduler/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question

opened this issue · comments

Hi,
i have one a bit more complex project in mind and i wanted to use cooperative multitasking to make the code simpler. My project will use one software serial port, one i2c device and 4 pwm pins (hardware) and I have a few questions about this library:

  1. The Arduinos only have one core and this library only executes a task when i want it to so why does it include thread safe data structures? Where is the problem with just communicating using a volatile variable?

  2. Can i use this library together with Cosa?

  3. What advantages does this library have over TaskScheduler (https://github.com/arkhipenko/TaskScheduler) ?
    The latter does not save the context so i think you can't pause the execution of the current task and execute another. So saving the context seems to be better but i'm not sure what disadvantages it has code wise. Could I for example run into problems with using the same instance of a class from multiple tasks?

I would be nice if you could answer these questions or explain me what i have to lookup to answer them myself.
Also thanks for providing such a nice library as Cosa. I have tried the examples yet but just looking at it I like the programming style and design of it much more than that of the standard arduino library :)

@marc010 Thanks for your interest in Cosa and this Scheduler library for arduino. Some attempts to answer your questions:

  1. There are many levels of concurrency. Each needs mechanisms for communication and synchronization. On the lowest level hardware units are running in parallel and synchronized to a common clock, event or state. The arduino/avr MCU has only one computation core but there are a lot of communication cores. These together with timers, etc, can interrupt the core. This library is to handle collaborative multi-tasking, i.e., the task decides when to yield to another task (on function yield() or delay()). On this level resources also need to be protected to maintain correct state. In this simple library this is done with yield based busy-waiting. This allows a simple single thread queue.

  2. No. This library is actually adapted from Cosa Nucleo. The arduino core doesn't really allow multi-tasking of device drivers (e.g. I2C, SPI, UART). The basic device driver support in the arduino core does not yield or protect (mutual exclude) calls to the core functions. Cosa does :)

  3. I would not really call that tasks. That is more delayed or periodic functions. It is possible to write multi-task "like" processing with this together with objects but only on a very simple level. It can become complex with larger projects. The functions may become very complex state machines to handle resources correctly.

Hope this can be of some help.

Cheers! Mikael