Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
Kase57
PostPosted: Feb 21, 2012 - 04:39 AM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Hi again Kartman,

Thank you for your contribution sharing this very
useful post here

So Does it mean we can't get interrupts in
a Co-operative Task Switcher? since you've mention that each task must run and finish at a definite amount of time?

Thank you.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 21, 2012 - 09:24 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71947
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

So Does it mean we can't get interrupts in
a Co-operative Task Switcher? since you've mention that each task must run and finish at a definite amount of time?


Just think of a co-op tasker as one big while(1) main() loop and in that sense it's no different to any other kind of program and there are no limits about also running ISRs alongside.

The usual rules about trying to keep ISRs as short as possible apply of course. If an ISR were to take 100ms for example then that would stop all the co-operating foreground tasks for all that time. So, as always, make it just a few time sensitive instructions and put any "long work" in another foreground task that usually does nothing but is triggered into action by some flag set in the ISR. The usual rules for foreground task timing apply to it too - so if it will take more than the usual task time limit then break its work up into a multi-stage state machine too.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Kase57
PostPosted: Feb 22, 2012 - 04:03 AM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Thank you again Cliff. it makes sense.
I am getting to know how things should really be done

If we can in some of the pseudo code that Kartman gave for ex.

1. for timers=0 to 7
we want to do
2. if timer[timers] >0, decrement timer[timers].
if timer[timers] = 0 then set_task timers.
3. next timers

i was concerned on this piece of code.

Code:


while (task < NUM_TASKS )
    {
    if (task_timers[task])
      {
        task_timers[task]--;            /* dec the timer */
       if (task_timers[task] == 0 )
            {
          set_task(task); /* if ==0 activate the task bit */
         }
      }
    task++;
    }


shouldn't on the 1st if statement have been
Code:

   if (task_timers[task]>0)   
     { 
        task_timers[task]--;
      }  // etc..

rather than if (task_timers[task])
Since Kartman said earlier that we want to decrement
the associated timer if it's found to be greater than 0?

Cheers.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 22, 2012 - 05:29 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 22314
Location: Lund, Sweden

Since task_timers is unsigned it can never be < 0, so the test is good as Kartman gave it.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Kase57
PostPosted: Feb 22, 2012 - 11:28 AM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Thank you answer Johan

You are right unsigned int can never be < 0,
but it can be greater than zero, which is the case
i was talking about that if is ever found to be > 0.
Code:

  if (task_timers[task]>0)
     {
       task_timers[task]--;
      }

then we would decrement it?
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Feb 22, 2012 - 11:50 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 22314
Location: Lund, Sweden

But testing with just
Code:
    if (task_timers[task])

for unsigned does just the same! Remember that as long as something is != 0 it is true.

So, technically it will do just as fine as testing for > 0.

Whether it would be clearer for the reader of the code to use > 0 is probably a matter of opinion.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Kase57
PostPosted: Feb 22, 2012 - 12:46 PM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Thank you Johan,

very well explained Smile


Last edited by Kase57 on Jun 28, 2012 - 04:25 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
Kase57
PostPosted: Feb 22, 2012 - 01:31 PM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Shouldn't also code

Code:

while (task <= NUM_TASKS )
    {
     if ((task_bits & pgm_read_byte(&bit_mask[task])))
           {
           break; /* if activate task found..*/
         }
      task++;         /* else try the next one */
    }

really be
Code:

while (task < NUM_TASKS )  // a slight change here for "<="
    {
     if ((task_bits & pgm_read_byte(&bit_mask[task])))
           {
           break; /* if activate task found..*/
         }
      task++;         /* else try the next one */
    }

since
Code:

unsigned int task_timers[NUM_TASKS]={0,0,0,0,0,0,0,0};

has 8 elements starting from 0 which makes the last element be the 7th?

thus from while (task <= NUM_TASKS ) this allows the 8th element aswell no? which might probably lead to access of the memory outside the array boundary when compiler gets to this if statement, as the last element is the 7th

Code:

if ((task_bits & pgm_read_byte(&bit_mask[task])))


Thanks all.
 
 View user's profile Send private message  
Reply with quote Back to top
Kase57
PostPosted: Feb 25, 2012 - 02:07 PM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Hi again all,

Kartman said that for example task 1 might need task 2 to take (run) 1sec!
So i understood that creating task_timers[NUM_TASKS]={0,0,0,0,0,0,0,0}; gives us extra opportunities to specify the time we want that task to run plus to keep in track if its flag is ON.
I was concerned about initializing the task to a certain amount of time! like some task run in 250 ms? won't this affect/delay other tasks as we had 10 ms for each task to run?
If i understood right we might want some tasks to take longer than others as long as the finish and other can also follow?
(which is the matter of the facts Kartman said that the worst case scenario would be having the task which will need to be done on every single 10 ms basis)
But then if the task could take longer than others as long as it finishes and allow other also to run would be fine?

(just like Cliff suggested like in the while (1) loop, the most important thing is that every single task will be done although some may take longer than others) right?

Thank all.


Last edited by Kase57 on Jun 28, 2012 - 03:24 PM; edited 2 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
Kase57
PostPosted: Mar 03, 2012 - 04:02 AM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Just out of curiosity question, Does anyone knows What adding a RTOS in this Cooperative Multitasking would benefit?

