How To Set Node Position in NetAnim?

219 views
Skip to first unread message

Lesehan Mas Dosen

unread,
Jul 23, 2020, 8:35:13 AM7/23/20
to ns-3-users

I am new to ns-3. I am trying to understand from the very basic. I created a network simulation containing two switches, then I want to visualize it using NetAnim.

Followings are my code


#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"
#include "ns3/netanim-module.h"
#include "stdio.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("HMTMRSim");

int 
main (int argc, char *argv[])
{

  std::cout << "create switches" << std::endl;
  NodeContainer switches;
  switches.Create(2); // create two switches

  AnimationInterface anim("myanim.xml");

  std::cout << "set position for switches" << std::endl;

  anim.SetConstantPosition(switches.Get(0), 48.0, 70.0, 0);
  anim.SetConstantPosition(switches.Get(1), 48.0, 90.0, 0);

  anim.UpdateNodeDescription(0, "Main");
  anim.UpdateNodeDescription(1, "Rack0");

  anim.UpdateNodeSize(0, 10, 10);
  anim.UpdateNodeColor(0, 0, 0, 255);
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}


When I run it, it works without problem, but I get this warning

Waf: Leaving directory `/Users/arwankhoiruddin/Downloads/ns-allinone-3.30.1/ns-3.30.1/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (18.508s)
create switches
AnimationInterface WARNING:Node:0 Does not have a mobility model. Use SetConstantPosition if it is stationary
AnimationInterface WARNING:Node:1 Does not have a mobility model. Use SetConstantPosition if it is stationary
AnimationInterface WARNING:Node:0 Does not have a mobility model. Use SetConstantPosition if it is stationary 
AnimationInterface WARNING:Node: 

When I open in NetAnim (shown in the picture below), it seems to ignore the position that I defined in my code. As you see in the following properties, the first node (node 0) has position of (x,y) = (81.00, 83.00), while in my code, I set the position as


anim.SetConstantPosition(switches.Get(0), 48.0, 70.0, 0);
anim.SetConstantPosition(switches.Get(1), 48.0, 90.0, 0);



I am aware that I can change the position by changing the value in properties, but later using ns-3, I want to create quite a complex network, so it will be difficult to set them manually.

In internet, I found this suggestion, i.e. adding mobility to replace the SetConstantPosition. However, the result in NetAnim is still similar.

MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0.0, 0.0, 0.0));
positionAlloc->Add(Vector(5.0, 0.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); 
mobility.Install(switches); 

Please let me know how to set the position from my code and get rid of the warning.

Thank you for your kind helps
 

Liu XinXin

unread,
Jul 28, 2020, 5:45:55 AM7/28/20
to ns-3-users
Try this 

int 
main (int argc, char *argv[])
{

  std::cout << "create switches" << std::endl;
  NodeContainer switches;
  switches.Create(2); // create two switches

  MobilityHelper mobility;
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (switches);

  AnimationInterface anim("myanim.xml");

  std::cout << "set position for switches" << std::endl;

  Ptr<ConstantPositionMobilityModel> s1 = switches.Get (0)->GetObject<ConstantPositionMobilityModel> ();
  Ptr<ConstantPositionMobilityModel> s2 = switches.Get (1)->GetObject<ConstantPositionMobilityModel> ();
  s1->SetPosition (Vector ( 48.0, 70.0, 0  ));
  s2->SetPosition (Vector ( 48.0, 90.0, 0  ));

Lesehan Mas Dosen

unread,
Jul 28, 2020, 6:28:11 AM7/28/20
to ns-3-users
Hi Liu XinXIn,

You saved my day. Many thanks for your helps. Now I can set the position of my nodes. You are awesome!

Screenshot 2020-07-28 at 6.26.04 PM.png

Lesehan Mas Dosen

unread,
Jul 28, 2020, 6:35:47 AM7/28/20
to ns-3-users
Hi Liu XinXin,

As previously I posted the same question in StackOverflow, I do hope that you don't mind if I post your answer there as well. Don't worry, I mentioned your name there. Here's the link to my post in StackOverflow: https://stackoverflow.com/questions/62543669/how-to-set-node-position-in-netanim-i-always-get-use-setconstantposition-if-it/63132108#63132108

Liu XinXin

unread,
Jul 28, 2020, 6:54:55 AM7/28/20
to ns-3-...@googlegroups.com
It's ok! I am learning too.
BTW I just made my stackoverflow account. My profile name is xxliu95.

Lesehan Mas Dosen <arwan.a...@gmail.com> 于 2020年7月28日周二 12:36写道:
--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/5k2HNfKcM8c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/777164aa-579d-40db-a6e4-625b867cf76bo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages