需要帮助来理解ns3 - >随机漫步2MobilityModel

时间:2014-12-02 18:41:44

标签: c++ networking simulator ns-3

我是NS3的新手;你可以解释RandomWalk2dMobilityModel的工作原理吗?

我正在尝试为用户提供目的地的环境建模,他将以随机生成的速度前往那里。这可以用这个模型设计吗?

谢谢。

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[])
{
  Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Mode", StringValue ("Time"));
  Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Time", StringValue ("2s"));
  Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
  Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Bounds", StringValue ("0|200|0|200"));

  CommandLine cmd;
  cmd.Parse (argc, argv);

  NodeContainer c;
  c.Create (100);

  MobilityHelper mobility;
  mobility.SetPositionAllocator ("ns3::RandomDiscPositionAllocator",
                                 "X", StringValue ("100.0"),
                                 "Y", StringValue ("100.0"),
                                 "Rho", StringValue ("ns3::UniformRandomVariable[Min=0|Max=30]"));
  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                             "Mode", StringValue ("Time"),
                             "Time", StringValue ("2s"),
                             "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
                             "Bounds", StringValue ("0|200|0|200"));
  mobility.InstallAll ();
  Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
                   MakeCallback (&CourseChange));

1 个答案:

答案 0 :(得分:1)

基本上答案是否定的, RandomWalk2dMobilityModel用于生成随机速度的随机方向,如果你想要随机速度的特定方向,那么你必须创建你的模式。

我创建了一个函数,用于在我传递node id,startPosition,endPosition和当前模拟时间的位置之间创建移动。此函数返回在两个位置之间移动所需的模拟时间

double GenerateMovementBetweenTwoPosition(Ptr<Node> node, Vector startPosition,Vector newPosition, double time)
{
    float x,y;
  Ptr<MobilityModel> mobilityModel = node->GetObject<MobilityModel> ();
  //Vector startPosition = mobilityModel->GetPosition();
  double distance = sqrt( pow(startPosition.x - newPosition.x, 2)
          + pow(startPosition.y - newPosition.y, 2)  );
  double tagSpeed = UniformVariable ().GetValue(0.7,1.3);
  double MovementTime = distance/tagSpeed; // time is in seconds as distance in meter and 1.5 m/s
  double distanceStep = distance/ceil(MovementTime);
  for ( int i=1; i <=  (int)ceil(MovementTime); i++)
  {
      if (time+i >= simTime)
          return time;
      x = startPosition.x + ( distanceStep * i / distance )*(newPosition.x - startPosition.x)+ NormalVariable (-2,2).GetValue(); //path width
      y = startPosition.y + ( distanceStep * i / distance )*(newPosition.y - startPosition.y)+ NormalVariable (-2,2).GetValue();
     // std::cout << "x: " << x << " y: "<< y<<std::endl;
      Simulator::Schedule (Seconds (time+i), &MobilityModel::SetPosition, mobilityModel,
     Vector( x, y,  0.0));

  }
    return time+ceil(MovementTime);

}

例如,节点10在模拟时间100从点A移动到点B,并且该移动花费50秒,因此返回模拟时间150。 但是,如果模拟时间在140秒结束,那么当模拟在轨迹中间结束时,此函数将返回140.