Shop OBEX P1 Docs P2 Docs Learn Events
Help with autopilot — Parallax Forums

Help with autopilot

GoogfanGoogfan Posts: 16
edited 2008-07-17 01:39 in BASIC Stamp
Hi i'm building a souped up boebot and i have a problem, when i·push the autopilot button [noparse][[/noparse]#21 - power] the robot begins roaming, but stops when i let go. I have a place in the code to exit autopilot BUT-- only when button 59 is pushed (mute). From the looks of things, my code should work confused.gif
It roams using PING))), and well, i dont want to hold the aoutopilot button.
OH, it uses an IR remote, (sony)

······· |
code \/

' {$STAMP BS2}
' {$PBASIC 2.5}

'variables

time········ VAR Word
irPulse····· VAR Word
topservo···· VAR Word
remoteCode·· VAR Byte
pulsecount·· VAR Byte
turncountl·· VAR Nib
turncountr·· VAR Nib

FREQOUT 4,2000,3000
turncountl = 0
turncountr = 0
'Main routine

Main:
DO
· GOSUB getremote
· IF (remoteCode = 2) THEN GOSUB forwardpulse
· IF (remoteCode = 8) THEN GOSUB backpulse
· IF (remoteCode = 4) THEN GOSUB leftpulse
· IF (remoteCode = 6) THEN GOSUB rightpulse
· IF (remoteCode = 21)THEN GOSUB autopilot
· IF (remotecode = 18) THEN topservo = topservo + 100
· IF (remotecode = 19) THEN topservo = topservo - 100
· topservo = topservo MIN 150
· topservo = topservo MAX 1200
· PULSOUT 14, topservo
LOOP

'MajorSubprograms


getremote:
remoteCode = 0
DO
· RCTIME 9, 1, irPulse
LOOP UNTIL irPulse > 1000
··· PULSIN 9, 0, irPulse
· IF irPulse > 500 THEN remoteCode.BIT0 = 1
··· RCTIME 9, 0, irPulse
· IF irPulse > 300 THEN remoteCode.BIT1 = 1
··· RCTIME 9, 0, irPulse
· IF irPulse > 300 THEN remoteCode.BIT2 = 1
··· RCTIME 9, 0, irPulse
· IF irPulse > 300 THEN remoteCode.BIT3 = 1
··· RCTIME 9, 0, irPulse
· IF irPulse > 300 THEN remoteCode.BIT4 = 1
··· RCTIME 9, 0, irPulse
· IF irPulse > 300 THEN remoteCode.BIT5 = 1
··· RCTIME 9, 0, irPulse
· IF irPulse > 300 THEN remoteCode.BIT6 = 1
· IF (remoteCode < 10) THEN remoteCode = remoteCode + 1
· IF (remoteCode = 10) THEN remoteCode = 0
RETURN

autopilot:
DO
· IF (turncountr > 2) AND (turncountl > 2)· THEN
··· turncountl = 0
··· turncountr = 0
· ENDIF

· PULSOUT 15, 5
· PULSIN 15, 1, time
· time = time ** 750
· IF (time < 12) THEN
··· IF (turncountl < 3) THEN
····· GOSUB turnleft
····· turncountl = turncountl + 1
··· ELSE
····· GOSUB turnright
····· turncountr = turncountr + 1
··· ENDIF
· ELSE
··· GOSUB forward
· ENDIF

· GOSUB getremote
· IF (remoteCode = 59) GOTO Main
LOOP

'autop subprograms


forward:
FOR pulsecount = 1 TO 20
· PULSOUT 13,840
· PULSOUT 12,650
· PAUSE 10
NEXT
RETURN

turnleft:
FOR pulsecount = 1 TO 20
· PULSOUT 13,650
· PULSOUT 12,650
· PAUSE 20
NEXT
RETURN

turnright:
FOR pulsecount = 1 TO 20
· PULSOUT 13,840
· PULSOUT 12,840
· PAUSE 20
NEXT
RETURN

