Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Error "uid<=m_information.size()&&uid!=0" in NS3-SUMO integration

69 views
Skip to first unread message

周承山

unread,
Nov 21, 2024, 8:30:22 AM11/21/24
to ns-3-users
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/applications-module.h"
#include "ns3/tap-bridge-module.h"
#include "ns3/log.h"
#include "iostream"
using namespace ns3;

static int currentChannel = 1;
NS_LOG_COMPONENT_DEFINE("FrequencyHoppingExample");
void ChangeFrequency(Ptr<NetDevice> device, double hopInterval) {
    Ptr<WifiNetDevice> wifiDevice = DynamicCast<WifiNetDevice>(device);
    if (wifiDevice == nullptr) {
        NS_LOG_UNCOND("wifiDevice is null for device ID: " << device->GetNode()->GetId());
        return;
    }
    Ptr<WifiPhy> wifiPhy = wifiDevice->GetPhy();
    Ptr<YansWifiPhy> yansWifiPhy = DynamicCast<YansWifiPhy>(wifiPhy);
    // 更新当前信道
    currentChannel = (currentChannel % 14) + 1; // 在1到14之间循环

    // 更改信道
    YansWifiChannelHelper wifiChannel;
    yansWifiPhy->SetChannel(wifiChannel.Create());
    //wifiPhy->SetAttribute("ChannelSettings", StringValue("{0, 0, BAND_2_4GHZ, 0}"));
    // Log the change
    NS_LOG_UNCOND("Time: " << Simulator::Now().GetSeconds()
                   << "s, Node " << device->GetNode()->GetId()
                   << " changed to channel " << currentChannel);

    // Schedule next change
    Simulator::Schedule(Seconds(hopInterval), &ChangeFrequency, device, hopInterval);
}

// void ChangeFrequency(Ptr<NetDevice> device, double hopInterval)

// {

// Ptr<WifiNetDevice> wifiDevice = DynamicCast<WifiNetDevice>(device);

// Ptr<WifiPhy> wifiPhy = wifiDevice->GetPhy();


// currentChannel = (currentChannel % 11) + 1; // Assuming 11 channels
// double frequency = 2407 + (currentChannel - 1) * 5;

// wifiPhy->SetOperatingChannel({currentChannel,0,frequency,0});

// LogComponentEnable("wifiPhy", LOG_LEVEL_INFO);

// NS_LOG_UNCOND("Node"  << device->GetNode()->GetId() << " changed to channel " << currentChannel);

// // Schedule the next frequency change

// Simulator::Schedule(Seconds(hopInterval), &ChangeFrequency, device, hopInterval);

// }

int main(int argc, char *argv[])

{

uint32_t numNodes = 2;

double simTime = 10.0;

double hopInterval = 1.0; // Interval in seconds for frequency hopping

CommandLine cmd;

cmd.AddValue("numNodes", "Number of nodes", numNodes);

cmd.AddValue("simTime", "Simulation time (seconds)", simTime);

cmd.AddValue("hopInterval", "Frequency hopping interval (seconds)", hopInterval);

cmd.Parse(argc, argv);

LogComponentEnable("WifiPhy", LOG_LEVEL_INFO);

NodeContainer nodes;

nodes.Create(numNodes);

YansWifiChannelHelper wifiChannel;
YansWifiPhyHelper wifiPhy;

wifiPhy.SetChannel(wifiChannel.Create());

WifiHelper wifi;

wifi.SetStandard(WIFI_STANDARD_80211n);

WifiMacHelper wifiMac;

wifiMac.SetType("ns3::AdhocWifiMac");

NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);

//配置IP
InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

address.SetBase("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Install applications

//发送端
uint16_t port = 9;

OnOffHelper onoff("ns3::UdpSocketFactory", Address(InetSocketAddress(interfaces.GetAddress(numNodes-1), port)));

onoff.SetConstantRate(DataRate("500kbps"));

ApplicationContainer apps = onoff.Install(nodes.Get(0));

apps.Start(Seconds(1.0));

apps.Stop(Seconds(simTime));
//接收端

PacketSinkHelper sink("ns3::UdpSocketFactory", Address(InetSocketAddress(Ipv4Address::GetAny(), port)));

apps = sink.Install(nodes.Get(numNodes-1));

apps.Start(Seconds(0.0));

apps.Stop(Seconds(simTime));
LogComponentEnable("FrequencyHoppingExample", LOG_LEVEL_ALL);

// Implement frequency hopping

for (uint32_t i = 0; i < numNodes; ++i)

{

Simulator::ScheduleWithContext(nodes.Get(i)->GetId(), Seconds(hopInterval), &ChangeFrequency, devices.Get(i), hopInterval);

}

Simulator::Stop(Seconds(simTime));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Tommaso Pecorella

unread,
Nov 21, 2024, 7:03:38 PM11/21/24
to ns-3-users
I don't know if you do understand how rude is your post. I guess you don't - but I warmly suggest to meditate on what you posted, and no, the language is not an acceptable excuse.

Since I'm not impolite, I'll give you some hints:
1) Use a debugger,
2) Read the error messages,
3) Read the documentation and the examples,
4) Don't use copilot, ChatGPT, or any LLM for writing code. If you do, remember that it's YOUR task to clean the code.

As for the error, it's a pretty easy one (repeated twice in the code).
However, you also did forget some very important code pieces. I'll not tell you which ones because you have to learn.

周承山

unread,
Nov 21, 2024, 9:06:19 PM11/21/24
to ns-3-users

Thanks for taking the time to provide feedback.I'm sorry for my rudeness. It's my first time to use this. I understand that my previous post might have come across as lacking effort, and I genuinely appreciate you pointing out areas for improvement.I value your direct approach and will take your advice to heart. I'll try to identify the missing pieces and fix the errors myself as a learning opportunity. If you don't mind, could you recommend some websites for learning these to me? I would be very grateful for your help.

Tommaso Pecorella

unread,
Nov 24, 2024, 9:26:42 AM11/24/24
to ns-3-users
Hi, apologies accepted, and the incident is forgiven and forgot.

About websites to learn, the request is a bit broad, like, do you need a C++ primer, a guide to use the debugger, to set it up in your IDE (e.g., VSCode), or to learn ns-3?
Reply all
Reply to author
Forward
0 new messages