Since robotics has many parallel functions happening at once, now I am wondering how robot builders handle the problem.
How do get past limited clock cycles?
Latch
I had a issue with generation a sine wave with limited resources. Since robotics has many parallel functions happening at once, now I am wondering how robot builders handle the problem.
How do get past limited clock cycles?
Latch
To begin implementing parallel functions within a single computer, you have to adjust your thinking into event oriented threads. Each thread needs to be broken down into separate event triggered actions. I call this technique "decoupling".
If you look at a single thread, it can be broken down and decoupled. Lets look at the push button and turn on an LED example.
In most implementations, you have a loop where you wait until the button is pushed and then you turn on the LED. This approach wastes a lot of computer cycles while you have the processor looking for the button push.
In a decoupled approach, you set up a timed interupt to look for the button push, say every tenth of a second.
You set boolean variable ButtonON, LEDON, and ButtonOFF to false.
Now you can set up the timer to run and check for the ON button to be pushed as long as the LEDON variable is false. If the button is on, you set a boolean variable ButtonON to True.
Now you set up your main progam loop to look for the boolean ButtonON to change to true. When the boolean goes true, your if statement enters the code to turn the LED on. You should also use an LEDON boolean variable and set it to true and set the ButtonON variable to false so you only go through the routine once.
Now you need a second stage in your interrupt routine that checks to see if the LEDON variable is true. So when the LEDON is true you check to see if the OFF button is pushed. When it is pushed, you set the ButtonOFF boolean variable to true .
Now in your main loop, you have one IF statement to check the ButtonON variable and a second IF statement to check the ButtonOFF variable. When the ButtonOFF variable is true, you run the code to turn the LED off and reset the the ButtonOFF variable to false.
This approach adds more code, but you now have a structure where you can add more boolean variables and IF statements for more than one action that will run independently of each other. You just have to make sure you keep each action thread separate and turn the appropriate variables true and false so that your code follows specific paths for each action.
Now to make the process work a little cleaner, you move the button checking code out of the interrupt routine and add a TimerOn variable and initialize it to false.
In your main loop, you have an IF statement to check TimerOn. When it is true, you set the TimerON variable to false and then check the LEDON variable for the ON button and the OFF button. This little change takes the checking out of the Interrupt Service Routine and puts most of the action into the main loop where it belongs. Now you have three parallel functions working together without a problem.
Function 1 is the Timer function, which is activated every tenth of a second and turns the TimerON variable to true.
Function 2 is the LEDON function, which is activated when the TimerON variable checks for the ON button to be pressed.
Function 3 is the LEDOFF function, which is activated when the TimerON variable checks for the OFF button to be pressed.
Using just a few IF statements and boolean variables you can set up a lot of simple threads that will work in parallel. When you start having timing problems, you know that you have put too many functions into that processor. Then you have to either reduce the number of functions or go into a more detailed timing analysis of the different threads to see if you can change the timing or execution sequences.
Hopefully this post gives you a hint on how you can add parallel processing using a very simple structure. I used this approach on a number of mission critical realtime systems and it works quite well for even some very complex threads.
Let me know if you have any questions on how to implement these concepts.
DAB
I (and many others) use the Parallax Propeller (http://www.parallax.com/propeller
It is an 8 core processor with each core capable of running at 20MIPS. It has 32 general purpose I/O pins controllable from any core and each core has 2 counters that can handle independent PWM and many other counter/timer functions. You don't need to worry about Interrupt programming, you can just start up a code thread in a core and it can communite via locks and shared variables with code running in the other cores.
Check them out, the forum is firendly and there are a lot of helpful, smart people doing some amazing things with this $8 micro-controller.
Rick
Hi Rick,
I agree, the parallax propeller is another option for moving into parallel tasks. Parallax is one of my main go to web sites. They always have good information available for download and they support their products well.
I just wanted to show that any processor can run parallel tasks. You just need to move to a little higher level of complexity. I have used my technique for over thirty years and it is simple and very responsive in most real time environments.
DAB