1

我开发了一个能量模型,可以为网络中存在的每个节点加载。该模型计算发射和接收能量。现在我想要做的是,如果一个节点请求它的所有相邻节点,那么这些相邻节点应该通过发送它们的剩余能量值来回复,以便节点可以决定哪个邻居具有更高的能量水平。

如何实现这一点以及请求节点如何获取从各个相邻节点接收到的所有能量值?

这是我的能量模型:

import org.arl.fjage.*
import org.arl.unet.*
import org.arl.unet.phy.*
import java.math.*
import Java.util.*

class EnergyModel extends UnetAgent {

int neighbor, addr
float neighbor_distance;
def ranging
def  init_energy = 10
def dist
def data
def depth
def C = 1.3312e-9       // empirical constant
static def Tot_bits
def fr = 10           //carrier freq.(Khz)
def d = 0.036*Math.pow(fr,1.5)    //Thorp's constant
static def source

static HashMap<Integer, Integer[]> map = new HashMap<>(); 

def sum=0.0,avg=0.0,count=0;

 public void startup() {
  AgentID phy = agentForService(Services.PHYSICAL);
  subscribe (topic(phy));

  ranging = agentForService Services.RANGING;
  subscribe topic(ranging);

  def nodeInfo = agentForService Services.NODE_INFO;
  addr = nodeInfo.address;

  depth = nodeInfo.location[2] 
  map.put(addr, nodeInfo.location); 

  }

 public void processMessage(Message msg) {

   if (msg instanceof DatagramFailureNtf){

   System.out.println "\n\tDatagramFailureNtf occured!!\nt"+msg.toString()
    println "\n\t BadFrameNtf occured !!!!\n\t"+msg.toString()

  }

   if (msg instanceof DatagramNtf && msg.protocol == Protocol.DATA) {              
       count++  

       neighbor = msg.from;
       source = msg.from;
       data = msg.getData()
       int[] loc1 = map.get(source)
       int[] loc2 = map.get(msg.getTo())
       def x = loc1[0] - loc2[0]
       def y = loc1[1] - loc2[1]
       def distance = Math.sqrt((x)*(x) +(y)*(y));
       def bits=32
       Tot_bits = bits*data.size()

       System.out.println "\n\tNumber of bits sent :"+Tot_bits

       dist = distance/1000.0      // converting the distance in Km.

       BigDecimal Tx_EG = new BigDecimal("1"); // Or BigInteger.ONE 

    Tx_EG = Tx_EG.multiply(BigDecimal.valueOf(Tot_bits*50e-9+ Tot_bits*                 
        (0.001)*dist*(depth*-0.001)*C*Math.pow(Math.E,d*dist))); 
       init_energy = init_energy - Tx_EG ;
       sum = sum + Tx_EG 
       avg = sum/count

       String value = String.valueOf(Tx_EG.doubleValue());
       System.out.println '\n\tTransmission Energy : '+value+" Joules";
       System.out.println '\tRemaining Energy : '+(init_energy)

       File file = new File("I:\\out.txt")
       def text = file.getText()

       System.out.println "ENERGY: -t "+text+" -i "+source+" -d      
        "+Tot_bits+" -e "+init_energy+"   T"
       println "ENERGY: -t "+text+" -i "+source+" -d "+Tot_bits+" -e  
          "+init_energy+"   T"
                 }

       if (msg instanceof RxFrameNtf && msg.protocol == Protocol.DATA){

       data = msg.getData()       // getting data
       System.out.println "\tData is :"+data
       def bits=32
       Tot_bits = bits*data.size()      //caculating total number of bits
       System.out.println "\tNumber of bits received :"+Tot_bits

       BigDecimal Rx_EG = new BigDecimal("1"); // Or BigInteger.ONE 

       Rx_EG = Rx_EG.multiply(BigDecimal.valueOf(Tot_bits*50e-9));        
       init_energy = init_energy - Rx_EG ;                                
       String value = String.valueOf(Rx_EG.doubleValue());
       System.out.println '\n\tReception Energy : '+value+" Joules";
       System.out.println '\tRemaining Energy : '+(init_energy)
       System.out.println '\tTime : '+msg.getRxTime()
       System.out.println '\tNode ID : '+msg.getTo()
       System.out.println "ENERGY: -t "+msg.getRxTime()+" -i 
        "+msg.getTo()+" -d "+Tot_bits+" -e "+init_energy+"   R"
       println "ENERGY: -t "+msg.getRxTime()+" -i "+msg.getTo()+" -d 
        "+Tot_bits+" -e "+init_energy+"   R"

     }

  if (msg instanceof BadFrameNtf){

      System.out.println "\n\tBadFrameNtf occured !!!!\n\t"+msg.toString()
      println "\n\t BadFrameNtf occured !!!!\n\t"+msg.toString()

     }

   if (msg instanceof CollisionNtf){

      System.out.println "\n\tCollision occured !!!!\n\t"+msg.toString()
      println "\n\tCollision occured !!!!\n\t"+msg.toString()

       }

         }

           void setup() {

              }

        }
4

1 回答 1

1

您需要定义特定于应用程序的 PDU 来请求此信息并提供此信息,并实施您自己的协议以在收到请求 PDU 时使用适当的响应 PDU 进行响应。samples/ping从开发人员指南中的 ping 示例(模拟器中的文件夹)中了解如何编写自己的应用程序特定协议。

另一种方法是将能量作为 PHY 的代理参数公开,并使用远程访问服务请求它以使用RemoteParamReq请求它。

于 2019-07-01T19:28:06.600 回答