setup_xbee.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2011, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 __author__ = "astambler@willowgarage.com (Adam Stambler)"
35 
36 
37 import serial
38 import yaml
39 import sys
40 import time
41 
42 from optparse import OptionParser
43 
44 #
45 
46 help = """
47 %prog [options] port my_adr
48 
49 setup_xbee.py is a configuration script for Xbees. It takes
50 factory fresh xbee and programs it to work with your rosserial network.
51 If XBee is not factory fresh, use Digi's X-CTU software to program it.
52 
53  port : serial port of port of the xbee (/dev/ttyUSB0)
54  my_adr: MY address is the 16 bit address of this xbee in the
55  network. This must be a unique address in the network.
56  This address is always 0 for the coordinator. """
57 parser = OptionParser(usage=help)
58 
59 
60 parser.add_option('-P', '--pan_id', action="store", type="int", dest="pan_id", default=1331, help="Pan ID of the xbee network. This ID must be the same for all XBees in your network.")
61 parser.add_option('-c', '--channel', action="store", type="string", dest="channel", default="0D", help="Frequency channel for the xbee network. The channel value must be the same for all XBees in your network.")
62 parser.add_option('-C', '--coordinator', action="store_true", dest="coordinator", default=False, help="Configures the XBee as Coordinator for the network. Only make the XBee connected to the computer a coordiantor.")
63 
64 
65 
66 def send(port, cmd):
67  for c in cmd+'\r':
68  port.write(c)
69  time.sleep(0.06)
70 
71 def setAT(port, cmd):
72  port.flushInput()
73  send(port, 'AT'+cmd)
74  rsp = port.readline()
75  print rsp
76  if 'OK' in rsp:
77  return True
78  else :
79  return False
80 
81 baud_lookup= { 1200 : 0,
82  2400 : 1,
83  4800 : 2,
84  9600 : 3,
85  19200 : 4,
86  38400 : 5,
87  57600 : 6,
88  115200 : 7}
89 
90 
91 
92 def beginAtMode(port):
93 
94  for i in range(0,3):
95  port.write('+')
96  time.sleep(0.05)
97  time.sleep(1)
98  if port.read(2) == 'OK':
99  return True
100  else :
101  return False
102 
103 if __name__ == '__main__':
104 
105  opts, args = parser.parse_args()
106 
107  if len(args) < 2:
108  print "Not enough arguments!"
109  exit()
110 
111  baud = 57600
112  port_name = args[0]
113  my_address = int(args[1])
114 
115  port = serial.Serial(port_name, baud, timeout=1.5)
116 
117  if beginAtMode(port):
118  print "Connected to the XBee"
119  else:
120  print "Failed to connect to the XBee"
121  exit()
122 
123 
124  cmd = ''
125  if (opts.coordinator):
126  cmd += 'AP2,CE1,' #API mode 2, and enable coordinator
127 
128  cmd += 'MY%d,'%int(args[1]) #set the xbee address
129  cmd += 'BD%d,'%baud_lookup[57600] #set the xbee to interface at 57600 baud
130  cmd += 'ID%d,'%opts.pan_id
131  cmd += 'CH%s,'%opts.channel
132  cmd += 'DL0,'
133  cmd += 'RN1,' #enables collision avoidance on first transmission
134  cmd += 'RO5,' #sets packetization timeout to 5 characters
135  cmd += 'WR' #wrtie the commands to nonvolatile memory
136 
137 
138  if setAT(port, 'RE'): #reset the xbee
139  print "XBee reset"
140  else:
141  print "Reset failed"
142  exit()
143  beginAtMode(port)
144  time.sleep(1)
145  print "Sending command : ", cmd
146 
147  if setAT(port, cmd):
148  print "XBee sucessfully programed!"
149  else:
150  print "XBee programming failed. Try again and then investigate using X-CTU"
151 
152 
153 
def send(port, cmd)
Definition: setup_xbee.py:66
def setAT(port, cmd)
Definition: setup_xbee.py:71
def beginAtMode(port)
Definition: setup_xbee.py:92


rosserial_xbee
Author(s): Adam Stambler
autogenerated on Fri Jun 7 2019 22:03:11