A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
28
using namespace
ns3
;
29
30
void
31
CourseChangeCallback
(
Ptr<const MobilityModel>
mobility)
32
{
33
std::cout <<
Simulator::Now
().
As
(
Time::S
) <<
" Movement detected (Node "
34
<< mobility->GetObject<
Node
>()->GetId() <<
")!\n"
;
35
}
36
37
void
38
MoveNode
(
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
50
int
51
main(
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
60
MobilityHelper
mobility
;
61
mobility
.SetMobilityModel(
"ns3::ConstantPositionMobilityModel"
);
62
63
Ptr<ListPositionAllocator>
positionAllocator
=
CreateObject<ListPositionAllocator>
();
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
74
Ptr<Node>
n1
=
CreateObject<Node>
();
75
Ptr<ConstantPositionMobilityModel>
mob1
=
CreateObject<ConstantPositionMobilityModel>
();
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), &
MoveNode
,
n1
, Vector(10, 0, 0));
84
85
Simulator::ScheduleWithContext
(
n1
->GetId(),
Seconds
(4), &
MoveNode
,
n1
, Vector(20, 0, 0));
86
87
Simulator::ScheduleWithContext
(
n1
->GetId(),
Seconds
(6), &
MoveNode
,
n1
, Vector(30, 0, 0));
88
89
Simulator::Stop
(
Seconds
(10));
90
Simulator::Run
();
91
92
Simulator::Destroy
();
93
return
0;
94
}
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition
mobility-helper.h:33
ns3::MobilityModel
Keep track of the current position and velocity of an object.
Definition
mobility-model.h:29
ns3::NodeContainer
keep track of a set of node pointers.
Definition
node-container.h:29
ns3::Node
A network Node.
Definition
node.h:46
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::ScheduleWithContext
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition
simulator.h:577
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition
simulator.cc:197
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::Simulator::Stop
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition
simulator.cc:175
ns3::Time::As
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
ns3::Time::S
@ S
second
Definition
nstime.h:105
MoveNode
void MoveNode(Ptr< Node > node, const Vector &pos)
Definition
constant-mobility-example.cc:38
CourseChangeCallback
void CourseChangeCallback(Ptr< const MobilityModel > mobility)
Definition
constant-mobility-example.cc:31
NS_ASSERT_MSG
#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
ns3::MakeBoundCallback
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition
callback.h:745
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
third.mobility
mobility
Definition
third.py:92
src
mobility
examples
constant-mobility-example.cc
Generated on Mon Dec 15 2025 15:22:00 for ns-3 by
1.9.8