Shop OBEX P1 Docs P2 Docs Learn Events
Multitasking? — Parallax Forums

Multitasking?

geoldrgeoldr Posts: 15
edited 2008-11-23 19:26 in BASIC Stamp
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.

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

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-23 05:31
    The BS2 is not capable of multitasking in the sense you mean. You can interweave several different tasks through. For example, servos require control pulses every 20ms or so. You can use that to control the timing for the LEDS rather than just using PAUSE statements. Here would be a subroutine that you could use (GOSUB LEDphase) instead of a PAUSE 20 in the servo control routines. There's a sequence of 8 actions that repeat. Some of them light LEDs. You can set up the label in the LOOKUP any way you want:
    ' Initialize LEDtime (a nibble) to zero and LEDbit (a byte) to $05
    LEDphase:
       LEDtime = (LEDtime + 1) // 10   ' Go through a cycle of 10 pauses, each 20ms
       pause 20   ' The state number is stored in bits 4-7.  The LED pin # is in bits 0-3.
       if LEDtime <> 0 then return
       if (LEDbit & $F) <> 0 then low LEDbit & $F   ' Turn off the previous LED
       lookup LEDbit >> 4,[noparse][[/noparse]$16,$27,$36,$45,$50,$60,$70,$00],LEDbit
       if (LEDbit & $F) <> 0 then high LEDbit & $F   ' Turn on the next LED
       return
    


    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
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-23 05:32
    geoldr -

    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.
  • $WMc%$WMc% Posts: 1,884
    edited 2008-11-23 06:40
    geoldr

    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%_______
  • awesomeduckawesomeduck Posts: 87
    edited 2008-11-23 15:30
    I've thought about this problem since my first BASIC Stamp purchase. While there is no way to do multitasking in the traditional sense, I have surmised that with the addition of a couple of functions you could do a reasonably good job of performing multiple tasks in a quasi realtime fashion. I have not coded up this concept yet, but I have designed it in my head....so take it for what its worth.
    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 smile.gif
  • stamptrolstamptrol Posts: 1,731
    edited 2008-11-23 19:26
    ·· As has been pointed out, real multitasking isn't possible on the stamp. But on other processors I've worked on that did support it , it may not be all that its cracked up to be, either.

    ·· 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
Sign In or Register to comment.