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.

Simple low cost MCUs of

Simple low cost MCUs of which the OOPIC sounds like a member cant multitask. Its a simple fact.

Of course, there are ways to fake it though :slight_smile: Using timers and interrupts you can set different tasks to run every certain time period. For example, you decide all sensors need polling every 500ms and a decision made, the LCD needs an update every 1s, and 1 LED on your light bar should move every 250ms. Then you could start a timer to generate an internal interrupt every 250ms. Each time the interrupt triggers, decide which routines need attention.

They will still be run in a one-by-one fashion but running fast enough they can give the impression of a multitasking robot to the human brain.

A better way to do it is to use something like an xmos chip which is designed from the ground up to support threading.

xmos

I have researched the XMOS processors, they are reasonably priced, and have amazing capabilities. The only problem is that as far as programming them, and their architecture, I’m clueless. Some day though. On my next robot I’ll use one.

So basically you’re telling me to just make it skip around really fast like in a multiplexed display. My MCU has a 20MHz clock speed. Do you think that will be fast enough? I can’t imagine it wouldn’t be. I don’t want the robot to miss a sensor reading or skip a LED on the light bar.

Yes pretty much. And

Yes pretty much. And depending on your processor and the language you choose, you can do a hell of a lot of skipping with 20Mhz. If there are still critical things that can`t be missed (like encoders) you can use interrupts to get the processors attention.

The OOPic is not

The OOPic is not programmable in C, Java or VB.  It allows you to program it using various syntax models, which isn’t the same thing.  In any case, if you’re after a cheap multi-tasking uController, perhaps the Parallax Propeller is an option?  It can be programmed in a language called Spin, which is quite easy to learn if you know VB or some other BASIC-like language or Python.  It has 8 cores running at 80MHz, so multitasking is not a problem.  It can also be programmed in C, if you’d prefer that.

- Xander

 

Have you looked into a chip

Have you looked into a chip that multitasks? The Propeller does and can be programmed in several languages like the ones you mentioned. In my TOBI robot I have one cog [sub processor] scanning all sensors and putting the results into variables, another feeding pulses to the drives, another doing ramp rate changes to the drives, and still another taking the sensor readings and doing object avoidance all the while the first one reads the RC link and decides how to move unless it is overrided or blended with the results of the object avoidance cog. In short I am using 5-6 cogs [sub processors] out of the available 8. I program in Spin [a spin off of basic that multitasks] but it can be programmed in C, Java, Basic and many more languages.

Also you can think of something else. You can use several of your Pic’s. Each working on a set of processes and each communicating to the others. I know of several robots using more than one Propeller. Norrislabs has several that use that approach.

More chips

Everyone has run into this problem and I have to say that “skipping” is a very valid option… There is another option as well, more chips! My robot solves this issue simply by having 7 different processors onboard. A master, some slaves, some interrupts and some serial communication and bam, 7 things at once.

More chips

Well, as soon as I learn C, I’ll probably start adding some PICs. I actually have an I2C controlled chip from TI that adds 8 I/O ports, but still gotta skip around.

Mutlitasking Propeller

Ya, I’ve looked into XMOS proccessors and heard the Propeller has threading, but I’m kinda low on funds at the moment. Man, I REALLY need to sit down and learn C. I feel so useless without knowing how to program in C.

Lanquages

I know it’s not actually programable in C, Java, or VB. The IDE just lets you use those. The IDE also let’s you review the actual output to the EEPROM. Quite a handy feature for double checking the efficiency of code. I’ll look into the Propeller. The VGA output capabilites are tempting.

Interupts

Ok, feeling stupid here because I used to know this, but never use them; what are interupts?

Interrupts are triggers that

Interrupts are triggers that make the processor pause its current operations and handle the interrupt. Once thats done it returns to where it left off. Interrupts can be triggered from lots of different stuff like internal timers ticking over or when an input pin state changes.

I found a great chip/language that would help.

I am thrilled by my discovery of ZBasic and the ZX series processors. One chip can directly replace a Basic Stamp and the bigger chips like the 1280 are just incredible (I know, I own one!) ZBasic runs really fast on their chips and the language is awesome.

The point here is ZBasic does a way-cool preemptive multitasking with all the bells and whistles. If your looking for multitasking on a single, but posssibly huge chip (my 1280 has lots of I/O with 100 pins) this could be it.

On the 1280n I have the basic is compiled down to machine code run by the procrssor. No “Byte Code” executed by a virtual machine at the processor end. The meaning of this lots and lots of speed (and mine has 124k program memory to boot!)

(Is this better than a Propeller chip? I don’t know, you decide. I know I like ZBasic a WHOLE lot better than “Spin” or even the just beginning Basic the propeller has now.

If you like ZBasic, why

If you like ZBasic, why don’t you run it on a Propeller?


Sinclair ZX Spectrum BASIC by Baggers
http://forums.parallax.com/showthread.php?p=905261 
…in the version I put up last, on the file selection menu, you just press ‘B’ it will perform a cold start on the ZX Spectrum, and you can write you’re own basic programs, there is yet no way to save them though, I will be working on that.

Sinclair ZX81 BASIC by Baggers
http://forums.parallax.com/showthread.php?p=809630 
"Also, you can actually use Sinclair ZX81 BASIC, using the Spectrum emulator, which then runs a ZX81 emulator."

http://humanoidolabs.blogspot.com/2012/03/ultimate-list-of-big-brain-languages.html

 

Running ZBasic on a Propeller

ZBasic won’t run on a Propeller. It will only run on specific ATmega chips that have either a native mode ZBasic bootloader or a ZBasic virtual machine bootloader. It won’t run on any other chip.

I am using ZBasic so I don’t have to run the Propeller. I have nothing against the chip itself, I just don’t like the software that runs on it. I prefer a more traditional multitasking and the Propeller is anything but traditional.

(ZBasic is no relation to Zebcore ZBasic, the ZX Spectrum, the ZX81 or anything else starting with a Z. It is a very specific set of chips and chips and hardware setup. They should have picked another name not starting with a Z!)

I hope someday a much better programmer than myself writes code both for the Propeller and ZBasic code. I do believe ZBasic will outrun the Propeller code.

Anyway, it’s all just apples and oranges and I was just suggesting trying a ZBasic chip. Just because it is my approach sure doesn’t mean it has to be anyone elses.

There is no “X” in ZBasic!

Somehow the editor is improperly changing my spelling and I can’t do a thing about it.

I saw a Propeller board at Radio Shack the other day…

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…” :slight_smile:

Very often people

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 :slight_smile:

 

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

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. 

It works fine here…

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!