NS-3 学习之main-random-walk.cc 分析

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2006,2007 INRIA
 *
 * 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
 */

#include "ns3/core-module.h"
#include "ns3/mobility-module.h"

using namespace ns3;

/**
 * Function called when there is a course change
 * \param context event context
 * \param mobility a pointer to the mobility model
 */
static void CourseChange(std::string context,
		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[]) {
	/*
	 * SetDefault 方法覆盖相关属性的默认初始值。个人理解就是为属性赋值。
	 * RandomWalk2dMobilityModel:2D 随机游走移动模型;
	 * Mode设置为Time。
	 */
	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(__FILE__);
	cmd.Parse(argc, argv);

	NodeContainer c;
	c.Create(100);

	MobilityHelper mobility;
	//X/Y/Rho 是类ns3::RandomDiscPositionAllocator 的属性,分别代表横坐标/纵坐标和半径。
	//属性Min 和Max是类ns3::UniformRandomVariable 的属性,分别代表随机数值域的上下限。
	mobility.SetPositionAllocator("ns3::RandomDiscPositionAllocator", "X",
			StringValue("100.0"), "Y", StringValue("100.0"), "Rho",
			StringValue("ns3::UniformRandomVariable[Min=90|Max=100]"));
	/**
	 * ns3::RandomWalk2dMobilityModel:
	 * 每个实例以使用用户提供的随机变量随机选择的速度和方向移动,直到已步行固定距离或固定时间。
	 * 如果我们触及模型的边界之一(由矩形指定),我们会以反射角度和速度在边界上反弹。
	 * 该模型通常被识别为布朗运动模型。
	 * Mode/Time/Speed 是类ns3::RandomWalk2dMobilityModel 的属性。
	 * Mode 可以赋两种值:Time或Distance,表示当前节点运行一段时间或者一段距离后改变运动方向和速度。
	 * Time 表示时间,比如设置为2秒,则每2秒后变换一下运动方向和速度。
	 * Speed 表示速度,此处设置为恒定速度Constant=1.0,表示类ConstantRandomVariable 产生恒定数1
	 * Bounds 巡航区域范围,四个值从左到右依次是X坐标最小值,X坐标最大值,Y坐标最小值,Y坐标最大值
	 */
	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();

	//个人了解*是通配符,代表所有节点号。只要某一节点的速度或者位置发生变化,则会调用CourseChange方法打印该节点当前位置等信息
	Config::Connect("/NodeList/*/$ns3::MobilityModel/CourseChange",
			MakeCallback(&CourseChange));

	Simulator::Stop(Seconds(100.0));

	Simulator::Run();

	Simulator::Destroy();
	return 0;
}

个人理解代码主要是在以某个点为圆心,随机半径范围内生成若干个节点,各节点可随机移动,并且各个节点每隔多长时间之后随机改变自己的方向。速度恒定为1。具体见代码注释。

运行效果:

1.启动之后的效果

2.运行一段时间之后的效果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值