Nodes move in NS3

2,404 views
Skip to first unread message

Steve

unread,
Nov 2, 2010, 8:32:13 AM11/2/10
to ns-3-...@googlegroups.com
Hello guys,
I was working on the handoff simulation. and for the last few days, i found my simulation confusing.
the result tell me my node wasn't move at all. can some body help me?
the program:
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"

// Handoff Network Topology
//
//    ʘ (Mobile_Station)⇛⇛⇛⇛⇛⇛⇛⇛⇛⇛⇛⇛⇛⇛  (向这个方向移动) (WIFI 10.1.3.0)(0,0,0 ----> )
//                   
//
//
// AP之间要保持距离,处于不能相互通信的距离内部。
//
//
//
//    * (AP)           *(AP)              
//     \              /   
//      \            /  
// \          /
//        \        /
//         \      /
// \    /
//           \  /
//   \/
//  
// Static_Node


using namespace ns3;

NS_LOG_COMPONENT_DEFINE("Hand-Off");

void CourseChange(std::string context, Ptr<const MobilityModel> model)
{
Vector position = model -> GetPosition();
NS_LOG_UNCOND(context << "x = " << position.x << ", y = " << position.y );
}

int main(int argc, char *argv[])
{
bool verbose = true;
uint32_t numAPs = 2;


CommandLine cmd;
cmd.AddValue("numAPs", "The number of APs", numAPs);
cmd.AddValue("verbose", "Tell Application to log if true", verbose);
cmd.Parse(argc, argv);

if (verbose) {
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}

NodeContainer wifiStationNodes; //生成一个wifiStation节点
wifiStationNodes.Create(1);


NodeContainer wifiAPNodes; //生成numAPs数量的AP节点
wifiAPNodes.Create(numAPs);


NodeContainer p2pNodes; //生成三个p2p节点。
p2pNodes.Create(3);


PointToPointHelper p2pHelper; //生成p2p的链接helper
p2pHelper.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
p2pHelper.SetChannelAttribute("Delay", StringValue("2ms"));


NetDeviceContainer p2pDevices_AP1, p2pDevices_AP2; //出错了,这里要求必须是install的数量为两个
p2pDevices_AP1 = p2pHelper.Install(p2pNodes.Get(0), p2pNodes.Get(1));
p2pDevices_AP2 = p2pHelper.Install(p2pNodes.Get(0), p2pNodes.Get(2));


wifiAPNodes.Add(p2pNodes.Get(2)); //p2pNodes2号作为一个AP
wifiAPNodes.Add(p2pNodes.Get(1)); //p2pNodes1号作为一个AP


NodeContainer Static_Node; //p2pNodes2号作为固定的节点
Static_Node.Add(p2pNodes.Get(0));


YansWifiChannelHelper channel = YansWifiChannelHelper::Default(); //设置好物理层
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
phy.SetChannel(channel.Create());


WifiHelper wifi = WifiHelper::Default();
wifi.SetRemoteStationManager("ns3::AarfWifiManager");


NqosWifiMacHelper mac = NqosWifiMacHelper::Default (); //设置好Mac


Ssid ssid = Ssid("Hand-Off-ns3");
mac.SetType("ns3::NqstaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));

NetDeviceContainer stationDevices; //
stationDevices = wifi.Install(phy, mac, wifiStationNodes);


mac.SetType("ns3::NqapWifiMac", "Ssid", SsidValue(ssid));


NetDeviceContainer apDevices;
apDevices = wifi.Install(phy, mac, wifiAPNodes);


//下面是设置APstation的位置
Vector AP1_Position(300.0, 200.0, 0.0); //第一个AP的位置
Vector AP2_Position(1000.0, 200.0, 0.0); //第二个AP的位置
Vector Static_Node_Position(700.0, 200.0, 0.0); //固定节点的位置


ListPositionAllocator myListPositionAllocator;
myListPositionAllocator.Add(AP1_Position);
myListPositionAllocator.Add(AP2_Position);
myListPositionAllocator.Add(Static_Node_Position);


MobilityHelper mobility;
mobility.SetPositionAllocator(&myListPositionAllocator);

/*
先设置一下固定的位置
*/


mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //AP的位置固定,先设置为固定位置模型
mobility.Install(wifiAPNodes);


MobilityHelper stationMobilityHelper;
Ptr<ListPositionAllocator> stationPositionAlloc = CreateObject<ListPositionAllocator>();
stationPositionAlloc -> Add(Vector(0.0, 0.0, 0.0)); //it is the starting position 
stationPositionAlloc -> Add(Vector(2000.0, 0.0, 0.0)); // it is the ending position that the station moves, i thought
stationMobilityHelper.SetPositionAllocator(stationPositionAlloc);
stationMobilityHelper.SetMobilityModel("ns3::ConstantVelocityMobilityModel");
stationMobilityHelper.Install(wifiStationNodes);
(wifiStationNodes.Get(0) -> GetObject<ConstantVelocityMobilityModel>()) -> SetVelocity(Vector(10.0, 0.0, 0.0));


mobility.Install(wifiStationNodes);
//上面是设置APstation的位置


InternetStackHelper stack;
stack.Install(wifiStationNodes);
stack.Install(wifiAPNodes);
stack.Install(Static_Node);

Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0"); //Point-2-Point部分的IP设置为 10.1.1.0
Ipv4InterfaceContainer p2pInterfaces_AP1, p2pInterfaces_AP2;
p2pInterfaces_AP1 = address.Assign(p2pDevices_AP1);
p2pInterfaces_AP2 = address.Assign(p2pDevices_AP2);


UdpEchoServerHelper echoServer(9);
ApplicationContainer servApps = echoServer.Install(Static_Node);
servApps.Start(Seconds(1.0));
servApps.Stop(Seconds(50.0));


Ipv4Address remoteAddress = Ipv4Address("10.1.4.1");
UdpEchoClientHelper echoClient(remoteAddress, 9);


ApplicationContainer clientApps = echoClient.Install(wifiStationNodes);
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(200.0));


Ipv4GlobalRoutingHelper::PopulateRoutingTables();
Simulator::Stop(Seconds(200.0));


AsciiTraceHelper ascii;
phy.EnableAsciiAll(ascii.CreateFileStream("phy.tr"));


std::ostringstream oss;
oss << "/NodeList/" << wifiStationNodes.Get(0)-> GetId() << "/$ns3::MobilityModel/CourseChange";

Config::Connect(oss.str(), MakeCallback(&CourseChange));


Simulator::Run();
Simulator::Destroy();
return 0;
}

