OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2014 Wireless Communications and Networking Group (WCNG), |
| 4 * University of Rochester, Rochester, NY, USA. |
| 5 * |
| 6 * This program is free software; you can redistribute it and/or modify |
| 7 * it under the terms of the GNU General Public License version 2 as |
| 8 * published by the Free Software Foundation; |
| 9 * |
| 10 * This program is distributed in the hope that it will be useful, |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 * GNU General Public License for more details. |
| 14 * |
| 15 * You should have received a copy of the GNU General Public License |
| 16 * along with this program; if not, write to the Free Software |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 * |
| 19 * Author: Cristiano Tapparello <cristiano.tapparello@rochester.edu> |
| 20 */ |
| 21 |
| 22 /** |
| 23 * |
| 24 * This example extends the energy model example by connecting a basic energy |
| 25 * harvester to the nodes. |
| 26 * |
| 27 * The example considers a simple communication link between a source and a |
| 28 * destination node, where the source node sends a packet to the destination |
| 29 * every 1 second. Each node is powered by a BasiEnergySource, which is recharge
d |
| 30 * by a BasicEnergyHarvester, and the WiFi radio consumes energy for the transmi
ssion/ |
| 31 * reception of the packets. |
| 32 * |
| 33 * For the receiver node, the example prints the energy consumption of the WiFi
radio, |
| 34 * the power harvested by the energy harvester and the residual energy in the |
| 35 * energy source. |
| 36 * |
| 37 * The nodes initial energy is set to 1.0 J, the transmission and reception enta
il a |
| 38 * current consumption of 0.0174 A and 0.0197 A, respectively (default values in |
| 39 * WifiRadioEnergyModel). The energy harvester provides an amount of power that
varies |
| 40 * according to a random variable uniformly distributed in [0 0.1] W, and is upd
ated |
| 41 * every 1 s. The energy source voltage is 3 V (default value in BasicEnergySour
ce) and |
| 42 * the residual energy level is updated every 1 second (default value). |
| 43 * |
| 44 * The simulation start at time 0 and it is hard stopped at time 10 seconds. Giv
en the |
| 45 * packet size and the distance between the nodes, each transmission lasts 0.002
3s. |
| 46 * As a result, the destination node receives 10 messages. |
| 47 * |
| 48 */ |
| 49 |
| 50 #include "ns3/core-module.h" |
| 51 #include "ns3/network-module.h" |
| 52 #include "ns3/mobility-module.h" |
| 53 #include "ns3/config-store-module.h" |
| 54 #include "ns3/wifi-module.h" |
| 55 #include "ns3/energy-module.h" |
| 56 #include "ns3/internet-module.h" |
| 57 |
| 58 #include <iostream> |
| 59 #include <fstream> |
| 60 #include <vector> |
| 61 #include <string> |
| 62 |
| 63 NS_LOG_COMPONENT_DEFINE ("EnergyWithHarvestingExample"); |
| 64 |
| 65 using namespace ns3; |
| 66 |
| 67 static inline std::string |
| 68 PrintReceivedPacket (Address& from) |
| 69 { |
| 70 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (from); |
| 71 |
| 72 std::ostringstream oss; |
| 73 oss << "--\nReceived one packet! Socket: " << iaddr.GetIpv4 () |
| 74 << " port: " << iaddr.GetPort () |
| 75 << " at time = " << Simulator::Now ().GetSeconds () |
| 76 << "\n--"; |
| 77 |
| 78 return oss.str (); |
| 79 } |
| 80 |
| 81 /** |
| 82 * \param socket Pointer to socket. |
| 83 * |
| 84 * Packet receiving sink. |
| 85 */ |
| 86 void |
| 87 ReceivePacket (Ptr<Socket> socket) |
| 88 { |
| 89 Ptr<Packet> packet; |
| 90 Address from; |
| 91 while ((packet = socket->RecvFrom (from))) |
| 92 { |
| 93 if (packet->GetSize () > 0) |
| 94 { |
| 95 NS_LOG_UNCOND (PrintReceivedPacket (from)); |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 /** |
| 101 * \param socket Pointer to socket. |
| 102 * \param pktSize Packet size. |
| 103 * \param n Pointer to node. |
| 104 * \param pktCount Number of packets to generate. |
| 105 * \param pktInterval Packet sending interval. |
| 106 * |
| 107 * Traffic generator. |
| 108 */ |
| 109 static void |
| 110 GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, Ptr<Node> n, |
| 111 uint32_t pktCount, Time pktInterval) |
| 112 { |
| 113 if (pktCount > 0) |
| 114 { |
| 115 socket->Send (Create<Packet> (pktSize)); |
| 116 Simulator::Schedule (pktInterval, &GenerateTraffic, socket, pktSize, n, |
| 117 pktCount - 1, pktInterval); |
| 118 } |
| 119 else |
| 120 { |
| 121 socket->Close (); |
| 122 } |
| 123 } |
| 124 |
| 125 /// Trace function for remaining energy at node. |
| 126 void |
| 127 RemainingEnergy (double oldValue, double remainingEnergy) |
| 128 { |
| 129 std::cout << Simulator::Now ().GetSeconds () |
| 130 << "s Current remaining energy = " << remainingEnergy << "J" << std:
:endl; |
| 131 } |
| 132 |
| 133 /// Trace function for total energy consumption at node. |
| 134 void |
| 135 TotalEnergy (double oldValue, double totalEnergy) |
| 136 { |
| 137 std::cout << Simulator::Now ().GetSeconds () |
| 138 << "s Total energy consumed by radio = " << totalEnergy << "J" << st
d::endl; |
| 139 } |
| 140 |
| 141 /// Trace function for the power harvested by the energy harvester. |
| 142 void |
| 143 HarvestedPower (double oldValue, double harvestedPower) |
| 144 { |
| 145 std::cout << Simulator::Now ().GetSeconds () |
| 146 << "s Current harvested power = " << harvestedPower << " W" << std::
endl; |
| 147 } |
| 148 |
| 149 /// Trace function for the total energy harvested by the node. |
| 150 void |
| 151 TotalEnergyHarvested (double oldValue, double TotalEnergyHarvested) |
| 152 { |
| 153 std::cout << Simulator::Now ().GetSeconds () |
| 154 << "s Total energy harvested by harvester = " |
| 155 << TotalEnergyHarvested << " J" << std::endl; |
| 156 } |
| 157 |
| 158 |
| 159 int |
| 160 main (int argc, char *argv[]) |
| 161 { |
| 162 /* |
| 163 LogComponentEnable ("EnergySource", LOG_LEVEL_DEBUG); |
| 164 LogComponentEnable ("BasicEnergySource", LOG_LEVEL_DEBUG); |
| 165 LogComponentEnable ("DeviceEnergyModel", LOG_LEVEL_DEBUG); |
| 166 LogComponentEnable ("WifiRadioEnergyModel", LOG_LEVEL_DEBUG); |
| 167 LogComponentEnable ("EnergyHarvester", LOG_LEVEL_DEBUG); |
| 168 LogComponentEnable ("BasicEnergyHarvester", LOG_LEVEL_DEBUG); |
| 169 */ |
| 170 |
| 171 std::string phyMode ("DsssRate1Mbps"); |
| 172 double Prss = -80; // dBm |
| 173 uint32_t PpacketSize = 200; // bytes |
| 174 bool verbose = false; |
| 175 |
| 176 // simulation parameters |
| 177 uint32_t numPackets = 10000; // number of packets to send |
| 178 double interval = 1; // seconds |
| 179 double startTime = 0.0; // seconds |
| 180 double distanceToRx = 100.0; // meters |
| 181 /* |
| 182 * This is a magic number used to set the transmit power, based on other |
| 183 * configuration. |
| 184 */ |
| 185 double offset = 81; |
| 186 ·· |
| 187 // Energy Harvester variables |
| 188 double harvestingUpdateInterval = 1; // seconds |
| 189 |
| 190 CommandLine cmd; |
| 191 cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); |
| 192 cmd.AddValue ("Prss", "Intended primary RSS (dBm)", Prss); |
| 193 cmd.AddValue ("PpacketSize", "size of application packet sent", PpacketSize); |
| 194 cmd.AddValue ("numPackets", "Total number of packets to send", numPackets); |
| 195 cmd.AddValue ("startTime", "Simulation start time", startTime); |
| 196 cmd.AddValue ("distanceToRx", "X-Axis distance between nodes", distanceToRx); |
| 197 cmd.AddValue ("verbose", "Turn on all device log components", verbose); |
| 198 cmd.Parse (argc, argv); |
| 199 |
| 200 // Convert to time object |
| 201 Time interPacketInterval = Seconds (interval); |
| 202 |
| 203 // disable fragmentation for frames below 2200 bytes |
| 204 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", |
| 205 StringValue ("2200")); |
| 206 // turn off RTS/CTS for frames below 2200 bytes |
| 207 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
| 208 StringValue ("2200")); |
| 209 // Fix non-unicast data rate to be the same as that of unicast |
| 210 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 211 StringValue (phyMode)); |
| 212 |
| 213 NodeContainer c; |
| 214 c.Create (2); // create 2 nodes |
| 215 NodeContainer networkNodes; |
| 216 networkNodes.Add (c.Get (0)); |
| 217 networkNodes.Add (c.Get (1)); |
| 218 |
| 219 // The below set of helpers will help us to put together the wifi NICs we want |
| 220 WifiHelper wifi; |
| 221 if (verbose) |
| 222 { |
| 223 wifi.EnableLogComponents (); |
| 224 } |
| 225 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 226 |
| 227 /** Wifi PHY **/ |
| 228 /***************************************************************************/ |
| 229 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
| 230 wifiPhy.Set ("RxGain", DoubleValue (-10)); |
| 231 wifiPhy.Set ("TxGain", DoubleValue (offset + Prss)); |
| 232 wifiPhy.Set ("CcaMode1Threshold", DoubleValue (0.0)); |
| 233 /***************************************************************************/ |
| 234 |
| 235 /** wifi channel **/ |
| 236 YansWifiChannelHelper wifiChannel; |
| 237 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 238 wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); |
| 239 // create wifi channel |
| 240 Ptr<YansWifiChannel> wifiChannelPtr = wifiChannel.Create (); |
| 241 wifiPhy.SetChannel (wifiChannelPtr); |
| 242 |
| 243 /** MAC layer **/ |
| 244 // Add a non-QoS upper MAC, and disable rate control |
| 245 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 246 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", |
| 247 StringValue (phyMode), "ControlMode", |
| 248 StringValue (phyMode)); |
| 249 // Set it to ad-hoc mode |
| 250 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 251 |
| 252 /** install PHY + MAC **/ |
| 253 NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, networkNodes); |
| 254 |
| 255 /** mobility **/ |
| 256 MobilityHelper mobility; |
| 257 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>
(); |
| 258 positionAlloc->Add (Vector (0.0, 0.0, 0.0)); |
| 259 positionAlloc->Add (Vector (2 * distanceToRx, 0.0, 0.0)); |
| 260 mobility.SetPositionAllocator (positionAlloc); |
| 261 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 262 mobility.Install (c); |
| 263 |
| 264 /** Energy Model **/ |
| 265 /***************************************************************************/ |
| 266 /* energy source */ |
| 267 BasicEnergySourceHelper basicSourceHelper; |
| 268 // configure energy source |
| 269 basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (1.0)); |
| 270 // install source |
| 271 EnergySourceContainer sources = basicSourceHelper.Install (c); |
| 272 /* device energy model */ |
| 273 WifiRadioEnergyModelHelper radioEnergyHelper; |
| 274 // configure radio energy model |
| 275 radioEnergyHelper.Set ("TxCurrentA", DoubleValue (0.0174)); |
| 276 radioEnergyHelper.Set ("RxCurrentA", DoubleValue (0.0197)); |
| 277 // install device model |
| 278 DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices,
sources); |
| 279 |
| 280 /* energy harvester */ |
| 281 BasicEnergyHarvesterHelper basicHarvesterHelper; |
| 282 // configure energy harvester |
| 283 basicHarvesterHelper.Set ("PeriodicHarvestedPowerUpdateInterval", TimeValue (S
econds (harvestingUpdateInterval))); |
| 284 basicHarvesterHelper.Set ("HarvestablePower", StringValue ("ns3::UniformRandom
Variable[Min=0.0|Max=0.1]")); |
| 285 // install harvester on all energy sources |
| 286 EnergyHarvesterContainer harvesters = basicHarvesterHelper.Install (sources); |
| 287 /***************************************************************************/ |
| 288 |
| 289 /** Internet stack **/ |
| 290 InternetStackHelper internet; |
| 291 internet.Install (networkNodes); |
| 292 |
| 293 Ipv4AddressHelper ipv4; |
| 294 NS_LOG_INFO ("Assign IP Addresses."); |
| 295 ipv4.SetBase ("10.1.1.0", "255.255.255.0"); |
| 296 Ipv4InterfaceContainer i = ipv4.Assign (devices); |
| 297 |
| 298 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 299 Ptr<Socket> recvSink = Socket::CreateSocket (networkNodes.Get (1), tid); // n
ode 1, Destination |
| 300 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); |
| 301 recvSink->Bind (local); |
| 302 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); |
| 303 |
| 304 Ptr<Socket> source = Socket::CreateSocket (networkNodes.Get (0), tid); // n
ode 0, Source |
| 305 InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetBroadcast (), 80
); |
| 306 source->SetAllowBroadcast (true); |
| 307 source->Connect (remote); |
| 308 |
| 309 /** connect trace sources **/ |
| 310 /***************************************************************************/ |
| 311 // all traces are connected to node 1 (Destination) |
| 312 // energy source |
| 313 Ptr<BasicEnergySource> basicSourcePtr = DynamicCast<BasicEnergySource> (source
s.Get (1)); |
| 314 basicSourcePtr->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&
RemainingEnergy)); |
| 315 // device energy model |
| 316 Ptr<DeviceEnergyModel> basicRadioModelPtr = |
| 317 basicSourcePtr->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get (0)
; |
| 318 NS_ASSERT (basicRadioModelPtr != NULL); |
| 319 basicRadioModelPtr->TraceConnectWithoutContext ("TotalEnergyConsumption", Make
Callback (&TotalEnergy)); |
| 320 // energy harvester |
| 321 Ptr<BasicEnergyHarvester> basicHarvesterPtr = DynamicCast<BasicEnergyHarvester
> (harvesters.Get (1)); |
| 322 basicHarvesterPtr->TraceConnectWithoutContext ("HarvestedPower", MakeCallback
(&HarvestedPower)); |
| 323 basicHarvesterPtr->TraceConnectWithoutContext ("TotalEnergyHarvested", MakeCal
lback (&TotalEnergyHarvested)); |
| 324 /***************************************************************************/ |
| 325 |
| 326 |
| 327 /** simulation setup **/ |
| 328 // start traffic |
| 329 Simulator::Schedule (Seconds (startTime), &GenerateTraffic, source, PpacketSiz
e, |
| 330 networkNodes.Get (0), numPackets, interPacketInterval); |
| 331 |
| 332 Simulator::Stop (Seconds (10.0)); |
| 333 Simulator::Run (); |
| 334 Simulator::Destroy (); |
| 335 |
| 336 return 0; |
| 337 } |
OLD | NEW |