Multi-Rate Main Loop Tasking

Multi-Rate Main Loop Tasking - Hallo gues welcome to my blog, you can read this article with title Multi-Rate Main Loop Tasking, Happy reading

IMPORTANT, MUST BE READ... : Multi-Rate Main Loop Tasking
Title : Multi-Rate Main Loop Tasking

Read More


Multi-Rate Main Loop Tasking

IMPORTANT, MUST BE READ...
Recently I was looking for an instance of a prioritized, cooperative multitasking principal loop together with realized it is surprisingly hard to respect ane that is (1) readily available together with (2) comprehensible.

Sure, ane time yous sympathize the concept perhaps yous tin give the sack dig through an industrial-strength implementation amongst all sorts of complexity. But proficient luck getting upwards the learning curve!  So, hither is a uncomplicated (I hope!) description of a multi-rate principal loop scheduler.

First of all, we're talking close non-preemptive multitasking. This is variously called principal loop scheduling, a principal loop tasker, a prioritized cooperative multitasker, a cyclic executive, a non-preemptive scheduler, together with no uncertainty a bunch of other terms. (Not all of these damage are exactly descriptive, but inwards damage of search damage they'll acquire yous inwards the ballpark.) The full general thought is to locomote able to schedule multiple tasks that operate at dissimilar periods without having to usage an RTOS together with without having to usage the mostly bad thought of stuffing everything into timer-based interrupts.

Single Rate Main Loop

The starting shout out for is a single-rate principal loop. This is a classical principal loop schedule inwards which all tasks are run to completion over together with over again:

void main(void)
{ ... initialization ...

  while(1)
  { Task0();
    Task1();
    Task2();
    Task3();
  }
}


The proficient tidings close this is that yous don't demand an RTOS, together with it's actually hard to acquire things wrong. (In other words reviews together with testing are piece of cake to create well.)

The bad tidings is that all tasks take away hold to run at the same period. That agency that if ane chore needs to run actually speedily you'll immature adult woman its deadlines.

I've seen way besides many hacks that usage conditional execution based on CPU charge to sometimes skip tasks. But promotion hoc approaches take away hold the work that yous can't actually know if you'll immature adult woman deadlines. Instead, yous should usage a methodical approach that tin give the sack locomote analyzed mathematically to brand certain you'll run across deadlines. The way people mostly snuff it is amongst unopen to variation of a multi-rate principal loop.

Multi-Rate Main Loop

The thought behind a multi-rate principal loop is that yous tin give the sack run each chore at a dissimilar periodic rate. Each chore (which is just a subroutine) nevertheless runs to completion, hence this is non a full-up preemptive multitasking system. But it is relatively uncomplicated to build, together with flexible plenty for many embedded systems.

Here is unopen to instance code of the principal loop itself, amongst unopen to explanation to follow.

void main(void)
{ ... initialization ...

  while(1)
  { if(       flag0 )  { flag0 = 0; Task0(); }
    else if ( flag1 )  { flag1 = 0; Task1(); }
    else if ( flag2 )  { flag2 = 0; Task2(); } 
    else if ( flag3 )  { flag3 = 0; Task3(); }
  }
}

The way this code industrial plant is every bit follows.  All the tasks that demand to locomote run take away hold an associated flag laid to 1.  So if Task1 together with Task2 are the exclusively tasks that demand to run, flag0 is 0, flag1 is 1, flag2 is 1, together with flag3 is 0. The code crawls through an "else if" cascade looking for the first non-zero flag.  When it finds a non-zero flag it executes that task, together with only that task.

Note that each chore sets its flag to nix hence that it runs exactly ane fourth dimension when it is activated yesteryear its flag. If all flags are nix hence no chore is executed together with the do..while loop only spins away until a flag finally becomes non-zero. More close how flags acquire laid to 1 inwards a moment.

After executing at most ane task, the loop goes dorsum to the beginning. Because at most ane chore is executed per iteration of the principal do..while loop, the tasks are prioritized. Task0 has the highest priority, together with Task3 the lowest priority.

Yes, this prioritization agency that if your CPU is overloaded Task0 may execute many times together with Task3 may never acquire a turn. That's why its of import to acquire scheduling correct (this volition locomote a theme inwards a afterwards weblog posting).

Multi-Rate Timers

The principal loop wasn't hence bad, except nosotros swept nether the carpet the messy delineate concern of getting the flags laid properly.  Trying to create that inwards the principal loop mostly leads to problems, because a long chore volition displace many milliseconds to snuff it yesteryear betwixt timer checks, together with it is besides piece of cake to take away hold a põrnikas that misses setting a flag unopen to of the time. Thus, inwards full general yous tend to consider flag maintenance inwards the timer interrupt service routine.

