Shop OBEX P1 Docs P2 Docs Learn Events
bluetooth communication — Parallax Forums

bluetooth communication

MrTurnerMrTurner Posts: 5
edited 2012-05-23 12:52 in BASIC Stamp
Hi all!

I'm using this code on my boebot:

'
[ Title ]
' File: BoeBotControlForMsrs.bs2

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

'
[ DATA ]

ResetOnOff DATA 0 ' On/off toggle w/ Reset button
RequestConnect DATA $FF, 0, 1, 0, 0
ConnectionGranted DATA $FF, 0, 2, 0, 0
RequestCommand DATA $FF, 0, 3, 0, 0
ServoSpeeds DATA $FF, 0, 4, 0, 0

'
[ Variables ]

tLeft VAR Word ' Servo control pulse durations
tRight VAR Word

temp VAR Word ' Temp variable

' Buffer array not declared as buffer VAR Word(5) for SERIN functionality.
' It can still be accessed as buffer(0), buffer(1), etc. However,
' buffer0, buffer1, etc. should be used in SERIN commands with variations
' of WAIT.
buffer0 VAR Byte ' Buffer - Start char = $ff
buffer1 VAR Byte ' Message Index value
buffer2 VAR Byte ' Command
buffer3 VAR Byte ' Argument 1 (return data 1)
buffer4 VAR Byte ' Argument 2 (return data 2)
buffer VAR buffer0 ' For standard array indexing

duration VAR Byte ' 50ths of ms duration
frequency VAR Byte ' 50ths of frequency
pointer VAR Byte ' EEPROM pointer
msgIndex VAR Byte ' EEPROM message index

routine VAR Nib ' Routine selector
counter VAR Nib
index VAR Nib

flagWhiskers VAR Bit ' Act on Whisker values
flagDigSens VAR Bit ' Send back digital sensors 1/0s
' with every reply
flagIr VAR Bit '
tempBit VAR Bit

'
[ Initialization ]

' This code makes it possible to toggle the Boe-Bot on/off by pressing and
' releasing the Board of Education's Reset button.

Program_Start:

' For on/off toggle with Reset
' READ ResetOnOff, temp
' temp = temp + 1
' WRITE ResetOnOff, temp
' IF temp.BIT0 = 0 THEN
' FREQOUT 4, 1000, 2000 - Indicate inactive
' END
' ENDIF

FREQOUT 4, 75, 4500

Reset:

'Wait for the eb500 radio to be ready.
PAUSE 1000

'Wait for the EB500 Bluetooth connection to be established.
DO UNTIL IN5 = 1: LOOP

GOSUB Ram_Clear

Request_Connect:
pointer = RequestConnect
GOSUB Get_Packet
SEROUT 1, 84, [STR buffer \5]

Confirm_Connect:
pointer = ConnectionGranted
GOSUB Get_Packet
Wait_For_Confirm:
SEROUT 1, 84, [255, 0, 1, 0, 0]
SERIN 0, 84, 20, Wait_For_Confirm, [WAITSTR buffer \2, buffer2, buffer3, buffer4]

Request_Packet:
msgIndex = msgIndex + 1
pointer = RequestCommand
GOSUB Get_Packet
SEROUT 1, 84, [STR buffer \5]

'
[ Main Routine ]

DO
Resume: ' If Message not rcvd, try again
IF IN5 = 0 THEN Reset ' EB500 disconnected?

PULSOUT 13, tLeft ' Servo control pulses
PULSOUT 12, tRight

SERIN 0, 84, 5, Resume, ' Get next command
[WAITSTR buffer \ 2, buffer2,
buffer3, buffer4]

PULSOUT 13, tLeft ' Servo control pulses again
PULSOUT 12, tRight

IF buffer2 = 192 THEN ' Handle restart req from PC
msgIndex = 0
GOTO Request_Connect
ENDIF

LOOKDOWN buffer2,[32, 33,
64, 65,
96, 97, 98,
128, 129, 130,
160, 161, 162], routine

