Task Sequencer methods

Discuss PC Developer and Web Developer projects and features here.
Post Reply
jay_dee
Posts: 130
http://meble-kuchenne.info.pl
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 46 times
Been thanked: 29 times

Task Sequencer methods

Post 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 152 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?

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Task Sequencer methods

Post 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

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Task Sequencer methods

Post 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 149 times

jay_dee
Posts: 130
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 46 times
Been thanked: 29 times

Re: Task Sequencer methods

Post 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.

Post Reply