Shop OBEX P1 Docs P2 Docs Learn Events
RC input to Basic Stamp — Parallax Forums

RC input to Basic Stamp

Joe FishbackJoe Fishback Posts: 99
edited 2005-01-03 19:43 in BASIC Stamp
I am trying to send a signal from a RC receiver·to·input pin 6 on a BS2 and have the BS2 send out a hobby servo signal. The purpose for this is so I can control the servo speed with Model airplane RC unit, but if the BS2 circuit detects an object, it will take control of the servo movement. To start with I am just trying to get the BS2 to repeat the RC signal.

Now here is what I have done. The RC receiver gets its power from the BOE, therefore both the receiver and BS2 have a common ground. The Pulse signal from the RC receiver is connected to pin 6 on BOE. The signal ground from the RC receiver is connect to the BOE Vss.

Here is the code-

' {$STAMP BS2}
' {$PBASIC 2.5}
' RC input to Basic Stamp for output to hobby servo

ch1 VAR Word··· 'time of pulse received from RC receiver
cycle VAR Bit·· 'cycle counter

main:
··· PULSIN 6, 1, ch1··· 'signal from RC receiver
··· DEBUG DEC ch1,CR··· 'show value of ch1
··· GOSUB forward······ 'go to sub "forward"

·· forward:············ 'sub "forward"
··· FOR cycle = 1 TO 5
····· PULSOUT 12, ch1·· 'pulseout pulse of time value "ch1" on pin 12 (servo)
····· PAUSE 20········· 'pause 20 ms
··· NEXT
··· GOTO main

My problem is that the BS2 only passes the first signal it gets, no matter how the RC input is changed. I think it is in my code. Any help will be greatly appreciated.

Joe Fishback

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-01-03 18:00
    Joe -

    You were DARNED CLOSE. Try the following which is a slight modification of your coding:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' RC input to Basic Stamp for output to hobby servo

    ch1 VAR Word 'time of pulse received from RC receiver
    cycle VAR Nib 'cycle counter

    main:

    PULSIN 6, 1, ch1 'signal from RC receiver
    DEBUG DEC ch1,CR 'show value of ch1
    GOSUB forward 'go to sub "forward"

    GOTO main

    forward: 'sub "forward"

    FOR cycle = 1 TO 5
    PULSOUT 12, ch1 'pulseout pulse of time value "ch1" on pin 12 (servo)
    PAUSE 20 'pause 20 ms
    NEXT

    RETURN

    END


    Regards,

    Bruce Bates
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-01-03 18:01
    Joe,

    First of all, you've committed a cardinal sin of programming. You've mixed GOSUBS and GOTOs. nono.gif Actually, in looking at the code, there must be something missing between the GOSUB and the forward: label. But anyway, proper use of the GOSUB is:

    <code>
    GOSUB label:
    <code>
    END

    label:
    <code>
    RETURN

    When the GOSUB is executed a return address is pushed onto a stack. When a RETURN is encountered, this address is popped back off the satck, and execution branches back to the instruction that follows it. If you intermingle GOTOs and GOSUBS you can have the situuation where you return to someplace other than what you intended. For example:

    <code>
    GOSUB Do_Some_Stuff ' Do_Some_Stuff is pushed on the stack
    <code>
    END


    Do_Some_stuff:
    GOSUB label1: ' label1: is now pushed on the stack
    label 2:
    <code>
    GOSUB label3: ' label3: is now pushed on the stack
    RETURN ' the stack is now popped, but label1: is returned and execution goes there, not after Do_Some_Stuff

    label1:
    <CODE>
    GOTO label2 ' Note GOTO, not RETURN. This will bite you!

    label3:
    <CODE>
    RETURN ' the stack is now popped and label3: is returned, execution proceeds after that GOSUB


    I'm not sure if this helps clear the issue of mixing the two styles. If not, please ask further. It is an important concept to understand.

    Jim
  • Joe FishbackJoe Fishback Posts: 99
    edited 2005-01-03 19:43
    Bruce & Jim,

    Thanks alot, I now see what I did wrong and interstand it.
    With that little change and alittle more code, I now have both wheels of my BOE-BOT robot controlled by RC. Also the Ir and bumper sensors over take control from the RC if an object is detected.

    Thanks,
    Joe Fishback
Sign In or Register to comment.