Shop OBEX P1 Docs P2 Docs Learn Events
BS2 To Xbee To Xbee To Ping Sensor. — Parallax Forums

BS2 To Xbee To Xbee To Ping Sensor.

Tony11Tony11 Posts: 41
edited 2012-08-15 17:25 in Accessories
What I am looking to do is, make the ping sensor wireless. I am going to use two Xbee module and two BS2 module. So the smart sensors and applications program work's great. (program is below) I just want to make the ping wireless, from the BS2. I still want to read the data on my computer, from the ping sensor.
I need help with making the program that will send serial data to and from the xbee's.


Ping to BS2 to Xbee to Xbee to BS2 to my computer.








' Smart Sensors and Applications - PingMeasureCmAndIn.bs2
' Measure distance with Ping))) sensor and display in both in & cm
' {$STAMP BS2}
' {$PBASIC 2.5}
' Conversion constants for room temperature measurements.

CmConstant CON 2260
InConstant CON 890
cmDistance VAR Word
inDistance VAR Word
time VAR Word

DO
PULSOUT 15, 5
PULSIN 15, 1, time
cmDistance = cmConstant ** time
inDistance = inConstant ** time
DEBUG HOME, DEC3 cmDistance, " cm"
DEBUG CR, DEC3 inDistance, " in"
PAUSE 100
LOOP

