Omnet++ OMNET++;:在INET 4.0中,如何在离散的NxN空间中移动节点?
正如问题所暗示的,我想在一个离散的Omnet++ OMNET++;:在INET 4.0中,如何在离散的NxN空间中移动节点?,omnet++,inet,Omnet++,Inet,正如问题所暗示的,我想在一个离散的(100m x 100m)空间中移动我的AdhocHost。也就是说,例如,假设节点位于(0,0)m,然后它等待一秒钟,然后它“传送”到(0,1)m,而不跨越两个位置之间的距离。我可以怎么做?编写移动模型。将实际整数坐标存储为状态。创建每秒触发的计时器事件,并在该事件上根据规则修改坐标。您可以从头开始,也可以以固定移动模块为例 关于INET中的移动性模型:下面是我如何使用小技巧实现的。 我的机动性模型基于拖拉机机动性 从类TractorMobility扩展之后,
(100m x 100m)
空间中移动我的AdhocHost
。也就是说,例如,假设节点位于(0,0)m
,然后它等待一秒钟,然后它“传送”到(0,1)m
,而不跨越两个位置之间的距离。我可以怎么做?编写移动模型。将实际整数坐标存储为状态。创建每秒触发的计时器事件,并在该事件上根据规则修改坐标。您可以从头开始,也可以以固定移动模块为例
关于INET中的移动性模型:下面是我如何使用小技巧实现的。 我的机动性模型基于拖拉机机动性
从类
TractorMobility
扩展之后,我添加了另一个名为vlasposition
的Coord
变量(表示虚拟最后位置)。此变量的作用与TractorMobility
的lastPosition
变量相同,并存储主机的最后位置。然后,当移动主机并因此设置lastPosition
变量时,我根据vlasposition
坐标的整数部分进行设置。因此,我的移动模块仍然“认为”主机在不断移动,而主机仅在其位置的整数部分发生变化时才移动。
我的模块的
.ned
文件是:
import inet.mobility.single.TractorMobility;
import inet.mobility.contract.IMobility;
simple OfflinePhaseMobility extends TractorMobility like IMobility
{
parameters:
double hopSize @unit(m) = default(1m);
@class(inet::OfflinePhaseMobility);
}
这是我的自定义移动类(OfflinePhaseMobility.h
)的头文件:
这是我的OfflinePhaseMobility.cc
文件:
#include "OfflinePhaseMobility.h"
namespace inet{
Define_Module(OfflinePhaseMobility);
void OfflinePhaseMobility::setTargetPosition()
{
int sign;
Coord positionDelta = Coord::ZERO;
switch (step % 4) {
case 0:
positionDelta.x = x2 - x1;
break;
case 1:
case 3:
sign = (step / (2 * rowCount)) % 2 ? -1 : 1;
positionDelta.y = (y2 - y1) / rowCount * sign;
break;
case 2:
positionDelta.x = x1 - x2;
break;
}
step++;
targetPosition = vLastPosition + positionDelta;
nextChange = simTime() + positionDelta.length() / speed;
}
void OfflinePhaseMobility::move(){
simtime_t now = simTime();
if (now == nextChange) {
vLastPosition = targetPosition;
EV_INFO << "reached current target position = " << vLastPosition << endl;
setTargetPosition();
EV_INFO << "new target position = " << targetPosition << ", next change = " << nextChange << endl;
lastVelocity = (targetPosition - vLastPosition) / (nextChange - simTime()).dbl();
}
else if (now > lastUpdate) {
ASSERT(nextChange == -1 || now < nextChange);
vLastPosition += lastVelocity * (now - lastUpdate).dbl();
}
double hopSize = par("hopSize");
int numHopsX = vLastPosition.x / hopSize;
int numHopsY = vLastPosition.y / hopSize;
double candidateX = (numHopsX * hopSize);
double deltaX = vLastPosition.x - candidateX;
if(deltaX <= hopSize/2)
lastPosition.x = candidateX;
else
lastPosition.x = candidateX + hopSize;
lastPosition.y = (numHopsY * hopSize);
}
}//ns inet
如你所见,我将速度设置为1mps。您可能还意识到,hopeSize
参数是我预先定义的位置之间的距离。就这么简单!希望这能帮助那些有同样问题并且不希望从头开始编写移动模块的人
#include "OfflinePhaseMobility.h"
namespace inet{
Define_Module(OfflinePhaseMobility);
void OfflinePhaseMobility::setTargetPosition()
{
int sign;
Coord positionDelta = Coord::ZERO;
switch (step % 4) {
case 0:
positionDelta.x = x2 - x1;
break;
case 1:
case 3:
sign = (step / (2 * rowCount)) % 2 ? -1 : 1;
positionDelta.y = (y2 - y1) / rowCount * sign;
break;
case 2:
positionDelta.x = x1 - x2;
break;
}
step++;
targetPosition = vLastPosition + positionDelta;
nextChange = simTime() + positionDelta.length() / speed;
}
void OfflinePhaseMobility::move(){
simtime_t now = simTime();
if (now == nextChange) {
vLastPosition = targetPosition;
EV_INFO << "reached current target position = " << vLastPosition << endl;
setTargetPosition();
EV_INFO << "new target position = " << targetPosition << ", next change = " << nextChange << endl;
lastVelocity = (targetPosition - vLastPosition) / (nextChange - simTime()).dbl();
}
else if (now > lastUpdate) {
ASSERT(nextChange == -1 || now < nextChange);
vLastPosition += lastVelocity * (now - lastUpdate).dbl();
}
double hopSize = par("hopSize");
int numHopsX = vLastPosition.x / hopSize;
int numHopsY = vLastPosition.y / hopSize;
double candidateX = (numHopsX * hopSize);
double deltaX = vLastPosition.x - candidateX;
if(deltaX <= hopSize/2)
lastPosition.x = candidateX;
else
lastPosition.x = candidateX + hopSize;
lastPosition.y = (numHopsY * hopSize);
}
}//ns inet
*.hostA.mobility.typename = "OfflinePhaseMobility"
*.hostA.mobility.rowCount = 101
*.hostA.mobility.x1 = 0m
*.hostA.mobility.y1 = 0m
*.hostA.mobility.x2 = 100m
*.hostA.mobility.y2 = 100m
*.hostA.mobility.speed = 1mps
*.hostA.mobility.constraintAreaMinX = 0m
*.hostA.mobility.constraintAreaMinY = 0m
*.hostA.mobility.constraintAreaMaxX = 100m
*.hostA.mobility.constraintAreaMaxY = 100m