如何从Random Waypoint模型中理解这段代码

时间:2017-04-22 15:53:30

标签: c++ ns-3

我是C ++的新手,但这里有一段来自Random waypoint的代码。这是它的引用:http://netdb.cis.upenn.edu/rapidnet/doxygen/html/classns3_1_1_random_waypoint_mobility_model.html#a2b8fc7b2cf1e2ffec7e6c6a9d5f7404

  

说:此方法返回与之关联的TypeId   NS3 :: RandomWaypointMobilityModel。

但我不知道如何使用它(更新:如何调用此模型的属性)?

TypeId
RandomWaypointMobilityModel::GetTypeId (void)
{
   static TypeId tid = TypeId ("ns3::RandomWaypointMobilityModel")
   .SetParent<MobilityModel> () 
   .SetGroupName ("Mobility") 
   .AddConstructor<RandomWaypointMobilityModel> ()
   .AddAttribute ("Speed",  //name
               "A random variable used to pick the speed of a random waypoint model.",  //help context
               StringValue ("ns3::UniformRandomVariable[Min=0.3|Max=0.7]"),
               MakePointerAccessor (&RandomWaypointMobilityModel::m_speed), //setup(initailize m_speed)
               MakePointerChecker<RandomVariableStream> ()) 
   .AddAttribute ("Pause",  
               "A random variable used to pick the pause of a random waypoint model.", //help context
               StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
               MakePointerAccessor (&RandomWaypointMobilityModel::m_pause),
               MakePointerChecker<RandomVariableStream> ())
  .AddAttribute ("PositionAllocator",
               "The position model used to pick a destination point.",
               PointerValue (),
               MakePointerAccessor (&RandomWaypointMobilityModel::m_position),
               MakePointerChecker<PositionAllocator> ());

return tid;
}

1 个答案:

答案 0 :(得分:0)

既然你说你想要理解代码我想指向ns-3教程,该教程解释了他们的属性系统是如何工作的(https://www.nsnam.org/docs/tutorial/html/index.html),但我会试着给你一个关于它的要点并指出一个例子:

如何使用ns3属性系统

  1. 正如您所做的那样,设置属性值的第一步是查找类文档,该文档告诉您如何使用标记为“Set with class”的项目符号设置属性(即RandomVariableValue在您的情况下)
  2. 然后,您可以通过ns3帮助程序创建您感兴趣的类的对象(此处:RandomWaypointMobilityModel),您可以借助在第一步中查找的信息设置该属性。
  3. 一个简短的例子

    #include "ns3/core-module.h"
    #include "ns3/mobility-module.h"
    
    using namespace ns3;
    
    static void 
    CourseChange (std::string foo, Ptr<const MobilityModel> mobility)
    {
      Vector pos = mobility->GetPosition ();
      Vector vel = mobility->GetVelocity ();
      std::cout << Simulator::Now () << ", model=" << mobility << ", POS: x=" << pos.x << ", y=" << pos.y
                << ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
                << ", z=" << vel.z << std::endl;
    }
    
    int main (int argc, char *argv[])
    {
    
      CommandLine cmd;
      cmd.Parse (argc, argv);
    
      NodeContainer c;
      c.Create (1);
    
      // Position allocator for the start of the simulation
      MobilityHelper mobility;
      mobility.SetPositionAllocator ("ns3::RandomDiscPositionAllocator",
                                     "X", StringValue ("100.0"),
                                     "Y", StringValue ("100.0"),
                                     "Rho", StringValue ("ns3::UniformRandomVariable[Min=0|Max=30]"));
    
      // The position allocator that will be used by the RandomWaypointMobilityModel
      GridPositionAllocator posAllocator;
      posAllocator.SetMinX(0.0);
      posAllocator.SetMinY(0.0);
      posAllocator.SetDeltaX(5.0);
      posAllocator.SetDeltaY(5.0);
      posAllocator.SetLayoutType(ns3::GridPositionAllocator::ROW_FIRST);
    
      mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
                                 "PositionAllocator", PointerValue(&posAllocator));
    
      mobility.InstallAll ();
      Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
                       MakeCallback (&CourseChange));
    
      Simulator::Stop (Seconds (500.0));
    
      Simulator::Run ();
    
      Simulator::Destroy ();
      return 0;
    }
    

    在这里,您可以看到一个长示例,其中详细描述了这些概念如何相互融合: https://www.nsnam.org/docs/tutorial/html/building-topologies.html#building-a-wireless-network-topology