Elmurod A. Talipov
5Apr/1031

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.

Comments (31) Trackbacks (2)
  1. so may i know how to i call it from my .tcl file?

  2. Call what? clarify your question. Guys, please don’t waste my time asking abstract questions. Before posting your comment read it first, can other person understand what you are trying to say. I WILL NEVER ANSWER if your question is NOT EXACT AND CLEAR.

  3. Thanks for the simple but useful post. I need a clarification to update the energy value of any node if its energy level falls below certain thresholds (66%,33%,10%) of its initial energy value. Updates should not be at periodic intervals but only when it falls below these levels. (P.S i should update only once after it falls below 66% or 33% or 10%)

    Any suggestions would be of great help!!

    Thanks & Regards,
    Jayavignesh

  4. sorry, i meant: how do i obtain the current energy and position of particular node in my .tcl coding? there must be a function that i can obtain the value to process.

  5. Jayavignesh, you have no option but to use timer. Check beacon timer implementation in WFRP.

    Scott i donno how to do that. And why would you need that.

    Daywalker you have to read the post carefully. You are just wastingmy time. My code has no error. Probably, you haven’t included proper files.

  6. Hi i am getting following error after changes.Sir please guide me

    wfrp/wfrp.cc: In constructor ‘WFRP::WFRP(nsaddr_t)’:
    wfrp/wfrp.cc:152: error: ‘MonileNode’ was not declared in this scope
    wfrp/wfrp.cc:152: error: expected primary-expression before ‘)’ token
    make: *** [wfrp/wfrp.o] Error 1

    //

    Code :
    ======================================================================
    // Agent Constructor
    // ======================================================================

    WFRP::WFRP(nsaddr_t id) : Agent(PT_WFRP), bcnTimer(this), rtcTimer(this) {

    #ifdef DEBUG
    printf(”N (%.6f): Routing agent is initialized for node %d \n”, CURRENT_TIME, id);
    #endif
    index = id;
    seqno = 1;

    LIST_INIT(&rthead);
    posx = 0;
    posy = 0;

    logtarget = 0;
    ifqueue = 0;

    // initialize energy
    iEnergy = 0;

    // Get pointer to the node
    iNode = (MonileNode *)(Node::get_node_by_address(index));
    }

  7. Hi…I am unable to generate simulation using nam files of your protocol…can u guide?

  8. how to give different energy for the nodes in ns2…. like node one has energy 1000 and node2 has 500…
    give me syntax in ns2….

  9. Hello..,

    Your code for “Getting the Node position” is helpful for us. But at the same time we would like to implement directional antennas for sending the packets in a specific direction.
    For that I installed the patch for directional antennas for ns2.33(allinone package)

    I got the patch for directional antennas from

    http://cbg.me/2009/02/adding-directional-antenna-and-multiple-interface-support-to-ns-233/

    After the installation of these patches i found that some extra code regarding interfaces was added to aodv.cc file and some more files like directionalantenna.cc & .h were added to ~ns2/mobile.
    and in my .tcl file i am able to create nodes with directional antennas.
    We can see the patches and understand what part of code in the files has been added.

    But we are unable to understand how to access a particular directional antenna for a node to send the packets in a specific direction.

    Any help in this regard will be appreciated.
    thanking u in advance.

  10. Aparna, I am not owner of the code. Why don’t you ask your question original author? I haven’t tried that…

    Guys, don’t ask me about codes of other people (if it is not NS2 source tree). I am not able to make time to review their code answer your question.

  11. where is wfrp.cc or wfrp.h located in ns2. i am using ns-2.30 simulator. kindly help

  12. Forget to mention the System config
    Ns2 pkg: same as you had used ns-2.34
    OS: Fedeora 12

    Thanks for consideration

  13. Hi ppl,
    Just wanted to updated the soln for the error reported earlier.
    Well we just had to make an entry for the new protocol in the Makefile.in file
    i.e. wfrp/wfrp.o \ in the common\Makefile.in
    Once you do that u got to fo the following
    1. ./Configure
    2. Make clean
    3. Make depend
    4. Make
    5. Make install

    then run wfrp_……tcl file. It should run perfectly fine.

    SmartNode, correct me if i am wrong.

  14. Sir i am trying for the getting node energy code for the same as per your guide line i am changing the wfrp.cc
    code Instruction given is to change the wfrp.cc line 530.
    but what ever file is there having no of lines 511 only.
    What to do for the same

  15. dear mr
    thanks for your very good helping.
    I learned very things from your site.
    but I have a question, if it is possible help me:
    how can I see xgraph output for aodv protocol?
    please and please if it possible send this question answer for my mail.
    my mail : a.heidari@live.com

  16. @Maverick: thanks for additional instruction..

    @NITIN: if you start adding codes from the top. Then line 530 will appear :)

    @ahmad: see this manaul http://www.maddireddy.com/file/btechprj/xgraph.pdf

  17. sir
    i want to create a hello flood attack in wsn what i do
    please help me to solve my problem.
    thanks you

  18. hello Sir… I have a small problem. I am working on detection/isolation of selfish/malicious node which drops data packet.
    I want to logically group nodes of active route found by DSR such that first three node form first group, next three nodes form second group and so on.
    Also want to know how to explicit send ACK packet for reception of data packets by a node to previous one of active path.

  19. Dear Sir

    Im irvan from Indonesia, i read your paper about RADV. I want to compare RAODV with AODV. I got RAODV’s source code from mr elmurod blog. I think this code had improved with ability in security especially with malicious node existance. I tried to delete it. When i started to simulate (i use RWM, and CBR traffic random generator, setdest and cbrgen) but i cannot found RAODV performances better than AODV. Sir could you explain to me in what scenario of simulation RAODV perform better. Especially based on your paper

    Thank you very much..i really need your help

  20. Al Salamo Alikom,
    Dear Elmurod,
    I have a question, concerning implemented WSN multipath routing protocols on NS-2.34.
    are there other than AOMDV? I searched for the source code of others like MDSR, MDR, SMR or… but in vain, aren’t there?
    need your help
    Jazak Allah kheran
    Mariam

  21. I am trying to implement a distributed time synchronization algorithm in Zigbee … if you have any suggestion please send me . That algorithm works independent of topology and every node will broadcast its offset and some other parameters . Based on that broadcast other nodes ( which are neighbors to that node)will update their clocks and so on . This is how they synchronize . I want to know how we have to send the packets and where we have to start in a topology . please give me your suggestion . If you want I will give the references also..

  22. i could not leave a message in malicious nodes
    i have a question i decided to simulate misbehaving node in mac layer protocol how can do that?
    do you any codes for that?
    please help me
    Ali Balador (balador.org)

  23. Elmurod,

    Thanks for such informative postings. I am new to the world of NS2 and need a explanation on how to debug the code in ns2 so that i am able to understand the flow of the code i added in the NS2. The code provided by you is working absolutely fine since your instructions were followed appropriately. I need to understand the WFRP initial posting including this on from Concept point of you.

    Since you are the owner please provide me the links or complete understanding of the source code you have shared for learners like me.

    Thanking you once again.

    Sons

  24. Can u please provide resources for me to understand the code.. that is direct diffusion. Also, I need to add x graph and routing table to this protocol how can i go about it. Please guide me Mr. Elmurod. I really hope for a positive response. Your research has not only contributed in triggering my interest in NS2 but one day i am sure under your guidance i will be able be call myself as researcher and expert in NS2. Thanking you once again.

    Sons.

  25. Thanks for your great effort to help others…..

    I would like to as u the following:
    WFRP::command() in wfrp.cc has id, start, sink, index, log-target …..
    Q: From where these command are tragger. It is not in tcl file?????
    Thanks,

  26. How can we control the transmission power…..

  27. hi, I have another question. I want to implement a new transport protocol in ns2. Is there any difference compare to adding a routing protocol in ns2? Do you have any experience and can share with me? Please give me some reference. Thanks in advance.

  28. A node can get its own position by your source code. Do you think we can get position of neighboring node without hello message?

  29. I’m sorry guys could not approve your comments earlier and could not give response. I was really busy for three months. I will reply one by one, hope it is not too late.

  30. @vp check wfrp code it will be helpful.

    @anand did not get what you are asking.

    @irvan i guess you got my mail and solved your problem.

    @mariam i dont have source code for other multipath routing protocols since authors hadnt published

    @dalera i guess i cant make time for that

    @ali the idea is similar one i have described in my other post. You just have to make those changes in mac layer.

    @Sons leave your email i’ll contact you

  31. hello,
    how could i call update_energy() ? shall i make a new TCL command to call it?
    thankls


Leave a comment