我是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));

最佳答案

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

我创建了一个函数,用于在传递节点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,因为模拟在轨迹的中间结束。

关于c++ - 需要帮助来了解ns3-> Random Walk 2MobilityModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27256622/

10-09 21:20