使用带有 groovy 脚本的 unetstack 查找两个节点之间的距离
Finding distance between two nodes using using unetstack with groovy script
我正在为基于相邻节点之间距离的水下通信网络实施路由协议。我创建了一个代理并编写了一个脚本来使用测距代理查找相邻节点之间的距离,但出现错误
No such property: ranging for class:.
我会附上我写的脚本。
- 3-node-network_test.groovy : 这是一个我部署了3个节点的模拟脚本。 Node 1为sink节点,测距agent和sink脚本将在该节点执行。
节点2和3是运行"ranging agent"和"node_agent".
- sink.groovy: 将通过广播信标来初始化信标进程。
- node_agent:从sink接收到一个beacon,识别到sink的距离。在这里,我编写了脚本来查找下沉距离:
完成脚本。如何在 unetstack 中查找距离?
3-节点-network_test.groovy:
//! Simulation: Simple 3-node network
import org.arl.fjage.*
// run the simulation forever
simulate {
node '1', remote: 1101, address: 1, location: [ 0.km, 0.km, -15.m], shell: true, stack: {container ->
container.add 'ranging', new org.arl.unet.phy.Ranging()
container.shell.addInitrc "${script.parent}/sink.groovy"
}
node '2', remote: 1102, address: 2, location: [ 1.km, 0.km, -15.m], shell: 5102, stack: {container ->
container.add 'ranging', new org.arl.unet.phy.Ranging();
//container.shell.addInitrc "${script.parent}/sink.groovy"
container.add 'node_agent', new node_agent();
}
node '3', remote: 1103, address: 3, location: [-1.km, 0.km, -15.m], shell: 5103, stack: { container ->
container.add 'ranging', new org.arl.unet.phy.Ranging()
container.add 'node_agent', new node_agent();
}
}
Sink.groovy:
import org.arl.unet.*
import org.arl.unet.phy.*
import org.arl.unet.mac.*
//import org.arl.unet.nodeinfo.NodeInfo
import org.arl.unet.PDU
import org.arl.fjage.*
import static org.arl.unet.Services.*
import static org.arl.unet.phy.Physical.*
import org.arl.unet.phy.Ranging.*
int hc = 0, ad;
float neighbor_dist;
float rang
subscribe phy;
send = { count = 1 ->
println ''' BROADCASTING '''
count.times {
phy << new DatagramReq(to: Address.BROADCAST, protocol: Protocol.MAC, data: [node.address, hc, 0]);
}
}
node_agent.groovy:
import org.arl.fjage.Message
import org.arl.unet.*
import org.arl.unet.net.Router
import org.arl.unet.phy.*
import org.arl.unet.mac.*
import org.arl.fjage.RealTimePlatform
import org.arl.unet.nodeinfo.NodeInfo
import org.arl.fjage.*
import org.arl.unet.phy.Ranging.*
import org.arl.unet.phy.RangeNtf.*
import org.arl.unet.phy.RangeReq
class node_agent extends UnetAgent {
int neighbor, addr;
float neighbor_distance;
void startup()
{
def phy = agentForService Services.PHYSICAL;
subscribe topic(phy);
def rang = agentForService Services.RANGING;
subscribe topic(rang);
def nodeInfo = agentForService Services.NODE_INFO;
addr = nodeInfo.address;
}
void processMessage(Message msg) {
if (msg instanceof DatagramNtf && msg.protocol == Protocol.MAC)
{
neighbor = msg.from;
println " BEACON RECEIVED FROM:" +neighbor
ranging<< new RangeReq(to: neighbor);
}
else if (msg instanceof RangeNtf )
{
float neighbor_distance = msg.getRange();
println " Distance between node "+addr + " and neighbor" +neighbor+ "is" + neighbor_dis
} // End of if*/
else {
}
} //End of process message
}
需要将RangeReq
发送给测距代理。您可以使用 agentForService Services.RANGING
获取代理。由于您无论如何都在 startup()
中查找它,因此您可以将它存储在一个属性中并稍后使用它来请求范围。类似于:
def ranging
void startup()
{
:
ranging = agentForService Services.RANGING;
:
}
void processMessage(Message msg)
{
if (msg instanceof DatagramNtf && msg.protocol == Protocol.MAC)
{
neighbor = msg.from;
println " BEACON RECEIVED FROM:" +neighbor
ranging << new RangeReq(to: neighbor);
}
else if (msg instanceof RangeNtf )
{
float neighbor_distance = msg.getRange();
:
}
}
}
我正在为基于相邻节点之间距离的水下通信网络实施路由协议。我创建了一个代理并编写了一个脚本来使用测距代理查找相邻节点之间的距离,但出现错误
No such property: ranging for class:.
我会附上我写的脚本。
- 3-node-network_test.groovy : 这是一个我部署了3个节点的模拟脚本。 Node 1为sink节点,测距agent和sink脚本将在该节点执行。 节点2和3是运行"ranging agent"和"node_agent".
- sink.groovy: 将通过广播信标来初始化信标进程。
- node_agent:从sink接收到一个beacon,识别到sink的距离。在这里,我编写了脚本来查找下沉距离:
完成脚本。如何在 unetstack 中查找距离?
3-节点-network_test.groovy:
//! Simulation: Simple 3-node network
import org.arl.fjage.*
// run the simulation forever
simulate {
node '1', remote: 1101, address: 1, location: [ 0.km, 0.km, -15.m], shell: true, stack: {container ->
container.add 'ranging', new org.arl.unet.phy.Ranging()
container.shell.addInitrc "${script.parent}/sink.groovy"
}
node '2', remote: 1102, address: 2, location: [ 1.km, 0.km, -15.m], shell: 5102, stack: {container ->
container.add 'ranging', new org.arl.unet.phy.Ranging();
//container.shell.addInitrc "${script.parent}/sink.groovy"
container.add 'node_agent', new node_agent();
}
node '3', remote: 1103, address: 3, location: [-1.km, 0.km, -15.m], shell: 5103, stack: { container ->
container.add 'ranging', new org.arl.unet.phy.Ranging()
container.add 'node_agent', new node_agent();
}
}
Sink.groovy:
import org.arl.unet.*
import org.arl.unet.phy.*
import org.arl.unet.mac.*
//import org.arl.unet.nodeinfo.NodeInfo
import org.arl.unet.PDU
import org.arl.fjage.*
import static org.arl.unet.Services.*
import static org.arl.unet.phy.Physical.*
import org.arl.unet.phy.Ranging.*
int hc = 0, ad;
float neighbor_dist;
float rang
subscribe phy;
send = { count = 1 ->
println ''' BROADCASTING '''
count.times {
phy << new DatagramReq(to: Address.BROADCAST, protocol: Protocol.MAC, data: [node.address, hc, 0]);
}
}
node_agent.groovy:
import org.arl.fjage.Message
import org.arl.unet.*
import org.arl.unet.net.Router
import org.arl.unet.phy.*
import org.arl.unet.mac.*
import org.arl.fjage.RealTimePlatform
import org.arl.unet.nodeinfo.NodeInfo
import org.arl.fjage.*
import org.arl.unet.phy.Ranging.*
import org.arl.unet.phy.RangeNtf.*
import org.arl.unet.phy.RangeReq
class node_agent extends UnetAgent {
int neighbor, addr;
float neighbor_distance;
void startup()
{
def phy = agentForService Services.PHYSICAL;
subscribe topic(phy);
def rang = agentForService Services.RANGING;
subscribe topic(rang);
def nodeInfo = agentForService Services.NODE_INFO;
addr = nodeInfo.address;
}
void processMessage(Message msg) {
if (msg instanceof DatagramNtf && msg.protocol == Protocol.MAC)
{
neighbor = msg.from;
println " BEACON RECEIVED FROM:" +neighbor
ranging<< new RangeReq(to: neighbor);
}
else if (msg instanceof RangeNtf )
{
float neighbor_distance = msg.getRange();
println " Distance between node "+addr + " and neighbor" +neighbor+ "is" + neighbor_dis
} // End of if*/
else {
}
} //End of process message
}
需要将RangeReq
发送给测距代理。您可以使用 agentForService Services.RANGING
获取代理。由于您无论如何都在 startup()
中查找它,因此您可以将它存储在一个属性中并稍后使用它来请求范围。类似于:
def ranging
void startup()
{
:
ranging = agentForService Services.RANGING;
:
}
void processMessage(Message msg)
{
if (msg instanceof DatagramNtf && msg.protocol == Protocol.MAC)
{
neighbor = msg.from;
println " BEACON RECEIVED FROM:" +neighbor
ranging << new RangeReq(to: neighbor);
}
else if (msg instanceof RangeNtf )
{
float neighbor_distance = msg.getRange();
:
}
}
}