Read node's energy level

178 views
Skip to first unread message

Thiago

unread,
Apr 4, 2018, 5:35:31 PM4/4/18
to ns-3-users
Hi,

I'm working on a wireless scenario where I compute the energy level of the nodes in the simulation file as follows:

LiIonEnergySourceHelper liIonSrcHelper;
liIonSrcHelper
.Set ("LiIonEnergySourceInitialEnergyJ", DoubleValue (1000.0));
EnergySourceContainer sources = liIonSrcHelper.Install (wifiNodes);
WifiRadioEnergyModelHelper radioEnergyHelper;
DeviceEnergyModelContainer wifideviceModels = radioEnergyHelper.Install (wifiNet1Devices, sources);

// ...

Ptr<LiIonEnergySource> liIonSourcePtr = DynamicCast<LiIonEnergySource> (sources.Get (1));
liIonSourcePtr
->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
Ptr<DeviceEnergyModel> liIonRadioModelPtr = liIonSourcePtr->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get (0);
NS_ASSERT
(liIonRadioModelPtr != NULL);
liIonRadioModelPtr
->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));

void
RemainingEnergy (double oldValue, double remainingEnergy) {
  NS_LOG_UNCOND
(Simulator::Now ().GetSeconds () << " s Current remaining energy = " << remainingEnergy << " J");
}

void
TotalEnergy (double oldValue, double totalEnergy) {
  NS_LOG_UNCOND
(Simulator::Now ().GetSeconds () << " s Total energy consumed by radio = " << totalEnergy << " J");
}

This works as expected. Now, if I want to get this information at the MAC level, how would I go about implementing this? I couldn't find any topic related to this issue on the mailing list.
I know I can get the node object using 
ns3::Ptr<ns3::Node> node = ns3::NodeList::GetNode(ns3::Simulator::GetContext());
but I don't know how to get the energy info from there.


Thanks for your help!


Thiago

unread,
Apr 4, 2018, 7:43:17 PM4/4/18
to ns-3-users
Got it working! 

Ptr<EnergySourceContainer> EnergySourceContainerOnNode = node->GetObject<EnergySourceContainer> ();
if (EnergySourceContainerOnNode == NULL)
{
   std
::cout << "Node Id: " << node->GetId () << " EnergySourceContainerOnNode == NULL"<< std::endl;
}else
{
   
Ptr<LiIonEnergySource> es = DynamicCast<LiIonEnergySource> (EnergySourceContainerOnNode->Get(0));
   std
::cout << "At " << Simulator::Now ().GetSeconds () << " Node Id: " << node->GetId ()
             
<< " Remaining Capacity: " << es->GetRemainingEnergy () / (3.6 * 3600) << " Ah" << std::endl;
}

Kemal Kilic

unread,
Jul 26, 2018, 11:06:44 AM7/26/18
to ns-3-users
Hello,
I am working on the same topic
Firstly where did you put the code below you mentioned? In main() or in RemainingEnergy()?
Which version was your ns-3?
The second question is somehow technical but I need it:
How can you find the node id in the  RemainingEnergy() callback function?

Basically, I need to find the node id and print (LOG actually) it.
Sometimes you need to find dead nodes (energy 0) or the node with max energy
For this, I guess finding a node id is very essential.

Thanks in advance...

Thiago Teixeira

unread,
Jul 26, 2018, 11:43:20 AM7/26/18
to ns-3-...@googlegroups.com
Hi Kemal,

This might help you. It explains how to use the energy framework (battery model, values, etc).

You can get the node id you can use node.GetId(), from here.

For logging, you can add NS_LOG_INFO("some message"); and enable to log on NS_LOG=..... ./waf ...


Hope that helps.


--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/OfKhWL3JUEc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at https://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

sapna jha

unread,
Aug 17, 2018, 4:24:45 AM8/17/18
to ns-3-users
Hello Thiago,
I am new to NS3 and have to simulate WSN. How to simulate a simple WSN.
         Any suggestion will be valuable.

Thiago Teixeira

unread,
Aug 17, 2018, 8:42:23 AM8/17/18
to ns-3-...@googlegroups.com
Have a look at this post



Francisco Eduardo Balart Sanchez

unread,
Oct 21, 2018, 10:27:37 PM10/21/18
to ns-3-users
Good day Thiago:

I have more less the same as you but the problem is i dont have ALL the energy level at time T
Just if something is triggered o get the nodes in that time T that were Rx or Tx

Ptr<BasicEnergySource>  basicSourcePtr  =  DynamicCast<BasicEnergySource>(p->GetNode ()->GetObject<EnergySourceContainer>()->Get(0));
    NS_ASSERT (basicSourcePtr != NULL);
    basicSourcePtr->TraceConnect ("RemainingEnergy",PrintID(p->GetNode ()), MakeCallback (&RemainingEnergyTrace)); 
 
Example on my log file

%INFO: TimeStamp: 60.988 segs Node: ID: 97   Current Remaining energy = 54.1867 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 69   Current Remaining energy = 54.366 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 75   Current Remaining energy = 54.9593 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 3   Current Remaining energy = 53.0506 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 38   Current Remaining energy = 52.698 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 76   Current Remaining energy = 53.2712 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 54   Current Remaining energy = 52.6771 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 22   Current Remaining energy = 52.641 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 47   Current Remaining energy = 55.0897 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 16   Current Remaining energy = 52.896 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 43   Current Remaining energy = 53.0377 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 49   Current Remaining energy = 55.104 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 61   Current Remaining energy = 55.0461 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 89   Current Remaining energy = 55.5411 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 26   Current Remaining energy = 54.9403 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 63   Current Remaining energy = 53.2001 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 90   Current Remaining energy = 53.2118 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 8   Current Remaining energy = 55.4874 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 42   Current Remaining energy = 56.2636 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 57   Current Remaining energy = 55.0819 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 48   Current Remaining energy = 53.6125 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 55   Current Remaining energy = 52.7304 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 13   Current Remaining energy = 56.2103 Joules
%INFO: TimeStamp: 60.988 segs Node: ID: 68   Current Remaining energy = 53.0735 Joules
%INFO: TimeStamp: 60.9884 segs Node: ID: 97   Current Remaining energy = 54.1867 Joules
%INFO: TimeStamp: 60.9884 segs Node: ID: 69   Current Remaining energy = 54.3659 Joules



Did you find a way to get at time T ALL the energy levels of all nodes at such time T?
Reply all
Reply to author
Forward
0 new messages