get the position of node using cooja contiki

795 views Asked by At

I want to get the position (x and y coordinates) of each node in the Cooja simulator. There are 30 nodes in total, of type 'sky mote'. The goal is to build a table consisting of (ID_node x_coordinate y_coordinate) for each node.

First I tried to get the x coordinate for each node by writing in script simulation editor this code:

array = new Array();
for (i = 1; i < 4; i++) {
  array[i] = mote.getInterfaces().getPosition().getXCoordinate();
  log.log(array[i] + "\n");
}

but I got this result:

59.09635997249024
59.09635997249024
59.09635997249024

All these numbers are the x coordinate for a particular node with ID 2. Please, can you help me to get the x and y coordinates for all nodes in Cooja?

1

There are 1 answers

0
kfx On

You should iterate over all nodes in the simulation and save the X and Y coordinates in the array. The following code saves the coordinates in an array and prints them:

 array = new Array();
 motes = sim.getMotes();
 for(var i = 0; i < motes.length; i++){
     var m = motes[i];
     var x = m.getInterfaces().getPosition().getXCoordinate();
     var y = m.getInterfaces().getPosition().getYCoordinate();
     var id = m.getID();
     array[i] = {id:id, x:x, y:y};
     log.log(id + " " + x + " " + y + "\n");
  }