在模拟NS2中无线传感器网络的路由协议时,从a函数内部访问节点会产生错误“没有这样的变量”。

发布于 2025-01-24 22:42:22 字数 2354 浏览 1 评论 0原文

我正在尝试编写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"

Error screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2025-01-31 22:42:22

问题在于,整体节点_数组不在过程中的范围(因为TCL仅在明确要求它们的情况下可以提供非本地变量)。幸运的是,修复很容易:更新全局ns命令上面几行,

global ns node_

请注意,您的代码中有几个错误:您不处理测量距离的情况从一个节点到自身(现在,您有效地返回空字符串; i bet 您都不想要!),您已经写了电话distbn错误,应该这样做:

set dist [distBN $node_($sink_loc) $node_($j) $sink_loc $j]

您写的是...不去工作。

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 the global ns command a few lines above to:

global ns node_

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:

set dist [distBN $node_($sink_loc) $node_($j) $sink_loc $j]

What you wrote was... not going to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文