Comments

  • TymkrsTymkrs Posts: 539
    edited 2012-06-26 18:43
    I'm afraid I can't speak for Basic Stamp as my exp has always been in Propeller/Spin but the basic premise is similar to what I think is on the tutorial:

    http://www.parallax.com/Portals/0/Downloads/docs/prod/rf/122-32450-XBeeTutorial-v1.0.1.pdf

    Starting on Page 74, it has an example of how to connect two BS2's together serially with the Xbees and how to get feedback from light sensors. The manual/automatic polling process should be helpful as well.

    And if it helps to read through my "thought process" here're some links to things I've done requiring communication between two xbees (and at the end of the day it just sounds like you want to set up a simple serial connection)

    http://tymkrs.tumblr.com/post/19784957623/xbee-using-a-switch-to-control-rc-servos-set-up
    http://tymkrs.tumblr.com/post/19837921484/xbee-using-a-switch-to-control-rc-servos-code

    http://tymkrs.tumblr.com/post/19896757309/xbee-using-a-switch-to-control-the-heart-led-project
    http://tymkrs.tumblr.com/post/20011194294/xbee-using-a-switch-to-control-the-heart-led-project

    So something like...First BS2 sends (through the Xbee) the start sequence for the Ping Sensor, then listens for anything sent back to it to display on a serial terminal. Second BS2 hears the start sequence for the Ping Sensor, and just sends back anything it gets (either timed or not). Like you could easily make a switch that once you press it, wirelessly gets a Ping data thing back.
  • Tony11Tony11 Posts: 41
    edited 2012-07-14 17:11
    Tymkrs I see you used Propeller/Spin, works good, but is there A way that I can use two Propeller, two ping sensor, two xbee's and two leds.

    So if both pings sensor's is > 50, turn on led's. Box1 and Box2 will be wireless with the xbee's. I want to put one (Box1,ping1,xbeex,led1, and propeller) in one room and in some other room, I want (Box2,ping2,xbee,led2 and propeller)

    "Box 1"
    ping1
    xbee
    led1
    Propeller

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    "Box 2"
    ping2
    xbee
    led2
    Propeller









    What is the best way to write the program for this=

    Box1

    (ping1 > 50) = 1
    (1 = box1)
    send (box1) with xbee to xbee2

    if (box1) and (box2)
    then turn on led1

    xbee's would send O or 1 tx and rx

    ====================================================

    Box2

    (ping1 > 50) =1
    (1 = box2)
    send (box2) with xbee to xbee1

    if (box1) and (box2)
    then turn on led2

    xbee's would send O or 1 tx and rx
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-15 09:54
    You can use either a Propeller or a Stamp for each box. You've already got a program that will do a Ping periodically and send the distance detected to an attached PC using DEBUG. You can just send the same number to the xBee and it will be transmitted to the PC. Use a SEROUT statement instead of DEBUG and use the pin number of the xBee transmit line. You'll need to configure the xBee using Digi's X-CTU utility so that the xBee (and its PC counterpart) starts up with a serial link already established. With this sort of setup, you'll need two xBee units at the PC, one for each Ping. If you want to only have a single xBee at the PC, that becomes more complicated since the PC has to establish a link to one xBee, ask for a Ping and receive the data, then switch the link to the other xBee and do the same. Alternatively, you could use a completely different type of xBees that establish a mesh network where each message is identified by its sender (and intended receiver). This is a more complicated system. Have you looked at Parallax's and Digi's documentation for xBee? If you haven't, you must at least look at Parallax's.
  • Tony11Tony11 Posts: 41
    edited 2012-07-16 16:25
    OBJ

    pst : "FullDuplexSerial"
    ping : "Ping"

    CON

    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000

    ping_pin = 0 ' I/O pin for Ping

    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    ' Set pins and Baud rate for XBee comms
    XB_Rx = 0 ' XBee DOUT
    XB_Tx = 1 ' XBee DIN
    XB_Baud = 9600
    ' Set pins and baud rate for PC comms
    PC_Rx = 31
    PC_Tx = 30
    PC_Baud = 9600





    VAR
    long stack[50] ' stack space for second cog
    long range
    word Box1
    PUB Go

    pst.start(31,30,0,115200)
    waitcnt(ClkFreq + Cnt)

    repeat ' Repeat forever
    range := ping.Inches(ping_pin) ' Get range in inches
    pst.dec(range) ' Display result
    pst.tx(13)
    WaitCnt(ClkFreq / 4 + Cnt) ' Short delay until next read
    (range > 50) = (Box1)
    XB_Rx = (box1)
    if (Box1) and (Box2)
    outa[4] :=1






    something like this.......
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-16 16:32
    If you're going to use Spin, you have to post code properly. See ...
    attachment.php?attachmentid=78421&d=1297987572

    How about starting with the minimum program that demonstrates successful sensing with the PING)))? What you've posted won't compile or run successfully.
  • Tony11Tony11 Posts: 41
    edited 2012-07-16 17:16
    OBJ
    
      pst   : "FullDuplexSerial"
      ping  : "Ping"
    
    CON
    
     _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      CR        = 13   ' Carriage Return value       
      Ping_Pin  = 4                           ' I/O pin for Ping
      
    VAR
    
      long  range
      
    PUB Go
    
      pst.start(31,30,0,115200)
      waitcnt(ClkFreq + Cnt)
        
      repeat                            ' Repeat forever
        range := ping.Inches(ping_pin)  ' Get range in inches
        pst.dec(range)                  ' Display result
        pst.tx(13)
        WaitCnt(ClkFreq / 4 + Cnt)	' Short delay until next read
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-16 20:10
    So, what does it do? Unless I've missed something simple, it should open a serial port to a PC, pause for a second, then Ping and display the distance found in inches in decimal, one value per line about 4 times a second.

    Note that the Parallax Serial Terminal object, while it uses FullDuplexSerial internally, adds some additional functionality which is understood by the Parallax Serial Terminal PC program. It may be confusing to others to use the object name "pst" for FullDuplexSerial. I usually use "ser" or "serial".
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-16 20:19
    To use the xBee serial port instead of the programming port, you'd just need to change the pst.start to pst.start(XB_Rx,XB_Tx,0,38400). You could probably use 115200 for the Baud, but I don't think the end-to-end effective Baud is quite that high. You'll need to configure the xBees using the X-CTU utility so that they power on and connect to each other automatically and have the Baud you expect on the Propeller end and the PC end.
  • Tony11Tony11 Posts: 41
    edited 2012-07-17 16:00
    What I was hoping to do is, if ping1 and ping2 sensor's are at (100). The output would be (1).
    (ping1 < 50) = (xbee1) (ping2 < 50) = (xbee2).

    if ping1 and ping2 sensor's are at (10). The output would be (0).
    (ping1 < 50) = (xbee1) (ping2 < 50) = (xbee2).

    Then on box1 and box2 program's. There would be this line that would read.
    What is the state of (xbee1 and xbee2)
    If (xbee1 and xbee2)then
    turn on ledbox1 and ledbox2

    What I need help is with sending the state of the output, using the xbee's, and no pc, hook up.

    Box1 = ping sensor, xbee, and led.

    Box2 = ping sensor, xbee, and led.


    (if xbee1 output is 0 and xbee2 output is 1. Led = 0)

    (if xbee1 output is 1 and xbee2 output is 0. Led = 0)

    (if xbee1 output is 1 and xbee2 output is 1. Led = 1)

    So in the end of all this Box1 and Box2 will be talking to each other, what is your state.





    Box1
    OBJ
    
      pst   : "FullDuplexSerial"
      ping  : "Ping"
    
    CON
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      ping_pin = 0                      ' I/O pin for Ping
      
    VAR
    
      long  range
      
    PUB Go
    
      pst.start(31,30,0,115200)
      waitcnt(ClkFreq + Cnt)
        
      repeat                            ' Repeat forever
        range := ping.Inches(ping_pin)  ' Get range in inches
        pst.dec(range)                  ' Display result
        pst.tx(13)
        WaitCnt(ClkFreq / 4 + Cnt)	' Short delay until next read 
    



    box2
    OBJ
    
      pst   : "FullDuplexSerial"
      ping  : "Ping"
    
    CON
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      ping_pin = 0                      ' I/O pin for Ping
      
    VAR
    
      long  range
      
    PUB Go
    
      pst.start(31,30,0,115200)
      waitcnt(ClkFreq + Cnt)
        
      repeat                            ' Repeat forever
        range := ping.Inches(ping_pin)  ' Get range in inches
        pst.dec(range)                  ' Display result
        pst.tx(13)
        WaitCnt(ClkFreq / 4 + Cnt)	' Short delay until next read 
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-17 16:42
    The xBees are capable of transmitting information from some sensors to other xBees attached to a microcontroller, like a Stamp or Propeller, or to a PC and an xBee can receive commands to turn I/O pins on and off, like to light an LED. That sort of use is beyond what we can help you with. The information is in the Digi documentation for the xBees. I'm not sure if you can do any decision making at all with the xBee. I doubt that you could use a Ping without at least some microcontroller to measure the width of the pulse produced by the Ping to indicate the distance involved. There are some wireless modules I've seen lately that can be programmed remotely using Python and might be able to do this, but I have no detailed information on them. They plug into the same sockets as xBees, but they work completely differently internally and can't be used with xBees. In any event, we can't help you with those either. You might find some ads in magazines like Nuts & Volts and there should be on-line documentation on them.

    Box1 and Box2 are going to have to have a Stamp or Propeller or some other kind of microcontroller inside in addition to a Ping and an xBee along with an LED. If a Stamp or Propeller, we can help you with it. If not, you're on your own.
  • Tony11Tony11 Posts: 41
    edited 2012-07-17 17:02
    Sorry I didn't put in Propeller chip in all my typing.

    Box1 = ping sensor, propeller, xbee, and led.

    Box2 = ping sensor, propeller, xbee, and led.


    This is all I would like to know, to make it easy. Switch, propeller, xbee, and
    led. (Box1) Switch, propeller, xbee, and led. (Box2).

    Switch goes high on box1, led goes high on box2

    Switch goes high on box2, led goes high on box1



    Here is code, to send switch input one way, how do I send it back, with switch and led. So on the code that has no Switch input, how do I make it take the input and send it two ways.................

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    #SELECT $STAMP
    #CASE BS2, BS2E, BS2PE
    T9600	CON	84
    #CASE BS2SX, BS2P
    T9600	CON	240
    #CASE BS2PX
    T9600	CON	396
    #ENDSELECT
    ' ***************** Variables, Constants and Pins
    Baud	CON	T9600	' Set baud rate
    Rx	PIN	15	' XBee DOUT
    Tx	PIN	14	' XBee DIN
    PB	PIN	0	' Pushbutton
    Freq	VAR	Word
    ' ***************** Main Loop
    DO
    IF PB = 1 THEN	' If button pressed...
    Freq = Freq + 500	' Increment Frequency
    IF Freq > 5000 THEN Freq = 500	' Limit to 500 to 5000
    SEROUT Tx, Baud,[DEC Freq,CR]	' Send Frequency as decimal
    PAUSE 500	' Short delay
    ENDIF
    LOOP
    
    



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    #SELECT $STAMP
    #CASE BS2, BS2E, BS2PE
    T9600	CON	84
    #CASE BS2SX, BS2P
    T9600	CON	240
    #CASE BS2PX
    T9600	CON	396
    #ENDSELECT
    ' ***************** Variables, Constants and Pins
    Baud	CON	T9600 ' Set Baud rate
    Rx	PIN	15	' XBee DOUT
    Tx	PIN	14	' XBee DIN
    Led	PIN	0
    Freq	VAR	Word
    ' ***************** Main Loop
    DO
    SERIN Rx, Baud, [DEC Freq]	' Wait for decimal and accept
    HIGH LED	' Turn on LED
    LOW LED	' Turn off LED
    LOOP
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-17 18:26
    First of all, what microcontroller are you going to use? A Propeller or a Stamp? They're different and the way you'd program them is different. What kind of board would you be using? Do you have something already? If you're using a Stamp, how are you handling the +3.3V power needs of the xBee and the interfacing between the 5V logic of the Stamp and the 3.3V logic of the xBee ... there are adapter boards ... what are you using? Similarly, the Ping works directly with the Stamp because they're both 5V devices. The Propeller can be interfaced to a Ping. How are you doing that?
  • Tony11Tony11 Posts: 41
    edited 2012-07-18 15:21
    I have=

    2 Propeller Demo Board (Item code 28015)

    2 xbee's with two Xbee's SIP Adapter (Item code 32402).

    2 ping sensor's (Item code 28015)

    2 led's



    I do know that I will have to use 1 kΩ resistor (when using the ping sinsors to the Propeller Demo Board) and
    I all so know that I will have to use the 5V on the Sip Adaptor board for the xbee's.

    So on the xbee's, in the program how do I send the state of the ping data (1), from one Box1 to Box2 and
    or Box2 to box1.

    I hope this helps
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-18 21:36
    As I mentioned before, you will have to configure the xBees. You can use the Digi X-CTU utility, but you'll need a USB adapter for the xBees. You can also write a simple program to configure them or configure them as part of the program initialization for your programs. The Parallax guide to the xBees discusses configuration of these. Essentially you want the xBees to power up with a configuration such that they connect to one another. Once they finish their initialization, they'll be ready to look like an ordinary wired serial connection.

    You use some kind of serial I/O in your program to talk to them. Since you're already using FullDuplexSerial to talk to the PC, you can use that to talk to the xBees. Just use the appropriate pin numbers in the FullDuplexSerial start call. I'd suggest that you use 9600 Baud for the xBees and FullDuplexSerial. If you want to send debug messages to the PC as well, use two instances of FullDuplexSerial, one for each serial port. Just declare two OBJs, both of them FullDuplexSerial.

    Whatever is sent from Box1 to its xBee will be received by Box2 and whatever is sent from Box2 to its xBee will be received by Box1. The simplest thing would be to send the number you get from the Ping routines, preferably the distance in millimeters since all of the distances are integers and you'll have finer resolution using millimeters.

    I'd send an identification character, then send the data as two 8-bit bytes like this:
    pingData := ping.Millimeters(pingPin)   ' Get Ping data
    xBee.tx("!")   ' Send identification byte
    xBee.tx(pingData>>8)   ' Send most significant byte
    xBee.tx(pingData&$FF)   ' Send least significant byte
    
    ping is the Ping object and xBee is the FullDuplexSerial object for the xBee.

    On the receiving end, you'd do something like:
    repeat until xBee.rx == "!"   ' Throw away bytes until identification byte received
    pingData := xBee.rx << 8   ' Get most significant byte
    pingData += xBee.rx   ' Add in least significant byte
    
    The identification byte is some arbitrary character used to help sync the two boxes. It's not perfect in that one of the data bytes might be the same and the two boxes might get confused for a while. There are ways to have a sync byte that doesn't occur in the data, but this is a good start.
  • Tony11Tony11 Posts: 41
    edited 2012-07-25 15:59
    I am trying to make this program work, can I have some help.






    CON 
      _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      ' Set pins and Baud rate for XBee comms   
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      ' Set pins and baud rate for PC comms  
      PC_Rx     = 31   
      PC_Tx     = 30 
      PC_Baud   = 9600 
     Ping_Pin  = 4        
        
    Var 
      long stack[50]                ' stack space for second cog 
                                                                            
    OBJ 
      PC    : "FullDuplexSerial"   
      XB    : "FullDuplexSerial" 
    Pub Start  
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee   
      cognew(XB_to_PC,@stack)       ' Start cog for XBee--> PC comms 
      PC.rxFlush                    ' Empty buffer for data from PC 
    
    
    Pub XB_to_PC 
      XB.rxFlush                    ' Empty buffer for data from XB 
      repeat                  
        PC.tx(XB.rx)                ' Accept data from XBee and send to PC
    
      repeat   
    
        range := ping. Millimeters(ping_pin)           ' Get range in  Millimeters           
        XB.str(string("Ping Measurement:"))      ' send string
        XB.dec(range)                            ' send decimal value
        XB.Tx(CR)                                ' send Carriage Return
        waitcnt (clkfreq/4 + cnt)                 
        XB.tx(PC.rx)                ' Accept data from PC and send to XBee 
            
    
    





    CON   _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      CR        = 13   ' Carriage Return value       
      Ping_Pin  = 4                                                                        
    OBJ 
       XB    : "FullDuplexSerial"
       Ping  : "Ping"
    Pub  Start | Counter, range 
    XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee 
       
    waitcnt(clkfreq + cnt)
    
    Pub XB_to_PC 
      XB.rxFlush                    ' Empty buffer for data from XB 
      repeat                  
        PC.tx(XB.rx)                ' Accept data from XBee and send to PC
    
     repeat
        range := ping. Millimeters(ping_pin)           ' Get range in  Millimeters           
        XB.str(string("Ping Measurement:"))      ' send string
        XB.dec(range)                            ' send decimal value
        XB.Tx(CR)                                ' send Carriage Return
        waitcnt (clkfreq/4 + cnt)
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-25 16:47
    You're going to have to just throw out what you've got so far and start over. For one thing, you can't have two cogs both using the serial objects as in your first program and your Start method in the second program never calls the XB_to_PC method, so nothing happens.

    Your CON and OBJ sections look fine, so start with that. You're going to need two separate programs, one for each board, and I'd only include the things that you need for the particular program, i.e. no Ping definition for the program that doesn't have a Ping attached. Write each program completely within a single method (call it Start, but the name doesn't matter). Start the method with the initialization of any objects used. One will have a .start call for its xBee serial port. The other will have a .start call for an xBee port and another port for the PC. The Ping object doesn't need a .start call. The .start call already initializes the receive buffer to empty, so you don't need to flush it as well.

    For the first program, test things by calling the Ping object and sending the data followed by a carriage return once a second. You've almost got that already, but it goes into a repeat loop in the Start method.

    For the second program, just have a repeat loop that reads a character from the xBee serial port and echos it immediately to the PC serial port. Once that works for you, you can fancy it up with labels sent to the PC.

    Once you have this working, swap the two boards' PC connection and swap programs so you have it working in the other direction. At this point, you've checked out the hardware and made sure the xBee link works.

    For bidirectional use, you can take what I've given in Post #15 and just do the transmit and receive parts in order. The serial port is buffered with a 16 byte buffer, so, if you keep the message length to under 16 bytes or change to a different serial port object in the Object Exchange that has a bigger buffer, you can wait to process the receive information while the Ping and transmit is being done. The data coming back from the other xBee will just wait in the buffer until you access it.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-25 17:00
    CON 
      _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      ' Set pins and Baud rate for XBee comms   
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      ' Set pins and baud rate for PC comms  
      PC_Rx     = 31   
      PC_Tx     = 30 
      PC_Baud   = 9600 
     Ping_Pin  = 4        
        
    OBJ 
      PC    : "FullDuplexSerial"   
      XB    : "FullDuplexSerial" 
      Ping  : "Ping"
    
    Pub Start  |  pingData
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee   
      repeat
        pingData := Ping.Millimeters(Ping_Pin)   ' Get Ping data
        XB.tx("!")   ' Send identification byte
        XB.tx(pingData>>8)   ' Send most significant byte
        XB.tx(pingData&$FF)   ' Send least significant byte
        PC.str(string("Ping data: "))
        PC.dec(pingData)
        PC.tx(13)
        ' Note: the following requires that a "!" be sent by the other program.  This will
        ' work as long as there's not an error in the xBee transmission so that the "!"
        ' gets dropped.  Since the xBees check and retry their transmissions, this will
        ' normally work.  There should be some kind of timeout that will force this program
        ' to go back to transmitting another packet with the other program doing the same.
        ' Alternatively, when a timeout occurs, this program could transmit a "!" followed
        ' by two zero bytes and both programs would otherwise ignore a "!" followed by two
        ' zero bytes (not display such a value).  That would allow the programs to sync again.
        repeat until XB.rx == "!"   ' Throw away bytes until identification byte received
        pingData := XB.rx << 8   ' Get most significant byte
        pingData += XB.rx   ' Add in least significant byte
        PC.str(string("xBee data: "))
        PC.dec(pingData)
        PC.tx(13)
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-25 17:07
    A timeout could be done by having:
    Pub Start | pingData, timeOut
    ' ...
        if pingData <> 0
          PC.str(string("Ping data: "))
          PC.dec(pingData)
          PC.tx(13)
    ' ...
        timeOut := CNT
        repeat until XB.rx == "!"
          if (CNT - timeOut) > CLKFREQ*2   ' Two second timeout
            XB.tx("!")   ' Send dummy packet to other board
            XB.tx(0)
            XB.tx(0)
            timeOut := CNT   ' Reset timeout time
      ' ...
    
  • Tony11Tony11 Posts: 41
    edited 2012-07-26 18:11
    Well I tried this code and the ping sensors didn't work. This just might be over my head, but I did give it A try.
    CON 
      _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      ' Set pins and Baud rate for XBee comms   
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      ' Set pins and baud rate for PC comms  
      PC_Rx     = 31   
      PC_Tx     = 30 
      PC_Baud   = 9600 
     Ping_Pin  = 4        
        
    OBJ 
      PC    : "FullDuplexSerial"   
      XB    : "FullDuplexSerial" 
      Ping  : "Ping"
    
    Pub Start  |  pingData , timeOut 
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee   
      repeat
        pingData := Ping.Millimeters(Ping_Pin)   ' Get Ping data
        XB.tx("!")   ' Send identification byte
        XB.tx(pingData>>8)   ' Send most significant byte
        XB.tx(pingData&$FF)   ' Send least significant byte
        PC.str(string("Ping data: "))
        PC.dec(pingData)
        PC.tx(13)
        ' Note: the following requires that a "!" be sent by the other program.  This will
        ' work as long as there's not an error in the xBee transmission so that the "!"
        ' gets dropped.  Since the xBees check and retry their transmissions, this will
        ' normally work.  There should be some kind of timeout that will force this program
        ' to go back to transmitting another packet with the other program doing the same.
        ' Alternatively, when a timeout occurs, this program could transmit a "!" followed
        ' by two zero bytes and both programs would otherwise ignore a "!" followed by two
        ' zero bytes (not display such a value).  That would allow the programs to sync again.
        repeat until XB.rx == "!"   ' Throw away bytes until identification byte received
        pingData := XB.rx << 8   ' Get most significant byte
        pingData += XB.rx   ' Add in least significant byte
        PC.str(string("xBee data: "))
        PC.dec(pingData)
        PC.tx(13)
    
         
    ' ...
        if pingData <> 0
          PC.str(string("Ping data: "))
          PC.dec(pingData)
          PC.tx(13)
    ' ...
        timeOut := CNT
        repeat until XB.rx == "!"
          if (CNT - timeOut) > CLKFREQ*2   ' Two second timeout
            XB.tx("!")   ' Send dummy packet to other board
            XB.tx(0)
            XB.tx(0)
            timeOut := CNT   ' Reset timeout time
      ' ... 
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-26 19:33
    What happens when you run this? What does the other xBee see? You'll have to write a simple program for the 2nd board that just copies the received data to the PC for display.
    CON 
      _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      ' Set pins and Baud rate for XBee comms   
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      ' Set pins and baud rate for PC comms  
      PC_Rx     = 31   
      PC_Tx     = 30 
      PC_Baud   = 9600 
      Ping_Pin  = 4        
        
    OBJ 
      PC    : "FullDuplexSerial"   
      XB    : "FullDuplexSerial" 
      Ping  : "Ping"
    
    Pub Start  |  pingData, timeOut
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee   
      repeat
        pingData := Ping.Millimeters(Ping_Pin)   ' Get Ping data
        XB.tx("!")   ' Send identification byte
        XB.tx(pingData>>8)   ' Send most significant byte
        XB.tx(pingData&$FF)   ' Send least significant byte
        if pingData <> 0
          PC.str(string("Ping data: "))
          PC.dec(pingData)
          PC.tx(13)
        timeOut := CNT
        repeat until XB.rx == "!"
          if (CNT - timeOut) > CLKFREQ*2   ' Two second timeout
            XB.tx("!")   ' Send dummy packet to other board
            XB.tx(0)
            XB.tx(0)
            timeOut := CNT   ' Reset timeout time
        pingData := XB.rx << 8   ' Get most significant byte
        pingData += XB.rx   ' Add in least significant byte
        PC.str(string("xBee data: "))
        PC.dec(pingData)
        PC.tx(13)
    
  • Tony11Tony11 Posts: 41
    edited 2012-07-27 07:12
    This code works great, thank you. But is there A way to speed it up, to read the ping data faster.
    For the led to turn on (Ping data < 100) and (xBee data < 100) turn on led
    if (Ping data < 100) and (xBee data < 100)
    outa[7] := 1
    waitcnt(clkfreq/4 + cnt)
    elseif 
    outa[7] := 0
    waitcnt(clkfreq/4 + cnt)
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-27 08:44
    What happens with this?
    CON 
      _clkmode = xtal1 + pll16x 
      _xinfreq = 5_000_000 
      ' Set pins and Baud rate for XBee comms   
      XB_Rx     = 0    ' XBee DOUT 
      XB_Tx     = 1    ' XBee DIN 
      XB_Baud   = 9600 
      ' Set pins and baud rate for PC comms  
      PC_Rx     = 31   
      PC_Tx     = 30 
      PC_Baud   = 9600 
      Ping_Pin  = 4        
        
    OBJ 
      PC    : "FullDuplexSerial"   
      XB    : "FullDuplexSerial" 
      Ping  : "Ping"
    
    Pub Start  |  pingData, timeOut, xBeeData
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee   
      repeat
        pingData := Ping.Millimeters(Ping_Pin)   ' Get Ping data
        XB.tx("!")   ' Send identification byte
        XB.tx(pingData>>8)   ' Send most significant byte
        XB.tx(pingData&$FF)   ' Send least significant byte
        if pingData <> 0
          PC.str(string("Ping data: "))
          PC.dec(pingData)
          PC.tx(13)
        timeOut := CNT
        repeat until XB.rxcheck == "!"
          if (CNT - timeOut) > CLKFREQ*2   ' Two second timeout
            XB.tx("!")   ' Send dummy packet to other board
            XB.tx(0)
            XB.tx(0)
            timeOut := CNT   ' Reset timeout time
        xBeeData := XB.rx << 8   ' Get most significant byte
        xBeeData += XB.rx   ' Add in least significant byte
        PC.str(string("xBee data: "))
        PC.dec(xBeeData)
        PC.tx(13)
        if (pingData < 100) and (xBeeData < 100)
          outa[7] := 1
        else 
          outa[7] := 0
    
  • Tony11Tony11 Posts: 41
    edited 2012-07-28 08:51
    Well I did try your code out and the led's didn't turn on. So I add some line's of code, one led would go High and the other led would blink with the ping sensor (led) blink. What I am shooting for is both led's to go High and the ping sensor's to read faster.

    Pub Start  |  pingData, timeOut, xBeeData
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee   
      repeat
        pingData := Ping.Millimeters(Ping_Pin)   ' Get Ping data
        XB.tx("!")   ' Send identification byte
        XB.tx(pingData>>8)   ' Send most significant byte
        XB.tx(pingData&$FF)   ' Send least significant byte
        if pingData <> 0
          PC.str(string("Ping data: "))
          PC.dec(pingData)
          PC.tx(13)
        timeOut := CNT
        repeat until XB.rxcheck == "!"
          if (CNT - timeOut) > CLKFREQ*2   ' Two second timeout
            XB.tx("!")   ' Send dummy packet to other board
            XB.tx(0)
            XB.tx(0)
            timeOut := CNT   ' Reset timeout time
        xBeeData := XB.rx << 8   ' Get most significant byte
        xBeeData += XB.rx   ' Add in least significant byte
        PC.str(string("xBee data: "))
        PC.dec(xBeeData)
        PC.tx(13)
        if (pingData < 100) and (xBeeData < 100)
          outa[7] := 1
          dira[7] : = 1
        else 
          outa[7] := 0
          dira[7] : = 0
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-28 09:39
    Sorry, I did forget to initialize DIRA for the LED. This is a better way to do it.

    So, how often does the LED light when the Ping data is within range? How often does the Ping trigger light come on? The PC debug display does take a little time, maybe 50ms when the transmit buffer gets full. The xBee also takes some time to actually send the data back and forth.
    Pub Start  |  pingData, timeOut, xBeeData
      PC.start(PC_Rx, PC_Tx, 0, PC_Baud) ' Initialize comms for PC   
      XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee
      outa[7] := 0
      dira[7] := 1   ' Set LED I/O pin to output low
      repeat
        pingData := Ping.Millimeters(Ping_Pin)   ' Get Ping data
        XB.tx("!")   ' Send identification byte
        XB.tx(pingData>>8)   ' Send most significant byte
        XB.tx(pingData&$FF)   ' Send least significant byte
        if pingData <> 0
          PC.str(string("Ping data: "))
          PC.dec(pingData)
          PC.tx(13)
        timeOut := CNT
        repeat until XB.rxcheck == "!"
          if (CNT - timeOut) > CLKFREQ*2   ' Two second timeout
            XB.tx("!")   ' Send dummy packet to other board
            XB.tx(0)
            XB.tx(0)
            timeOut := CNT   ' Reset timeout time
        xBeeData := XB.rx << 8   ' Get most significant byte
        xBeeData += XB.rx   ' Add in least significant byte
        PC.str(string("xBee data: "))
        PC.dec(xBeeData)
        PC.tx(13)
        if (pingData < 100) and (xBeeData < 100)
          outa[7] := 1
        else 
          outa[7] := 0
    
  • Tony11Tony11 Posts: 41
    edited 2012-08-07 15:31
    Well the led's, one works good, but the other one blinks on and off. The one led that works good has the usb data line to the computer and propeller chip.

    Is there some way that I can use the xbee to send on-off signal transmission, like Momentary.






    Box1 (ping,xbee,propeller,and led)

    if (ping1 < 100) := (xbeeon1)

    then send that (:=xbeeon1) to xbee2

    if ( xbeeon1 and xbeeon2)

    then high led

    =========================================



    Box2 (ping,xbee,propeller,and led)

    if (ping2 < 100) := (xbeeon2)

    then send that (:=xbeeon2) to xbee1

    if ( xbeeon1 and xbeeon2)

    then high led
  • Tony11Tony11 Posts: 41
    edited 2012-08-15 15:26
    What I need some help on, is there A way to use two xbee's. To use the xbee's Input/Output Line Passing, like I found this setup, on utub. Or where can I find
    some info, about it. Here is the link.




    www.youtube.com/watch?v=xNru4zHEERc
  • Mike GreenMike Green Posts: 23,101
    edited 2012-08-15 17:25
    Can't help you there. Perhaps someone else will chime in. There's information on this capability in the xBee manual.
Sign In or Register to comment.