A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
constant-mobility-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Tokushima University, Tokushima, Japan.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
7 */
8
9/*
10 This example shows a comparison between node mobility set with and without the
11 use of helpers. Furthermore it shows a simple way to update the node positions.
12 More advanced position allocations and mobility patterns can be achieve with
13 the use of other helpers.
14
15 In this example, node N1 moves to the right in 3 different instances
16 during the simulation while node N0 remains static. A trace is used to print a
17 message when movement is detected in node N1.
18
19 10 m {Sec 2} 10m {Sec 4} 10 m {Sec 6}
20 N0,N1 --------------------->N1--------------------> N1 ------------------> N1
21 (0,0,0) Move Right (10,0,0) Move Right (20,0,0) Move Right (30,0,0)
22*/
23
24#include <ns3/mobility-module.h>
25#include <ns3/node.h>
26#include <ns3/simulator.h>
27
28using namespace ns3;
29
30void
32{
33 std::cout << Simulator::Now().As(Time::S) << " Movement detected (Node "
34 << mobility->GetObject<Node>()->GetId() << ")!\n";
35}
36
37void
38MoveNode(Ptr<Node> node, const Vector& pos)
39{
40 Ptr<MobilityModel> mobilityModel = node->GetObject<MobilityModel>();
41 NS_ASSERT_MSG(mobilityModel, "Node doesn't have a mobility model");
42 mobilityModel->SetPosition(pos);
43
44 Vector newPos = mobilityModel->GetPosition();
45
46 std::cout << Simulator::Now().As(Time::S) << " Node " << node->GetId() << " | MoveRight, Pos ("
47 << newPos.x << "," << newPos.y << "," << newPos.z << ")\n";
48}
49
50int
51main(int argc, char* argv[])
52{
53 // Method 1: Install the mobility model using the helper
54 // The helper creates and installs the mobility to a single node
55 // or multiple nodes in a node container. Different helpers might
56 // have additional options.
57
58 NodeContainer nodeContainer(1);
59
61 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
62
64 positionAllocator->Add(Vector(0, 0, 0));
65
66 mobility.SetPositionAllocator(positionAllocator);
67 Ptr<Node> n0 = nodeContainer.Get(0);
68 mobility.Install(n0);
69
70 // Method 2: Install the mobility manually
71 // You need to manually create both, the node object and the mobility object.
72 // After that, you aggregate the mobility to a node.
73
76 n1->AggregateObject(mob1);
77 mob1->SetPosition(Vector(0, 0, 0));
78
79 // Set the mobility trace hook to node n1 to keep track of its movements
80 mob1->TraceConnectWithoutContext("CourseChange", MakeBoundCallback(&CourseChangeCallback));
81
82 // Schedule movements to node n1
83 Simulator::ScheduleWithContext(n1->GetId(), Seconds(2.0), &MoveNode, n1, Vector(10, 0, 0));
84
85 Simulator::ScheduleWithContext(n1->GetId(), Seconds(4.0), &MoveNode, n1, Vector(20, 0, 0));
86
87 Simulator::ScheduleWithContext(n1->GetId(), Seconds(6.0), &MoveNode, n1, Vector(30, 0, 0));
88
91
93 return 0;
94}
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
keep track of a set of node pointers.
A network Node.
Definition node.h:46
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
void MoveNode(Ptr< Node > node, const Vector &pos)
void CourseChangeCallback(Ptr< const MobilityModel > mobility)
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition third.py:92