在 NS2 中模拟无线传感器网络的路由协议时,从函数内部访问节点会出现错误 "No such variable"
While simulating a routing protocol for Wireless Sensor Network in NS2, accessing the nodes from inside the a function gives error "No such variable"
我正在尝试为 60 个节点和 1 个移动接收器编写代码,其中 18 个节点充当高层节点或 Backbone 个节点。移动sink周期性更新位置,仅Backbone个节点,其他节点可在需要时查询sink位置。
#node creation
#Node 1 to 18 are backbone nodes
#nodes 19 to 59 are normal sensor nodes
#node 60 is the mobile sink
set node_(0) [$ns node]
set node_(1) [$ns node]
.
.
.
#node no 60 is the mobile sink..
set node_(60) [$ns node]
#node X Y coordinate initialized
$node_(0) set X_ 2
$node_(0) set Y_ 3
$node_(1) set X_ 517
$node_(1) set Y_ 299
.
.
$node_(63) set X_ 306
$node_(63) set Y_ 178
proc distBN {n1 n2 nid1 nid2} {
set x1 [expr int([$n1 set X_])]
set y1 [expr int([$n1 set Y_])]
set x2 [expr int([$n2 set X_])]
set y2 [expr int([$n2 set Y_])]
set d [expr int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]
#checking that it is in the transmission range i.e 100m
if {$d<100} {
if {$nid2!=$nid1} {
return $d;
}
}
}
#This function starts the transmission of Mobile Sink location every 5 seconds
proc locTrans {} {
global ns
set dist_min 101
set dist 101
set sink_loc 60
puts "[$ns now] : Finding the nearest backbone node to sink node $sink_loc..."
for {set j 0} {$j < 19} {incr j} {
set dist [expr "distBN $node_($sink_loc) $node_($j) $sink_loc $j"]; ## <<< THIS LINE
puts "Distance between sink and node $j is $dist...\n"
if{$dist_min > $dist} {
set dist_min $dist
}
puts "Running loop for $j time...\n"
}
puts "The smallest distance is $dist_min between node_(60) and node_($j)\n"
puts "[$ns now] : transmitting sink location to nearest backbone node...\n"
$ns at [expr [$ns now] + 3.0] "locTrans"
}
过程 locTrans() 调用另一个函数 distBN() 来计算传入参数的节点之间的距离。但是 运行 代码给出了以下错误:
> ns: locTrans: can't read "node_(60)": no such variable
while executing
"expr "distBN $node_($sink_loc) $node_($j) $sink_loc $j""
(procedure "locTrans" line 9)
invoked from within
"locTrans"
Error screenshot
问题是全局 node_
数组不在过程内的范围内(因为 Tcl 仅在您明确要求时才使 non-local 变量可用)。幸运的是,修复很简单:将上面几行的 global ns
命令更新为:
global ns node_
还要注意您的代码中有几个错误:您没有处理正确测量节点到自身距离的情况(现在,您实际上返回的是空字符串;我 打赌 你不想要那个!)并且你已经将对 distBN
的调用写错了,应该这样做:
set dist [distBN $node_($sink_loc) $node_($j) $sink_loc $j]
你写的是...行不通。
我正在尝试为 60 个节点和 1 个移动接收器编写代码,其中 18 个节点充当高层节点或 Backbone 个节点。移动sink周期性更新位置,仅Backbone个节点,其他节点可在需要时查询sink位置。
#node creation
#Node 1 to 18 are backbone nodes
#nodes 19 to 59 are normal sensor nodes
#node 60 is the mobile sink
set node_(0) [$ns node]
set node_(1) [$ns node]
.
.
.
#node no 60 is the mobile sink..
set node_(60) [$ns node]
#node X Y coordinate initialized
$node_(0) set X_ 2
$node_(0) set Y_ 3
$node_(1) set X_ 517
$node_(1) set Y_ 299
.
.
$node_(63) set X_ 306
$node_(63) set Y_ 178
proc distBN {n1 n2 nid1 nid2} {
set x1 [expr int([$n1 set X_])]
set y1 [expr int([$n1 set Y_])]
set x2 [expr int([$n2 set X_])]
set y2 [expr int([$n2 set Y_])]
set d [expr int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]
#checking that it is in the transmission range i.e 100m
if {$d<100} {
if {$nid2!=$nid1} {
return $d;
}
}
}
#This function starts the transmission of Mobile Sink location every 5 seconds
proc locTrans {} {
global ns
set dist_min 101
set dist 101
set sink_loc 60
puts "[$ns now] : Finding the nearest backbone node to sink node $sink_loc..."
for {set j 0} {$j < 19} {incr j} {
set dist [expr "distBN $node_($sink_loc) $node_($j) $sink_loc $j"]; ## <<< THIS LINE
puts "Distance between sink and node $j is $dist...\n"
if{$dist_min > $dist} {
set dist_min $dist
}
puts "Running loop for $j time...\n"
}
puts "The smallest distance is $dist_min between node_(60) and node_($j)\n"
puts "[$ns now] : transmitting sink location to nearest backbone node...\n"
$ns at [expr [$ns now] + 3.0] "locTrans"
}
过程 locTrans() 调用另一个函数 distBN() 来计算传入参数的节点之间的距离。但是 运行 代码给出了以下错误:
> ns: locTrans: can't read "node_(60)": no such variable
while executing
"expr "distBN $node_($sink_loc) $node_($j) $sink_loc $j""
(procedure "locTrans" line 9)
invoked from within
"locTrans"
Error screenshot
问题是全局 node_
数组不在过程内的范围内(因为 Tcl 仅在您明确要求时才使 non-local 变量可用)。幸运的是,修复很简单:将上面几行的 global ns
命令更新为:
global ns node_
还要注意您的代码中有几个错误:您没有处理正确测量节点到自身距离的情况(现在,您实际上返回的是空字符串;我 打赌 你不想要那个!)并且你已经将对 distBN
的调用写错了,应该这样做:
set dist [distBN $node_($sink_loc) $node_($j) $sink_loc $j]
你写的是...行不通。