ddwrt_sim.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2008, 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 the Willow Garage 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 # Revision $Id: gossipbot.py 1013 2008-05-21 01:08:56Z sfkwc $
00035 
00036 ## chat is a simple IM-like test node. It demonstrates publishing and
00037 ## subscribing to the same topic.
00038 
00039 PKG = 'wifi_ddwrt'
00040 import roslib; roslib.load_manifest(PKG) 
00041 
00042 import os, sys, string, time, getopt, re
00043 import StringIO
00044 import random
00045 
00046 import rospy
00047 from wifi_ddwrt.msg import *
00048 from pr2_msgs.msg import AccessPoint
00049 
00050 from std_msgs.msg import Header
00051 
00052 wifi_data = {"header": {"seq": 24, "stamp": 1257901832.6, "frame_id": "0"}, "networks": [{"macaddr": "00:18:F8:F9:6C:41", "essid": "willow", "channel": 11, "rssi": -35, "noise": -88, "beacon": 100}, {"macaddr": "00:18:F8:F9:6B:BD", "essid": "willow", "channel": 11, "rssi": -71, "noise": -88, "beacon": 100}, {"macaddr": "00:18:F8:F9:6C:1D", "essid": "willow", "channel": 1, "rssi": -70, "noise": -92, "beacon": 100}, {"macaddr": "00:30:44:03:1F:F9", "essid": "PRLAN", "channel": 1, "rssi": -73, "noise": -92, "beacon": 100}, {"macaddr": "00:30:44:03:1F:F4", "essid": "PRGLAN", "channel": 2, "rssi": -80, "noise": -89, "beacon": 100}, {"macaddr": "00:18:F8:F9:6C:4D", "essid": "willow", "channel": 6, "rssi": -81, "noise": -86, "beacon": 100}, {"macaddr": "00:18:F8:F9:6C:44", "essid": "willow", "channel": 6, "rssi": -79, "noise": -85, "beacon": 100}]}
00053 
00054 def minmax(v, lower, upper):
00055     if v < lower: v = lower
00056     if v > upper: v = upper
00057     return v
00058 
00059 class WifiAP:
00060   def __init__(self):
00061       self.ap = AccessPoint()
00062       self.t = time.time()
00063       self._pick_ap()
00064       
00065   def _pick_ap(self):
00066       ap = random.choice(wifi_data['networks'])
00067       self.ap.macaddr = ap['macaddr']
00068       self.ap.channel = ap['channel']
00069       self.ap.essid = ap['essid']
00070       self.ap.rate = '54 Mbps'
00071       self.ap.tx_power = '71 mW'
00072       self.ap.signal = random.randint(-100, -48)
00073       self.ap.snr = 44
00074       self.ap.noise = -92
00075       self.ap.quality = 56
00076 
00077   def _gen_snr(self):
00078       d = 2
00079       if time.time() - self.t > 20:
00080           d = 10
00081       self.ap.signal = minmax(self.ap.signal + random.randint(-d, d), -70, -20)
00082 
00083       self.ap.snr = minmax(self.ap.snr + random.randint(-d, d), 30, 90)
00084       self.ap.noise = minmax(self.ap.noise + random.randint(-d, d), -95, -30)
00085       self.ap.quality = int(self.ap.signal * 1.24 + 116)
00086   
00087   def fetchSiteSurvey(self):
00088     header = Header()
00089     header.stamp = rospy.Time.now()
00090     networks = []
00091     survey = SiteSurvey(header, networks)
00092     
00093     for network in wifi_data["networks"]:
00094       network = Network(network['macaddr'], network['essid'], network['channel'], network['rssi'], network['noise'], network['beacon'])
00095       survey.networks.append(network)
00096     return survey
00097 
00098   def fetchCurrentAP(self):
00099     if time.time() - self.t > 60:
00100         self._pick_ap()
00101     self._gen_snr()
00102     
00103     #make sure that we put a stamp on things
00104     self.ap.header = Header()
00105     self.ap.header.stamp = rospy.Time.now()
00106 
00107     return self.ap
00108 
00109 def loop():
00110   rospy.init_node("wifi_ddwrt")
00111 
00112   ap = WifiAP()
00113 
00114   pub1 = rospy.Publisher("ddwrt/sitesurvey", SiteSurvey)
00115   pub2 = rospy.Publisher("ddwrt/accesspoint", AccessPoint)
00116 
00117   while not rospy.is_shutdown():
00118     survey = ap.fetchSiteSurvey()
00119     pub1.publish(survey)
00120     node = ap.fetchCurrentAP()
00121     pub2.publish(node)
00122     time.sleep(5)
00123         
00124 def usage(progname):
00125   print __doc__ % vars()
00126 
00127 def main(argv, stdout, environ):
00128   progname = argv[0]
00129   optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
00130 
00131   testflag = 0
00132 
00133   for (field, val) in optlist:
00134     if field == "--help":
00135       usage(progname)
00136       return
00137     elif field == "--debug":
00138       debugfull()
00139     elif field == "--test":
00140       testflag = 1
00141 
00142   loop()
00143 
00144 if __name__ == "__main__":
00145   main(sys.argv, sys.stdout, os.environ)
00146         
00147 


wifi_ddwrt
Author(s): Scott Hassan/hassan@willowgarage.com, Eitan Marder-Eppstein/eitan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:36:08