Chrisy Sa
unread,Nov 18, 2011, 4:10:08 PM11/18/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ns-3-users
i have implement a simple manet but i cannot understand how to set the
transmission rate.... please help!
here is the code for my network:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/energy-module.h"
#include "ns3/internet-module.h"
#include "ns3/aodv-helper.h"
#include "ns3/aodv-routing-protocol.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
NS_LOG_COMPONENT_DEFINE ("Manet_Simple_Example_Energy");
using namespace ns3;
using namespace std;
void ReceivePacket (Ptr<Socket> socket)
{
ofstream outfile;
outfile.open("Simulation.txt", ios::app);
Ptr<Packet> packet;
Address from;
while (packet = socket->RecvFrom (from))
{
if (packet->GetSize () > 0)
{
InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (from);
NS_LOG_UNCOND ("--\nReceived one packet! Socket: "<< iaddr.GetIpv4
()<< " port: " << iaddr.GetPort () << " at time = " << Simulator::Now
().GetSeconds () << "\n--");
outfile << "--\nReceived one packet! Socket: "<< iaddr.GetIpv4 ()<< "
port: " << iaddr.GetPort () << " at time = " << Simulator::Now
().GetSeconds () << "\n--";
(void) iaddr;
}
}
outfile.close();
}
/**
* \param socket Pointer to socket.
* \param pktSize Packet size.
* \param n Pointer to node.
* \param pktCount Number of packets to generate.
* \param pktInterval Packet sending interval.
*
* Traffic generator.
*/
static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
Ptr<Node> n, uint32_t pktCount, Time pktInterval)
{
if (pktCount > 0)
{
socket->Send (Create<Packet> (pktSize));
Simulator::Schedule (pktInterval, &GenerateTraffic, socket, pktSize,
n, pktCount - 1, pktInterval);
}
else
{
socket->Close ();
}
}
void RemainingEnergy (double oldValue, double remainingEnergy)
{
ofstream outfile;
outfile.open("Simulation.txt", ios::app);
NS_LOG_UNCOND (Simulator::Now().GetSeconds() << "s Current remaining
energy = " << remainingEnergy << "J");
outfile << Simulator::Now().GetSeconds() << "s Current remaining
energy = " << remainingEnergy << "J\n";
outfile.close();
}
void TotalEnergy (double oldValue, double totalEnergy)
{
ofstream outfile;
outfile.open("Simulation.txt", ios::app);
NS_LOG_UNCOND ( Simulator::Now().GetSeconds() <<"s Total energy
consumed by radio = " << totalEnergy << "J");
outfile << Simulator::Now().GetSeconds() << "s Total energy consumed
by radio = " << totalEnergy << "J\n";
outfile.close();
}
int main (int argc, char *argv[])
{
std::string rate("8kbps");
std::string phyMode ("DsssRate2Mbps");
double Prss = -80; // dBm
uint32_t PpacketSize = 2048; // bytes
bool verbose = false;
// simulation parameters
uint32_t numNodes = 25; // number of nodes (5x5)
uint32_t numPackets = 6; // number of packets to send
uint32_t sourceNode = 24;
uint32_t receiverNode = 0;
double interval = 0.2; // seconds
double startTime = 0.0; // seconds
double distanceToRx = 100.0; // meters
int nodeSpeed = 1000;
int nodePause = 0;
ofstream outfile;
outfile.open("Simulation.txt", ios::app);
outfile << "Simulation Parameters" << endl
<< "Number of Nodes: " << numNodes << endl
<< "Numbet of Packets: " << numPackets << endl
<< "Packet Size: " << PpacketSize << endl
<< "Send Node: " << sourceNode << endl
<< "Receiver Node: " << receiverNode << endl
<< "Interval: " << interval << endl;
/*
* This is a magic number used to set the transmit power, based on
other
* configuration.
*/
double offset = 81;
CommandLine cmd;
cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
cmd.AddValue ("Prss", "Intended primary RSS (dBm)", Prss);
cmd.AddValue ("PpacketSize", "size of application packet sent",
PpacketSize);
cmd.AddValue ("numPackets", "Total number of packets to send",
numPackets);
cmd.AddValue ("startTime", "Simulation start time", startTime);
cmd.AddValue ("distanceToRx", "X-Axis distance between nodes",
distanceToRx);
cmd.AddValue ("verbose", "Turn on all device log components",
verbose);
cmd.Parse (argc, argv);
Config::SetDefault ("ns3::OnOffApplication::PacketSize",
StringValue("1000"));
Config::SetDefault ("ns3::OnOffApplication::DataRate",
StringValue(rate));
// Convert to time object
Time interPacketInterval = Seconds (interval);
// disable fragmentation for frames below 2200 bytes
Config::SetDefault
("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue
("2200"));
// turn off RTS/CTS for frames below 2200 bytes
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold",
StringValue ("2200"));
// Fix non-unicast data rate to be the same as that of unicast
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
StringValue (phyMode));
NodeContainer networkNodes;
networkNodes.Create (numNodes);
// The below set of helpers will help us to put together the wifi NICs
we want
WifiHelper wifi;
if (verbose)
{
wifi.EnableLogComponents ();
}
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
//wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
/** Wifi PHY **/
/
***************************************************************************/
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.Set ("RxGain", DoubleValue (-10));
wifiPhy.Set ("TxGain", DoubleValue (offset + Prss));
wifiPhy.Set ("CcaMode1Threshold", DoubleValue (0.0));
/
***************************************************************************/
/** wifi channel **/
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay
("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
// create wifi channel
Ptr<YansWifiChannel> wifiChannelPtr = wifiChannel.Create ();
wifiPhy.SetChannel (wifiChannelPtr);
/** MAC layer **/
// Add a non-QoS upper MAC, and disable rate control
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue (phyMode),
"ControlMode",StringValue (phyMode));
// Set it to ad-hoc mode
wifiMac.SetType ("ns3::AdhocWifiMac");
/** install PHY + MAC **/
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac,
networkNodes);
/** mobility **/
MobilityHelper mobility;
ObjectFactory pos;
pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
pos.Set("X", RandomVariableValue (UniformVariable (0.0, 300.0)));
pos.Set("Y", RandomVariableValue (UniformVariable (0.0, 1500.0)));
Ptr<PositionAllocator> taPositionAlloc = pos.Create()-
>GetObject<PositionAllocator>();
mobility.SetMobilityModel("ns3::RandomWaypointMobilityModel",
"Speed", RandomVariableValue (UniformVariable(0.0, nodeSpeed)),
"Pause", RandomVariableValue (ConstantVariable (nodePause)),
"PositionAllocator", PointerValue (taPositionAlloc));
mobility.SetPositionAllocator(taPositionAlloc);
mobility.Install (networkNodes);
/* Energy Model*/
BasicEnergySourceHelper basicSourceHelper;
basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ",
DoubleValue(0.1));
EnergySourceContainer sources =
basicSourceHelper.Install(networkNodes);
WifiRadioEnergyModelHelper radioEnergyHelper;
radioEnergyHelper.Set("TxCurrentA", DoubleValue(0.0174));
DeviceEnergyModelContainer deviceModels =
radioEnergyHelper.Install(devices, sources);
/** Enable Routing Protocol (OLSR) **/
AodvHelper aodv;
/** Internet stack **/
InternetStackHelper internet;
internet.SetRoutingHelper(aodv);
internet.Install (networkNodes);
/** Assign Network Adresses **/
Ipv4AddressHelper ipv4;
NS_LOG_INFO ("Assign IP Addresses.");
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (devices);
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> recvSink = Socket::CreateSocket (networkNodes.Get
(receiverNode), tid); // node 1, receiver
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (),
80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
Ptr<Socket> source = Socket::CreateSocket (networkNodes.Get
(sourceNode), tid); // node 25, sender
InetSocketAddress remote = InetSocketAddress (i.GetAddress
(receiverNode, 0), 80);
source->SetAllowBroadcast (true);
source->Connect (remote);
/** Energy **/
/*
Ptr<BasicEnergySource> basicSourcePtr = DynamicCast<BasicEnergySource>
(sources.Get(24));
basicSourcePtr -> TraceConnectWithoutContext("RemainingEnergy",
MakeCallback(&RemainingEnergy));
Ptr<DeviceEnergyModel> basicRadioModelPtr = basicSourcePtr ->
FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get(0);
NS_ASSERT (basicRadioModelPtr != NULL);
basicRadioModelPtr ->
TraceConnectWithoutContext("TotalEnergyConsumption",
MakeCallback(&TotalEnergy));
*/
/** simulation setup **/
// start traffic
Simulator::Schedule (Seconds (10.0), &GenerateTraffic, source,
PpacketSize, networkNodes.Get (sourceNode), numPackets,
interPacketInterval);
Simulator::Stop (Seconds (20.0));
Simulator::Run ();
Simulator::Destroy ();
outfile << "End of Simulation" << endl << endl;
outfile.close();
return 0;
}