Sie sind auf Seite 1von 8

/

*__________________________________________________________________________________
____________________________________/
+
+
+ Estudio de Energìa Remanente en un Escenario Ad-Hoc
+
+
+
+
+__________________________________________________________________________________
_____________________________________+
*/

#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/internet-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/netanim-module.h"
#include "ns3/energy-module.h"
#include "ns3/applications-module.h"
#include "ns3/application.h"
#include "ns3/onoff-application.h"
#include "ns3/object-factory.h"
#include "ns3/evalvid-client-server-helper.h"
#include "ns3/evalvid-client.h"
#include "ns3/evalvid-server.h"

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

NS_LOG_COMPONENT_DEFINE ("Estudio de Energía en un Escenario Ad-Hoc");//Log del


Escenario

using namespace ns3;


using namespace std;

double ini_Energy=13320;//Variable de Energía Inicial en cada nodo en Julios


double stop_simulacion=1800; //Tiempo de simulación
/
*__________________________________________________________________________________
______
+ +
+ +
+ Métodos: Print Energy en Nodos
+
+ +
+
___________________________________________________________________________________
_____+
*/
void
RemainingEnergy (double oldValue, double remainingEnergy)
{
NS_LOG_UNCOND (Simulator::Now ().GetSeconds ()
<< "s Current remaining energy = " << remainingEnergy << "J");
}
void PrintEnergy(EnergySourceContainer &a) {

EnergySourceContainer::Iterator i;
double frac;
double frac_Energy;
double med_Energy;
int id;
double tiempo = Now ().GetSeconds ();
string direccion = "/home/pablo/Documentos/ns-allinone-3.29/ns-
3.29/trafic2.txt";
string num = "";
string direcion1 = "";

for (i=a.Begin(); i != a.End(); i++) {


med_Energy = (*i)->GetRemainingEnergy();
frac = (*i)->GetEnergyFraction();
id = (*i)->GetNode()->GetId();
frac_Energy = frac*100;
ostringstream convert1;//Conversión Int to Str-->Name of Traza
convert1 << id;
num = convert1.str();
ofstream file;
direcion1 = direccion + num;
file.open(direcion1,std::ios::app);
file << tiempo;
file << " ";
file << frac_Energy;
file << "\n";
file.close();
NS_LOG_UNCOND(Simulator::Now ().GetSeconds ()
<< "s " "Node[" << id << "]"<< " \tRemaining Energy= " <<
med_Energy<<"[J]");
}
cout<<"\n";
}

/
*__________________________________________________________________________________
______
+ +
+ +
+ Int main / LogComponentEnable / Variables
+
+ +
+
___________________________________________________________________________________
_____+
*/

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

{
//++++++++Logs se pueden habilitar para analizar los mensajes que genera el
simulador en cada módulo +++++++++

// LogComponentEnable ("EvalvidClient", LOG_LEVEL_ALL);


// LogComponentEnable ("EvalvidServer", LOG_LEVEL_ALL);
//LogComponentEnable ("YansWifiPhy", LOG_LEVEL_ALL);
//LogComponentEnable ("WifiPhyStateHelper", LOG_LEVEL_ALL);
// LogComponentEnable ("OnOffApplication", LOG_LEVEL_INFO);
//LogComponentEnable ("WifiPhy", LOG_LEVEL_ALL);
//LogComponentEnable ("WifiRadioEnergyModel", LOG_ALL);
//LogComponentEnable ("EnergySource", LOG_LEVEL_ALL);
//LogComponentEnable ("EnergySource", LOG_ALL);
//LogComponentEnable ("BasicEnergySource", LOG_LEVEL_DEBUG);
//LogComponentEnable ("DeviceEnergyModel", LOG_LEVEL_DEBUG);
//LogComponentEnable ("WifiRadioEnergyModel", LOG_LEVEL_DEBUG);

std::string phyMode ("ErpOfdmRate24Mbps");//DsssRate1Mbps OfdmRate54Mbps


ErpOfdmRate54Mbps DsssRate11Mbps
double distance = 500;
uint32_t numNodes = 4;
uint32_t sinkNode = 3;
uint32_t sourceNode = 0;
bool verbose = false;
bool tracing = true;

/
*__________________________________________________________________________________
______
+ +
+ +
+ CommandLine cmd / Calculo Variables
+
+ +
+
___________________________________________________________________________________
_____+
*/

CommandLine cmd;

cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);