ON routine GOSUB Set_Servo_Speed, Maneuver,
Get_Ir, Get_Whiskers,
Speaker_Tone, Set_Pins, Delay,
Enable_Digital_Sensors, Enable_Ir, Enable_Whiskers,
Disable_Digital_Sensors, Disable_Ir, Disable_Whiskers


' Load digital sensor values into buffer byte 3 for return message.
IF flagDigSens = 1 THEN GOSUB Digital_Sensors

' Increment message index for reply. Next message from PC has to use
' reply's buf[1].
msgIndex = msgIndex + 1
buffer1 = msgIndex
SEROUT 1, 84, [STR buffer \5]

LOOP

'
[ Subroutine Digital_Sensors ]

' This subroutine is used by the main routine to return digital sensor values
' with each reply.

Digital_Sensors:

buffer3 = 0
GOSUB Get_Ir
GOSUB Get_Whiskers

IF ((buffer3.BIT2 = 0 OR buffer3.BIT3 = 0) AND (flagWhiskers = 1)) THEN
tLeft = tLeft MAX 750
tRight = tRight MIN 750
ENDIF

RETURN

'
[ Subroutine Set_Servo_Speed ]

' Range of 0 to 200 with 100 = stopped maps to 650 to 850 with 750 stopped.

Set_Servo_Speed:

tLeft = buffer3 + 650 ' Decode servo speed.
tRight = buffer4 + 650

RETURN

'
[ Subroutine Speaker_Tone ]

Speaker_Tone:

duration = buffer3 ' Decode speaker tone
frequency = buffer4
FREQOUT 4, duration * 50, frequency * 50 ' Transmit tone

RETURN ' Go to resume routine

'
[ Subroutine Get_Ir ]

' IR object detection for buf[3] bits 1 (left) and 0 (right).

Get_Ir:

' IF flagIr THEN

FREQOUT 9, 1, 38500 ' IR headlight
tempBit = IN10 ' IR receiver
buffer3.BIT1 = tempBit ' Left IR reply bit
FREQOUT 3, 1, 38500
tempBit = IN2
buffer3.BIT0 = tempBit

' ENDIF

RETURN

'
[ Subroutine Get_Packet ]

' Loads packet into buffer for transmitting. Used by initialization.

Get_Packet:

FOR index = 0 TO 4
READ pointer + index, buffer(index)
NEXT

buffer(1) = msgIndex

RETURN

'
[ Subroutine Display_Command ]

' For debugging.

Display_Command:

' FOR index = 0 TO 4
' DEBUG DEC buffer(index), CR
' NEXT

RETURN

'
[ Subroutine - Check_Whiskers ]

' Stores left and right wiskers (contact switches) for buf[3] bit 3 (left) and
' 2 (right).

Get_Whiskers:

'IF FlagWhiskers THEN

buffer3.BIT3 = IN7 ' Left whisker
buffer3.BIT2 = IN8 ' Right whisker

'ENDIF

RETURN

'
[ Subroutine - Set_Pins ]

' Sets up to two pins. buf[3] high nibble specifies the operation, and the
' low nibble specifies the pin. The same applies to buf[4].

' High nibble:
' 0 - no action
' 1 - set to output
' 2 - set to input
' 3 - set to (output-high)
' 4 - set to (output-low)
' Low Nibble:
' 0 to 15 - Specifies I/O pin.
' DO NOT try to set 0, 1, 5, or 6.

Set_Pins:

temp.NIB1 = buffer3.HIGHNIB
temp.NIB0 = buffer3.LOWNIB
GOSUB Op_Pins

temp.NIB1 = buffer4.HIGHNIB
temp.NIB0 = buffer4.LOWNIB
GOSUB Op_Pins

RETURN

Op_pins:

SELECT temp.NIB1
CASE 1
OUTPUT temp.NIB0
CASE 2
INPUT temp.NIB0
CASE 3
HIGH temp.NIB0
CASE 4
LOW temp.NIB0
ENDSELECT

RETURN

'
[ Subroutine - Delay ]

