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.

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.

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

 

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.

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

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. :-)

 

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!