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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
18 */
19
20/*
21 This example shows a comparison between node mobility set with and without the
22 use of helpers. Furthermore it shows a simple way to update the node positions.
23 More advanced position allocations and mobility patterns can be achieve with
24 the use of other helpers.
25
26 In this example, node N1 moves to the right in 3 different instances
27 during the simulation while node N0 remains static. A trace is used to print a
28 message when movement is detected in node N1.
29
30 10 m {Sec 2} 10m {Sec 4} 10 m {Sec 6}
31 N0,N1 --------------------->N1--------------------> N1 ------------------> N1
32 (0,0,0) Move Right (10,0,0) Move Right (20,0,0) Move Right (30,0,0)
33*/
34
35#include <ns3/mobility-module.h>
36#include <ns3/node.h>
37#include <ns3/simulator.h>
38
39using namespace ns3;
40
41void
43{
44 std::cout << Simulator::Now().As(Time::S) << " Movement detected (Node "
45 << mobility->GetObject<Node>()->GetId() << ")!\n";
46}
47
48void
49MoveNode(Ptr<Node> node, const Vector& pos)
50{
51 Ptr<MobilityModel> mobilityModel = node->GetObject<MobilityModel>();
52 NS_ASSERT_MSG(mobilityModel, "Node doesn't have a mobility model");
53 mobilityModel->SetPosition(pos);
54
55 Vector newPos = mobilityModel->GetPosition();
56
57 std::cout << Simulator::Now().As(Time::S) << " Node " << node->GetId() << " | MoveRight, Pos ("
58 << newPos.x << "," << newPos.y << "," << newPos.z << ")\n";
59}
60
61int
62main(int argc, char* argv[])
63{
64 // Method 1: Install the mobility model using the helper
65 // The helper creates and installs the mobility to a single node
66 // or multiple nodes in a node container. Different helpers might
67 // have additional options.
68
69 NodeContainer nodeContainer(1);
70
72 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
73
74 Ptr<ListPositionAllocator> positionAllocator = CreateObject<ListPositionAllocator>();
75 positionAllocator->Add(Vector(0, 0, 0));
76
77 mobility.SetPositionAllocator(positionAllocator);
78 Ptr<Node> n0 = nodeContainer.Get(0);
79 mobility.Install(n0);
80
81 // Method 2: Install the mobility manually
82 // You need to manually create both, the node object and the mobility object.
83 // After that, you aggregate the mobility to a node.
84
85 Ptr<Node> n1 = CreateObject<Node>();
86 Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel>();
87 n1->AggregateObject(mob1);
88 mob1->SetPosition(Vector(0, 0, 0));
89
90 // Set the mobility trace hook to node n1 to keep track of its movements
91 mob1->TraceConnectWithoutContext("CourseChange", MakeBoundCallback(&CourseChangeCallback));
92
93 // Schedule movements to node n1
94 Simulator::ScheduleWithContext(n1->GetId(), Seconds(2.0), &MoveNode, n1, Vector(10, 0, 0));
95
96 Simulator::ScheduleWithContext(n1->GetId(), Seconds(4.0), &MoveNode, n1, Vector(20, 0, 0));
97
98 Simulator::ScheduleWithContext(n1->GetId(), Seconds(6.0), &MoveNode, n1, Vector(30, 0, 0));
99
102
104 return 0;
105}
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:57
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
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:86
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:761
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns mobility
Definition: third.py:103