00001
00002 import rospy
00003 import sys
00004 from mm2client import *
00005 import setfilters
00006 from periodic import *
00007 from dynamic_reconfigure.server import Server as DynamicReconfigureServer
00008 from mini_maxwell.cfg import DRCEnvironmentConfig as ConfigType
00009 from threading import Lock
00010 from random import randint
00011 from std_msgs.msg import Bool, Time
00012
00013 LANA_to_LANB = True
00014 LANB_to_LANA = False
00015
00016 class DRCEnvironment():
00017 def __init__(self):
00018 self.mm2name = rospy.get_param('~ip', '133.11.216.47')
00019 self.low_speed_name = "drc_low_speed"
00020 self.high_speed_name = "drc_high_speed"
00021 self.bands = Bands()
00022 self.lock = Lock()
00023 self.LOW_SPEED_BAND_NUM = 1
00024 self.HIGH_SPEED_BAND_NUM = 2
00025
00026 self.pub_is_disabled = rospy.Publisher("~is_disabled", Bool)
00027 self.pub_is_blackout = rospy.Publisher("~is_blackout", Bool)
00028 self.pub_next_whiteout_time = rospy.Publisher("~next_whiteout_time", Time)
00029
00030
00031 self.all_filter_names = setfilters.GetAllFilterNames(self.mm2name)
00032 low_speed_filter = setfilters.FiltSetting(self.low_speed_name,
00033 self.LOW_SPEED_BAND_NUM)
00034
00035 high_speed_filter = setfilters.FiltSetting(self.high_speed_name,
00036 self.HIGH_SPEED_BAND_NUM)
00037 self.filters = [low_speed_filter, high_speed_filter]
00038 self.server = DynamicReconfigureServer(ConfigType, self.reconfigure)
00039 self.blackoutp = False
00040 self.next_blackout = rospy.Time.now()
00041 self.next_whiteout = rospy.Time.now()
00042 self.blackout()
00043 self.timer = rospy.Timer(rospy.Duration(0.1), self.publishStatus)
00044 self.timer = rospy.Timer(rospy.Duration(1.0), self.updateBlackout)
00045 rospy.spin()
00046 def publishStatus(self, event):
00047 with self.lock:
00048 self.pub_is_disabled.publish(Bool(data=self.disable_network_limitation))
00049 self.pub_is_blackout.publish(Bool(data=self.blackoutp))
00050 self.pub_next_whiteout_time.publish(Time(data=self.next_whiteout))
00051 def updateBlackout(self, event):
00052 with self.lock:
00053 if not self.disable_network_limitation:
00054 now = rospy.Time.now()
00055 if not self.blackoutp:
00056 if (self.next_blackout - now).to_sec() < 0:
00057 self.blackout()
00058 else:
00059 if (self.next_whiteout - now).to_sec() < 0:
00060 self.whiteout()
00061 def reconfigure(self, config, level):
00062 with self.lock:
00063 self.disable_network_limitation = config["disable_network_limitation"]
00064
00065 self.low_speed_link_bandwidth = config["low_speed_link_bandwidth"]
00066
00067 self.high_speed_link_bandwidth = config["high_speed_link_bandwidth"]
00068 self.high_speed_link_duration = config["high_speed_link_duration"]
00069 self.high_speed_link_blackout_duration = config["high_speed_link_blackout_duration"]
00070 self.updateMM()
00071 return config
00072 def blackout(self):
00073 rospy.loginfo("blackout")
00074 self.bands.SetDropAmount(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB,
00075 100)
00076 SetMM(self.mm2name, self.bands,
00077 self.filters, self.filters, self.all_filter_names)
00078 self.blackoutp = True
00079 self.next_whiteout = rospy.Time.now() + rospy.Duration(randint(1, self.high_speed_link_blackout_duration))
00080 def whiteout(self):
00081 rospy.loginfo("whiteout")
00082 self.bands.SetDropAmount(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB,
00083 0)
00084 SetMM(self.mm2name, self.bands,
00085 self.filters, self.filters, self.all_filter_names)
00086 self.blackoutp = False
00087 self.next_blackout = rospy.Time.now() + rospy.Duration(self.high_speed_link_duration)
00088 def updateMM(self):
00089 self.bands.SetDelayAmount(self.LOW_SPEED_BAND_NUM, LANA_to_LANB, 0)
00090 self.bands.SetDelayAmount(self.LOW_SPEED_BAND_NUM, LANB_to_LANA, 0)
00091 if not self.disable_network_limitation:
00092 self.bands.SetRateLimit(self.LOW_SPEED_BAND_NUM, LANA_to_LANB,
00093 self.low_speed_link_bandwidth)
00094 self.bands.SetRateLimit(self.LOW_SPEED_BAND_NUM, LANB_to_LANA,
00095 self.low_speed_link_bandwidth)
00096 else:
00097 self.bands.SetRateLimit(self.LOW_SPEED_BAND_NUM, LANA_to_LANB,
00098 100 * 1000 * 1000)
00099 self.bands.SetRateLimit(self.LOW_SPEED_BAND_NUM, LANB_to_LANA,
00100 100 * 1000 * 1000)
00101 self.bands.SetDelayReorder(self.LOW_SPEED_BAND_NUM, LANB_to_LANA, False)
00102 self.bands.SetDelayReorder(self.LOW_SPEED_BAND_NUM, LANA_to_LANB, False)
00103 self.bands.SetDelayAmount(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB, 0)
00104 self.bands.SetDelayAmount(self.HIGH_SPEED_BAND_NUM, LANB_to_LANA, 0)
00105 if not self.disable_network_limitation:
00106 self.bands.SetRateLimit(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB,
00107 self.high_speed_link_bandwidth)
00108 self.bands.SetRateLimit(self.HIGH_SPEED_BAND_NUM, LANB_to_LANA,
00109 self.high_speed_link_bandwidth)
00110 self.bands.SetDropAmount(self.HIGH_SPEED_BAND_NUM, LANB_to_LANA,
00111 100)
00112 self.bands.SetDropAmount(5, LANB_to_LANA, 0)
00113 self.bands.SetDropAmount(5, LANA_to_LANB, 0)
00114 else:
00115 self.bands.SetRateLimit(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB,
00116 100 * 1000 * 1000)
00117 self.bands.SetRateLimit(self.HIGH_SPEED_BAND_NUM, LANB_to_LANA,
00118 100 * 1000 * 1000)
00119 self.bands.SetDropAmount(self.HIGH_SPEED_BAND_NUM, LANB_to_LANA,
00120 0)
00121 self.bands.SetDropAmount(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB,
00122 0)
00123 self.bands.SetDropAmount(5, LANB_to_LANA, 0)
00124 self.bands.SetDropAmount(5, LANA_to_LANB, 0)
00125 self.bands.SetDelayReorder(self.HIGH_SPEED_BAND_NUM, LANB_to_LANA, False)
00126 self.bands.SetDelayReorder(self.HIGH_SPEED_BAND_NUM, LANA_to_LANB, False)
00127 SetMM(self.mm2name, self.bands,
00128 self.filters, self.filters, self.all_filter_names)
00129 rospy.loginfo("updated speed settings")
00130
00131 if __name__ == "__main__":
00132 rospy.init_node("drc_2015_environment")
00133 drc = DRCEnvironment()
00134
00135