I am just learning on Multitasking RTOS.

Cheers.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 03, 2012 - 09:45 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71947
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Does anyone knows What adding a RTOS in this Cooperative Multitasking would benefit?

Your question makes little sense - are you saying you would run a secondary co-operative tasking system within a single thread of a pre-emptive, time-slicing tasking system? To what gain? If you have a pre-emptive scheduler then why not make all tasks separate - that's kind of the point of pre-emption isn't it?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Kase57
PostPosted: Mar 04, 2012 - 05:27 AM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

Thank you for your comment Cliff, apologize me if you think i am asking a silly question, but is any "time slicing operation also a RTOS" ?
I think yes! since I can myself define a RTOS as an operating system which has a consistent timing and high degree of task priority.

so it seems like my original question was whether a RTOS is a software or hardware or if we can we it in have both parties,


(again my apologies if you think i am asking a silly question)

Thanks.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 04, 2012 - 03:21 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71947
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

but is any "time slicing operation also a RTOS" ?

It's an "OS" but whether it is "T" is another matter. For example Linux is a time slicing OS but even when you apply the real-time patches to the kernel it's still very difficult to guarantee true real time operation. The latency on interrupt handling can be quite a problem!
Quote:

so it seems like my original question was whether a RTOS is a software or hardware or if we can we it in have both parties,

There is no question it's simply a piece of software however it would be difficult to envisage a time slicing OS that does not use some form of timer. In Linux and Windos this means a 10ms or 1ms timer that switches between anything up to about 10,000 threads of execution at any one time.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Janitorr
PostPosted: Mar 08, 2012 - 12:48 PM
Wannabe


Joined: Apr 06, 2006
Posts: 90
Location: Sweden

Great tutorial Kartman, as always!

This is supposed to be a question, but I am not really sure how to ask it, but here goes:

In the code example, task6 and task7 will end up running in roud-robin style after the first execution of each task, because of the higher priority of task6, right? But sometimes you would want two tasks running syncronously, not one time slot apart.
So what would the solution be then? I guess there is only one, do a task with two sub-tasks in one time slot?


I hope someone understand what I am talking about.
 
 View user's profile Send private message  
Reply with quote Back to top
Kartman
PostPosted: Mar 20, 2012 - 06:26 AM
10k+ Postman


Joined: Dec 30, 2004
Posts: 11883
Location: Melbourne,Australia

Say you wanted task 6 & 7 to run every second but half a second apart. Task6 would load the task timer for task7 with half a second. Similarly, task7 would load the timer for task6 with half a second. These two tasks would happily ping pong.
 
 View user's profile Send private message  
Reply with quote Back to top
Janitorr
PostPosted: Apr 04, 2012 - 09:07 AM
Wannabe


Joined: Apr 06, 2006
Posts: 90
Location: Sweden

Hehe Smile I actually meant the other way, two tasks beeing executed in the SAME time slot.

What it all comes down to is probably that I don't see how the prioritization actually is thought to work.

I see the tasks running in round-robin style after all tasks have been executed once and everything has settled. With this the highest priority task will simply be the first one in the "round-robin schedule".

Ok, last thought/question, why would one want to use prioritization? I obviously fail, big time, to see the reason Sad
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Apr 04, 2012 - 09:21 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71947
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Ok, last thought/question, why would one want to use prioritization?

Windows (these days) does it for example. Things like the mouse and keyboard service have a higher priority than when, for example, Excel is recalculating a spreadsheet. In days gone by you spent a lot of time looking at an "hour glass" mouse cursor (often that would not move) while Excel calculated.

Even when you aren't using an OS you do the same in the design of your main/interrupt mixed programs where you do things like updating LCDs in the "spare time" while things like reading the spindle rotation or operating the control valves is done as a matter of urgency.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Ghost90
PostPosted: Jun 07, 2012 - 07:48 PM
Newbie


Joined: May 03, 2012
Posts: 6


At first I wanna thank the author for writing this great tutorial!
I have been searching a long time for a easy to use and understand "RTOS" or something that could handle tasks. After I never got DuinOS running and FreeRTOS and others were just too complicated to get it running with the arduino IDE I thought I would never find something, untill I found this tutorial.
So I just wanted to say that it works perfect with the arduino IDE, just copy-> paste and rename the "main" into "loop" and you can have fun with the arduino libraries and create tasks.

THANKS!!! you saved me a lot of time!!!
 
 View user's profile Send private message  
Reply with quote Back to top
Kartman
PostPosted: Jun 13, 2012 - 04:38 AM
10k+ Postman


Joined: Dec 30, 2004
Posts: 11883
Location: Melbourne,Australia

Another happy customer! I'm glad you grasped the concept and changed the code to suit your requirements.
 
 View user's profile Send private message  
Reply with quote Back to top
Kase57
PostPosted: Jun 27, 2012 - 11:05 AM
Hangaround


Joined: Jan 14, 2012
Posts: 127
Location: New Zealand

He is not the only one Mate.
I myself really have found this tutorial extremely useful I dont know if it was because i am actually new into RTOS & Multitasking, But I very much appreciated all the effort you put into this tutorial man. except that i forgot to say Thank you Wink
I really wanted to thank you Kartman, Cliff, Johan, and many other moderators here
at avrfreaks who really put a lot of effort into helping us.


Cheers Guys.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits