From f8ba7d643e31ff76130da3f2d399d86b1d4c9d08 Mon Sep 17 00:00:00 2001 From: Alberto Gallegos Ramonet Date: Mon, 20 May 2024 18:02:11 +0900 Subject: [PATCH] mobility: Adds a simple mobility example --- RELEASE_NOTES.md | 1 + src/mobility/examples/CMakeLists.txt | 1 + .../examples/constant-mobility-example.cc | 105 ++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/mobility/examples/constant-mobility-example.cc diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 4fe7a3ba3..c1549b1f3 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -34,6 +34,7 @@ been tested on Linux. The latest known version to work with ns-3 is cppyy==3.1.2 - (wifi) - The `ApWifiMac` provides new attributes (`CwMinsForSta`, `CwMaxsForSta`, `AifsnsForSta` and `TxopLimitsForSta`) to define the EDCA access parameters to include in the EDCA Parameter Set advertised to associated stations - (wifi) - The `WifiMacHelper` provides a `SetChannelAccessManager` and a `SetFrameExchangeManager` methods to configure attributes of `ChannelAccessManager` and `FrameExchangeManager` objects, respectively - (wifi) - Simulation duration and data rate parameters of existing wifi examples changed to use Time and DataRate types +- (mobility) !1986 - Adds simple constant-mobility-example ### Bugs fixed diff --git a/src/mobility/examples/CMakeLists.txt b/src/mobility/examples/CMakeLists.txt index 578c4e10f..3a7eccbbd 100644 --- a/src/mobility/examples/CMakeLists.txt +++ b/src/mobility/examples/CMakeLists.txt @@ -1,5 +1,6 @@ set(base_examples bonnmotion-ns2-example + constant-mobility-example main-random-topology main-random-walk ns2-mobility-trace diff --git a/src/mobility/examples/constant-mobility-example.cc b/src/mobility/examples/constant-mobility-example.cc new file mode 100644 index 000000000..9b960310f --- /dev/null +++ b/src/mobility/examples/constant-mobility-example.cc @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2024 Tokushima University, Tokushima, Japan. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Alberto Gallegos Ramonet + */ + +/* + This example shows a comparison between node mobility set with and without the + use of helpers. Furthermore it shows a simple way to update the node positions. + More advanced position allocations and mobility patterns can be achieve with + the use of other helpers. + + In this example, node N1 moves to the right in 3 different instances + during the simulation while node N0 remains static. A trace is used to print a + message when movement is detected in node N1. + + 10 m {Sec 2} 10m {Sec 4} 10 m {Sec 6} + N0,N1 --------------------->N1--------------------> N1 ------------------> N1 + (0,0,0) Move Right (10,0,0) Move Right (20,0,0) Move Right (30,0,0) +*/ + +#include +#include +#include + +using namespace ns3; + +void +CourseChangeCallback(Ptr mobility) +{ + std::cout << Simulator::Now().As(Time::S) << " Movement detected (Node " + << mobility->GetObject()->GetId() << ")!\n"; +} + +void +MoveNode(Ptr node, const Vector& pos) +{ + Ptr mobilityModel = node->GetObject(); + NS_ASSERT_MSG(mobilityModel, "Node doesn't have a mobility model"); + mobilityModel->SetPosition(pos); + + Vector newPos = mobilityModel->GetPosition(); + + std::cout << Simulator::Now().As(Time::S) << " Node " << node->GetId() << " | MoveRight, Pos (" + << newPos.x << "," << newPos.y << "," << newPos.z << ")\n"; +} + +int +main(int argc, char* argv[]) +{ + // Method 1: Install the mobility model using the helper + // The helper creates and installs the mobility to a single node + // or multiple nodes in a node container. Different helpers might + // have additional options. + + NodeContainer nodeContainer(1); + + MobilityHelper mobility; + mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); + + Ptr positionAllocator = CreateObject(); + positionAllocator->Add(Vector(0, 0, 0)); + + mobility.SetPositionAllocator(positionAllocator); + Ptr n0 = nodeContainer.Get(0); + mobility.Install(n0); + + // Method 2: Install the mobility manually + // You need to manually create both, the node object and the mobility object. + // After that, you aggregate the mobility to a node. + + Ptr n1 = CreateObject(); + Ptr mob1 = CreateObject(); + n1->AggregateObject(mob1); + mob1->SetPosition(Vector(0, 0, 0)); + + // Set the mobility trace hook to node n1 to keep track of its movements + mob1->TraceConnectWithoutContext("CourseChange", MakeBoundCallback(&CourseChangeCallback)); + + // Schedule movements to node n1 + Simulator::ScheduleWithContext(n1->GetId(), Seconds(2.0), &MoveNode, n1, Vector(10, 0, 0)); + + Simulator::ScheduleWithContext(n1->GetId(), Seconds(4.0), &MoveNode, n1, Vector(20, 0, 0)); + + Simulator::ScheduleWithContext(n1->GetId(), Seconds(6.0), &MoveNode, n1, Vector(30, 0, 0)); + + Simulator::Stop(Seconds(10)); + Simulator::Run(); + + Simulator::Destroy(); + return 0; +}