/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2012 University of Washington
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Test that mobility tracing works
#include "ns3/core-module.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
using namespace ns3;
int main(int argc, char *argv[]) {
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
NodeContainer sta;
sta.Create(9);
MobilityHelper mobility;
/*
* SetPositionAllocator,设置位置分配器
* GridPositionAllocator,网格位置分配器
* MinX/MinY/DeltaX/DeltaY/GridWidth/LayoutType/RowFirst 是SetPositionAllocator的属性
* MinX/MinY 节点活动区域的起始坐标
* DeltaX/DeltaY 节点间的横纵坐标间隔
* GridWidth 网格宽度,即节点列数
* LayoutType 布局类型,行还是列优先
*/
mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX",
DoubleValue(1.0), "MinY", DoubleValue(1.0), "DeltaX",
DoubleValue(5.0), "DeltaY", DoubleValue(5.0), "GridWidth",
UintegerValue(3), "LayoutType", StringValue("RowFirst"));
// mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel", "Mode",
// StringValue("Time"), "Time", StringValue("2s"), "Speed",
// StringValue("ns3::ConstantRandomVariable[Constant=1.0]"), "Bounds",
// RectangleValue(Rectangle(0.0, 20.0, 0.0, 20.0)));
/**
* SetMobilityModel 设置移动模型
* Time 2s,代表各节点每隔两秒改变一次速度和方向
* Speed ,速度,值是随机产生的,范围是1到10
* Bounds,节点游走边界,矩形区域,从左到右依次为X坐标最小值,X坐标最大值,Y坐标最小值和Y坐标最大值
*/
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel", "Mode",
StringValue("Time"), "Time", StringValue("2s"), "Speed",
StringValue("ns3::UniformRandomVariable[Min=1.0|Max=10.0]"),
"Bounds", RectangleValue(Rectangle(0.0, 200.0, 0.0, 200.0)));
mobility.Install(sta);
// Set mobility random number streams to fixed values
mobility.AssignStreams(sta, 0);
AsciiTraceHelper ascii;
MobilityHelper::EnableAsciiAll(
ascii.CreateFileStream("mobility-trace-example.mob"));
Simulator::Stop(Seconds(5.0));
Simulator::Run();
Simulator::Destroy();
}
该源码源自NS-3 3.35 src/mobility/examples/mobility-trace-example.cc
该例子的主要意图是输出节点移动模型的跟踪文件mobility-trace-example.mob。我将移动模型的节点移动速率、范围等简单修改了下,主要目的是想看下随机速度运行效果。
1.初始运行效果
2.运行一段时间后的效果
3.trace 文件部分内容