在NS2
中获取信号强度
p
是数据包
TxPr
是传输信号强度
RxPr
是接收信号强度
Packet *p = Packet::alloc();
//SIGNAL
double receiveSignalStrength = p->txinfo_.RxPr;
double RSS;
if(receiveSignalStrength == 0)
{
RSSI = -200;
}
else
{
RSS = 10*log10(receiveSignalStrength);
}
if(SIGNAL_THRESHOLD >= RSS)
{
Packet::free((Packet *)p);
return;
}
在NS3
中获取信号强度
回调函数
void ReceivePacketWithRss(std::string context, Ptr<const Packet> packet, uint16_t channelFreqMhz, WifiTxVector txVector, MpduInfo aMpdu, SignalNoiseDbm signalNoise, uint16_t staId)
{
WifiMacHeader hdr;
packet->PeekHeader(hdr);
uint32_t index = std::stoi(context.substr(10, 1));
Vector pos = GetPosition(c.Get(index));
Vector Tpos = GetPosition(c.Get(0)); // Target Position
NS_LOG_UNCOND("*******************************************************************************************************************");
NS_LOG_UNCOND("%INFO: I am Node " << index << " My Position is:" << pos << " And I Recieved " << signalNoise.signal << " dbm");
NS_LOG_UNCOND("*******************************************************************************************************************");
outFiles[0] << Simulator::Now().GetMilliSeconds() << "," << context.substr(10, 1) <<"," << pos.x << "," << pos.y << "," << signalNoise.signal <<"," << CalculateDistance(pos,Tpos) << "," << Tpos.x << "," << Tpos.y <<"\n";
outFiles[index] << Tpos.x << "," << Tpos.y << "," << signalNoise.signal << "\n";
}
跟踪
Config::Connect("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/$ns3::WifiPhy/MonitorSnifferRx", MakeCallback(&ReceivePacketWithRss));
节点位置
void SetPosition(Ptr<Node> node, Vector position)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
mobility->SetPosition(position);
}
Vector GetPosition(Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
return mobility->GetPosition();
}
传播模型(可更改)
NS_LOG_UNCOND("Configuring PHY Loss model...");
wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");