' Specify delay in ms.

Delay:

temp.LOWBYTE = buffer3
temp.HIGHBYTE = buffer4
PAUSE temp

RETURN

'
[ Subroutine - Enable_Digital_Sensors ]

' Causes program to put digital sensor values in buf[3] of the reply packet.

Enable_Digital_Sensors:

FlagDigSens = 1

RETURN

'
[ Subroutine - Disable_Digital_Sensors ]

' Causes program to put digital sensor values in buf[3] of the reply packet.

Disable_Digital_Sensors:

FlagDigSens = 0

RETURN

'
[ Subroutine Enable_Whiskers ]

' Causes program to stop the Boe-Bot if Whisker contact is sensed. This gives
' the PC bluetooth system time to reply.

Enable_Whiskers:

flagWhiskers = 1

RETURN


'
[ Subroutine Disable_Whiskers ]

' Causes program to ignore the whisker contact. Also ignores when they are
' not connected.

Disable_Whiskers:

flagWhiskers = 0

RETURN

'
[ Subroutine Enable_Ir ]

' Not implemented.

Enable_Ir:

flagIr = 1

RETURN


'
[ Subroutine Enable_Whiskers ]

' Not implemented.

Disable_Ir:

flagIr = 0

RETURN

'
[ Subroutine - Maneuver ]

' Preprogrammed maneuver example. Current setup allows:
' "U" - Back up then U-turn
' "R" - Back up then right turn
' "L" - Back up then left turn

Maneuver:

FOR temp = 0 TO 35
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
NEXT

SELECT buffer3
CASE "U"
FOR temp = 0 TO 40
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT
CASE "R"
FOR temp = 0 TO 20
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
CASE "L"
FOR temp = 0 TO 20
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT
ENDSELECT

RETURN

'
[ Subroutine Ram_Clear ]

' Clears ram for reset from PC. This prevents retaining the msgIndex and
' pulseLeft/right variable values, which would otherwise cause problems.

Ram_Clear:

W0 = 0
W1 = 0
W2 = 0
W3 = 0
W4 = 0
W5 = 0
W6 = 0
W7 = 0
W8 = 0
W9 = 0
W10 = 0
W11 = 0
W12 = 0

RETURN