Conceptually the code looks similar this, together with for our instance lives inwards a timer interrupt service routine (ISR) that is called every 1 msec.  A variable called "timer" keeps rail of organization fourth dimension together with is incremented ane time every msec.

 // inwards a timer ISR that is called ane time every msec
 timer++;
 if ((timer %   5) == 0) { flag0 = 1; } // enable v msec task0 

 if ((timer %  10) == 0) { flag1 = 1; } // enable 10 msec task1
 if ((timer %  20) == 0) { flag2 = 1; } // enable twenty msec task2
 if ((timer % 100) == 0) { flag3 = 1; } // enable 100 msec task3
 if (timer >= 100) { timer = 0; } // avoid timer overflow bug


Every v msec the timer volition locomote nix modulo 5, every 10 msec the timer volition locomote nix modulo 10, together with hence on.  So this gives us tasks amongst periods of 5, 10, 20, together with 100 msec.   Division is slow, together with inwards many low-end microcontrollers should locomote avoided inwards an ISR. So it is mutual to consider a laid of counters (one per flag), where each counter is laid to the catamenia of a detail chore together with counts downward toward nix ane time per msec. When a counter reaches nix the associated flag is laid to 1 together with the counter is reset to the tasks' period. This takes a niggling to a greater extent than RAM, but avoids division. How it's implemented depends upon your organization tradeoffs.

The in conclusion delineate of this code avoids weird things happening when the timer overflows. The reset to nix should run at the to the lowest degree mutual multiple of all periods, which inwards this instance happens to locomote equal to the longest period.

Concurrency Issues

As amongst whatsoever scheduling organization at that spot are potential concurrency issues. One subtle ane is that the timer ISR tin give the sack run part-way downward the else..if construction inwards the principal loop. This could displace a low-priority chore to run earlier a high priority chore if they both take away hold their flags laid to 1 on the same timer tick. It turns out that this doesn't brand the worst instance latency much worse. You could essay to synchronize things, but that adds complexity. Another way to take away hold this is to re-create the electrical flow fourth dimension into a temporary variable together with create the checks for when to run each chore inwards the principal loop instead of the timer ISR.

It's also of import to banknote that at that spot is a potential concurrency work inwards writing flags inwards the principal loop since both the ISR together with the principal chore tin give the sack write the flag variables. In do the concurrency põrnikas volition exclusively striking when you're missing deadlines, but proficient coding fashion dictates disabling interrupts when yous update the flag values inwards the principal loop, which isn't shown inwards the principal loop code inwards an endeavor to snuff it on things uncomplicated for the purpose of explanation.

The Big Picture

OK, that's pretty much it.  We take away hold a principal loop that runs each chore when its ready-to-run-flag is set, together with a timer ISR that sets a ready-to-run flag for each chore at the desired period. The number is a organization that has the next properties:
  • Each chore runs ane time during its assigned period
  • The tasks are prioritized, hence for instance chore ii exclusively runs when chore 0 together with chore 1 create non demand to run
The big create goodness is that, hence long every bit yous pay attending to schedulability math, yous tin give the sack run both fast together with tedious tasks without needing a fancy RTOS together with without missing deadlines.

In damage of practical application this is quite similar to what I frequently consider inwards commercial systems. Sometimes developers usage arrays of counters, arrays of flags, together with sometimes fifty-fifty arrays of pointers to functions if they take away hold a whole lot of functions, allowing the code to locomote a generic loop rather than spelling out each flag shout out together with each chore name. This mightiness locomote necessary, but I recommend keeping things uncomplicated together with avoiding arrays together with pointers if it is practical for your system.

Coming soon ... existent fourth dimension analysis of a multi-rate principal loop



IMPORTANT, MUST BE READ...

Thank for your attention Multi-Rate Main Loop Tasking

my blog Multi-Rate Main Loop Tasking, Have a nice day.

Now you read article Multi-Rate Main Loop Tasking this permalink article is http://fairemirima.blogspot.com/2017/11/multi-rate-main-loop-tasking.html Thank you and Best regards. You Can read nice Tips below. It was always better to choose topics that interest you or in wich you at least have some knowledge about . When creating targeted internet copywriting , you have to stick with your strong points , or everyone will know it . Make a list of all of the things and or topics that you are interested in . . . How much do you know ? Can you tell it as a story ? That is The essence of writing for the web . You Have to know your subject well , or nobody will believe you it is always better to impress someone then upset them . When Writing Targeted Internet Copywriting , you have to choose your appropriate target group of customers . without a target group of customers , you could ramble on incessantly about random subjects for days on end with no essence of a final goal . You always have to keep in mind who your customers are and what they are looking for . . . . . . . . . IMPORTANT, MUST BE READ...

0 Response to "Multi-Rate Main Loop Tasking"

Post a Comment