BS2 RC/Autonomous Robot using Xbee's
Wheelie016
Posts: 11
Hi all,
I'm currently trying to program a remote control robot that can also be put into autonomous mode. AS the title says, I'm using the Basic Stamp 2, and 2 Xbee's, (one one the Bot, and the other connected through USB to my PC.) I have the remote control part working fine, as well as the "Autopilot" as we'll call it code working fine. They both could be tweaked, but for now they are fine. I'm even able to make it roam autonomously when I send the character "F," but my problem is that I can't get it to leave that mode and return to manual control. Here is my code:
'Remote Control Bot v1-Upgraded
Xb_D VAR Word
Pass_W VAR Byte
Dist VAR Word
Rand VAR Word
Servo_Ctrl CON 15 'Maistro Rx Pin
R_ServoP CON 0
L_ServoP CON 5
Xb_Rx CON 12 'Serin pin for the BS2
Xb_Tx CON 11 'Serout pin for the BS2
Setup:
'SEROUT 14, Baud_Rate, [22,12]
PAUSE 5
SEROUT Xb_Tx, 84, 100, ["Intializing RCB...", CR, CR]
PAUSE 250
SEROUT Xb_Tx, 84, 100, ["Enter Passcode...", CR]
Password: 'This section is just for a "pasword"
SERIN Xb_Rx, 84, [DEC Xb_D]
PAUSE 5
IF (Xb_D = 4321) THEN
SEROUT Xb_Tx, 84, 100, ["Password Valid", CR, CR]
GOTO Directions
ELSE
SEROUT Xb_Tx, 84, 100, ["Password Invalid", CR, "Retry...", CR]
GOTO Password
ENDIF
PAUSE 200
Directions: 'Shows character keys to control bot while in RC mode
SEROUT Xb_Tx, 84, ["Controls:", CR, "W = Forward", CR, "A/D = Left/Right", CR, "S = Reverse", CR, "'@' = Stop", CR, CR]
Main:
SERIN 12, 84, [Xb_D] 'Checks to see what character has been sent from the PC
PAUSE 100
SELECT Xb_D
CASE "w" 'Forward
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 96, 43]
'SEROUT 14, 84, [12,"Moving Forward"]
GOTO Main
CASE "a" 'Left
SEROUT 15, 84, [132, 5, 96, 43]
SEROUT 15, 84, [132, 0, 96, 43]
'SEROUT 14, 84, [12,"Turning Left"]
GOTO Main
CASE "s" 'Reverse
SEROUT 15, 84, [132, 5, 0, 50]
SEROUT 15, 84, [132, 0, 96, 43]
'SEROUT 14, 84,[12,"Moving Backwards"]
GOTO Main
CASE "d" 'Right
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 0, 50]
'SEROUT 14, 84,[12,"Turning Right"]
GOTO Main
CASE "@" 'Stops the Servos
SEROUT 15, 84, [ 132, 0, 112, 46]
SEROUT 15, 84, [ 132, 5, 112, 46]
'Puts bot into AutoPilot
CASE "F"
AutoPilot: 'Gets Distance from the Ping
PULSOUT 0, 5
PULSIN 0, 1, Dist
PAUSE 100
'SEROUT 13, 84, [12, Dist]
DEBUG DEC Dist, CR
IF Dist > 400 THEN 'If greater than 400usec's then go forward
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 96, 43]
ELSE 'If less, then turn bot around
RANDOM Rand
IF Rand > 32768 THEN 'Uses a Random command to make the bot turn Left or Right.
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 0, 50]
ELSE
SEROUT 15, 84, [132, 5, 96, 43]
SEROUT 15, 84, [132, 0, 96, 43]
ENDIF
ENDIF
SERIN 12, 84,1000, AutoPilot, [Xb_D] 'Checks to see if the Exit Autopilot command has been sent.
IF Xb_D = "F" THEN
GOTO Main 'Goes back to the RC Mode
ELSE
GOTO AutoPilot 'Continues roaming using the Ping
ENDIF
ENDSELECT
The Autopilot subroutine is towards the end. I didn't believe the SERIN command would work. I know the Xbee has a buffer, and I was thinking I could have it hold the char while the Xbee was fiddling with the PING and when it got back it would send the "F" and then enter the RC mode. But, while the BS2 is running the code, it can't tell the Xbee to NOT send the character. So what could i do here?
Thanks in advance.
I'm currently trying to program a remote control robot that can also be put into autonomous mode. AS the title says, I'm using the Basic Stamp 2, and 2 Xbee's, (one one the Bot, and the other connected through USB to my PC.) I have the remote control part working fine, as well as the "Autopilot" as we'll call it code working fine. They both could be tweaked, but for now they are fine. I'm even able to make it roam autonomously when I send the character "F," but my problem is that I can't get it to leave that mode and return to manual control. Here is my code:
'Remote Control Bot v1-Upgraded
Xb_D VAR Word
Pass_W VAR Byte
Dist VAR Word
Rand VAR Word
Servo_Ctrl CON 15 'Maistro Rx Pin
R_ServoP CON 0
L_ServoP CON 5
Xb_Rx CON 12 'Serin pin for the BS2
Xb_Tx CON 11 'Serout pin for the BS2
Setup:
'SEROUT 14, Baud_Rate, [22,12]
PAUSE 5
SEROUT Xb_Tx, 84, 100, ["Intializing RCB...", CR, CR]
PAUSE 250
SEROUT Xb_Tx, 84, 100, ["Enter Passcode...", CR]
Password: 'This section is just for a "pasword"
SERIN Xb_Rx, 84, [DEC Xb_D]
PAUSE 5
IF (Xb_D = 4321) THEN
SEROUT Xb_Tx, 84, 100, ["Password Valid", CR, CR]
GOTO Directions
ELSE
SEROUT Xb_Tx, 84, 100, ["Password Invalid", CR, "Retry...", CR]
GOTO Password
ENDIF
PAUSE 200
Directions: 'Shows character keys to control bot while in RC mode
SEROUT Xb_Tx, 84, ["Controls:", CR, "W = Forward", CR, "A/D = Left/Right", CR, "S = Reverse", CR, "'@' = Stop", CR, CR]
Main:
SERIN 12, 84, [Xb_D] 'Checks to see what character has been sent from the PC
PAUSE 100
SELECT Xb_D
CASE "w" 'Forward
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 96, 43]
'SEROUT 14, 84, [12,"Moving Forward"]
GOTO Main
CASE "a" 'Left
SEROUT 15, 84, [132, 5, 96, 43]
SEROUT 15, 84, [132, 0, 96, 43]
'SEROUT 14, 84, [12,"Turning Left"]
GOTO Main
CASE "s" 'Reverse
SEROUT 15, 84, [132, 5, 0, 50]
SEROUT 15, 84, [132, 0, 96, 43]
'SEROUT 14, 84,[12,"Moving Backwards"]
GOTO Main
CASE "d" 'Right
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 0, 50]
'SEROUT 14, 84,[12,"Turning Right"]
GOTO Main
CASE "@" 'Stops the Servos
SEROUT 15, 84, [ 132, 0, 112, 46]
SEROUT 15, 84, [ 132, 5, 112, 46]
'Puts bot into AutoPilot
CASE "F"
AutoPilot: 'Gets Distance from the Ping
PULSOUT 0, 5
PULSIN 0, 1, Dist
PAUSE 100
'SEROUT 13, 84, [12, Dist]
DEBUG DEC Dist, CR
IF Dist > 400 THEN 'If greater than 400usec's then go forward
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 96, 43]
ELSE 'If less, then turn bot around
RANDOM Rand
IF Rand > 32768 THEN 'Uses a Random command to make the bot turn Left or Right.
SEROUT 15, 84, [132, 0, 0, 50]
SEROUT 15, 84, [132, 5, 0, 50]
ELSE
SEROUT 15, 84, [132, 5, 96, 43]
SEROUT 15, 84, [132, 0, 96, 43]
ENDIF
ENDIF
SERIN 12, 84,1000, AutoPilot, [Xb_D] 'Checks to see if the Exit Autopilot command has been sent.
IF Xb_D = "F" THEN
GOTO Main 'Goes back to the RC Mode
ELSE
GOTO AutoPilot 'Continues roaming using the Ping
ENDIF
ENDSELECT
The Autopilot subroutine is towards the end. I didn't believe the SERIN command would work. I know the Xbee has a buffer, and I was thinking I could have it hold the char while the Xbee was fiddling with the PING and when it got back it would send the "F" and then enter the RC mode. But, while the BS2 is running the code, it can't tell the Xbee to NOT send the character. So what could i do here?
Thanks in advance.
Comments