backup:
FOR pulsecount = 1 TO 20
· PULSOUT 13,650
· PULSOUT 12,840
· PAUSE 10
NEXT
RETURN
'manual subprograms

leftpulse:
· PULSOUT 13,650
· PULSOUT 12,650
· PAUSE 10
· RETURN

rightpulse:
· PULSOUT 13,840
· PULSOUT 12,840
· PAUSE 10
· RETURN

forwardpulse:
· PULSOUT 13,840
· PULSOUT 12,650
· PAUSE 10
· RETURN

backpulse:
· PULSOUT 13,650
· PULSOUT 12,840
· PAUSE 10
· RETURN

Comments

  • kenwtnkenwtn Posts: 250
    edited 2008-07-09 18:09
    You seem to be missing a RETURN at end of autopilot

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • GoogfanGoogfan Posts: 16
    edited 2008-07-09 18:11
    You seem to be missing a RETURN at end of autopilot

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later youwill get it RIGHT



    thats because autopilot is a loop and will continue forever untill it is exited by·GOTO Main
  • GoogfanGoogfan Posts: 16
    edited 2008-07-09 18:15
    ·GOSUB getremote
    · IF (remoteCode = 59) GOTO Main


    its supposed to be
    ·

    GOSUB getremote
    · IF (remoteCode = 59) THEN GOTO Main


    it still doesn't work though
  • kenwtnkenwtn Posts: 250
    edited 2008-07-09 18:25
    Still think you need a return after loop

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-07-09 18:42
    You're clearing "RemoteCode" to zero at the top of the subroutine.
    So, if you're not holding down a button on the remote, you return "zero" to the "top level".
    A "zero" for "RemoteCode" does nothing at the top level -- thus your robot stops.

    Probably, the default should be 21 "Auto-Pilot".
  • GoogfanGoogfan Posts: 16
    edited 2008-07-09 20:15
    allanlane5 said...
    You're clearing "RemoteCode" to zero at the top of the subroutine.
    So, if you're not holding down a button on the remote, you return "zero" to the "top level".
    A "zero" for "RemoteCode" does nothing at the top level -- thus your robot stops.

    Probably, the default should be 21 "Auto-Pilot".
    i tried what you said but no luck. it still stops...
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-07-09 20:23
    Strange, the code still looks the same to me.

    What did you do?

    It's also possible your code is "hanging" in the IR reader -- I'm not sure how it determines when there's NO IR being recieved.
  • GoogfanGoogfan Posts: 16
    edited 2008-07-09 20:30
    I dont know... the code seems like it should work but it always exits the loop, even when no ir is recieved. the only way it works is if you hold the button down and start the loop over every time.

    Also, [noparse][[/noparse]totally off topic] i just realized, Boe-Bots and the Erector set make a perfect combo. You can build boving arms and stuff attached to the chassis
  • FranklinFranklin Posts: 4,747
    edited 2008-07-09 21:51
    Try removing the do in autopilot and changing the loop at the end to goto autopilot. See what happens then.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • GoogfanGoogfan Posts: 16
    edited 2008-07-09 23:45
    Franklin said...
    Try removing the do in autopilot and changing the loop at the end to goto autopilot. See what happens then.

    jumpin.gifmad.gifmad.gifskull.gifconfused.gif· that didnt work!!

    oh and the remoteCode = 0 is nescessary otherwise the servos go mad.
  • kenwtnkenwtn Posts: 250
    edited 2008-07-09 23:50
    Try this:

    autopilot:
    DO
    IF (turncountr > 2) AND (turncountl > 2) THEN
    turncountl = 0
    turncountr = 0
    ENDIF
    PULSOUT 15, 5
    PULSIN 15, 1, time
    time = time ** 750
    IF (time < 12) THEN
    IF (turncountl < 3) THEN
    GOSUB turnleft
    turncountl = turncountl + 1
    ELSE
    GOSUB turnright
    turncountr = turncountr + 1
    ENDIF
    ELSE
    GOSUB forward
    ENDIF
    GOSUB getremote
    IF (remoteCode = 59) THEN
    RETURN
    ENDIF
    LOOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • GoogfanGoogfan Posts: 16
    edited 2008-07-09 23:56
    kenwtn said...
    Try this:

    autopilot:
    DO
    IF (turncountr > 2) AND (turncountl > 2) THEN
    turncountl = 0
    turncountr = 0
    ENDIF
    PULSOUT 15, 5
    PULSIN 15, 1, time
    time = time ** 750
    IF (time < 12) THEN
    IF (turncountl < 3) THEN
    GOSUB turnleft
    turncountl = turncountl + 1
    ELSE
    GOSUB turnright
    turncountr = turncountr + 1
    ENDIF
    ELSE
    GOSUB forward
    ENDIF
    GOSUB getremote
    IF (remoteCode = 59) THEN
    RETURN
    ENDIF
    LOOP

    that was the first thing i tried. for an unkown reason it quits.
    oh and

    GOSUB getremote
    IF (remoteCode = 59) THEN
    RETURN <-- its not a true subrogram in a stack
    ENDIF
    LOOP

    i used GOTO Main to conserve stacking space.
  • NosePickerNosePicker Posts: 54
    edited 2008-07-10 04:00
    It looks like you will eventually get stack overflow.

    You should remove the DO and LOOP and change the last two lines to:

    IF (remoteCode <> 59) GOTO autopilot
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thanks

    NosePicker
  • MSDTechMSDTech Posts: 342
    edited 2008-07-10 12:07
    Your problem is in the getremote subroutine. It has the following loop:
    DO
    · RCTIME 9, 1, irPulse
    LOOP UNTIL irPulse > 1000
    This loop will just sit there and wait until it sees the IR remote. You need to change it to something like this:
    Define a variable Counter as a byte.

    counter=0
    DO
    · RCTIME 9, 1, irPulse
    · counter = counter + 1
    · PAUSE 10
    LOOP UNTIL (irPulse > 1000) OR (counter > 20)

    You can adjust the Pause statement and the value of counter to get a reasonable operation, although there will always be some hesitation of the robot while its checking to see if the remote is being pressed. I have some similar code and just decided that once I put it into autopilot, I'll chase it down and hit the reset button.

    Post Edited (MSDTech) : 7/10/2008 1:37:08 PM GMT
  • kenwtnkenwtn Posts: 250
    edited 2008-07-10 15:31
    Have you tried using DEBUG? Debug is a great tool to see where the program is going and to see the values you expect are getting set correctly. There are things in your code that do not make sense.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • patterson7019patterson7019 Posts: 25
    edited 2008-07-10 21:16
    The loop in autopilot seems to look okay, so I would suggest you check the value coming back from the getremote routine by putting either a debug (if you can) to print the value of remoteCode after the gosub in autopilot:

      ...
      GOSUB getremote
      DEBUG ? remoteCode,CR
      IF (remoteCode = 59) GOTO Main
    LOOP
    
    



    or if you can't put a debug in for some reason, put in an infinite loop to stop everything:

      ...
      GOSUB getremote
      'DEBUG ? remoteCode,CR
    infiniteloop:
      IF (remoteCode = 59) GOTO infiniteloop
    LOOP
    
    



    If everything stops, you know remoteCode is 59, and that your loop is fine, but the getremote is wrong.

    Also note, that a gosub will add to the stack. The goto in "IF (remoteCode = 59) GOTO Main" does not remove the previos gosub from the stack. So you will eventually run out of stack space.
  • GoogfanGoogfan Posts: 16
    edited 2008-07-17 01:39
    ughh nothing works. counter idea hangs and debug did not help... srry

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mixing things...

    It can be deadly -- Potassium Carbon Nitrogen [noparse][[/noparse]KCN]
    It can be fun
    ·Potassium Nitrogen Oxogen Carbon Sulfer [noparse][[/noparse]KNO3+C+S]
    It can be useless - Napkin Butter
    wait, what point was I·trying to establish?

    ·
Sign In or Register to comment.