Multitasking?
geoldr
Posts: 15
Hey everyone, so I have built a robot car that roams around and avoids obstacles. Cool. I wanted to spice it up a bit, out of 4 red LED's I built a scanner like KITT had from the old Knight Rider. Here is the code for it.
But with this code running, the car driving.. it.. basically doesn't happen. Is there anything I can do to make this whole process better? Or is the BS2 not capable of multitasking?
Here is the full code:
HIGH 5 PAUSE 200 LOW 5 HIGH 6 PAUSE 200 LOW 6 HIGH 7 PAUSE 200 LOW 7 HIGH 6 PAUSE 200 LOW 6 HIGH 5
But with this code running, the car driving.. it.. basically doesn't happen. Is there anything I can do to make this whole process better? Or is the BS2 not capable of multitasking?
Here is the full code:
' {$STAMP BS2} ' {$PBASIC 2.5} ' Roaming with IR irdetectleft VAR Bit irdetectright VAR Bit pulseleft VAR Word pulseright VAR Word pulsecount VAR Byte FREQOUT 4, 200, 3000 PAUSE 100 FREQOUT 4, 200, 3000 DO HIGH 5 PAUSE 200 LOW 5 HIGH 6 PAUSE 200 LOW 6 HIGH 7 PAUSE 200 LOW 7 HIGH 6 PAUSE 200 LOW 6 HIGH 5 FREQOUT 8, 1, 38500 irdetectleft = IN9 FREQOUT 2, 1, 38500 irdetectright = IN0 '--------------------- IF (irdetectleft = 0) AND (irdetectright = 0) THEN HIGH 1 HIGH 10 pulseleft = 650 pulseright = 850 ELSEIF (irdetectleft = 0) THEN HIGH 1 pulseleft = 850 pulseright = 850 ELSEIF (irdetectright = 0) THEN HIGH 10 pulseleft = 650 pulseleft = 650 ELSE LOW 6 LOW 1 LOW 10 pulseleft = 850 pulseright = 650 ENDIF PULSOUT 14, pulseleft PULSOUT 12, pulseright LOOP
Comments
Do a GOSUB LEDphase between the 2nd PULSOUT and the LOOP and remove the other code to light the LEDs and PAUSE between flashes.
Post Edited (Mike Green) : 11/23/2008 5:37:24 AM GMT
In the future please use the attachment manager to upload your program code. In this case, I suspect the entire program may not be necessary for troubleshooting the problem.
Unfortunately the Stamp can only do one thing at a time, so multi-tasking is out. However you may not need to multi-task.
Try this - Rather than using a constant in your PAUSE statement, make it a variable:
Long var BYTE
Long = 200 (as it is now)
PAUSE Long
Now reduce "Long" or whatever you choose to use as a variable name, just a bit. Keep reducing it until you (hopefully) reach a happy medium between drive-ability and Knight Rider lighting effects.
Let us know how you make out, or if you need more assistance.
Nice project!
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When all else fails, try inserting a new battery.
Have You thought of "redirecting" code?. In this I mean instead of "PAUSE 200" ,Jump to something else for "PAUSE 200" like updateing servos etc. And then jumping back to the LED blink gemick. With every " PAUSE " statement, the $stamp is tied up.
___Please set the $stamp free____________$WMc%_______
The two functions needed are a timer and the BS2p's POLL related functions. The timer is easy to get...the ServoPAL (I just bought one last week) has an ALARM function. Using the ALARM to keep a global timer and then allowing each "task", one in each program page, should enable several tasks to cooperatively share the resources.
By "cooperatively" I mean that every task has to be cognizant of the fact that its on a shared CPU. Unlike programming for Windows or Linux, where the application need not be concerned about hogging the CPU, unless the BS tasks are mindful they can dominate and starve other tasks.
I look forward to coding a shell program up in the near future.
I suspect that it will be difficult, and that's why products like PWMPAL and ServoPal were developed
·· For what its worth, I've attached the motor control program I used on a small twin motor barge. It used two fan drives and used differential speed of the motors to steer....no rudder. I needed some timers to delay when going from full forward to full reverse so the motors wouldn't be damaged.
· The method I used to ensure the joysticks were always being polled even when the timers were running was to use very small time increments and to track how many increments had accumulated. Its not a new technique but bears repeating.
· You can see the timers at the bottom of the sub-routine section.
· Cheers
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk
http://www.siskconsult.com