Getting Node Position and Energy in NS2
It has been really long time since my last post my and I am back. Finally, I could make little time to write this post. I believe such information is available in elsewhere. But, I also believe explaining simple method of obtaining node position and current remaining energy would help a lot of people. I strongly suggest scanning the code of WFRP before reading the post further.
Alright, lets get on the work. First you have to include mobilenode.h (located in NSROOT/ns-2.xx/common/ ) into wfrp.h file. Add following line after #include <cmu-trace.h>
#include <mobilenode.h>
We have to make little more modifications to wfrp.h, thus change the lines from 119 to 122 (wfrp.h) as below
// Node Location double posx; // position x; double posy; // position y; // Remaining Energy double iEnergy;
At line 153 (wfrp.h) add following code
// Energy Management void update_energy(); // This node; MobileNode *iNode;
Now we are going to update actual node position and energy. Add following code to line 147 of wfrp.cc :
//initialize energy iEnergy = 0; // Get pointer to the node iNode = (MobileNode *) (Node::get_node_by_address(index));
In above code get_node_by_address(nsaddr_t id) is located in $NSROOT/ns-2.xx/common/node.h, which obtains pointer to the node according to given address. The class Node contains various information about node, including energy model, location, speed, neighbors, etc.
Update the wfrp.cc code from the line 530 as below
void
WFRP::update_position() {
iNode->update_position();
posx = iNode->X();
posy = iNode->Y();
#ifdef DEBUG
printf("U (%.6f): UPDATE POSITION, for Node %d, X: %.4f and Y : %.4f \n", CURRENT_TIME, index, posx, posy);
#endif
}
void
WFRP:: update_energy() {
iEnergy = iNode->energy_model()->energy();
#ifdef DEBUG
printf("U (%.6f): UPDATE ENERGY, for Node %d, Energy %.4f \n", CURRENT_TIME, index, iEnergy);
#endif
}
First routine (update_position) updates the position at request time and puts X and Y position to posx, posy respectively. Note, iNode->update_position() routine is in $NSROOT/ns-2.xx/common/mobilenode.cc. You may also obtain node's current speed, radio range, and Z position.
Second routine (update_energy) gets nodes current remaining energy and puts it to iEnergy. For information refer to $NSROOT/ns-2.xx/mobile/energy-model.cc.
After completing modification just re-make. For location based routing protocols this might very useful. Leave comments here if you have any problems with the code.
Note : energyModel must be set in TCL file, in order to obtain node's energy or energy model related information.