cmd.AddValue ("distance", "distance (m)", distance);
cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);
cmd.AddValue ("tracing", "turn on ascii and pcap tracing", tracing);
cmd.AddValue ("numNodes", "number of nodes", numNodes);
cmd.AddValue ("sinkNode", "Receiver node number", sinkNode);
cmd.AddValue ("sourceNode", "Sender node number", sourceNode);
cmd.Parse (argc, argv);

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuraciòn de Nodos Ad-Hoc +
+ +
+
___________________________________________________________________________________
_____+
*/

NodeContainer AdHocNode;
AdHocNode.Create (numNodes);

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración de Canal Inalàmbrico (Phy Layer)
+
+ +
+
___________________________________________________________________________________
_____+
*/

NetDeviceContainer WifiDevices;// Wireless Cards


YansWifiChannelHelper WifiChannel = YansWifiChannelHelper::Default (); //Channel
Model Loss and Delay (Default)
YansWifiPhyHelper WifiPhy = YansWifiPhyHelper::Default (); //Wireless Interface
WifiPhy.SetChannel (WifiChannel.Create ()); // Asocia Channel-Wireless Interface

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración de Capa MAC +
+ +
+
___________________________________________________________________________________
_____+
*/

WifiHelper Wifi;
Wifi.SetStandard (WIFI_PHY_STANDARD_80211n_2_4GHZ);
WifiMacHelper WifiMac;
WifiMac.SetType ("ns3::AdhocWifiMac");
Wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue (phyMode),
"ControlMode",StringValue (phyMode));
WifiDevices = Wifi.Install (WifiPhy, WifiMac, AdHocNode);

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración de Movilidad Nodos +
+ +
+
___________________________________________________________________________________
_____+
*/
MobilityHelper Mobility;
Mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
Mobility.Install (AdHocNode);

uint32_t nodo=0;
double posx=0;
double posy=0;
for(nodo=0;nodo<=numNodes-1; nodo=nodo+1){
AnimationInterface::SetConstantPosition (AdHocNode.Get (nodo), posx, posy);
posx=posx+20;
}

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración Protocolo de Enrutamiento OLSR
+
+ +
+
___________________________________________________________________________________
_____+
*/

OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;

Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10);

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración Capa de Red:Direccionamiento IP
+
+ +
+
___________________________________________________________________________________
_____+
*/
InternetStackHelper internet;
internet.SetRoutingHelper (list); // => has effect on the next Install ()
internet.Install (AdHocNode);

Ipv4AddressHelper ipv4;
NS_LOG_INFO ("Assign IP Addresses.");
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (WifiDevices);

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración Capa Aplicaciones: Video_Evalvid Traza
+
+ +
+
___________________________________________________________________________________
_____+
*/
NS_LOG_INFO ("Create Applications.");
int NumApp=180;//Número de Aplicaciones o veces que se envia el Video
int duracion=10;//duración de un video
int inicio=11;//inicio del video
int Interval_App=3;
int App=0;
uint16_t port = 4000;
ApplicationContainer apps[NumApp];
for(App=0;App<NumApp;App++)
{
// Create one udpServer applications on node one.

EvalvidServerHelper server (port);


server.SetAttribute ("SenderTraceFilename", StringValue("crew_traza.f"));//Se
utiliza la traza st_highway_ci.st
server.SetAttribute ("SenderDumpFilename", StringValue("EE_Video_S"));
server.SetAttribute ("PacketPayload",UintegerValue(1014));
apps[App]= server.Install (AdHocNode.Get(0));
apps[App].Start (Seconds (inicio));
apps[App].Stop (Seconds (inicio+duracion));
// Create one EvalvidClient application
EvalvidClientHelper client (i.GetAddress (0),port);
client.SetAttribute ("ReceiverDumpFilename", StringValue("EE_Video_R"));
apps[App]=client.Install (AdHocNode.Get (3));
apps[App].Start (Seconds (inicio+1));
apps[App].Stop (Seconds (inicio+duracion+1));

port=port+1;
inicio=inicio+Interval_App;

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración Parámetros de Energía +
+ +
+
___________________________________________________________________________________
_____+
*/

BasicEnergySourceHelper basicSourceHelper; /* => Energy Source */


basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue
(ini_Energy)); // => Configure Energy Source
basicSourceHelper.Set ("BasicEnergySupplyVoltageV", DoubleValue (3.3));//--
>Datasheet

