Gabrielcarvfer / ns3_for_education

Repository of educational examples using ns-3
5 stars 3 forks source link

NS-3 Mobility Question #1

Open liyang19960201 opened 6 months ago

liyang19960201 commented 6 months ago

Hello, dear Gabrielcarvfer

I am Yang Li, a master's degree student who is currently using NS-3 to implementation a project, I really appreciate your educational material here since I am using python bindings to build all simulation, and your example has been a significant help for me to set up some mobility attributes. My own project also runs in a wifi environment.

The thing is that I would like to set three node as racing cars and one node as an access point, it is successfully running with help of your examples that I can actually import a CSV file with a list of coordinates as X and Y to let racing car (nodes) moving as the same pattern as CSV file does in the simulation.

However, when I tried to find how to apply 'the simulation time' properly, (I want to set node at position x, y at a given time t) I get stuck here, because it doesnt seem like NS-3 has the mechanism to allow the simulation to use same method as ns-2, the best example would be bonnomotion-ns2-example.cc, I will attach them here, if you could instruct me how to make it possible to run on a python bindings environment, that would be really grateful:)

Thank you and have a lovely day:D

mobility-0

Gabrielcarvfer commented 6 months ago

Hi Yang, if your csv is in the same format of the ns2 trace file, you can do the same.

ns2 = ns.Ns2MobilityHelper(traceFile)
ns3.Install()

If you are not, then I'd say to use the csv library of Python, read as a DictReader

from csv import DictReader
with open(file,"r") as f:
    movements = DictReader(f.read())

Then generate your mobility pattern with the WaypointMobilityModel

    mobilityHelper = ns.MobilityHelper()
    mobilityHelper.SetMobilityModel ("ns3::WaypointMobilityModel")
    mobilityHelper.SetPositionAllocator (randomBoxPositions)
    mobilityHelper.Install (nodes)

    # Assuming we read movements with the following structure
    #
    # Timestamp, Node ID, X, Y, Z
    for entry in movements:
        node = nodes.Get(entry["Node ID"]).__deref__()
        mobility = node.GetObject[ns.WaypointMobilityModel]().__deref__()
        time = ns.Seconds(entry["Timestamp"])
        nextPos = ns.Vector(entry["X"], entry["Y"], entry["Z"])
        wpt = ns.Waypoint (time, nextPos);
        mobility.AddWaypoint(wpt)
        currPos = nextPos