ddwrt_sim.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, 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 the Willow Garage 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 # Revision $Id: gossipbot.py 1013 2008-05-21 01:08:56Z sfkwc $
35 
36 import os, sys, string, time, getopt, re
37 import StringIO
38 import random
39 
40 import rospy
41 from wifi_ddwrt.msg import *
42 from pr2_msgs.msg import AccessPoint
43 
44 from std_msgs.msg import Header
45 
46 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}]}
47 
48 def minmax(v, lower, upper):
49  if v < lower: v = lower
50  if v > upper: v = upper
51  return v
52 
53 class WifiAP:
54  def __init__(self):
55  self.ap = AccessPoint()
56  self.t = time.time()
57  self._pick_ap()
58 
59  def _pick_ap(self):
60  ap = random.choice(wifi_data['networks'])
61  self.ap.macaddr = ap['macaddr']
62  self.ap.channel = ap['channel']
63  self.ap.essid = ap['essid']
64  self.ap.rate = '54 Mbps'
65  self.ap.tx_power = '71 mW'
66  self.ap.signal = random.randint(-100, -48)
67  self.ap.snr = 44
68  self.ap.noise = -92
69  self.ap.quality = 56
70 
71  def _gen_snr(self):
72  d = 2
73  if time.time() - self.t > 20:
74  d = 10
75  self.ap.signal = minmax(self.ap.signal + random.randint(-d, d), -70, -20)
76 
77  self.ap.snr = minmax(self.ap.snr + random.randint(-d, d), 30, 90)
78  self.ap.noise = minmax(self.ap.noise + random.randint(-d, d), -95, -30)
79  self.ap.quality = int(self.ap.signal * 1.24 + 116)
80 
81  def fetchSiteSurvey(self):
82  header = Header()
83  header.stamp = rospy.Time.now()
84  networks = []
85  survey = SiteSurvey(header, networks)
86 
87  for network in wifi_data["networks"]:
88  network = Network(network['macaddr'], network['essid'], network['channel'], network['rssi'], network['noise'], network['beacon'])
89  survey.networks.append(network)
90  return survey
91 
92  def fetchCurrentAP(self):
93  if time.time() - self.t > 60:
94  self._pick_ap()
95  self._gen_snr()
96 
97  #make sure that we put a stamp on things
98  self.ap.header = Header()
99  self.ap.header.stamp = rospy.Time.now()
100 
101  return self.ap
102 
103 def loop():
104  rospy.init_node("wifi_ddwrt")
105 
106  ap = WifiAP()
107 
108  pub1 = rospy.Publisher("ddwrt/sitesurvey", SiteSurvey)
109  pub2 = rospy.Publisher("ddwrt/accesspoint", AccessPoint)
110 
111  while not rospy.is_shutdown():
112  survey = ap.fetchSiteSurvey()
113  pub1.publish(survey)
114  node = ap.fetchCurrentAP()
115  pub2.publish(node)
116  time.sleep(5)
117 
118 def usage(progname):
119  print __doc__ % vars()
120 
121 def main(argv, stdout, environ):
122  progname = argv[0]
123  optlist, args = getopt.getopt(argv[1:], "", ["help",])
124 
125  for (field, val) in optlist:
126  if field == "--help":
127  usage(progname)
128  return
129 
130  loop()
131 
132 if __name__ == "__main__":
133  main(sys.argv, sys.stdout, os.environ)
134 
135 
def fetchSiteSurvey(self)
Definition: ddwrt_sim.py:81
def _gen_snr(self)
Definition: ddwrt_sim.py:71
def __init__(self)
Definition: ddwrt_sim.py:54
def main(argv, stdout, environ)
Definition: ddwrt_sim.py:121
def minmax(v, lower, upper)
Definition: ddwrt_sim.py:48
def usage(progname)
Definition: ddwrt_sim.py:118
def _pick_ap(self)
Definition: ddwrt_sim.py:59
def loop()
Definition: ddwrt_sim.py:103
def fetchCurrentAP(self)
Definition: ddwrt_sim.py:92


wifi_ddwrt
Author(s): Scott Hassan , Eitan Marder-Eppstein
autogenerated on Wed May 12 2021 02:21:31