EnergySourceContainer sources = basicSourceHelper.Install (AdHocNode); // =>


Install Source

/* Device Energy Model */

WifiRadioEnergyModelHelper radioEnergyHelper;

/*Se especifican los valores de consumo de una tarjeta real, en este caso de la
tarjeta Intel Wireless3945ABG*/

radioEnergyHelper.Set ("TxCurrentA", DoubleValue (0.545));//=> Consumo Max-


>DataSheet 1.8W/3.3V/ 0.545
radioEnergyHelper.Set ("RxCurrentA", DoubleValue (0.424));//=> Consumo Max-
>DataSheet 1.4W/3.3V/ 0.424
radioEnergyHelper.Set ("IdleCurrentA", DoubleValue (0.0454));//=> Consumo
Nominal->DataSheet 150mW/ 0.0454
radioEnergyHelper.Set ("CcaBusyCurrentA", DoubleValue (0.0091));//=> Consumo Max-
>DataSheet(Modo Sleep) 30mW/ 0.0091

/*Install Device Model*/

DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (WifiDevices,


sources);

/*Print----> Remaining Energy*/

//Ptr<BasicEnergySource> basicSourcePtr = DynamicCast<BasicEnergySource>


(sources.Get(3));
//basicSourcePtr->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback
(&RemainingEnergy));
// Ptr<DeviceEnergyModel> basicRadioModelPtr = basicSourcePtr-
>FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get (0);
// NS_ASSERT (basicRadioModelPtr != NULL);

//---------------------------------------------------------------------------------
-----

/* Metodo--> Print Energy in Nodos, se llama de forma iterativa a la función


Print Energy hasta el tiempo en que finaliza la simulación */

double medicion=0;
for(medicion=0;medicion<=stop_simulacion;medicion=medicion+60)//intevalos en que
se imprimira
{
Simulator::Schedule(Seconds(medicion), &PrintEnergy, sources);
}
//---------------------------------------------------------------------------------
-----

/
*__________________________________________________________________________________
______
+ +
+ +
+ Configuración Archivos: Trazas, Animación, Tablas de Enrutamiento
+
+ +
+
___________________________________________________________________________________
_____+
*/

Simulator::Stop (Seconds (stop_simulacion)); //La simulaciòn finaliza en el


tiempo definido por la variable stop_simulacion

if (tracing == true)
{
AsciiTraceHelper ascii;
WifiPhy.EnableAsciiAll (ascii.CreateFileStream ("Energy_Video.tr"));
WifiPhy.EnablePcap ("Energy_Video", WifiDevices);

//=> Trace routing tables

Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>


("Energy_Video.routes", std::ios::out);
olsr.PrintRoutingTableAllEvery (Seconds (2), routingStream);
}
AnimationInterface anim ("Energy_Video.xml"); // => Mandatory

NS_LOG_INFO ("Run Simulation.");


Simulator::Run ();

Simulator::Destroy ();
NS_LOG_INFO ("Done.");
return 0;
}

Das könnte Ihnen auch gefallen