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 import os, sys, string, time, getopt, re
00037 import StringIO
00038 import random
00039 
00040 import rospy
00041 from wifi_ddwrt.msg import *
00042 from pr2_msgs.msg import AccessPoint
00043 
00044 from std_msgs.msg import Header
00045 
00046 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}]}
00047 
00048 def minmax(v, lower, upper):
00049     if v < lower: v = lower
00050     if v > upper: v = upper
00051     return v
00052 
00053 class WifiAP:
00054   def __init__(self):
00055       self.ap = AccessPoint()
00056       self.t = time.time()
00057       self._pick_ap()
00058       
00059   def _pick_ap(self):
00060       ap = random.choice(wifi_data['networks'])
00061       self.ap.macaddr = ap['macaddr']
00062       self.ap.channel = ap['channel']
00063       self.ap.essid = ap['essid']
00064       self.ap.rate = '54 Mbps'
00065       self.ap.tx_power = '71 mW'
00066       self.ap.signal = random.randint(-100, -48)
00067       self.ap.snr = 44
00068       self.ap.noise = -92
00069       self.ap.quality = 56
00070 
00071   def _gen_snr(self):
00072       d = 2
00073       if time.time() - self.t > 20:
00074           d = 10
00075       self.ap.signal = minmax(self.ap.signal + random.randint(-d, d), -70, -20)
00076 
00077       self.ap.snr = minmax(self.ap.snr + random.randint(-d, d), 30, 90)
00078       self.ap.noise = minmax(self.ap.noise + random.randint(-d, d), -95, -30)
00079       self.ap.quality = int(self.ap.signal * 1.24 + 116)
00080   
00081   def fetchSiteSurvey(self):
00082     header = Header()
00083     header.stamp = rospy.Time.now()
00084     networks = []
00085     survey = SiteSurvey(header, networks)
00086     
00087     for network in wifi_data["networks"]:
00088       network = Network(network['macaddr'], network['essid'], network['channel'], network['rssi'], network['noise'], network['beacon'])
00089       survey.networks.append(network)
00090     return survey
00091 
00092   def fetchCurrentAP(self):
00093     if time.time() - self.t > 60:
00094         self._pick_ap()
00095     self._gen_snr()
00096     
00097     #make sure that we put a stamp on things
00098     self.ap.header = Header()
00099     self.ap.header.stamp = rospy.Time.now()
00100 
00101     return self.ap
00102 
00103 def loop():
00104   rospy.init_node("wifi_ddwrt")
00105 
00106   ap = WifiAP()
00107 
00108   pub1 = rospy.Publisher("ddwrt/sitesurvey", SiteSurvey)
00109   pub2 = rospy.Publisher("ddwrt/accesspoint", AccessPoint)
00110 
00111   while not rospy.is_shutdown():
00112     survey = ap.fetchSiteSurvey()
00113     pub1.publish(survey)
00114     node = ap.fetchCurrentAP()
00115     pub2.publish(node)
00116     time.sleep(5)
00117         
00118 def usage(progname):
00119   print __doc__ % vars()
00120 
00121 def main(argv, stdout, environ):
00122   progname = argv[0]
00123   optlist, args = getopt.getopt(argv[1:], "", ["help",])
00124 
00125   for (field, val) in optlist:
00126     if field == "--help":
00127       usage(progname)
00128       return
00129 
00130   loop()
00131 
00132 if __name__ == "__main__":
00133   main(sys.argv, sys.stdout, os.environ)
00134         
00135 


wifi_ddwrt
Author(s): Scott Hassan , Eitan Marder-Eppstein
autogenerated on Mon Sep 14 2015 04:01:40