Page 1 of 1

Task Sequencer methods

Posted: Thu Jun 02, 2022 3:26 pm
by jay_dee
Hi,
With many programs I wish to have certain task run at a consistant rate. I've put togher a simple Task sequencer based on a counter and a MOD calculation.
Task_Sequencer_V1-0.fcsx
Sequencer
(21.27 KiB) Downloaded 158 times
I think this should work for my needs but I'm always interested in other methods that are more professional or 'cleaner' . I think the version attached is nice and simple but it might be a bit clunky. :)

This idea is that certain groups of tasks run at a repeatable period.
The master 'Counter' is done by a simple Counter+1 calculation and it could just as easily be done by an interupt based counter.
Given that any given set of tasks might take different amount of time to execute, this is not an guarenteed time execution however it is very simple and you should get a reliable spacing between your groups of tasks.
Task1 should run every Loop.
Task2 every other loop.
Task3 every 5th loop.
Taks4 every 10th loop
Task5 every 20th loop.

This is probably all very obvious stuff but not being a coder by trade I'm learning by trial and error! J. :)
Any other simple methods people regularly use?

Re: Task Sequencer methods

Posted: Thu Jun 02, 2022 4:32 pm
by mnfisher
What 'target' do you have in mind?

If it's a MCU such as esp32 - use the RTOS capabilities and let it handle the hard work for you.

There are commonly two type of multitasking (on a single core MCU - multicores are another matter) Time-slice - where the o.s. (or your code) - allocates an amount of time to each task. Each task is a 'loop' that executes when it has 'focus' - then stops (the state needs to be saved) until it next gets a time allocation.
Co-operative - where each task runs for a bit and then 'yields' to the next task.

For your use - where each task is a discreet and fairly short piece of code.

Create multiple macros Task1...TaskN

Create an interrupt routine that increments a counter (say at 1Hz)

Then (for your example)

Loop forever:
Task1() // Every loop
If(counter % 2) = 0 Task2() // Every other loop
if(counter % 5) = 0 Task3() // Every 5th loop
if(counter % 10) = 0 Task4() // Every 10th loop
etc
Sleep until next interrupt....

Martin

Re: Task Sequencer methods

Posted: Thu Jun 02, 2022 5:14 pm
by mnfisher
I tried a version of your program... I modified it slightly - in that all the indicators for a particular loop are illuminated at the same time and then extinguished at the same time (modify the task to switchon / delay / switchoff /delay if required)

If there were lots of 'tasks' or even indicators then a loop to handle them would be much more elegant.

On an MCU it would be better to use a timer interrupt to update ticks (and sleep the MCU at the end of the loop / use a flag to indicate next run)
tasks.fcsx
(19.87 KiB) Downloaded 157 times

Re: Task Sequencer methods

Posted: Tue Jun 07, 2022 2:04 pm
by jay_dee
Thanks Martin,
I'm using almost exclusivly single core low power PICs ~20 to 48mhz.
Yes agree with feedabck, my program stucture can always be improved but the Principal of activating the tasks make sense.
Thanks.