Is there a problem in this code? I can't comunicate with it in java language. I send handshake, and then read until I get 5 bytes. Boebot sends me: 255 0 1 0 0. I Try to send it 255 0 2 0 0 to confirm, and I'm waiting for 255 0 3 0 0, but it keeps sending me back 255 0 1 0 0

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-05-13 06:35
    "...and I'm waiting for 255 0 3 0 0..."
    Where up there is that supposed to happen (SEROUT)?

    Strip this down to the bluetoothing essentials, make it about the connectivity issue, first.
  • MrTurnerMrTurner Posts: 5
    edited 2012-05-13 06:54
    Right now there is no connectivity issue I think. I can connect to the Boe-Bot through java, and I'm sending him that 5byte packet to initalize handshake. Boe-Bot sends me back handshake initialization. I'm trying to confirm it, but it doesn't work. Which is odd, because this code is downloaded from here:
    http://forums.parallax.com/showthread.php?138582-BoeBot-Connection-to-MSRD-R3-R4&highlight=boebot
    which is a solved post.
    I'm trying to send out 255 0 3 0 0 with SEROUT now but I need help :S
    Is there someone who has made bluetooth communication before within boebot and an other language, for example C# or java?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-05-13 10:26
    Boebot sends me: 255 0 1 0 0. I Try to send it 255 0 2 0 0 to confirm, and I'm waiting for 255 0 3 0 0, but it keeps sending me back 255 0 1 0 0

    If you make a simple programme to do only that much, and no more, it doesn't happen?
  • MrTurnerMrTurner Posts: 5
    edited 2012-05-13 11:48
    can you please help me with that?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-05-13 12:22
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Response  VAR Byte(5)
    Response(0)  = 255
    Response(1)  = 0
    Response(2)  = 2
    Response(3)  = 0
    Response(4)  = 0
    
    BT_test:
      SEROUT 1,84,[255, 0, 1, 0, 0]
      SERIN 0,84,[WAITSTR Response]    ' waiting for 225,0,2,0,0
      DEBUG "Got Response", CR
      SEROUT 1,84,[255, 0, 3, 0, 0]
      DEBUG "Reply issued", CR
      PAUSE 1500
      DEBUG CLS
    
      GOTO BT_test
    
  • MrTurnerMrTurner Posts: 5
    edited 2012-05-14 01:06
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    buffer1        VAR     Byte                  ' Message Index value
    buffer2        VAR     Byte                  ' Argument 1 (return data 1)
    buffer3        VAR     Byte                  ' Argument 2 (return data 2)
    
    Handshake  VAR Byte(5)
    Handshake(0) = 255
    Handshake(1) = buffer1
    Handshake(2) = 192
    Handshake(3) = 0
    Handshake(4) = 0
    
    Confirm  VAR Byte(5)
    Confirm(0)  = 255
    Confirm(1)  = buffer1
    Confirm(2)  = 2
    Confirm(3)  = 0
    Confirm(4)  = 0
    
    Command  VAR Byte(5)
    Command(0) = 255
    Command(0) = buffer1
    Command(0) = 32
    Command(0) = buffer2
    Command(0) = buffer3
    
    BT_test:
      SERIN 0,84, [WAITSTR Handshake]
      buffer1 = buffer1 + 1
      SEROUT 1,84,[255, buffer1, 1, 0, 0]
      SERIN 0,84,[WAITSTR Confirm]    ' waiting for 225,index,2,0,0
      DEBUG "Got Response", CR
      buffer1 = buffer1 + 1
      SEROUT 1,84,[255, buffer1, 3, 0, 0]
      DEBUG "Reply issued", CR
    
      DO
          SERIN 0,84, [WAITSTR Command]
          DEBUG "Got Command", CR
          PULSOUT 13, 650 + buffer2
          PULSOUT 12, 650 + buffer3
          SEROUT 1,84,["Next Command needed: "]
          buffer1 = buffer1 + 1
          SEROUT 1,84,[255, buffer1, 3, 0, 0]
          PAUSE 500
      LOOP
    
      PAUSE 500
      DEBUG CLS
    
      GOTO BT_test
    

    I modified it a little because it wasn't working, but it still doesn't. Any suggestions?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-05-14 05:49
    MrTurner wrote: »
    I modified it a little because it wasn't working, but it still doesn't.
    Be specific. Where's it getting, what's the DEBUG situation?

    I didn't include the BT initialisation line:
    'Wait for the EB500 Bluetooth connection to be established.
    DO UNTIL IN5 = 1: LOOP
    
    but I didn't intend it to be the last word.
  • Mike4421Mike4421 Posts: 131
    edited 2012-05-14 15:58
    @ MrTurner..... I forgot to state this on the video's and the other post..... when I was using the code with MSRDS R3/R4; I used the regular Windows Bluetooth "Divers" , because I noticed when I used third party Bluetooth Driver's; it made some kind of interference with the communication's... although MSRDS did make the connection to the BoeBot...... it would'nt let me drive the servo's....

    this also happend with the Properller code for the Spinstamp's SPIN code on the Quick Start Board... see link for Pic's and info..

    http://forums.parallax.com/showthread.php?140007-Propeller-(Quick-Start-Board)-working-with-MSRDS

    Not sure if this third party software "Blue Soloile" or any other software has fixed the issue, but you may want to try this if your using 3rd party software. I use "BlueSoloile" for the Wii controllers with GlovePie for some PC game experiments.

    hope this helps.....
  • MrTurnerMrTurner Posts: 5
    edited 2012-05-23 12:52
    I'm not using any third party software. Its just a regular USB bluetooth adapter and the Boe-Bot, and nothing else between. But thanks, I will follow the steps on the topic you gave, and see whats going to happen.
Sign In or Register to comment.