Let's Make Robots!

Tips for making a robot multi-task

I have a robot, and my robot has a brain. However, my robot's brain can only think about one thing at a time. I can set I/O pins high or low, and they will stay there until I change them, but that isn't going to work. Here's the deal:

  • I have an OOPIC with firmware version B.3.something+ (whatever the newest is)
  • The OOPIC is a 40-pin pic with custom firmware. It is a development board
  • It can be programmed in C, Visual Basic, Or Java
  • It only does one thing at a time due to the nature of those programming languages (I use visual basic)
  • I need to proccess 3-5 things at once

My robot has a light-bar with 8 LEDs that I want to scroll back and forth like KITT. I could use a decade counter for this, but I want the rate and sequence to change based on the robot's activity. This is something that needs to run in the background.

My robot has two tank tread and a turret to control. The driving can be done with one proccess.

My robot has sensors and outputs to manage (ultrasonic, infrared, bumpers, speakers, etc.)

My robot has an LCD that needs to display its mode, speed, activity, etc.


So here is my problem: I need to do all of this at once. The KITT LED bar thing needs to be constantly going, the sensors need to be constantly sensing, and the drivetrain needs to be responding accordingly, etc. Each of these bits are super-easy to program because of the object-oriented programming environment with a few lines of code. The only problem is that it is a PIC, and it only executes one line of code at a time. Now, taking in mind that this is after all, just a PIC with some shiny firmware, how can I make it run multiple lines of code simultaneously? I can't have it sense, then drive, then update LCD, then stop and do the KITT LED Thing, then drive again, then beep, etc. I am not familiar with programming in C, but the logic still applies. So if you know anything about making a robot do multiple things at once, please leave a comment.

P.S. - If this seems badly worded/typed/confusing, well I typed it late at night and I can't think perfectly straight.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I have been working with the multitasking in ZBasic and it is VERY stable and easy to work with (and it's fully preemptive). Using hardware PWM to drive servos and they don't 'glitch' at all with the multitasking. I am quite pleased and I think anyone would be if they would just give it a try.

Now put that in a 40 pin dip package, equally as fast, and charge $59.95 and you have a controller really worth exploring. (Its called the ZX-24su and is made by a ZBasic "helper" company named Oak Micro.)

With the programming being very easy, one can devote more time to some serious AI. That is what I am going to TRY to do.

I have heard rumor that an effort is being made to translate ZBasic to run on a PROPELLER!!! If that can be done (it sounds really hard) I couldn't resist finally buying a Propeller! (I think it would be on a board with an external memory for the ZBasic native boot code). If they do it right they won't put a VM - Virtal Machine on the chip but have ZBasic compile down to Propeller machine code. That could be incredibaly fast if done properly. I don't think they could keep them in stock! :-)

Don't expect ZBasic for the Propeller to be out any time soon... The "team" that is working on it is one person and he sounds like he doing it in his spare time as part of his hobby.

I myself see huge problems from both the Parallax and ZBasic companies. Who owns the thing ? What about licencing it?

IF he is doing so it as a Basic that runs somewhat like ZBasic (gets just the most important features running) without using any of the ZBasic originally engineered code, it may make for a awesome Open Source project! If it ran well (not even perfectly) I would be willing to buy a Propeller and help with finding bugs, I think it would be a really worthy effort, and then I would never have to think about learning Spin again! Yes, I hate Spin that much! :-)

Computers have multitasked long before multicore chips arrived on the scene. Because they operate so fast it is just a matter of time management or "scheduling" as they say, to make the chip appear it is doing multple tasks simultaneously.

There are a number of ways to implement this but there is an interesting and simple method here with an avr. You could use an extra chip or 2 but I wonder whether this really simplifies things any further as you then need to add communications code when you could just use the space to write interrupt service routines and timer code.

I solved the multitasking question by using more than one microcontroller.

They are reasonably inexpensive nowadays.

I don't know anything about the oopic, but a small picaxe or arduino are quite reasonable. (less than $10 US and the little 8-pin ones are only $2 or $3 each.)

Very often people underestimate the speed at which their processors run. 20MHz, that's 20 million operations per second! I don't know how many clockcycles an OOPIC uses but an Atmega uses 1 cycle to read a digital input. That means you can read an input 20 million times each second. That is quite a lot :)


Just letting your robot do stuff in the logical order and skipping delays/sleeps that aren't absolutely necessary could give the impression that it is indeed multitasking.

Perfectly said. There are several preemptive multitasking libraries for AVR based microcontrollers, the first that comes to mind is DuinOS for Arduino. The only problem I have with it is that it uses Timer1 for the OS so it can't be used for servos. However, in a multi-microcontroller setup, the logic may use DuinOS to keep track of sensors, motors and order a servo controller what to do. 

With ATmega1280 processor I am using, ZBasic multitasks and runs multiple servos without a problem (because the chip has quite a few hardware PWM channels, up to 12, 16bit).

I don't have robot body for the board yet, but I can rig it up on my desktop to check what it can and can not do. So far it does everything with flying colors, but then I am learning quite a bit too (like the concept of semaphores in multitasking?)

As for program execution speed, it is up to 2.9 million instructions per second (an instruction being like b=b+1). I have had it doing a lot and it hasn't even hinted at slowing down yet!

And for a comparrison of programming languages, ZBasic is Basic! It is not nearly as hard as something like scripting for the Arduino, and it seems (to me) a heck of a lot easier than Spin. The Basic's for the Propeller I have seen seem to be in the early stages of development (one COULD mature to be something like ZBasic, I would have to buy a "Prop" at that point!)

Now I am doing a LOT of thinking of the robot hardware to put this board in, and it won't be small!

C on an Atmega is (in my humble opinion) even better, since you can use timers, interrupts and other magical stuff to make your robot "multitask".

I find it that BASIC in almost any form make me want to shower, but each to his own I guess.

The Arduino language(Based on Wiring) is a simplified version of C++, sort of. It's very simple to learn, I think that it's well worth the time to learn it. I haven't tried out ZBasic though, may it has some nifty features that I'm not aware of.

I almost picked it up to tinker with it and primarily to run some speed tests against ZBasic. I would have had to run the Propeller program in "Brads Propeller Tool" Basic as I don't know (and don't want to learn!) Spin)

I ended up not getting the board but I did notice it was like $39.99 and at Parallax the same board is $25! It still is convenient to just grab it in a bubble pack, and someday I might just do that. Before I would buy it I would look at the Basic to see if I could do anything serious with it...

There are a LOT of microcontroller options and we choose the one we think we will get the most from (in my case, its ZBasic).

"Caution, your milage may vary..." :-)

Here comes a shocker, I am going to buy and try programming a Propeller! I have to admit I know little about but there are more than a few people who say it would be neat to try out.

I am going to do it! And I am glad a section of the forum exists for it. I will need it. :-)