From e52fffe564a08589bab337579c3f4b35227530db Mon Sep 17 00:00:00 2001 From: awlane Date: Fri, 22 Nov 2024 15:58:18 -0600 Subject: [PATCH] Add option to bind mobility model ticks to real time clock. Adds parameters 'use_rtc_model_mob' and 'rtc_model_mob_tick' to mobility model code. When enabled, these will cause mobility ticks to only occur every 'rtc_model_mob_tick' seconds as long as the expected number of ticks for this time interval has occurred. --- mn_wifi/mobility.py | 35 +++++++++++++++++++++++++++++++++++ mn_wifi/net.py | 13 +++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/mn_wifi/mobility.py b/mn_wifi/mobility.py index 50bd64ec..3f7660fa 100644 --- a/mn_wifi/mobility.py +++ b/mn_wifi/mobility.py @@ -381,6 +381,41 @@ def start_mob_mod(self, mob, nodes, draw): while self.pause_simulation: pass +class RTCModel(model): + def __init__(self, **kwargs): + self.tick_time = kwargs.get('rtc_model_mob_tick', 1) + super().__init__(**kwargs) + + def start_mob_mod(self, mob, nodes, draw): + """ + :param mob: mobility params + :param nodes: list of nodes + """ + next_tick_time = time() + self.tick_time + for xy in mob: + for idx, node in enumerate(nodes): + pos = round(xy[idx][0], 2), round(xy[idx][1], 2), 0.0 + self.set_pos(node, pos) + if draw: + node.update_2d() + if draw: + PlotGraph.pause() + if self.pause_simulation: + while self.pause_simulation: + pass + # When resuming simulation, reset the tick timing + next_tick_time = time() + self.tick_time + continue + # We try to use "best effort" scheduling- we want to have + # done as many ticks as there are elapsed seconds since we last + # performed a mobility tick and try to iterate the loop as consistently + # as possible + else: + while time() < next_tick_time: + # If time() has been exceeded since the while loop check, don't sleep + sleep(max(next_tick_time - time(), 0)) + next_tick_time = next_tick_time + self.tick_time + class Tracked(Mobility): "Used when the position of each node is previously defined" diff --git a/mn_wifi/net.py b/mn_wifi/net.py index 2b76cacc..b6710daa 100644 --- a/mn_wifi/net.py +++ b/mn_wifi/net.py @@ -29,7 +29,7 @@ master, managed, physicalMesh, PhysicalWifiDirectLink, _4addrClient, \ _4addrAP, phyAP from mn_wifi.mobility import Tracked as TrackedMob, model as MobModel, \ - Mobility as mob, ConfigMobility, ConfigMobLinks + Mobility as mob, ConfigMobility, ConfigMobLinks, RTCModel from mn_wifi.module import Mac80211Hwsim from mn_wifi.node import AP, Station, Car, OVSKernelAP, physicalAP from mn_wifi.plot import Plot2D, Plot3D, PlotGraph @@ -145,6 +145,8 @@ def __init__(self, accessPoint=OVSKernelAP, station=Station, car=Car, self.wwan_module = wwan_module self.ifbIntf = 0 self.mob_object = None + self.use_rtc_model_mob = False + self.rtc_model_mob_tick = 1.0 self.mob_start_time = 0 self.mob_stop_time = 0 self.mob_rep = 1 @@ -1231,7 +1233,10 @@ def start_mobility(self, **kwargs): if self.roads: self.mob_object = vanet(**kwargs) else: - self.mob_object = MobModel(**kwargs) + if kwargs.get('use_rtc_model_mob'): + self.mob_object = RTCModel(**kwargs) + else: + self.mob_object = MobModel(**kwargs) def setMobilityModel(self, **kwargs): for key in kwargs: @@ -1267,12 +1272,12 @@ def get_mobility_params(self): 'max_x', 'max_y', 'max_z', 'min_v', 'max_v', 'min_wt', 'max_wt', 'velocity_mean', 'alpha', 'variance', 'aggregation', - 'g_velocity'] + 'g_velocity', 'rtc_model_mob_tick'] args = ['stations', 'cars', 'aps', 'draw', 'seed', 'roads', 'mob_start_time', 'mob_stop_time', 'links', 'mob_model', 'mob_rep', 'reverse', 'ac_method', 'pointlist', 'n_groups', 'aggregation_epoch', 'epoch', - 'velocity'] + 'velocity', 'use_rtc_model_mob'] args += float_args for arg in args: if arg in float_args: