| Propeller Programming: Simultaneous Multitasking |
| All News -> Designer News |
| Written by Nicholas McClanahan |
| Tuesday, 20 April 2010 12:09 |
How to do Multitasking with the Propeller
Let's imagine a hypothetical 'obstacle avoidance' robot with a set of wheels and a ping sensor. But instead of being controlled by an microcontroller, it's controlled by a little rat. Here's what you tell the rat to do;
As you can see, this is a linear, single-threaded activity. To speed things up, we can use a faster rat, but he can only work on one thing at a time. Simultaneous Multitasking lets you recruit another rat to help out. Rat #2 will man the ping sensor, and tell rat #1 which way to push. You'll instruct Rat #2; "Watch the ping sensor. If it gets under 10, measure left and right. Tell the other rat which direction is larger." No more running around, and no pausing to get distance measurements. Each rat has a simple task that's easy to understand and optimize. That's the idea of simultaneous multitasking. WHY SIMULTANEOUS MULTITASKING1 - You can always hire more rats 2 - Splitting up the work is often more efficient and easier to understand. 3 - It's good practice WHY NOT SIMULTANEOUS MULTITASKINGPrimarily, it's when the task is so simple that there is no benefit to dividing the labor. But understanding multitasking will only help you to better understand when it is and isn't a good approach. You don't have to implement simultaneous multitasking on the Prop, it supports either approach. ARCHITECTURESThere are 3 approaches I can think of; 1 - Multi-CPU 2 - Multi-core 3 - Peripherals All 3 approaches are possible with the Propeller. I'm going to talk about #2 (multi-core) in this walkthrough. PROPELLER MULTITASKINGThe Prop has 32k of RAM that's shared by all cogs. When the Prop starts up, it loads your code into RAM, and the first cog starts running it. If you want to start a second cog on a task, you use the command: COGNEW tells the active cog to start a new cog and have it run the method specified. You can also pass values to the method, just like calling any other method. The only thing that might look odd is '@stack'. Each cog needs a bit of working space. In your VAR section, just declare: How much stack space to reserve depends on the method you're going to run. But you can start with 20 LONG stack[20]) and bump it up if you have any problems or have extra memory. Once your object is complete, there are a couple tools in the object exchange to help you see exactly how big your stack needs to be. Here's the code for our obstacle avoider; Let's walk through it;
You already know from previous walkthroughs what that CON block holds program constants. I've decided to put the motor control pins there, and the pin that gets ping data. The VAR block reserves some stack space and one variable (direction). The second cog will figure out what direction to go and place the result in that variable. The first cog will read it and do as told.
There's already an object that comes with the Propeller tool for reading ping sensors. So I'm including that in the Object block.
The first PUB block is where execution begins;
Cog1 starts up with checkping, where it runs through a loop; if there's something within 10mm, check to the right and left. Tell Cog0 to push the robot by writing a value to the direction variable. And that's it! That's an example of how to get multiple cogs to work at the same time on the Prop. In summary;
As usual, let me know if you have any questions! Comments (2)
![]() |

How to do Multitasking with the Propeller










