在模拟NS2中无线传感器网络的路由协议时,从a函数内部访问节点会产生错误“没有这样的变量”。
我正在尝试编写60个节点和1个移动水槽的代码,其中18个节点充当高层节点或骨干节点。移动水槽会定期更新位置,以便只有骨干节点和其他节点可以在需要时查询水槽位置。
#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"
I am trying to write a code for 60 nodes and 1 mobile sink among which 18 nodes act as high-tier nodes or Backbone nodes. The mobile sink updates the location periodically to only the Backbone nodes and other nodes can query the sink location at the time of need.
#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"
}
The procedure locTrans() calls another function distBN() to calculate the distance between the nodes passed in arguments. But running the code gives the following error:
> 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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,整体
节点_
数组不在过程中的范围(因为TCL仅在明确要求它们的情况下可以提供非本地变量)。幸运的是,修复很容易:更新全局ns
命令上面几行,请注意,您的代码中有几个错误:您不处理测量距离的情况从一个节点到自身(现在,您有效地返回空字符串; i bet 您都不想要!),您已经写了电话
distbn
错误,应该这样做:您写的是...不去工作。
The problem is that the global
node_
array isn't in scope inside the procedure (because Tcl only makes non-local variables available if you explicitly ask for them). Luckily the fix is easy: update theglobal ns
command a few lines above to:Also be aware that you have a couple of bugs in your code: you don't handle the case where you measure the distance from a node to itself correctly (right now, you're effectively returning the empty string; I bet you don't want that!) and you have written the call to
distBN
wrong and should do:What you wrote was... not going to work.