and the result 
[ Steve@bogon#/Applications/ns-allinone-3.9/ns-3.9 ] $:./waf --run scratch/Handoff-2nd-normal
Waf: Entering directory `/Applications/ns-allinone-3.9/ns-3.9/build'
[ 509/1059] cxx: scratch/Handoff-2nd-normal.cc -> build/debug/scratch/Handoff-2nd-normal_2.o
[1059/1059] cxx_link: build/debug/scratch/Handoff-2nd-normal_2.o -> build/debug/scratch/Handoff-2nd-normal
Waf: Leaving directory `/Applications/ns-allinone-3.9/ns-3.9/build'
'build' finished successfully (2.590s)
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1
Sent 100 bytes to 10.1.4.1

the static node i put among the 2 AP seems don't get the message send from the mobile station. and the show position method seems don't called
what is wrong with my simulation?
---== Steve @ MBP==--
                                  




Mathieu Lacage

unread,
Nov 15, 2010, 2:16:02 AM11/15/10
to ns-3-...@googlegroups.com
On Tue, Nov 2, 2010 at 13:32, Steve <bapu...@gmail.com> wrote:
> Hello guys,
> I was working on the handoff simulation. and for the last few days, i found
> my simulation confusing.
> the result tell me my node wasn't move at all. can some body help me?

CourseChange is triggered only when the velocity vector of your node
changes. Its velocity does not change (it's a constant): its position
changes but the velocity does not which is why your function is never
called.

Mathieu
--
Mathieu Lacage <mathieu...@gmail.com>

Sergio Mtnez

unread,
Nov 15, 2010, 2:43:56 AM11/15/10
to ns-3-...@googlegroups.com
According with doxigen:

"CourseChange: The value of the position and/or velocity vector changed"

So, I think that the conjunction "and" should be deleted ;).

Thanks in advance.

2010/11/15 Mathieu Lacage <mathieu...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.


Mathieu Lacage

unread,
Nov 15, 2010, 2:50:30 AM11/15/10
to ns-3-...@googlegroups.com
On Mon, Nov 15, 2010 at 08:43, Sergio Mtnez <sert...@gmail.com> wrote:
> According with doxigen:
> "CourseChange: The value of the position and/or velocity vector changed"
> So, I think that the conjunction "and" should be deleted ;).

Well, it depends on the mobility model you use. The
ConstantPositionMobilityModel will emit CourseChange whenever the
position changes. The ConstantVelocity model will emit only on
velocity changes and the ConstantAcceleration model will emit only on
acceleration changes.

Patches to improve the doc are most welcome :)

伟伟

unread,
Nov 17, 2010, 2:47:07 AM11/17/10
to ns-3-...@googlegroups.com
Thanks for the reply ,the internet connection has been poor recently, 
Well, the mobility mode i set for the station, is constant velocity and i want to make it move towards the direction, in order to trace the movement of the station, i want it to print the location repeatedly,  so according to the mail received, only change the position won't lead to the call of the CourseChange. right?   

here is my question, what method I should use to print out the location while the station's moving?
and in my simulation, i found it difficulty to set the signal range. the only way came to my mind is to set the distance through the propagation loss mode, which is exactly the  transmit power, is there other way?

second problem in my simulation is that, if I set two node in two position,let's say(0, 0) and (0, 1) the distance between the two is 1, is it 1 meter of what?

2010/11/15 Mathieu Lacage <mathieu...@gmail.com>

Mathieu Lacage

unread,
Nov 17, 2010, 10:20:21 AM11/17/10
to ns-3-...@googlegroups.com
On Wed, Nov 17, 2010 at 08:47, 伟伟 <bapu...@gmail.com> wrote:
> Thanks for the reply ,the internet connection has been poor recently,
> Well, the mobility mode i set for the station, is constant velocity and i
> want to make it move towards the direction, in order to trace the movement
> of the station, i want it to print the location repeatedly,  so according to

all you have to do is setup a periodic timer that prints the current position.

Babu Mandal

unread,
Feb 13, 2018, 11:20:51 AM2/13/18
to ns-3-users
How can you set the periodic timer...can u give me any hints pls..???????

pdbarnes

unread,
Feb 13, 2018, 7:25:58 PM2/13/18
to ns-3-users
Please follow the posting guidelines, especially regarding new posts to ancient threads.

Here is the api you need:
https://www.nsnam.org/docs//doxygen/classns3_1_1_timer.html#details
Peter

Reply all
Reply to author
Forward
0 new messages