xbee_network.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2011, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 __author__ = "astambler@willowgarage.com (Adam Stambler)"
00035 
00036 from xbee import XBee
00037 import serial
00038 
00039 from rosserial_python import SerialClient
00040 import rospy
00041 
00042 import threading
00043 import sys
00044 import time
00045 import struct
00046 
00047 client_ports=  {}
00048 clients = {}
00049 
00050 debug = False;
00051 
00052 import threading
00053 
00054 
00055 class FakeSerial():
00056         def __init__(self, id, xbee):
00057                 self.rxdata =''
00058                 self.xbee  = xbee
00059                 self.id = id
00060                 self.lock = threading.Lock()
00061                 self.timeout = 0.1
00062                 
00063         def read(self, size = 1):
00064                 t= 0
00065                 counts = self.timeout/0.01
00066                 #print "s%d   %s"%(size, self.rxdata)
00067                 while( ( len(self.rxdata) < size ) and  (not rospy.is_shutdown()) ):
00068                         time.sleep(0.01)
00069                         t = t +1
00070                         if (t >  counts):
00071                                 return ''
00072                         
00073                 with (self.lock):
00074                         out = self.rxdata[:size]
00075                         self.rxdata = self.rxdata[size:]
00076                         
00077                 #print "fake out " , out
00078                 return out
00079                 
00080         def write(self, data):
00081                 if (debug):
00082                         print "Sending ", [d for d in data]
00083                 self.xbee.send('tx', frame_id='0', options="\x01", dest_addr=self.id,data=data)
00084                 
00085         def putData(self, data):
00086                 with (self.lock):
00087                         self.rxdata = self.rxdata+data
00088         
00089         def flushInput(self):
00090                 self.rxdata = ''
00091 
00092         # Returns the number of bytes available to be read
00093         def inWaiting(self):
00094             return len(self.rxdata)
00095 
00096 if __name__== '__main__':
00097         print "RosSerial Xbee Network"
00098         rospy.init_node('xbee_network')
00099         sys.argv= rospy.myargv(argv=sys.argv) 
00100         
00101         xbee_port = ''
00102         network_ids = [] 
00103         
00104         if len(sys.argv) <3 :
00105                 print """
00106 This program connects to rosserial xbee nodes.  The program must be called
00107 like :
00108 
00109 ./xbee_network.py <xbee_serial_port> ID1 [ ID2 ID3 ....] 
00110 """
00111                 exit()
00112         else :
00113                 xbee_port = sys.argv[1]         
00114                 network_ids  = [ struct.pack('>h', int(id) ) for id in sys.argv[2:] ]
00115         
00116         print "Contacting Xbees : " , network_ids
00117 
00118                 
00119         # Open serial port
00120         ser = serial.Serial(xbee_port, 57600, timeout=0.01)
00121         ser.flush()
00122         ser.flushInput()
00123         ser.flushOutput()
00124         time.sleep(1)
00125         # Create API object
00126         xbee = XBee(ser, escaped= True)
00127         
00128         for xid in network_ids:
00129                 client_ports[xid] = FakeSerial(xid, xbee)
00130                 time.sleep(.2)
00131                 clients[xid] = SerialClient(client_ports[xid])
00132                  
00133         threads = [ threading.Thread(target=c.run) for c in clients.values()]
00134         
00135         for t in threads:
00136                 t.deamon =True 
00137                 t.start()
00138 
00139         while not rospy.is_shutdown():
00140                 try:
00141                         msg = xbee.wait_read_frame()
00142                         if (debug):
00143                                 print "Received " , msg
00144         
00145                         if  msg['id'] == 'rx':
00146                                 src = msg['source_addr']
00147                                 data = msg['rf_data']
00148                                 try:
00149                                         client_ports[src].putData(data)
00150                                 except KeyError as e:
00151                                         print "Rcv ID corrupted"
00152                 except KeyboardInterrupt:
00153                         break
00154         ser.close()
00155         
00156         print "Quiting the Sensor Network"
00157 
00158 


rosserial_xbee
Author(s): Adam Stambler
autogenerated on Sat Oct 7 2017 03:09:02